いろはちゃんとマス目 / Iroha and a Grid (组合数学)

题目链接:http://abc042.contest.atcoder.jp/tasks/arc058_b

Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

We have a large square grid with H rows and W columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.

However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A×B forbidden cells.) There is no restriction on entering the other cells.

Find the number of ways she can travel to the bottom-right cell.

Since this number can be extremely large, print the number modulo 109+7.

Constraints

  • 1≦H,W≦100,000
  • 1≦A<H
  • 1≦B<W

Input

The input is given from Standard Input in the following format:

H W A B

Output

Print the number of ways she can travel to the bottom-right cell, modulo 109+7.


Sample Input 1

Copy
2 3 1 1

Sample Output 1

Copy
2

We have a 2×3 grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".


Sample Input 2

Copy
10 7 3 4

Sample Output 2

Copy
3570

There are 12 forbidden cells.


Sample Input 3

Copy
100000 100000 99999 99999

Sample Output 3

Copy
1

Sample Input 4

Copy
100000 100000 44444 55555

Sample Output 4

Copy
738162020

题意:n*m矩阵 左下方A*B为禁区,每次可以走下或者左
n,m<=1e5,问从左上到右下不经过禁区时的方案数?

题解:就是找路吧 以为可以动态规划但是数据太大 数组开不了 看了下大佬的 原来可以用组合数 

若无禁区,则方案数为C(n+m-2,n-1)
有禁区时 每个合法路径都会到达(n-A,i)i>B 即n-A行的某一列上.
则每个合法路径都可以分成两段,(1,1)->(n-A,i),(n-A,i)->(n,m) (i>B)
注意枚举到(n-A,i)不能向右移动,否则重复枚举.所以路径变为三段.

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <vector>
 6 #include <cstdlib>
 7 #include <iomanip>
 8 #include <cmath>
 9 #include <ctime>
10 #include <map>
11 #include <set>
12 #include <queue>
13 using namespace std;
14 #define lowbit(x) (x&(-x))
15 #define max(x,y) (x>y?x:y)
16 #define min(x,y) (x<y?x:y)
17 #define MAX 100000000000000000
18 #define MOD 1000000007
19 #define pi acos(-1.0)
20 #define ei exp(1)
21 #define PI 3.141592653589793238462
22 #define INF 0x3f3f3f3f3f
23 #define mem(a) (memset(a,0,sizeof(a)))
24 typedef long long ll;
25 ll gcd(ll a,ll b){
26     return b?gcd(b,a%b):a;
27 }
28 bool cmp(int x,int y)
29 {
30     return x>y;
31 }
32 const int N=2e5+20;
33 const ll mod=1e9+7;
34 ll f[N],n,m,A,B;
35 ll powmod(ll x,ll n)
36 {
37     ll s=1;
38     while(n){
39         if(n&1)
40             s=(s*x)%mod;
41         x=(x*x)%mod;
42         n>>=1;
43     }
44     return s;
45 }
46 ll C(ll n,ll m)
47 {
48     ll a=f[n];
49     ll b=(f[m]*f[n-m])%mod;
50     return (a*powmod(b,mod-2))%mod;
51 }
52 int main()
53 {
54     f[0]=1;
55     for(ll i=1;i<N;i++)
56         f[i]=(f[i-1]*i)%mod;
57     while(cin>>n>>m>>A>>B){
58         ll res=0;
59         for(ll i=B+1;i<=m;i++){
60             ll tmp=(C(i-1+n-A-1,n-A-1)*C(m-i+A-1,m-i))%mod;
61             res=(res+tmp)%mod;
62         }
63         cout<<res<<endl;
64     }
65     return 0;
66 }
posted @ 2017-08-04 20:51  wydxry  阅读(77844)  评论(0编辑  收藏  举报
Live2D