HDU 5673 Robot 数学

Robot

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5673

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(1≤T≤100) indicating the number of test cases. For each test case:

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

Output

For each test case, output one integer.

Sample Input

3
1
2
4

Sample Output

1
2
9

Hint

题意

有一个机器人位于坐标原点上。每秒钟机器人都可以向右移到一个单位距离,或者在原地不动。如果机器人的当前位置在原点右侧,它同样可以
向左移动单位距离。一系列的移动(左移,右移,原地不动)定义为一个路径。问有多少种不同的路径,使得\(n\)秒后机器人仍然位于坐标原点?
答案可能很大,只需输出答案对\(1,000,000,007\)的模。

题解:

作为数学智障……

象征性的oeis了一波,然后第一个就是……

http://oeis.org/A001006

公式抄过来就AC了……

代码

//(n+2)a(n) = (2n+1)a(n-1)+(3n-3)a(n-2)

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
const int mod = 1e9+7;
long long dp[maxn];
long long quickpow(long long  m,long long n,long long k)//返回m^n%k
{
    long long b = 1;
    while (n > 0)
    {
          if (n & 1)
             b = (b*m)%k;
          n = n >> 1 ;
          m = (m*m)%k;
    }
    return b;
}
void pre()
{
    dp[1]=1;
    dp[2]=2;
    for(int i=3;i<maxn;i++)
        dp[i]=(1ll*(2*i+1)*dp[i-1]+1ll*(3*i-3)*dp[i-2])%mod*quickpow(i+2,mod-2,mod)%mod;
}
void solve()
{
    int x;
    scanf("%d",&x);
    printf("%lld\n",dp[x]);
}
int main()
{
    pre();
    int t;scanf("%d",&t);
    while(t--)solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值