HDU6172 Array Challenge

Array Challenge

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



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
  
  
3 4 7 9
 

Sample Output
  
  
1255 324725 13185773
 

题目解析:打表发现f n=4*fn-1+17*fn-2-12*fn-3用矩阵快速幂即可解决问题。
AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>

using namespace std;
typedef long long LL;
const int mod=1e9+7;

struct matrix
{
    LL v[3][3];
    matrix()
    {
        memset(v,0,sizeof(v));
    }
};

matrix M,E,ans;

void Init()
{
    for(int i=0; i<3; i++) E.v[i][i]=1;
    M.v[0][0]=4;
    M.v[0][1]=17;
    M.v[0][2]=-12;
    M.v[1][0]=1;
    M.v[1][1]=0;
    M.v[1][2]=0;
    M.v[2][0]=0;
    M.v[2][1]=1;
    M.v[2][2]=0;
}

matrix mul(matrix a,matrix b)
{
    matrix c;
    for(int i=0; i<3; i++)
        for(int j=0; j<3; j++)
            for(int k=0; k<3; k++)
                c.v[i][j]=(c.v[i][j]%mod+a.v[i][k]*b.v[k][j]%mod+mod)%mod;
    return c;
}

matrix pow(matrix p,LL k)
{
    matrix tmp=E;
    while(k)
    {
        if(k&1)
        {
            tmp=mul(tmp,p);
            k--;
        }
        k>>=1;
        p=mul(p,p);
    }
    return tmp;
}


int main()
{
    LL n;
    Init();
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld",&n);
        if(n==2) printf("31\n");
        else if(n==3) printf("197\n");
        else if(n==4) printf("1255\n");
        else
        {
            ans=pow(M,n-4);
            LL res=(ans.v[0][0]*1255%mod+ans.v[0][1]*197%mod+ans.v[0][2]*31%mod+mod)%mod;
            printf("%lld\n",res);
        }
    }
    return 0;
}





Source
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值