HDU 5673 Robot

来源:互联网 发布:上海婚纱摄影推荐知乎 编辑:程序博客网 时间:2024/06/07 02:06
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(1T100) indicating the number of test cases. For each test case:

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

Output
For each test case, output one integer.
 

Sample Input
3124
 

Sample Output
129

组合数学问题,可以看成是一个只有0,1,-1的序列,任何时候前缀和都大于等于0,然后把0先拿出来,剩下的相当于是括号匹配的数量

那么就是卡特兰数,然后就是把0插入的过程,相当于把n个相同的球放到m个不同的盒子里,等于C(n-1,n-1+m),然后预处理一下

各个数组即可。然而其实有更简单的办法,这玩意儿叫做默慈金数,百度可以找到线性的递推公式。。。。。。。

#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;const int low(int x) { return x&-x; }const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int maxn = 1e6 + 10;int T, n;int h[maxn], inv[maxn], c[maxn], cc[maxn];int Inv(int x){    if (x < maxn) return inv[x];    return (LL)Inv(mod%x)*(mod - mod / x) % mod;}void init(){    cc[0] = c[0] = h[0] = inv[1] = 1;    for (int i = 2; i < maxn; i++)    {        inv[i] = (LL)inv[mod%i] * (mod - mod / i) % mod;    }    for (int i = 1; i < maxn - 1; i++)    {        h[i] = (LL)h[i - 1] * (4 * i - 2) % mod*inv[i + 1] % mod;        c[i] = (LL)c[i - 1] * i % mod;        cc[i] = Inv(c[i]);    }}int C(int x, int y){    return (LL)c[y] * cc[x] % mod * cc[y - x] % mod;}int main(){    init();    scanf("%d", &T);    while (T--)    {        scanf("%d", &n);        int ans = 0;        for (int i = 0; i + i <= n; i++)        {            int m = n - i - i, k = i + i + 1;            (ans += (LL)C(k - 1, m + k - 1)*h[i] % mod) %= mod;        }        printf("%d\n", ans);    }    return 0;}Close


0 0