Kiki & Little Kiki 2
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2265 Accepted Submission(s): 1146
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)
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.
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
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
#define bug printf("hihi\n")
#define eps 1e-8
typedef __int64 ll;
using namespace std;
#define INF 0x3f3f3f3f
#define N 101
int n;
struct mat{
mat(){memset(a,0,sizeof(a));}
int a[N][N];
mat operator *(mat b)
{
mat c;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
for(int k=0;k<n;k++)
c.a[i][j]=(c.a[i][j]+a[i][k]*b.a[k][j])%2;
return c;
}
};
mat fdd(mat s,int m)
{
mat ss;
for(int i=0;i<n;i++) ss.a[i][i]=1;
while(m)
{
if(m&1) ss=ss*s;
s=s*s;
m>>=1;
}
return ss;
}
int main()
{
int i,j;
char c[1000];
int m;
while(~scanf("%d",&m))
{
scanf("%s",c);
mat s;
n=strlen(c);
for(i=0;i<n;i++)
s.a[i][0]=c[i]-'0';
mat ss;
ss.a[0][0]=ss.a[0][n-1]=1;
for(i=1;i<n;i++)
ss.a[i][i]=ss.a[i][i-1]=1;
ss=fdd(ss,m);
s=ss*s;
for(i=0;i<n;i++)
printf("%d",s.a[i][0]);
printf("\n");
}
return 0;
}