HDU 5673 Robot

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

组合数学问题,可以看成是一个只有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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值