HDU 2276 Kiki & Little Kiki 2 矩阵构造

Kiki & Little Kiki 2

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1659    Accepted Submission(s): 837


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
 

 

Recommend
lcy
 
  1 /*
  2 题意:一个灯环,输出进行N此变化的结果,变化的规则是:如果左边是1,那么自己变成相反的
  3       1变成0 0变成1 。第一个要以最后一个为参考。
  4       
  5       矩阵的变化,
  6 
  7       | 1 0 0 0 0 1|  a1      | (a1+a6)%2 |
  8       | 1 1 0 0 0 0|  a2      | (a1+a2)%2 |
  9       | 0 1 1 0 0 0|  a3 ===  | (a2+a3)%2 |
 10       | 0 0 1 1 0 0|  a4      | (a3+a4)%2 |
 11       | 0 0 0 1 1 0|  a5      | (a4+a5)%2 |
 12       | 0 0 0 0 1 1|  a6      | (a5+a6)%2 |
 13       
 14       利用矩阵相乘就可以快速到求取出来.
 15       优化1.由于左边是稀疏矩阵,那么在矩阵相乘到时候,可以优化。代码中有!!~
 16       优化2.用位操作。怎么用位操作实现?
 17       优化3.在快速幂的时候,顺序的变化。因为稀疏矩阵的存在,所以改变一下顺序
 18             就会有提高。代码中有...
 19 */
 20 
 21 #include<iostream>
 22 #include<cstdio>
 23 #include<cstring>
 24 #include<cstdlib>
 25 using namespace std;
 26 
 27 
 28 char a[103];
 29 struct node
 30 {
 31     int mat[103][103];
 32 }M_tom,M_hxl;
 33 
 34 void make_first(node *cur,int len)
 35 {
 36     int i,j;
 37     for(i=1;i<=len;i++)
 38     for(j=1;j<=len;j++)
 39     if(i==j) cur->mat[i][j]=1;
 40     else cur->mat[i][j]=0;
 41 }
 42 
 43 void cheng2(node cur,char a[],int len)
 44 {
 45     node ww;
 46     int i,j,k;
 47     memset(ww.mat,0,sizeof(ww.mat));
 48     for(i=1;i<=len;i++)
 49     for(k=1;k<=len;k++)
 50     if(cur.mat[i][k])
 51     {
 52         for(j=1;j<=1;j++)
 53         {
 54            ww.mat[i][j]=(ww.mat[i][j]^(cur.mat[i][k]&(a[k]-'0')))&1;
 55         }
 56     }
 57     for(i=1;i<=len;i++)
 58     printf("%d",ww.mat[i][1]);
 59     printf("\n");
 60 }
 61 
 62 struct node cheng(node cur,node now,int len)
 63 {
 64     node ww;
 65     int i,j,k;
 66     memset(ww.mat,0,sizeof(ww.mat));
 67     for(i=1;i<=len;i++)
 68     for(k=1;k<=len;k++)
 69     if(cur.mat[i][k])//对稀疏矩阵的优化
 70     {
 71         for(j=1;j<=len;j++)
 72         {
 73             if(now.mat[k][j])//同理
 74             {
 75                ww.mat[i][j]=(ww.mat[i][j]^(cur.mat[i][k]&now.mat[k][j]))&1;
 76             }
 77         }
 78     }
 79     return ww;
 80 }
 81 
 82 void make_init(int len)
 83 {
 84 
 85     for(int i=1;i<=len;i++)
 86     M_hxl.mat[1][i]=0;
 87 
 88     M_hxl.mat[1][1]=1;
 89     M_hxl.mat[1][len]=1;
 90     for(int i=2;i<=len;i++)
 91     for(int j=1;j<=len;j++)
 92     if(i==j || i-1==j)
 93     M_hxl.mat[i][j]=1;
 94     else M_hxl.mat[i][j]=0;
 95 }
 96 
 97 
 98 void power_sum2(int n,int len,char a[])
 99 {
100     make_first(&M_tom,len);
101     while(n)
102     {
103         if(n&1)
104         {
105             M_tom=cheng(M_hxl,M_tom,len);//这也是优化。顺序改变结果就不同了
106         }
107         n=n>>1;
108         M_hxl=cheng(M_hxl,M_hxl,len);
109     }
110    // cs(len);
111     cheng2(M_tom,a,len);
112 }
113 
114 int main()
115 {
116     int n,len;
117     while(scanf("%d",&n)>0)
118     {
119         scanf("%s",a+1);
120         len=strlen(a+1);
121         make_init(len);
122         power_sum2(n,len,a);
123     }
124     return 0;
125 }

 

posted @ 2013-08-20 09:41  芷水  阅读(332)  评论(0编辑  收藏  举报