矩阵快速幂模板

const long long mod = 2147493647;
struct prog
{
    long long a[8][8];
};
prog s,B;
prog matrixmul(prog a,prog b)
{
    prog c;
    for(int i=1;i<8;++i)for(int j=1;j<8;++j)
    {
        c.a[i][j]=0;
        for(int k=1;k<8;k++)
            c.a[i][j]+=(a.a[i][k]*b.a[k][j])%mod;
        c.a[i][j]%=mod;
    }
    return c;
}
prog mul(prog s,int k)
{
    prog ans;
    for(int i=1;i<8;++i)for(int j=1;j<8;++j) ans.a[i][j]=(i==j)?1:0;
    while(k){
        if(k&1)
            ans=matrixmul(ans,s);
        k>>=1;
        s=matrixmul(s,s);
    }
    return ans;
}

例题:

Recursive sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2864    Accepted Submission(s): 1275


Problem Description
Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers a and b on a blackboard. And then, the cows would say their identity number one by one. The first cow says the first number a and the second says the second number b. After that, the i-th cow says the sum of twice the (i-2)-th number, the (i-1)-th number, and i4. Now, you need to write a program to calculate the number of the N-th cow in order to check if John’s cows can make it right.
 

Input
The first line of input contains an integer t, the number of test cases. t test cases follow.
Each case contains only one line with three numbers N, a and b where N,a,b < 231 as described above.
 

Output
For each test case, output the number of the N-th cow. This number might be very large, so you need to output it modulo 2147493647.
 

Sample Input
23 1 24 1 10
 
Sample Output
85
369
Hint
In the first case, the third number is 85 = 2*1十2十3^4. In the second case, the third number is 93 = 2*1十1*10十3^4 and the fourth number is 369 = 2 * 10 十 93 十 4^4.

测试数据是:

有两组测试数据,

三个数,f(1) = 1,f(2) = 2;

f(3) = 2 * f(1) + f(2) + 3^4 = 85;输出f(n) ,即f(3)

大致题意就是这样;

可以参考这个题解

代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;
const long long mod = 2147493647;
struct prog
{
    long long a[8][8];
};
prog s,B;
prog matrixmul(prog a,prog b)
{
    prog c;
    for(int i=1;i<8;++i)for(int j=1;j<8;++j)
    {
        c.a[i][j]=0;
        for(int k=1;k<8;k++)
            c.a[i][j]+=(a.a[i][k]*b.a[k][j])%mod;
        c.a[i][j]%=mod;
    }
    return c;
}
prog mul(prog s,int k)
{
    prog ans;
    for(int i=1;i<8;++i)for(int j=1;j<8;++j) ans.a[i][j]=(i==j)?1:0;
    while(k){
        if(k&1)
            ans=matrixmul(ans,s);
        k>>=1;
        s=matrixmul(s,s);
    }
    return ans;
}
int main()
{
    int n,t,a,b;
    for(scanf("%d",&t);t--;){
        scanf("%d %d %d",&n,&a,&b);
        if(n==1){printf("%lld
",a%mod);continue;}
        if(n==2){printf("%lld
",b%mod);continue;}
        if(n==3){printf("%lld
",(81+2*a%mod+b%mod)%mod);continue;}
        n-=2;
        for(int i=1;i<=7;++i)for(int j=1;j<=7;++j) s.a[i][j]=0,B.a[i][j]=0;
        for(int i=1; i<=5; i++)s.a[i][1]=1;
        for(int i=2; i<=5; i++)s.a[i][2]=i-1;
        s.a[3][3]=1;s.a[4][3]=3;s.a[5][3]=6;
        s.a[4][4]=1;s.a[5][4]=4;
        s.a[5][5]=1;s.a[6][5]=1;
        s.a[6][6]=1;s.a[7][6]=1;
        s.a[6][7]=2;
        B.a[1][1]=1;B.a[2][1]=3;B.a[3][1]=9;B.a[4][1]=27;B.a[5][1]=81;B.a[6][1]=b;B.a[7][1]=a;
        s=mul(s,n);
        s=matrixmul(s,B);
        printf("%lld
",s.a[6][1]%mod);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/lu1nacy/p/10016659.html