Polya count +DP to satisfy the fixed point of the corresponding cycle there are several
1004: [Hnoi2008]cards time limit: ten Sec Memory Limit: 162 MB
Submit: 2046 Solved: 1212
[Submit] [Status] [Discuss] 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.
/* ***********************************************author:ckbosscreated time:2015 May 07 Thursday 09:37 37 seconds file Name : bzoj1004.cpp************************************************ * * #include <iostream> #include <cstdio># Include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib > #include <vector> #include <queue> #include <set> #include <map>using namespace Std;typedef Long long int LL; LL R,g,b,m,mod,n; ll Xp[100];void EX_GCD (ll a,ll b,ll& d,ll& x,ll &y) {if (!b) {d=a; x=1; y=0;} else {EX_GCD (b,a%b,d,y,x); y-=x* (A/b);}} LL INV (ll A,ll n=mod) {ll d,x,y;ex_gcd (a,n,d,x,y); return (d==1)? ( x+n)%n:-1;} ll Jiecheng (ll x) {ll ret=1;for (ll i=2;i<=x;i++) ret= (ret*i)%mod;return ret;} BOOL vis[100]; ll deep;vector<ll> v;void Dfs (ll u) {vis[u]=true; LL v=xp[u];if (vis[v]==false) {deep++; Dfs (v);}} LL Dp[110][30][30];int Main () {//freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "W", StdOut); scanf ("%lld%lld%lld%lld%lld", &r,&g,&b,&m,&mod); n=r+g+b; ll Ans=jiecheng (n) *inv (Jiecheng (R)) *inv (Jiecheng (g)) *inv (Jiecheng (b)); for (ll i=1;i<=m;i++) {-LL j=1;j<=n; J + +) scanf ("%lld", &xp[j]); memset (vis,false,sizeof (VIS)); V.clear (); V.push_back (0); for (LL j=1;j<=n;j++) {if (vis[j]==false) {deep=1; Dfs (j); V.push_back (deep);}} Memset (Dp,0,sizeof (DP));DP [0][0][0]=1; LL presum=0; ll Sz=v.size (); for (ll j=1;j<sz;j++) {presum+=v[j];for (ll r=0; r<=r; r++) {for (LL g=0; g<=g; g++) {LL b=presum-r-g;if (v[j]<=r) {dp[j][r][g]+=dp[j-1][r-v[j]][g];} if (v[j]<=g) {dp[j][r][g]+=dp[j-1][r][g-v[j]];} if (v[j]<=b) {dp[j][r][g]+=dp[j-1][r][g];}}}} Ans= (Ans+dp[sz-1][r][g])%mod;} Ans= (ANS*INV (m+1))%mod;printf ("%lld\n", ans); return 0;}
Bzoj 1004: [Hnoi2008]cards Polya Count +DP