问题 H: Hopscotch
问题 H: Hopscotch
时间限制: 5 Sec 内存限制: 128 MB提交: 42 解决: 18
[提交][状态][讨论版][命题人:admin]
题目描述
You’re playing hopscotch! You start at the origin and your goal is to hop to the lattice point (N, N). A hop consists of going from lattice point (x1, y1) to (x2, y2), where x1 < x2 and y1 < y2.
You dislike making small hops though. You’ve decided that for every hop you make between two lattice points, the x-coordinate must increase by at least X and the y-coordinate must increase by at least Y .
Compute the number of distinct paths you can take between (0, 0) and (N, N) that respect the above constraints. Two paths are distinct if there is some lattice point that you visit in one path which you don’t visit in the other.
Hint: The output involves arithmetic mod 109+ 7. Note that with p a prime like 109+ 7, and x an integer not equal to 0 mod p, then x(xp−2) mod p equals 1 mod p.
You dislike making small hops though. You’ve decided that for every hop you make between two lattice points, the x-coordinate must increase by at least X and the y-coordinate must increase by at least Y .
Compute the number of distinct paths you can take between (0, 0) and (N, N) that respect the above constraints. Two paths are distinct if there is some lattice point that you visit in one path which you don’t visit in the other.
Hint: The output involves arithmetic mod 109+ 7. Note that with p a prime like 109+ 7, and x an integer not equal to 0 mod p, then x(xp−2) mod p equals 1 mod p.
输入
The input consists of a line of three integers, N X Y . You may assume 1 ≤ X, Y ≤ N ≤ 106.
输出
The number of distinct paths you can take between the two lattice points can be very large. Hence output this number modulo 1 000 000 007 (109+ 7).
样例输入
7 2 3
样例输出
9
我们枚举多少步到达终点,然后判断在这种情况下,x有多少种方案,y有多少种,即相乘
打表找规律
若该数是21 ,x为3或4
表如下:
次数 1 2 3 4 5
3 1 16 91 220 210
4 1 14 55 56 5
通过这几项发现为斜的组合数
组合数表如下:
1,1,
1,2,1,
1,3,3,1,
1,4,6,4,1,
1,5,10,10,5,1,
1,6,15,20,15,6,1,
1,7,21,35,35,21,7,1,
1,8,28,56,70,56,28,8,1,
1,9,36,84,126,126,84,36,9,1,
1,10,45,120,210,252,210,120,45,10,1,
1,11,55,165,330,462,462,330,165,55,11,1,
1,12,66,220,495,792,924,792,495,220,66,12,1,
1,13,78,286,715,1287,1716,1716,1287,715,286,78,13,1,
1,14,91,364,1001,2002,3003,3432,3003,2002,1001,364,91,14,1,
1,15,105,455,1365,3003,5005,6435,6435,5005,3003,1365,455,105,15,1,
1,16,120,560,1820,4368,8008,11440,12870,11440,8008,4368,1820,560,120,16,1,
比赛的时候找的规律,然后代码一直运行错误,最后发现可能是自己写的组合数有问题
c++ code:
#include<bits/stdc++.h> using namespace std; const int mod= 1e9+7; const int MOD= 1e9+7; typedef long long ll; const int N=2e6+20; ll inv(ll b){ return b==1||b==0?1:(mod-mod/b)*inv(mod%b)%mod; } ll f[N]; ll C(ll n,ll m) { return f[n]*inv(f[m])%mod*inv(f[n-m])%mod; } void init() { f[0]=1; for(int i=1;i<=N;i++) f[i]=f[i-1]*i%mod; } int main() { init(); ll n,x,y; scanf("%lld%lld%lld",&n,&x,&y); ll ans=0,len=n/max(x,y); ll xn=(n-2*x+1),yn=(n-2*y+1); //cout<<xn<<" "<<yn<<" "<<len<<endl; if(len>=1) ans=1; for(int i=2;i<=len;i++) { // cout<<C(xn-(x-1)*(i-2),i-1)<<" "<<C(yn-(y-1)*(i-2),i-1)<<endl; ans=(ans%mod+C(xn-(x-1)*(i-2),i-1)*C(yn-(y-1)*(i-2),i-1)%mod)%mod; } printf("%lld\n",ans); return 0; }