0 1 0 0 0 0 1 ios
而後直接求就好了。spa
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<string> #include<algorithm> using namespace std; typedef unsigned long long ll; #define inf 99999999 #define pi acos(-1.0) #define MOD 1000000007 struct matrix{ ll n,m,i; ll data[9][9]; void init_danwei(){ for(i=0;i<n;i++){ data[i][i]=1; } } }; matrix multi(matrix &a,matrix &b){ ll i,j,k; matrix temp; temp.n=a.n; temp.m=b.m; for(i=0;i<temp.n;i++){ for(j=0;j<temp.m;j++){ temp.data[i][j]=0; } } for(i=0;i<a.n;i++){ for(k=0;k<a.m;k++){ if(a.data[i][k]>0){ for(j=0;j<b.m;j++){ temp.data[i][j]=(temp.data[i][j]+(a.data[i][k]*b.data[k][j])%MOD )%MOD; } } } } return temp; } matrix fast_mod(matrix &a,ll n){ matrix ans; ans.n=a.n; ans.m=a.m; memset(ans.data,0,sizeof(ans.data)); ans.init_danwei(); while(n>0){ if(n&1)ans=multi(ans,a); a=multi(a,a); n>>=1; } return ans; } int main() { ll n,m,i,j; ll ax,bx,ay,by,a0,b0,a1,b1; while(scanf("%llu",&n)!=EOF) { scanf("%llu%llu%llu",&a0,&ax,&ay); scanf("%llu%llu%llu",&b0,&bx,&by); if(n==0){ printf("0\n");continue; } matrix a; a.n=a.m=7; memset(a.data,0,sizeof(a.data)); a.data[0][0]=a.data[1][0]=a.data[4][2]=a.data[4][4]=a.data[5][3]=a.data[5][5]=a.data[6][1]=a.data[6][6]=1; a.data[1][1]=ax*bx%MOD; a.data[2][1]=ax*by%MOD; a.data[2][2]=ax%MOD; a.data[3][1]=ay*bx%MOD; a.data[3][3]=bx%MOD; matrix cnt; cnt=fast_mod(a,n-1); matrix ant; ant.n=1;ant.m=7; a1=((a0*ax)%MOD+ay)%MOD; b1=((b0*bx)%MOD+by)%MOD; ant.data[0][0]=a0*b0%MOD; ant.data[0][1]=a1*b1%MOD; ant.data[0][2]=a1%MOD; ant.data[0][3]=b1%MOD; ant.data[0][4]=ay%MOD; ant.data[0][5]=by%MOD; ant.data[0][6]=ay*by%MOD; matrix juzhen; juzhen=multi(ant,cnt); printf("%llu\n",juzhen.data[0][0]%MOD); } return 0; }