Kiki & Little Kiki 2
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1232 Accepted Submission(s): 645
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
题意:
题目大意:有一圈的灯,其中0和1分别表示灯的暗亮,灯每秒会以一种规律改变状态,规则是
当前一盏灯亮时,就改变状态,否则就不改变状态。
思路: 很容易看出
可以用矩阵去运算,先要构造一个矩阵!
| 1 0 0 0 ....1 |
| 1 1 0 0.....0 |
| 0 1 1 0 ....0|
......
我们只要把这个矩阵^M再乘以初始状态的就可以了注意:
在代码中注意mod 的应用 如果没有用的mod用多了 就会造成超时 所以除了必须的那个mod 能省的mod还是要省去的 不然很超时
#include<stdio.h>
#include<string.h>
#define ll int
struct Mat
{
int martix[102][102];
};
int N,n,d,mod=2;
Mat temp,res,init,q;
Mat Martix_Add(Mat a,Mat b)
{
int i,j;
Mat c;
for (i=0;i<N;i++)
{
for (j=0;j<N;j++)
{
c.martix[i][j]=(a.martix[i][j]+b.martix[i][j])%mod;
}
}
return c;
}
Mat Martix_Mul(Mat a,Mat b)
{
int i,j,l;
Mat c;
for (i=0;i<N;i++)
{
for (j=0;j<N;j++)
{
c.martix[i][j]=0;
for (l=0;l<N;l++)
{
c.martix[i][j]+=a.martix[i][l]*b.martix[l][j];
}
c.martix[i][j]%=mod;
}
}
return c;
}
Mat er_fun(Mat e,ll x) //求矩阵e^x
{
Mat tp;
tp=e;
e=res; //res是单位矩阵
while(x)
{
if(x&1)
e=Martix_Mul(e,tp);
tp=Martix_Mul(tp,tp);
x>>=1;
}
return e;
}
int main()
{
int n,g[102],i,j,k;
char s[102];
memset(res.martix,0,sizeof(res.martix));
for(i=0;i<=100;i++)
res.martix[i][i]=1;
while(scanf("%d",&n)!=EOF)
{
scanf("%s",s);
d=strlen(s);
for(i=0;i<d;i++)
g[i]=s[i]-'0';
N=d;
memset(init.martix,0,sizeof(init.martix));
init.martix[0][0]=init.martix[0][d-1]=1;
k=0;
for(i=1;i<d;i++)
{
init.martix[i][k]=init.martix[i][k+1]=1;
k++;
}
q=er_fun(init,n);
for(i=0;i<d;i++)
{
int ans=0;
for(j=0;j<d;j++)
ans+=q.martix[i][j]*g[j]%mod;
printf("%d",ans%2);
}
printf("\n");
}
return 0;
}