[Bzoj] 1004: [Hnoi2008]cards

Source: Internet
Author: User
Tags shuffle

Description

Koharu is now very quiet, facing the desk of n cards, he decided to give each stain, currently Koharu only 3 colors: red, blue, green. He asked Sun
The number of staining schemes sun soon gave the answer. Further, the Koharu request to dye the SR Zhang Red, SB Zhang Blue, the SG Zhang. He asked how many kinds of parties
Case, Sun thought about it and gave the correct answer. Finally Koharu invented M different shuffle methods, where he asked Sun how many different dyeing schemes.
The two staining methods are the same when and only if one can be used by arbitrary shuffle method (that is, you can use a variety of shuffle method, and each method can be used multiple) 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 shuffle method, each line has n spaces separated by a number of integers x1x2 ... Xn, just a permutation of 1 to N,
Represents the use of this shuffle method, the first bit into the original XI position of the card. Input data to ensure that any number of shuffle can be used in this M-shuffle method of a generation
, 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.

Source

Here is a relatively good article to be reproduced: Burnside lemma and Polya theorem

The difference between lemma and theorem:

The lemma is a proposition that is proved as a step in mathematics in order to obtain a better theorem, and its significance is not to prove itself, but to contribute to the achievement of the final theorem. A lemma can be used to prove multiple theorems. There are many famous lemma in mathematics, these lemma may be helpful to solve many problems. For example, Burnside lemma such as Euclid's lemma: Set G={A1,A2,... AG} is the permutation group on the target set [1,n].  Each permutation is written as the product of the disjoint loop. Is the number of fixed points under the action of the substitution AK, that is, the number of loops with a length of 1. Elements that can be equal after the transformation operation of the above permutation belong to the same equivalence class. If g divides [1,n] into an L equivalence class, then: The number of equivalence classes is: What Ghost is written on the encyclopedia ...(Black question mark??? )Use this example to illustrate:

Example 1: A square divided into 4 squares, 2 coloring, how many kinds of programs? The same scheme is calculated by rotating the same image.

The displacement of the image in the picture can be divided into the following four kinds: a1= (1) (2) ... (16) Turn counterclockwise 90 degrees: a2= (1) (2) (3 4 5 6) (7 8 9 10) (11 12) (13 14 15 16) Turn clockwise 90 degrees: a3= (1) (2) (6 5 4 3) (10 9, 8) (7 11) (12 16 15 14) Ext. 13 0 degrees: a4= (1) (2) (3 5) (4 6) (7 9) (8 10) (11) (12) (13 15) (14 16) by Burnside Lemma, Total (16+2+2+4)/4=6 (kind of scheme) here divided by 4 represents 4 scenarios 16+2+2+ 4 represents the same lattice A1 as the original after each scheme is replaced: (1) (2) ... (+) A2: (1) (2) A3: (1) (2) A4: (1) (2) (11) (12) Then we can write the DP transfer equation for the number of scenarios: f[i][j][k]=f[i-d[h]][j][k]+f[i][j-d[h]][k]+f[i][j][ K-d[h]]f[i][j][k] Represents the number of methods R,g,b, respectively I,j,k, using the extended Euclidean theorem to seek ans%p
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 6 intSr,sb,sg,n,m,p,ans;7 inta[ -][ -],f[ -][ -][ -],d[ -];8 BOOLb[ -];9 Ten intdpintx) One { Amemset (b,0,sizeof(b)); -     intsum=0, T; -      for(intI=1; i<=n;i++) the         if(!B[i]) -         { -d[++sum]=1; t=i; -b[t]=1; +              while(!B[a[x][t]]) -             { +d[sum]++; Ab[a[x][t]]=1; att=A[x][t]; -             } -         } -Memset (F,0,sizeof(f)); -f[0][0][0]=1; -      for(intH=1; h<=sum;h++) in          for(inti=sr;i>=0; i--) -              for(intj=sb;j>=0; j--) to                  for(intk=sg;k>=0; k--) +                 { -                     if(I>=d[h]) f[i][j][k]= (F[i][j][k]+f[i-d[h]][j][k])%p; the                     if(J>=d[h]) f[i][j][k]= (F[i][j][k]+f[i][j-d[h]][k])%p; *                     if(K>=d[h]) f[i][j][k]= (F[i][j][k]+f[i][j][k-d[h])%p; $                 }Panax Notoginseng     returnF[SR][SB][SG]; - } the  + intEXGCD (intAintBint&x,int&y) A { the     if(b==0) +     { -x=1; y=0; $         returnA; $     } -     intQ=EXGCD (b,a%b,y,x); -y-=a/b*x; the     returnQ; - }Wuyi  the intMain () - { Wuscanf"%d %d%d%d%d",&sr,&sb,&sg,&m,&p); -n=sr+sb+SG; About      for(intI=1; i<=m;i++) $          for(intj=1; j<=n;j++) -scanf"%d",&a[i][j]); -m++; -      for(intI=1; i<=n;i++) a[m][i]=i; A      for(intI=1; i<=m;i++) +Ans= (ANS+DP (i))%p; the     intx, y; - EXGCD (m,p,x,y); $      while(x<=0) x+=p,y-=m; theprintf"%d", ans*x%p); the     return 0; the}

[Bzoj] 1004: [Hnoi2008]cards

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.