Kiki & Little Kiki 2
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2175 Accepted Submission(s): 1110
Problem Description
There are n lights in a circle numbered from 1 to n. The left of light 1 is light n, and the left of light k (1< k<= n) is the light k-1.At time of 0, some of them turn on, and others turn off. Change the state of light i (if it‘s on, turn off it; if it is not on, turn on it) at t+1 second (t >= 0), if the left of light i is on !!! Given the initiation state, please find all lights’ state after M second. (2<= n <= 100, 1<= M<= 10^8)
Input
The input contains one or more data sets. The first line of each data set is an integer m indicate the time, the second line will be a string T, only contains ‘0‘ and ‘1‘ , and its length n will not exceed 100. It means all lights in the circle from 1 to n.
If the ith character of T is ‘1‘, it means the light i is on, otherwise the light is off.
Output
For each data set, output all lights‘ state at m seconds in one line. It only contains character ‘0‘ and ‘1.
Sample Input
1
0101111
10
100000001
Sample Output
1111000
001000010
Source
HDU 8th Programming Contest Site(1)
用矩阵快速幂来优化DP问题的状态转移,思路如下:
代码如下:
1 #include <iostream> 2 #include <cstring> 3 #include <string> 4 #include <cstdio> 5 #include <cmath> 6 7 using namespace std; 8 9 #define MOD 2 10 #define MAXN 101 11 12 typedef struct MAT 13 { 14 int 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, int 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 char T[105]; 69 scanf("%s", T); 70 int n = strlen(T); 71 MAT A, B, ans; 72 A.r = 1, A.c = n; 73 B.r = n, B.c = n; 74 for(int i = 0; i < n; i++) { 75 A.d[0][i] = T[i] - ‘0‘; 76 B.d[i][i] = 1; 77 B.d[i][(i+1)%n] = 1; 78 } 79 ans = quickmul(B, t, MOD); 80 ans = mul(A, ans, MOD); 81 for(int i = 0; i < n; i++) { 82 printf("%d", ans.d[0][i]); 83 } 84 printf("\n"); 85 } 86 return 0; 87 }