标签:
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4742 Accepted Submission(s): 1985
f[i][1] = 2 * f[i-1][1] + f[i-1][2] + f[i-1][3];
f[i][2] = f[i-1][1] + 2 * f[i-1][2] + f[i-1][4];
f[i][3] = f[i-1][1] + 2 * f[i-1][3] + f[i-1][4];
f[i][4] = f[i-1][2] + f[i-1][3] + 2 * f[i-1][4];
2 1 1 0
1 2 0 1
1 0 2 1
0 1 1 2
代码:
1 #include <iostream> 2 #include <cstring> 3 #include <string> 4 #include <cstdio> 5 #include <cmath> 6 7 using namespace std; 8 9 #define MOD 100 10 #define MAXN 5 11 12 typedef struct MAT 13 { 14 __int64 d[MAXN][MAXN]; 15 int r, c; 16 MAT() 17 { 18 r = c = 0; 19 memset(d, 0, sizeof(d)); 20 } 21 }MAT; 22 23 MAT mul(MAT m1, MAT m2, int mod) 24 { 25 MAT ans = MAT(); 26 ans.r = m1.r; 27 ans.c = m2.c; 28 for(int i = 0; i < m1.r; i++) 29 { 30 for(int j = 0; j < m2.r; j++) 31 { 32 if(m1.d[i][j]) 33 { 34 for(int k = 0; k < m2.c; k++) 35 { 36 ans.d[i][k] = (ans.d[i][k] + m1.d[i][j] * m2.d[j][k]) % mod; 37 } 38 } 39 } 40 } 41 return ans; 42 } 43 44 MAT quickmul(MAT m, __int64 n, int mod) 45 { 46 MAT ans = MAT(); 47 for(int i = 0; i < m.r; i++) 48 { 49 ans.d[i][i] = 1; 50 } 51 ans.r = m.r; 52 ans.c = m.c; 53 while(n) 54 { 55 if(n & 1) 56 { 57 ans = mul(m, ans, mod); 58 } 59 m = mul(m, m, mod); 60 n >>= 1; 61 } 62 return ans; 63 } 64 65 int main() { 66 int T; 67 while(scanf("%d", &T) != EOF && T) { 68 __int64 n; 69 for(int i = 1; i <= T; i++) { 70 scanf("%I64d", &n); 71 MAT A = MAT(); 72 A.r = 4, A.c = 4; 73 A.d[0][0] = 2; A.d[0][1] = 1; A.d[0][2] = 1; A.d[0][3] = 0; 74 A.d[1][0] = 1; A.d[1][1] = 2; A.d[1][2] = 0; A.d[1][3] = 1; 75 A.d[2][0] = 1; A.d[2][1] = 0; A.d[2][2] = 2; A.d[2][3] = 1; 76 A.d[3][0] = 0; A.d[3][1] = 1; A.d[3][2] = 1; A.d[3][3] = 2; 77 A = quickmul(A, n, MOD); 78 printf("Case %d: %d\n", i, A.d[0][0]); 79 } 80 printf("\n"); 81 } 82 }
标签:
原文地址:http://www.cnblogs.com/vincentX/p/4579353.html