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

 

 

转载于:https://www.cnblogs.com/lyzuikeai/p/7747777.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值