Kiki & Little Kiki 2
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2805 Accepted Submission(s): 1493
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 | We have carefully selected several similar problems for you: 1757 1588 2604 2294 2254
sol::
M很大,肯定是log级别的做法。容易发现a[i]=(a[i]+a[(i-1+n)%n])%2,矩乘即可
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
using namespace std;
int n,m;
inline int read()
{
char c;
int res,flag=0;
while((c=getchar())>'9'||c<'0') if(c=='-')flag=1;
res=c-'0';
while((c=getchar())>='0'&&c<='9') res=(res<<3)+(res<<1)+c-'0';
return flag?-res:res;
}
const int N=110;
int a[N][N],b[N][N],tmp[N][N];
inline void simplex(int a[N][N],int b[N][N])
{
memset(tmp,0,sizeof(tmp));
for(int i=0;i<=n;++i)
for(int j=0;j<=n;++j)
for(int k=0;k<=n;++k)
tmp[i][j]=(tmp[i][j]+a[i][k]*b[k][j]%2)%2;
for(int i=0;i<=n;++i)
for(int j=0;j<=n;++j)
a[i][j]=tmp[i][j];
}
inline void ksm(int t)
{
while(t)
{
if(t&1) simplex(a,b);
simplex(b,b);
t>>=1;
}
}
char sr[N];
int main()
{
// freopen("2276.in","r",stdin);
// freopen(".out","w",stdout);
while(scanf("%d",&m)!=EOF)
{
scanf("%s",sr);
n=strlen(sr);
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=0;i<n;++i)
{
a[0][i]=sr[i]-'0';
b[i][i]=1;
b[(i-1+n)%n][i]=1;
}
--n;
ksm(m);
for(int i=0;i<=n;++i)
printf("%d",a[0][i]);
printf("\n");
}
}