Kiki & Little Kiki 2
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1147 Accepted Submission(s): 607
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
一直没想明白我的为什么TLE,他的却AC........
把TLE的代码贴出来,先阶段是找不出什么优化了.....以后再看吧0.0
TLE的代码:
题目大意:有一圈的灯,其中0和1分别表示灯的暗亮,灯每秒会以一种规律改变状态,规则是
当前一盏灯亮时,就改变状态,否则就不改变状态。
我们仔细观察:每次灯的状态变化会有下面规律:
a1=(a1+an)%2
a2=(a1+a2)%2
a3=(a1+a2)%2.............ai=(a(i-1)+ai)%2;
这个题开始想了很久才想明白,对矩阵做题也有了进一步的了解,前几道矩阵题都没认真想有些步骤为什么要那样,这道题倒是仔细想了下。不过TLE,题给的数据有点大,n^3logm>10^7,不过在网上看到一代码,给我的思路和操作都差不多吧,结果把他的代码一提交,300多ms,我就仔细看了下我和他代码的差异,感觉不会差这么多吧.......一直没想明白我的为什么TLE,他的却AC........
把TLE的代码贴出来,先阶段是找不出什么优化了.....以后再看吧0.0
TLE的代码:
#include<stdio.h>
#include<string.h>
#define MAXN 111
int num[MAXN];
int d,cur1,cur2;
char ch[MAXN];
int Matrix[2][MAXN][MAXN],tmp[2][MAXN][MAXN];
void fun(int x)
{
int i,j,k;
cur1=1,cur2=1;
x--;
memset(Matrix,0,sizeof(Matrix));
Matrix[1][1][1]=Matrix[1][1][d]=1;
for(i=2;i<=d;i++)
Matrix[1][i][i-1]=Matrix[1][i][i]=1;
for(i=1;i<=d;i++)
for(j=1;j<=d;j++)
tmp[cur2][i][j]=Matrix[cur1][i][j];
while(x)
{
if(x&1)
{
memset(Matrix[1-cur1],0,sizeof(Matrix[0]));
for(i=1;i<=d;i++)
for(j=1;j<=d;j++)
for(k=1;k<=d;k++)
Matrix[1-cur1][i][j]=(Matrix[1-cur1][i][j]+(tmp[cur2][i][k]&Matrix[cur1][k][j]))%2;
cur1=1-cur1;
}
x>>=1;
memset(tmp[1-cur2],0,sizeof(tmp[0]));
for(i=1;i<=d;i++)
for(j=1;j<=d;j++)
for(k=1;k<=d;k++)
tmp[1-cur2][i][j]=(tmp[1-cur2][i][j]+(tmp[cur2][i][k]&tmp[cur2][k][j]))%2;
cur2=1-cur2;
}
}
int main(void)
{
int m,i,j;
// freopen("d:\\in.txt","r",stdin);
while(scanf("%d%s",&m,ch)==2)
{
d=strlen(ch);
for(i=1;i<=d;i++)
num[i]=ch[i-1]-'0';
num[0]=num[d];
fun(m);
int a;
for(i=1;i<=d;i++)
{
a=0;
for(j=1;j<=d;j++)
a=(a+(Matrix[cur1][i][j]&num[j]))%2;
printf("%d",a);
}
printf("\n");
}
}