Kiki & Little Kiki 2
时间限制:
3000 ms | 内存限制:
65535 KB
难度:
4
-
描述
-
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)
-
输入
-
The input contains no more than 1000 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.
输出
- For each data set, output all lights' state at m seconds in one line. It only contains character '0' and '1. 样例输入
-
1 0101111 10 100000001
样例输出
-
1111000 001000010
分析:这个题目的意思是给你一个字符串其中字符串中只含 0 和 1 在 t 秒时如果s[i-1]==1 则是s[i]^1。 - 这个题目我是想了好久都没做出来了,后来看了解题报告,才知道通过矩阵运算可以解的。
- 还未解决:1、不知道为什么这么做就行???
- 2、在杭电AC了,可是到南阳就TLE???
- 3、为什么矩阵运算时使用递归会错???
-
View Code
#include<iostream> #include<cstdio> #include<cstring> #define N 105 using namespace std; struct matrix{ int m[N][N];}a,b,unit; char s[N]; void init(int n) { int i,j; for(i=0;i<n;i++) { for(j=0;j<n;j++) { unit.m[i][j]=a.m[i][j]=(i==j); b.m[i][j]=0; } } a.m[0][0]=a.m[0][n-1]=1; for(i=1;i<n;i++) { a.m[i][i-1]=a.m[i][i]; } for(i=0;i<n;i++) { b.m[i][0]=s[i]-'0'; } } matrix matr_mult(matrix x,matrix y,int n) { int i,j,k; matrix z; for(i=0;i<n;i++) { for(j=0;j<n;j++) { z.m[i][j]=0; for(k=0;k<n;k++) { z.m[i][j]+=(x.m[i][k]*y.m[k][j]); } z.m[i][j]=z.m[i][j]%2; } } return z; } matrix matr_pow_mod(matrix A,int n,int m) { /* 递归为什么不行呀??? matrix t; if(m==1) return A; t=matr_pow_mod(A,n,m/2); t=matr_mult(t,t,n); if(m%2==1) t=matr_mult(A,t,n); return t; */ matrix t,tt; t=A;tt=unit; while(m) { if(m%2==1) tt=matr_mult(tt,t,n); m=m/2; t=matr_mult(t,t,n); } return tt; } int main() { int len,i,t; while(scanf("%d",&t)!=EOF) { scanf("%s",s); matrix ans; len=strlen(s); init(len); ans=matr_pow_mod(a,len,t); ans=matr_mult(ans,b,len); for(i=0;i<len;i++) printf("%d",ans.m[i][0]); printf("\n"); } return 0; }