1004: [Hnoi2008]cards
Test instructions: There are n cards, dyed into S1 Zhang Red, s2 blue and S3 Green, then there is m substitution relationship, asked the nature of the different dyeing program how many kinds?
Input
The first line enters 5 integers: sr,sb,sg,m,p (m<=60,m+1<p<100). N=SR+SB+SG. Next m line, each line describes
A shuffling method, each line has n a space separated by the integer x1x2 ... Xn, just a permutation of 1 to N, represents the use of this shuffling method,
The first bit becomes the card of the original XI position . Input data to ensure that any number of shuffle can be used in this m shuffle method of a substitution, and for each
Shuffle method, there is a shuffling method so that can return to the original state.
Output
The remainder of the different dyes divided by P
Sample Input1 1 1) 2 7
2 3 1
3 1 2Sample Output
2
HINT
There are 2 essentially different staining methods RGB and RBG, using shuffle method 2,311 times can get GBR and BGR, using shuffle method 3,121 times can get BRG and GRB.
100% data satisfies max{sr,sb,sg}<=20.
Idea: For the input permutation, first uses the cyclic decomposition algorithm to decompose the CNT cycle, and obtains each loop inside the element number is num;
If there is no mod operation, the sum of all the k^ (M (f)) of the permutation F is directly the average;
K represents the number of colors, M (f) indicates the number of cyclic sections (i.e., the number of disjoint loops) of permutation f after the sequence is decomposed.
This is because, under the action of displacement, the color of elements within each loop must be the same; the average is the total number of permutations;
An implied permutation is not operation, f[i] = i;
Difficulty: The title of each color of the number of colors are specified, because each cycle of the elements of the color is the same, then the cycle must be dyed as a whole; so in 01 backpacks, the size of each cycle is equivalent to the weight of the item; Note that each color is likely to dye a loop;
After finding the sum of the MoD, the sum/m becomes the addition of the MoD meaning;
That is m*x = SUM (mod p) ==> m*x + p*y = SUM (in exgcd () is calculated = GCD (m,p) = 1);
#include <iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<vector>#include<cmath>#include<stdlib.h>#include<time.h>#include<stack>#include<Set>#include<map>#include<queue>using namespacestd;#defineRep0 (I,L,R) for (int i = (l); i < (R); i++)#defineREP1 (I,L,R) for (int i = (l); I <= (r); i++)#defineRep_0 (i,r,l) for (int i = (r); i > (l); i--)#defineRep_1 (i,r,l) for (int i = (r); I >= (l); i--)#defineMS0 (a) memset (A,0,sizeof (a))#defineMS1 (a) memset (A,-1,sizeof (a))#defineMSi (a) memset (A,0x3f,sizeof (a))#defineINF 0x3f3f3f3f#defineLson L, M, RT << 1#defineRson m+1, R, RT << 1|1//typedef __int64 LL;Template<typename t>voidRead1 (T &m) {T x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} M= x*F;} Template<typename t>voidRead2 (T &a,t &b) {Read1 (a); Read1 (b);} Template<typename t>voidRead3 (T &a,t &b,t &c) {Read1 (a); Read1 (b); Read1 (c);} Template<typename t>void out(T a) {if(a>9) out(ATen); Putchar (A%Ten+'0');}intS1,s2,s3,p,m,n;intvis[ -],f[ -][ -][ -],num[ -],b[ -][ -];intDP (intID) {MS0 (VIS); intCNT =0; REP1 (i,1, N)if(!vis[i]) {//decomposition of the cycleNUM[++CNT] =0; intTMP =i; Do{vis[tmp]=CNT; NUM[CNT]++; TMP=B[id][tmp]; } while(!vis[tmp]); } MS0 (f); f[0][0][0] =1; REP1 (i,1, CNT) {rep_1 (J,S1,0) rep_1 (K,S2,0) rep_1 (Q,S3,0){ if(J >= Num[i]) (F[j][k][q] + = F[j-num[i]][k][q])%=p; if(k >= Num[i]) (F[j][k][q] + = F[j][k-num[i]][q])%=p; if(q >= num[i]) (F[j][k][q] + = F[j][k][q-num[i]])%=p; } } returnF[S1][S2][S3];}voidEXGCD (intAintBint& X,int&y) { if(b = =0) {x=1, y =0;//the right side of the equation is gcd (a,p) = 1; At this time a = gcd (a,p); return ; } exgcd (B,a%b,y,x); Y-= a/b*x;}intMain () {read3 (S1,S2,S3); Read2 (M,P); N= S1 + s2 +S3; REP1 (i,1, M) Rep1 (J,1, N) read1 (b[i][j]); ++m;//set a permutation that does not change;REP1 (I,1, n) b[m][i] =i; intsum =0; REP1 (i,1, m) sum + = DP (i);//Records the K^m (f) of all displacement F and intx, y;//ans = sum/m; it's when there's no mod.EXGCD (m,p,x,y); while(X <0) x + = P,y-=m; printf ("%d\n", sum*x%p); return 0;}
View Code
Bzoj 1004: [Hnoi2008]cards