题目链接: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
2 3 1 1
Sample Output 1
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
10 7 3 4
Sample Output 2
3570
There are 12 forbidden cells.
Sample Input 3
100000 100000 99999 99999
Sample Output 3
1
Sample Input 4
100000 100000 44444 55555
Sample Output 4
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 }
今天的文章いろはちゃんとマス目 / Iroha and a Grid (组合数学)分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/45736.html