Kiki & Little Kiki 2
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2100 Accepted Submission(s): 1078
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
lcy
这道题是典型的dp+矩阵快速幂优化。状态转移很明显。如果一个灯左边的灯开着,就改变该灯的状态。那么这时我们就应该对只有0,1两种状态时的情况有所警觉,在这种想法之下把0可以变成1,把1可以变成0,这就要用到对2取模。
考虑a(i,j)到a(i+1,j)的状态转移,假设a(i,j-1)表示a(i,j)的左边。当然j=1时它的左边为a(i,n),这个与其他的不同。
则可以得到a(i+1,j)=(a(i,j)+a(i,j-1))%2。有了该式,直接矩阵快速幂即可。
参考代码:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<ctime>
#include<cstdlib>
#include<iomanip>
#include<utility>
#define pb push_back
#define mp make_pair
#define CLR(x) memset(x,0,sizeof(x))
#define _CLR(x) memset(x,-1,sizeof(x))
#define REP(i,n) for(int i=0;i<n;i++)
#define Debug(x) cout<<#x<<"="<<x<<" "<<endl
#define REP(i,l,r) for(int i=l;i<=r;i++)
#define rep(i,l,r) for(int i=l;i<r;i++)
#define RREP(i,l,r) for(int i=l;i>=r;i--)
#define rrep(i,l,r) for(int i=1;i>r;i--)
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<11
using namespace std;
int n,k;
char a[110];
struct mat
{
int d[101][101];
}A,B,E;
mat multi(mat a,mat b)
{
mat ans;
rep(i,0,k)
{
rep(j,0,k)
{
ans.d[i][j]=0;
rep(l,0,k)
ans.d[i][j]=(ans.d[i][j]+a.d[i][l]*b.d[l][j])%2;
}
}
return ans;
}
mat quickmulti(int n)
{
if(n==0) return E;
if(n==1) return A;
mat p=E,q=A;
while(n)
{
if(n&1)
{
n--;
p=multi(p,q);
}
else
{
n>>=1;
q=multi(q,q);
}
}
return p;
}
int main()
{
CLR(E.d);
rep(i,0,100)
E.d[i][i]=1;
while(~read(n))
{
scanf(" %s",a);
CLR(A.d);CLR(B.d);
k=strlen(a);
rep(i,0,k)
B.d[i][0]=a[i]-'0';
A.d[0][0]=A.d[0][k-1]=1;
rep(i,1,k)
A.d[i][i]=A.d[i][i-1]=1;
mat ans=quickmulti(n);
ans=multi(ans,B);
rep(i,0,k)
printf("%d",ans.d[i][0]);
printf("\n");
}
}