标签:des style blog http color io os ar java
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<vector> 5 #include<algorithm> 6 #include<cmath> 7 #define M(a,b) memset(a,b,sizeof(a)) 8 typedef long long LL; 9 10 using namespace std; 11 12 13 long long a,b,n,m; 14 15 struct matrix 16 { 17 LL mat[2][2]; 18 void init() 19 { 20 mat[0][0] = a; 21 mat[0][1] = b; 22 mat[1][0] = 1; 23 mat[1][1] = a; 24 } 25 }; 26 27 matrix mamul(matrix aa,matrix bb) 28 { 29 matrix c; 30 for(int i = 0;i<2;i++) 31 { 32 for(int j = 0;j<2;j++) 33 { 34 c.mat[i][j] = 0; 35 for(int k = 0;k<2;k++) 36 c.mat[i][j]+=(aa.mat[i][k]*bb.mat[k][j]); 37 c.mat[i][j]%=m; 38 } 39 } 40 return c; 41 } 42 43 matrix mul(matrix s, int k) 44 { 45 matrix ans; 46 ans.init(); 47 while(k>=1) 48 { 49 if(k&1) 50 ans = mamul(ans,s); 51 k = k>>1; 52 s = mamul(s,s); 53 } 54 return ans; 55 } 56 57 int main() 58 { 59 while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&n,&m)==4) 60 { 61 matrix ans; 62 ans.init(); 63 ans = mul(ans,n-1); 64 printf("%I64d\n",(ans.mat[0][0]*2+m)%m); 65 } 66 return 0; 67 }
标签:des style blog http color io os ar java
原文地址:http://www.cnblogs.com/haohaooo/p/4019934.html