题目链接:hdoj 5673 Robot
有一个机器人位于坐标原点上。每秒钟机器人都可以向右移到一个单位距离,或者在原地不动。如果机器人的当前位置在原点右侧,它同样可以
向左移动单位距离。一系列的移动(左移,右移,原地不动)定义为一个路径。问有多少种不同的路径,使得n秒后机器人仍然位于坐标原点?
答案可能很大,只需输出答案对1,000,000,007的模。
51nod的某一场有这样一道题目。。。
AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const int MAXN = 1e6 + 10;
const int MOD = 1e9 + 7;
LL pow_mod(LL a, LL n) {
LL ans = 1LL;
while(n) {
if(n & 1LL) {
ans = ans * a % MOD;
}
a = a * a % MOD;
n >>= 1LL;
}
return ans;
}
LL f[MAXN];
void get()
{
f[1] = 1LL; f[2] = 2;
for(int i = 2; i < 1000000; i++) {
f[i+1] = (f[i] * (2 * i + 3) % MOD * pow_mod(i + 3, MOD - 2) % MOD + 3 * i * f[i-1] % MOD * pow_mod(i + 3, MOD - 2) % MOD) % MOD;
}
}
int main()
{
get();
int t; scanf("%d", &t);
while(t--) {
int n; scanf("%d", &n);
printf("%I64d\n", f[n]);
}
return 0;
}