2017 Multi-University Training Contest

来源:互联网 发布:在js中引入html页面 编辑:程序博客网 时间:2024/06/04 00:21

Array Challenge

                                                                Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 153428/153428 K (Java/Others)
                                                                                   Total Submission(s): 393    Accepted Submission(s): 206


Problem Description
There’s an array that is generated by following rule.
h0=2,h1=3,h2=6,hn=4hn1+17hn212hn316
And let us define two arrays bnandan as below.
bn=3hn+1hn+9hn+1hn1+9h2n+27hnhn118hn+1126hn81hn1+192(n>0)
an=bn+4n
Now, you have to print (an) , n>1.
Your answer could be very large so print the answer modular 1000000007.
 

Input
The first line of input contains T (1 <= T <= 1000) , the number of test cases.
Each test case contains one integer n (1 < n <= 1015) in one line.
 

Output
For each test case print &#8970;√(a_n )&#8971; modular 1000000007.
 

Sample Input
3479
 

Sample Output
125532472513185773

官方题解:http://bestcoder.hdu.edu.cn/blog/


√(a_n )=h(n+1)+h(n)-6*h(n-1)+8;即矩阵快速幂;

#include<bits/stdc++.h>using namespace std;const long long MOD=1000000007;struct lenka{    long long a[4][4];};lenka cla(const lenka& a,const lenka& b){    lenka c;    memset(c.a,0,sizeof c.a);    for(int i=0;i<4;i++)    {        for(int j=0;j<4;j++)        {            for(int k=0;k<4;k++)            {                c.a[i][j]+=a.a[i][k]*b.a[k][j];                c.a[i][j]=(c.a[i][j]+MOD)%MOD;  //负数取模            }        }    }    return c;}long long POW(long long n){    if(n==3)return 31;    n-=4;    lenka res,a,ans;    memset(res.a,0,sizeof res.a);    memset(a.a,0,sizeof a.a);    memset(ans.a,0,sizeof ans.a);    for(int i=0;i<4;i++)res.a[i][i]=1;    a.a[0][0]=4,a.a[0][1]=1;    a.a[1][0]=17,a.a[1][2]=1;    a.a[2][0]=-12;    a.a[3][0]=-16,a.a[3][3]=1;    while(n)    {        if(n%2)res=cla(res,a);        a=cla(a,a);        n/=2;    }    ans.a[0][0]=190,ans.a[0][1]=35,ans.a[0][2]=6,ans.a[0][3]=1;//初始化为h4,h3,h2    ans=cla(ans,res);    return (((ans.a[0][0]+ans.a[0][1])%MOD-6*ans.a[0][2])%MOD+8ll+MOD)%MOD; //注意取模}int main(){    int T;cin>>T;    while(T--)    {        long long n;        cin>>n;        cout<<POW(n+1)<<endl;    }    return 0;}