hdu5673 Robot 卡特兰数+组合数学+线性筛逆元

Robot

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 483    Accepted Submission(s): 244


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
3 1 2 4
 
Sample Output
1 2 9
 
Source
 
/**
题目:Robot
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5673
题意:在x轴上,机器人从原点出发,如果它在原点,他只可以向右走一格,或者停留原处(表明机器人不可以到负数坐标的位置);
如果不在原点,它可以向右,向左,停留原地;每次操作花费1秒;问n秒后,机器人回到原点的行走方法数;

思路:
过程中一定是向右走的步数>=向左走的步数,最后是相等。想到了什么?括号匹配? 求方法数->卡特兰数。
现在还有一个是停在原地。设停在原地为y次。向右走为x次,那么向左走也为x次。
2*x+y==n;
那么确定了x,y。方法数:C(n,y)*h(x). 很显然;

*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int mod=1e9+7;
const int maxn=1e6+5;
LL h[maxn], c[maxn], inv[maxn];
void init()
{
    inv[1] = 1;
    for(int i = 2; i < maxn; i++){
        inv[i] = (mod-mod/i)*inv[mod%i]%mod;
    }
    h[0] = 1;
    for(int i = 1; i < maxn; i++){
        h[i] = (4*i-2)*h[i-1]%mod*inv[i+1]%mod;
    }
}
int main()
{
    init();
    int T;
    int n;
    cin>>T;
    while(T--)
    {
        scanf("%d",&n);
        LL ans = 0;
        c[0] = 1;
        for(int i = 1; i <= n; i++){///c(n,i);
            c[i] = (n-i+1)*c[i-1]%mod*inv[i]%mod;
        }
        for(int x = 0; x*2<=n; x++){
            int y = n-2*x;
            ans = (ans+c[y]*h[x]%mod)%mod;
        }
        printf("%lld
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/xiaochaoqun/p/6884746.html