6451: Iroha and a Grid
时间限制: 1 Sec 内存限制: 128 MB提交: 226 解决: 67
[ 提交][ 状态][ 讨论版][命题人: admin]
题目描述
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
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
输入
The input is given from Standard Input in the following format:
H W A B
H W A B
输出
Print the number of ways she can travel to the bottom-right cell, modulo 109+7.
样例输入
2 3 1 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".
分为两部分求解
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<queue>
#include<vector>
#include<map>
#include<algorithm>
#define mod 1000000007
using namespace std;
long long jie[1000005];
long long qpow(long long a, long long b)
{
long long ans=1;
while(b)
{
if(b%2)
ans=(ans*a)%mod;
a=(a*a)%mod;
b/=2;
}
return ans%mod;
}
void f()
{
jie[0]=1;
for(int i=1;i<1000001;i++)
jie[i]=(jie[i-1]*i)%mod;
}
long long zuhe(long long n, long long m)
{
long long a=jie[n];
long long b=(jie[m]*jie[n-m])%mod;
return (a*qpow(b, mod-2))%mod;
}
int main()
{
f();
long long h, w, a, b, ans=0;
scanf("%lld%lld%lld%lld", &h, &w, &a, &b);
for(int i=b+1;i<=w;i++)
{
long long temp=(zuhe(h-a+i-2, h-a-1)%mod*zuhe(w-i+a-1, w-i)%mod)%mod;
ans=(ans+temp)%mod;
}
printf("%lld\n", ans);
return 0;
}
/**************************************************************
Problem: 6451
User: ldu_reserver201701
Language: C++
Result: 正确
Time:112 ms
Memory:8904 kb
****************************************************************/