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 aftern seconds the robot is still located on the origin point?
The answer may be large. Please output the answer modulo1,000,000,007
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
The answer may be large. Please output the answer modulo
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 integern(1≤n≤1,000,000) .
The only line contains one integer
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
- HDU 5673 Robot
- HDU 5673 Robot
- Robot HDU
- HDU 5673 Robot【卡特兰数】
- HDU 5673 Robot(卡特兰数)
- hdu 1035 Robot Motion
- HDU 1035 Robot Motion
- HDU-1035 ROBOT NOTION
- hdu 1035 Robot Motion
- hdu 1035 Robot Motion
- hdu 4375 Programmable Robot
- hdu~4166~Robot Navigation
- hdu 1035 Robot Motion
- HDU 1035 Robot Motion
- hdu 1035 Robot Motion
- hdu 1035 robot motion
- HDU-1035Robot Motion
- hdu 1035 Robot Motion
- 搭建Android开发环境
- HDU 5671 Matrix
- Web开发(二)--JSP
- MATLAB函数运算
- IIR型高斯滤波的原理及实现
- HDU 5673 Robot
- 分类与扩展
- 求最长有效匹配括号子串的长度(Longest Valid Parentheses)
- iOS--UINavigationController学习笔记
- link和@import的区别
- 几个Android开发中遇到的问题
- servlet详解(第三篇)
- android 的四种枚举Context.MODE_PRIVATE
- 安装Baidu Eclipse插件