Hopscotch(数论,组合数学)

5095: Hopscotch

时间限制: 5 Sec   内存限制: 128 MB
提交: 46   解决: 19
[ 提交][ 状态][ 讨论版][命题人: 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.

输入

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

提示

来源

mcpc2017 


题意:从(0,0)点走到(n,n)点,每次x坐标的增长值不能小于X,y坐标的增长值不能小于Y,一共有多少种走法。

题解:x走的步数等于y走的步数,可以枚举走几步到达终点,例如走i步走到终点,则可以每步走x-1,则问题变为剩下的n-(x-1)*i步分给这i步,有多少种分发,可以用隔板法,n-(x-1)*i中间有n-(x-1)*i-1个空,将i-1个隔板插到这些空中,那么有C(i-1,n-(x-1)*i-1)种插法( 组合数C(m,n)=n!/(m!*(n-m)!) ),答案即为枚举的i的x轴的走法*y轴的走法再求和。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
#define mem(a,b) memset(a,b,sizeof(a))
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;

inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}

ll jie[1000005],dpx[1000005],dpy[1000005];

void init()
{
    jie[0]=1;
    for(int i=1;i<=1000000;i++)
        jie[i]=(jie[i-1]*i)%mod;
    for(int i=1;i<=1000000;i++)
    {
        dpx[i]=1;
        dpy[i]=1;
    }
}

ll c(ll b,ll a)
{
    return jie[a]*qpow(jie[b]*jie[a-b]%mod,mod-2)%mod;
}

int main()
{
    init();
    ll n,x,y;

    cin>>n>>x>>y;

    ll ans=0;
    for(ll i=1;i*x<=n;i++)
        dpx[i]=c(i-1,n-(x-1)*i-1);

    for(ll i=1;i*y<=n;i++)
        dpy[i]=c(i-1,n-(y-1)*i-1);

    for(ll i=1;i*x<=n && i*y<=n;i++)
        ans=(ans+dpx[i]*dpy[i]%mod)%mod;

    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sudu6666/article/details/80052303
今日推荐