Matrix Fast power.
0 1-> The second number becomes 1.
0 0-> The second number becomes 0.
1 0-> The second number becomes 1.
1 1-> The second number becomes 0.
According to these four characteristics, it is possible to write the transfer matrix.
#pragmaComment (linker, "/stack:1024000000,1024000000")#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<map>#include<Set>#include<queue>#include<stack>#include<iostream>using namespaceStd;typedefLong LongLL;Const DoublePi=acos (-1.0), eps=1e-6;voidFile () {freopen ("D:\\in.txt","R", stdin); Freopen ("D:\\out.txt","W", stdout);} Template<classT>inlinevoidRead (T &x) { Charc =GetChar (); X=0; while(!isdigit (c)) C =GetChar (); while(IsDigit (c)) {x = x *Ten+ C-'0'; c =GetChar ();}}Chars[ the];intlen,m;structmatrix{inta[ the][ the]; intR, C; Matrixoperator*(Matrix b);}; Matrix X, Y, Z; Matrix CH (matrix A, matrix B) {matrix C; memset (C.A,0,sizeof(C.A)); intI, J, KK; for(i =1; I <= Len; i++) for(j =1; J <= Len; J + +) { if(A.a[i][j]) { for(KK =1; KK <= Len; kk++) C.a[i][kk]= (C.a[i][kk]+a.a[i][j] * B.A[J][KK])%2; } } returnC;}voidinit () { for(intI=1; i<=len;i++) z.a[1][i] = s[i-1]-'0'; Z.R=1; Z.C =Len; memset (Y.A,0,sizeofy.a); for(intI=1; i<=len;i++) y.a[i][i]=1; Y.R= Len; Y.C =Len; memset (X.A,0,sizeofx.a); x.a[1][1]=1; x.a[len][1]=1; for(intI=2; i<=len;i++) x.a[i][i]=1, x.a[i-1][i]=1; X.R= Len; X.C =Len;}voidWork () { while(m) {if(M%2==1) Y =CH (y,x); M= M >>1; X=CH (x,x); } Z=CH (z,y); for(intI=1; i<=len;i++) printf ("%d", z.a[1][i]); printf ("\ n");}intMain () { while(~SCANF ("%d%s",&m,s)) {Len=strlen (s); Init (); Work (); } return 0;}
HDU 2276 Kiki & Little Kiki 2