Build a matrix, run fast power
1 /**************************************************************2 problem:40003 user:idy0024 language:c++5 result:accepted6 time:32 Ms7 memory:836 KB8 ****************************************************************/9 Ten#include <cstdio> One#include <cstring> A #defineM 6 - - structMatrix { theunsigned v[1<<m][1<<M]; - Constunsigned *operator[](intIConst{returnv[i];} - }; - + intN, M, p, K, bound; - intattack[3]; + intstat[1<<m], id[1<<M], Stot; AMatrixBase, dest; at - voidMake_unit (Matrix &x) { - for(intI=0; i<stot; i++ ) - for(intj=0; j<stot; J + + ) -X.V[I][J] = i==J; - } inMatrixoperator*(ConstMatrix &a,ConstMatrix &b) { - Matrix C; to for(intI=0; i<stot; i++ ) + for(intj=0; j<stot; J + + ) { -C.V[I][J] =0; the for(intk=0; k<stot; k++ ) *C.V[I][J] + = a[i][k]*B[k][j]; $ }Panax Notoginseng returnC; - } theMatrix Mpow (Matrix A,intb) { + Matrix RT; A for(Make_unit (RT); b; b>>=1, a=a*a) the if(b&1) rt=rt*A; + returnRT; - } $ intGetarea (intSinta) { $ intRT =0; - for(intb=0; b<m; b++ ) { - if((s>>b) &1 ) { the intAA =A; - if(b<k)WuyiAA >>= Kb; the Else -AA <<= BK; WuAA &=bound; -RT |=AA; About } $ } - returnRT; - } - voidbuild () { AStot =0; +memset (ID,-1,sizeof(ID)); the for(ints=0; s<=bound; s++ ) { - if(Getarea (s,attack[1]) &s)Continue; $stat[stot]=s; theId[s] =Stot; thestot++; the } the for(ints1=0; s1<=bound; s1++ ) { - if(id[s1]==-1)Continue; in for(intS2=0; s2<=bound; s2++ ) { the if(id[s2]==-1)Continue; the if(Getarea (s1,attack[2]) &S2)Continue; About if(Getarea (s2,attack[0]) &s1)Continue; the Base. v[id[s1]][id[s2]] =1; the } the } + } - intMain () { thescanf"%d%d%d%d", &n, &m, &p, &k);BayiBound = (1<<M)-1; the for(intI=0; i<3; i++ ) the for(intj=0, O; j<p; J + + ) { -scanf"%d", &o); -Attack[i] = Attack[i] | (o<<j); the } theattack[1] ^=1<<K; the build (); theDest = Mpow (Base, N-1 ); -unsigned ans =0; the for(intI=0; i<stot; i++ ) the for(intj=0; j<stot; J + + ) theAns + =Dest[i][j];94printf"%u\n", ans); the}
View Code
Bzoj 4000 matrix fast Power optimization DP