HDU 6172-Array Challenge

Array Challenge
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others)
Total Submission(s): 97 Accepted Submission(s): 38

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 (an) modular 1000000007.

Sample Input

3
4
7
9

Sample Output

1255
324725
13185773

Source
2017 Multi-University Training Contest - Team 10

题意: h0=2,h1=3,h2=6,hn=4hn1+17hn212hn316
bn=3hn+1hn+9hn+1hn1+9h2n+27hnhn118hn+1126hn81hn1+192(n>0)
an=bn+4n
(an)
一波打表后可以发现答案和 hn 十分接近,由此猜想答案和 hn 有较接近的递推公式,实际上 ansn=4ansn1+17ansn212ansn3
事实上进一步地有 ansn=4ansn17ansn2
n1015 ,用矩阵快速幂做, [0417][ansnansn+1]=[ansn+1ansn+2]

  • AC代码
#include <bits/stdc++.h>
using namespace std;
#define mem(s,v) memset(s,v,sizeof(s))
typedef long long ll;
const double PI = acos(-1);
const ll MOD = 1000000007;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 2;//方阵维数
struct Mat{
    ll mat[N][N];
}E;
//初始化单位矩阵
Mat init(){
    Mat E;
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            if(i == j)
            E.mat[i][i] = 1;
            else
            E.mat[i][j] = 0;
        }
    }
    return E;
}
//重载乘法
Mat operator *(Mat a,Mat b){
    Mat c;
    memset(c.mat,0,sizeof(Mat));
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            for(int k = 0; k < N; k++){
                if(a.mat[i][k] && b.mat[k][j]){
                    c.mat[i][j] += a.mat[i][k] * b.mat[k][j];
                    c.mat[i][j] %= MOD;
                }
            }
        }
    }
    return c;
}
//重载加法
Mat operator +(Mat a,Mat b){
    Mat c;
    memset(c.mat,0,sizeof(Mat));
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            c.mat[i][j] = a.mat[i][j] + b.mat[i][j];
        }
    }
    return c;
}
//矩阵快速幂
Mat operator ^(Mat A,ll x){
    Mat c;
    c = init();
    for(; x ; x >>= 1){
        if(x&1){
            c = c*A;
        }
        A = A*A;
    }
    return c;
}

ll get(ll x){
    if(x == 2)return 31;
    if(x == 3)return 197;
    Mat EE;
    EE.mat[0][0] = 0;
    EE.mat[0][1] = 1;
    EE.mat[1][0] = -4;
    EE.mat[1][1] = 7;
    ll beg[2] = {31,197};
    EE = EE^(x - 3);
    ll s1 = EE.mat[0][0]*beg[0] + EE.mat[0][1]*beg[1];
    ll s2 = EE.mat[1][0]*beg[0] + EE.mat[1][1]*beg[1];
    while(s2<0) s2+=MOD;
    s2%=MOD;
    return s2;
}
int main(){
    int T;
    scanf("%d",&T);
    ll n;
    while(T--){
        scanf("%lld",&n);
        printf("%lld\n",get(n));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值