51nod 1120 机器人走方格 V3

N * N的方格,从左上到右下画一条线。一个机器人从左上走到右下,只能向右或向下走。
并要求只能在这条线的上面或下面走,不能穿越这条线,有多少种不同的走法?
由于方法数量可能很大,只需要输出Mod 10007的结果。
 
Input
输入一个数N(2 <= N <= 10^9)。
Output
输出走法的数量 Mod 10007。
Input示例
4
Output示例
10
————————————————————————————
这题是裸的卡特兰数 不过因为mod比2*n小 所以要加上lucas 然后就没辣
详情请自行百度卡特兰数
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
const int mod=10007,M=2e4+7;
int read(){
    int ans=0,f=1,c=getchar();
    while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}
    while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();}
    return ans*f;
}
int n,m;
LL w[M],b[M];
LL pmod(LL a,LL b){
    LL ans=1;
    while(b){
        if(b&1) ans=ans*a%mod;
        b>>=1; a=a*a%mod;
    }return ans;
}
void prepare(){
    int mx=mod-1;
    w[0]=1; for(int i=1;i<=mx;i++) w[i]=w[i-1]*i%mod;
    b[mx]=pmod(w[mx],mod-2);
    for(int i=mx;i;i--) b[i-1]=b[i]*i%mod;
}
LL C(LL n,LL m){
    if(n<m) return 0;
    if(n<mod) return w[n]*b[m]%mod*b[n-m]%mod; 
    return C(n/mod,m/mod)*C(n%mod,m%mod);//C(n%mod,m%mod)=w[n%mod]*b[m%mod]%mod*b[n%mod-m%mod]%mod
}
int main(){
    n=read()-1; prepare();
    printf("%lld\n",(2*(C(2*n,n)-C(2*n,n-1))%mod+mod)%mod);
    return 0;
}
View Code

 

 
posted @ 2017-10-28 19:15  友人Aqwq  阅读(168)  评论(0编辑  收藏  举报