矩阵快速幂

看题:

传送门

就是说a[1]=a,a[2]=b--------a[n]=2*a[n-2]+a[n-1]+n^4

注意这个a[n-1]=a[n-1]

这个第二行没有想到

 代码:这个就是个板子,换一个矩阵和N就行

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
const int N = 7;
const LL mod = 2147493647;
LL n, m;
struct mac {
    LL c[N][N];
    void reset() {
        memset(c, 0, sizeof(c));
        for (int i = 0; i <= 6; i++)
            c[i][i] = 1;
    }
};
mac multi(mac a, mac b)
{
    mac ans;
    for (int i = 0; i <= 6; i++)
        for (int j = 0; j <= 6; j++) {
            ans.c[i][j] = 0;
            for (int k = 0; k <= 6; k++) {
                ans.c[i][j] += (a.c[i][k] * b.c[k][j]) % mod;
                ans.c[i][j] %= mod;
            }
        }
    return ans;
}
mac Pow(mac a, LL b)
{
    mac ans; ans.reset();
    while (b) {
        if (b & 1)
            ans = multi(ans, a);
        a = multi(a, a);
        b >>= 1;
    }
    return ans;
}
int main()
{
    int t; 
    LL n,kk,ll;
    mac a={
        1,2,1,0,0,0,0,
        1,0,0,0,0,0,0,
        0,0,1,4,6,4,1,
        0,0,0,1,3,3,1,
        0,0,0,0,1,2,1,
        0,0,0,0,0,1,1,
        0,0,0,0,0,0,1
    };
    cin>>t;
    while(t--){
        cin>>n>>kk>>ll; 
        if(n==1){
            printf("%lld
",kk);
        }
        else if(n==2){
            printf("%lld
",ll);
        }
        else{
            mac res=Pow(a,n-2);
            LL ans=(ll*res.c[0][0]%mod+kk*res.c[0][1]%mod+81*res.c[0][2]%mod+27*res.c[0][3]%mod+9*res.c[0][4]%mod+3*res.c[0][5]%mod+res.c[0][6]%mod)%mod;
            printf("%lld
",ans%mod);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lipu123/p/14026449.html