Description
Koharu is now very quiet, facing the desk of the n cards, he decided to give each stain, the current Koharu only 3 colors: red, blue, green. He asked Sun how many kinds of staining options he had, and sun quickly gave the answer. Further, Koharu asked to dye SR red, SB-Blue, SG Zhang. He asked how many options he had, and sun thought about it and gave the correct answer. Finally Koharu invented M different shuffle methods, where he asked Sun how many different staining schemes. Two staining methods are the same when and only if one of them can be used by arbitrary shuffle method (that is, you can use a variety of shuffle method, and each method can be used several times) to wash into another. Sun found the problem a bit difficult, and decided to give it to you, the answer may be very large, just ask for the answer divided by the remainder of P (p is prime).
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 Output2HINT
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.
According to the legend of the substitution group of the Burnside Lemma (Baidu Encyclopedia) DP. According to this theorem, we only need DP (not repeating) for Each loop, and finally exgcd the answer. F[i][j][k] That is the first color with I, the second with J, the third with K-sheet and displacement and non-repetition of the number of programs. The equation is shown in code:
#include <cstring>#include<cstdio>#include<cstdlib>using namespacestd;#defineMAXN 70#defineMAXM 70#defineMAXS 25intSr,sg,sb,n,m,p,ans;intREV[MAXM][MAXN],S[MAXN],F[MAXS][MAXS][MAXS];BOOLFlag[maxn];inlineintEXGCD (intDintf) { intX1 =1, x2 =0, x3 =F; intY1 =0, y2 =1, y3 =D; while(true) { if(Y3 = =1)return(y2%f+f)%F; intQ = x3/Y3; intT1 = x1-q * y1,t2 = x2-q * Y2,t3 = x3-q *Y3; X1= y1; x2 = y2; x3 =Y3; Y1= T1; y2 = t2; Y3 =T3; }}inlineintdpinta) {memset (flag,false,sizeof(flag)); Memset (F,0,sizeof(f)); intI,j,k,h,sz =0, Next; for(i =1; I <= n;++i)if(!Flag[i]) {s[++SZ] =1; Flag[i]=true; Next=i; while(!Flag[rev[a][next]]) {Flag[rev[a][next]]=true; S[SZ]++; Next=Rev[a][next]; }} f[0][0][0] =1; for(h =1; h <= sz;++h) for(i = Sr;i >=0;--i) for(j = sb;j >=0;--j) for(k = sg;k >=0;--k) {if(i >= s[h]) (F[i][j][k] + = F[i-s[h]][j][k])%=p; if(J >= S[h]) (F[i][j][k] + = F[i][j-s[h]][k])%=p; if(k >= S[h]) (F[i][j][k] + = F[i][j][k-s[h]])%=Q; } returnF[SR][SB][SG];}intMain () {Freopen ("1004.in","R", stdin); Freopen ("1004.out","W", stdout); scanf ("%d %d%d%d%d", &sr,&sb,&sg,&m,&p); n = sr + SB +SG; inti,j; for(i =1; I <= m;++i) for(j =1; J <= n;++j) scanf ("%d", rev[i]+j); ++m; for(i =1; I <= n;++i) rev[m][i] =i; for(i =1; I <= m;++i) (ans+ = DP (i))%=p; printf ("%d", ANS*EXGCD (m,p)%p); Fclose (stdin); Fclose (stdout); return 0;}
Bzoj 1004 Cards