标签:can alt pre lld www string friend end cstring
矩阵裸体。
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 typedef long long ll; 5 ll n,mod; 6 struct Matrix{ 7 ll a[4][4]; 8 Matrix (){ 9 memset(a,0,sizeof(a)); 10 } 11 inline void e(){ 12 for(int i=1;i<=3;i++) 13 a[i][i]=1; 14 } 15 friend Matrix operator *(Matrix x,Matrix y){ 16 Matrix c; 17 for(int k=1;k<=3;k++) 18 for(int i=1;i<=3;i++) 19 for(int j=1;j<=3;j++){ 20 c.a[i][j]+=x.a[i][k]*y.a[k][j]; 21 if(c.a[i][j]>mod) 22 c.a[i][j]%=mod; 23 } 24 return c; 25 } 26 friend Matrix operator ^(Matrix a,ll b){ 27 Matrix ans;ans.e(); 28 while(b){ 29 if(b&1) ans=ans*a; 30 b>>=1; 31 a=a*a; 32 }return ans; 33 } 34 inline void kmod(ll k){ 35 for(int i=1;i<=3;i++) 36 for(int j=i;j<=3;j++) 37 a[i][j]=1; 38 a[1][1]=k%mod; 39 } 40 }; 41 int main(){ 42 scanf("%lld%lld",&n,&mod); 43 Matrix ans; 44 ans.a[1][1]=0; 45 ans.a[2][1]=0; 46 ans.a[3][1]=1; 47 int i;ll j; 48 for(i=1,j=1;j*10<=n;j*=10,i++){ 49 Matrix b;b.kmod(j*10); 50 ans=(b^(j*10-j))*ans; 51 }//printf("%lld\n",ans.a[1][1]); 52 Matrix b;b.kmod(j*10); 53 if(n-j+1>0) 54 ans=(b^(n-j+1))*ans; 55 printf("%lld\n",ans.a[1][1]); 56 }
标签:can alt pre lld www string friend end cstring
原文地址:http://www.cnblogs.com/Troywar/p/7400528.html