Topic links
n a circle of lights, 1 to the left is N. There are two states, 1 is bright, and 0 is not lit. If a lamp is lit on the left, then the next moment the lamp will change state, 1 becomes 0, and 0 becomes 1. Give the initial state and time t, and ask what the status of each lamp is at t time.
Ai = (A (i-1) +ai)%2, based on this build matrix.
/*1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1*///A fast power to the matrix, the result is multiplied with the initial state is good. #include <iostream>#include<vector>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<map>#include<Set>#include<string>#include<queue>#include<stack>#include<bitset>using namespacestd;#definePB (x) push_back (x)#definell Long Long#defineMK (x, y) make_pair (x, y)#defineLson L, M, rt<<1#defineMem (a) memset (a, 0, sizeof (a))#defineRson m+1, R, rt<<1|1#defineMem1 (a) memset (a,-1, sizeof (a))#defineMEM2 (a) memset (a, 0x3f, sizeof (a))#defineRep (i, N, a) for (int i = A; i<n; i++)#defineFi first#defineSe Secondtypedef pair<int,int>PLL;Const DoublePI = ACOs (-1.0);Const DoubleEPS = 1e-8;Const intMoD = 1e9+7;Const intINF =1061109567;Const intdir[][2] = { {-1,0}, {1,0}, {0, -1}, {0,1} };intN;structmatrix{inta[102][102]; Matrix () {mem (a); }}; Matrixoperator*(matrix A, matrix B) {matrix C; for(inti =0; i<n; i++) { for(intj =0; j<n; J + +) { for(intK =0; k<n; k++) {C.a[i][j]+ = a.a[i][k]*B.a[k][j]; C.A[I][J]%=2; } } } returnC;} Matrixoperator^(Matrix A, ll b) {matrix tmp; for(inti =0; i<n; i++) Tmp.a[i][i]=1; while(b) {if(b&1) TMP= tmp*A; A= A *A; b>>=1; } returntmp;}intMain () {intm; strings; while(cin>>m) {cin>>s; N=s.size (); Matrix tmp; for(inti =0; i<n; i++) {Tmp.a[i][i]=1; tmp.a[i][(i-1+n)%n] =1; } Matrix b= tmp^m; Matrix A; for(inti =0; I<s.size (); i++) {a.a[i][0] = s[i]-'0'; } Matrix C= b*A; for(inti =0; I<s.size (); i++) {printf ("%d", c.a[i][0]); } cout<<Endl; } return 0;}
Hdu 2276 Kiki & Little Kiki 2 matrix fast Power