mymilkbottles

越努力,越幸运! 以现在大多数人的努力程度之低,根本轮不到可以拼天赋。...

hdu5673(默慈金数、卡特兰数)

Robot
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 337 Accepted Submission(s): 173

Problem Description
There is a robot on the origin point of an axis.Every second, the robot can move right one unit length or do nothing.If the robot is
on the right of origin point,it can also move left one unit length.A route is a series of movement. How many different routes there are
that after n seconds the robot is still located on the origin point?
The answer may be large. Please output the answer modulo 1,000,000,007

Input
There are multiple test cases. The first line of input contains an integer T(1≤T≤100) indicating the number of test cases. For each test case:

The only line contains one integer n(1≤n≤1,000,000).

Output
For each test case, output one integer.

Sample Input

3
1
2
4

Sample Output

1
2
9

#include<cstdio>

typedef long long ll;
const ll mod=1000000007;
const int maxn=1000005;
ll catalan[maxn],inv[maxn],fact[maxn];

ll Pow(ll n,ll m){
    ll sum=1,t=n;
//    printf("%lld %lld\n",n,m);
    while(m){
        if(m&1){
            sum*=t;
            if(sum>=mod)sum%=mod;
        }
        t*=t;
        if(t>=mod)t%=mod;
        m>>=1;
    }
    return sum;
}

ll C(ll n,ll m){
    if(m>n||m<0)return 0;
    return fact[n]*inv[m]%mod*inv[n-m]%mod;
}

int main(){
//    printf("%lld %lld\n",Pow(2,3),Pow(2,0));return 0;
    catalan[0]=1;
    for(int i=1;i<maxn;++i){
        catalan[i]=(catalan[i-1]*(4*i-2)%mod)*Pow(i+1,mod-2)%mod;
    }
    fact[0]=1;
    for(int i=1;i<maxn;++i){
        fact[i]=fact[i-1]*i%mod;
    }
    inv[0]=1;
    for(int i=1;i<maxn;++i){
        inv[i]=Pow(fact[i],mod-2)%mod;
    }

    int T;
    scanf("%d",&T);
    while(T--){
        int n;
        scanf("%d",&n);
        ll ans=0;
        for(int i=0;i<=(n>>1);++i){
            ans=ans+((catalan[i]*C((ll)n,(ll)i<<1))%mod);
            ans%=mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mymilkbottles/article/details/51560077
个人分类: ACM_数论
上一篇hdu5672 string(尺取法)
下一篇hdu5666
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭