Kiki & Little Kiki 2
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3076 Accepted Submission(s): 1640
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)
Recommend
lcy
Statistic | Submit | Discuss | Note
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 105;
int n , len;
char str[MAXN];
struct Matrix{
int mat[MAXN][MAXN];
Matrix operator*(const Matrix& m)const{
Matrix tmp;
for(int i = 0 ; i < len ; i++){
tmp.mat[0][i] = 0;
for(int j = 0 ; j < len ; j++)
tmp.mat[0][i] ^= (mat[0][j]&m.mat[j][i]);
}
for(int i = 1 ; i < len ; i++)
for(int j = 0; j < len ; j++)
tmp.mat[i][j] = tmp.mat[i-1][(j-1+len)%len];
return tmp;
}
};
void solve(){
len = strlen(str);
Matrix m , ans;
memset(m.mat , 0 , sizeof(m.mat));
for(int i = 1 ; i < len ; i++)
m.mat[i][i] = m.mat[i][i-1] = 1;
m.mat[0][0] = m.mat[0][len-1] = 1;
memset(ans.mat , 0 , sizeof(ans.mat));
for(int i = 0 ; i < len ; i++)
ans.mat[i][i] = 1;
while(n){
if(n&1)
ans = ans*m;
n >>= 1;
m = m*m;
}
for(int i = 0 ; i < len ; i++){
int x = 0;
for(int k = 0 ; k < len ; k++)
x ^= ans.mat[i][k]&(str[k]-'0');
printf("%d" , x);
}
puts("");
}
int main(){
while(scanf("%d%s" , &n , str) != EOF)
solve();
return 0;
}