【默慈金数+求逆元】HDU5673Robot【BestCoder Round #81 (div.2)】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5673

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

数论啊数论。。。。

首先,必须知道这是个默慈金数的模板题,知道是模板题之后,因为是取模,所以我们直接可以用long long 型来解决问题,但是需要注意的是,不能用除法,所以这里我们就又学到了一点,那就是逆元。日后来好好整理orz........


代码:

#include<iostream>
#define LL long long
#define mod 1000000007
using namespace std;
const int N=1e6+5;
LL M[N],inv[N];
int main()
{
    int t,n;
    inv[1]=1;
    //  求逆元
    for(int i=2;i<N;i++){
        inv[i]=(mod-mod/i)*inv[mod%i]%mod;
    }
    M[0]=1;
    M[1]=1;
    //  求默慈金数
    for(int i=2;i<=N-5;i++){
        LL m1=M[i-1]*(2*i+1)%mod;
        LL m2=(3*i-3)*M[i-2]%mod;
        M[i]=(m1+m2)%mod*inv[i+2]%mod;     //  这里直接除法,有问题,这里我们需要用到逆元
    }
    cin.sync_with_stdio(false);
    cin>>t;
    while(t--){
        cin>>n;
        cout<<M[n]<<endl;
    }
    return 0;
}


你可能感兴趣的:(【默慈金数+求逆元】HDU5673Robot【BestCoder Round #81 (div.2)】)