Combinatorics Hopscotch

Topic links: https://nanti.jisuanke.com/t/40451

Meaning of the questions: from (0,0) come (N, N), each abscissa least go x, ordinate least go y, inquired to an end at least how many moves.

Analysis: start with a one-dimensional point of view, only provides for a minimum, but does not specify a maximum, then theoretically you can go from x each step (a total of n / x steps) to each pass n steps (a total of 1 step) so a variety of moves. This is just under one-dimensional case, while two-dimensional though how many steps each pass are independent of each other, but the number of steps to go is common, for example, n = 6, x = 2, y = 3 If you want to go up step number, then, need to take three steps x and y can only take two steps, so x must take two steps. And go that one step is to limit the number to go small.

Then start analyzing the formula, which is actually a grouping problem, from a one-dimensional look, about to step assigned to n m (m is the number of steps to go) the group, and each group had to walk at least x steps, we can first of each group x-1 sub-step, after at least one step of each fraction on it, this method can be implemented by a separator, i.e., m-1 plates inserted in the space between the n-1 in the n items, put the n Item divided into m groups

Is the formula

Direct template code number combination, the first parameter is the index of c, is the second subscript, is FAC factorial, INV is inverse, is defined using maxn, mod, initialization init () function can be

#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>
typedef long long ll;
using namespace std;
ll mod = 1000000007;
const int maxn = 1000010;
 
ll qpow(ll a,ll x){
    ll ret=1;
    while (x){
        if (x&1)
            ret = ret*a%mod;
        a=a*a%mod;
        x>>=1;
    }
    return ret;
}
ll fac[maxn],inv[maxn];
 
ll init(){
    fac[0]=1;
    for (int i=1;i<maxn;i++)
        fac[i]=fac[i-1]*i%mod;
    inv[maxn-1]=qpow(fac[maxn-1],mod-2);
    for (int i=maxn-2;i>=0;i--)
        inv[i]=inv[i+1]*(i+1)%mod;
    return 0;
}
 
ll c(ll n,ll m){
    if (n<m) return 0;
    return fac[n]*inv[m]%mod*inv[n-m]%mod;
}
 
ll dp1[maxn],dp2[maxn];
 
 
int main() {
    init();
    ll n,x,y;
    scanf("%lld%lld%lld",&n,&x,&y);

    for (int i=1;i*x<=n;i++)
    {
        dp1[i]=c(n-(x-1)*i-1,i-1);
    }
 
    for (int i=1;i*y<=n;i++)
    {
        dp2[i]=c(n-(y-1)*i-1,i-1);
    }
 
    ll ans = 0;
    for (int i=1;i*x<=n&&i*y<=n;i++)
        ans =( ans + dp1[i]*dp2[i]%mod)%mod ;
 
    printf("%lld\n",ans);
 
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/qingjiuling/p/11297207.html