8.3.6 Kiki & Little Kiki 2


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

大水题啊,,

自己多想想,应该想的出来的,提醒一句就是尝试用矩阵表示灯的状态,再转移即可

 1 /*
 2 Author:wuhuajun
 3 */
 4 #include <cmath>
 5 #include <cstdio>
 6 #include <algorithm>
 7 #include <cstring>
 8 #include <string>
 9 #include <cstdlib>
10 using namespace std;
11 
12 typedef long long ll;
13 typedef double dd;
14 const int maxn=110;
15 struct matrix
16 {
17     int m[maxn][maxn];
18 } ans,base,c,a;
19 int n,m,l;
20 char s[maxn];
21 
22 void close()
23 {
24 exit(0);
25 }
26 
27 void print(matrix a)
28 {
29     for (int i=1;i<=n;i++)
30     {
31         for (int j=1;j<=n;j++)
32             printf("%d ",a.m[i][j]);
33         puts("");
34     }
35 }
36 
37 matrix mul(matrix a,matrix b)
38 {
39     memset(c.m,0,sizeof(c.m));
40     for (int i=1;i<=n;i++)
41         for (int j=1;j<=n;j++)
42             for (int k=1;k<=n;k++)
43             {
44                 c.m[i][j]+=a.m[i][k]*b.m[k][j];
45                 c.m[i][j] %= 2;
46             }
47     return c;
48 }
49 
50 void work()
51 {
52     memset(ans.m,0,sizeof(ans.m));
53     memset(base.m,0,sizeof(base.m));
54     for (int i=1;i<=n;i++)
55         ans.m[i][i]=1;
56     base.m[1][n]=1;
57     for (int i=1;i<=n;i++)
58         base.m[i][i-1]=base.m[i][i]=1;
59     while (m!=0)
60     {
61         if (m & 1)
62             ans=mul(ans,base);
63         m/=2;
64         base=mul(base,base);
65     }
66     a=mul(ans,a);
67     for (int i=1;i<=n;i++)
68         printf("%d",a.m[i][1]);
69     puts("");
70 }
71 
72 
73 void init()
74 {
75     while (scanf("%d",&m)!=EOF)
76     {
77         scanf("%s",s);
78         l=strlen(s);
79         n=l;
80         for (int i=0;i<l;i++)
81             a.m[i+1][1]=s[i]-'0';
82         work();
83     }
84 }
85 
86 int main ()
87 {
88     init();
89     close();
90     return 0;
91 }
原文地址:https://www.cnblogs.com/cssystem/p/3335298.html