lightoj 1096【矩阵快速幂(作为以后的模板)】

敲打基础矩阵快速幂何必看题解

#include <bits/stdc++.h>
using namespace std;

/*
0 1 2 3 4 5 6 7
0 0 0

*/
const int mod=10007;
struct asd{
    int num[4][4];
};

asd mul(asd a,asd b)
{
    asd ans;
    memset(ans.num,0,sizeof(ans.num));
    for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
            for(int k=0;k<4;k++)
                ans.num[i][j]=(ans.num[i][j]+a.num[i][k]*b.num[k][j]%mod)%mod;
    return ans;
}

asd quickmul(int g,asd x)
{
    asd ans;
    for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
        {
            if(i==j)
                ans.num[i][j]=1;
            else
                ans.num[i][j]=0;
        }
    while(g)
    {
        if(g%2)
            ans=mul(ans,x);
        x=mul(x,x);
        g>>=1;
    }
    return ans;
}

int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        int a,b,c,n;
        scanf("%d%d%d%d",&n ,&a ,&b ,&c);
        printf("Case %d: ",cas++);
        if(n<=2)
        {
            puts("0");
            continue;
        }
        asd tmp;
        tmp.num[0][0]=a;tmp.num[0][1]=0;tmp.num[0][2]=b;tmp.num[0][3]=c;
        tmp.num[1][0]=1;tmp.num[1][1]=0;tmp.num[1][2]=0;tmp.num[1][3]=0;
        tmp.num[2][0]=0;tmp.num[2][1]=1;tmp.num[2][2]=0;tmp.num[2][3]=0;
        tmp.num[3][0]=0;tmp.num[3][1]=0;tmp.num[3][2]=0;tmp.num[3][3]=1;

        asd ans=quickmul(n-2,tmp);
        printf("%d
",ans.num[0][3]);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/keyboarder-zsq/p/6777543.html