题目:https://ac.nowcoder.com/acm/contest/221/C
构造矩阵
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=10;
const int mod=1e9+7;
typedef long long LL;
LL n;
typedef struct node
{
LL z[maxn][maxn];
}matrix;
matrix matrix_mul(matrix a,matrix b)
{
matrix c;
memset(c.z,0,sizeof(c.z));
for(int i=1;i<=6;i++)
{
for(int j=1;j<=6;j++)
{
for(int u=1;u<=6;u++)
{
c.z[i][j]+=(a.z[i][u]%mod)*(b.z[u][j]%mod)%mod;
}
}
}
return c;
}
matrix matrix_pow(matrix a,int n)
{
matrix ans;
memset(ans.z,0,sizeof(ans.z));
for(int i=1;i<=6;i++)
{
ans.z[i][i]=1;
}
while(n>0)
{
if(n&1)
ans=matrix_mul(ans,a);
n>>=1;
a=matrix_mul(a,a);
}
return ans;
}
int main()
{
while(cin>>n)
{
if(n==0)
break;
matrix t;
memset(t.z,0,sizeof(t.z));
t.z[1][1]=1;t.z[1][2]=1;t.z[1][3]=0;t.z[1][4]=1;t.z[1][5]=0;t.z[1][6]=0;
t.z[2][1]=0;t.z[2][2]=2;t.z[2][3]=3;t.z[2][4]=0;t.z[2][5]=1;t.z[2][6]=1;
t.z[3][1]=0;t.z[3][2]=1;t.z[3][3]=0;t.z[3][4]=0;t.z[3][5]=0;t.z[3][6]=0;
t.z[4][1]=0;t.z[4][2]=0;t.z[4][3]=0;t.z[4][4]=1;t.z[4][5]=2;t.z[4][6]=1;
t.z[5][1]=0;t.z[5][2]=0;t.z[5][3]=0;t.z[5][4]=0;t.z[5][5]=1;t.z[5][6]=1;
t.z[6][1]=0;t.z[6][2]=0;t.z[6][3]=0;t.z[6][4]=0;t.z[6][5]=0;t.z[6][6]=1;
matrix y;
y.z[1][1]=2;
y.z[2][1]=2;
y.z[3][1]=1;
y.z[4][1]=4;
y.z[5][1]=2;
y.z[6][1]=1;
matrix zz=matrix_pow(t,n-1);
LL res=0;
// for(int i=1;i<=6;i++)
// cout << ans.z[1][i] << ' ' ;
//cout << endl;
for(int i=1;i<=6;i++)
{
res+=zz.z[1][i]*y.z[i][1];
res%=mod;
}
cout << res << endl;
}
return 0;
}