题目大意:给出n张牌,分别染a张红色,b张蓝色,c张绿色(n=a+b+c),给出m个置换,问不同的染色方案。
置换群中的染色方案问题可用Burnside引理解决。
Burnside引理可以借助百度百科 中应用的例1理解,或者参考白书理解。
现在要求出不动点的个数。每个置换中都有若干个循环,不动点在一个循环中的颜色必须相同。可以转化成背包问题,将每个循环看作是一个物品,物品的重量为循环元素个数。
最后做除法在模意义下需要逆元,题目中保证模数为质数所以费马小定理一下就好…
#include <cstdio>
#include <cstring>
#define N 105
using namespace std;
typedef long long LL;
int a,b,c,n,m,MOD,p[N];
LL f_pow(LL x,LL y) {
LL tmp=1;
while(y) {
if(y&1) (tmp*=x)%=MOD;
(x*=x)%=MOD;
y>>=1;
}
return tmp;
}
int calc() {
static bool k[N];
static int s[N],top,f[N][N][N];
memset(k,false,sizeof k);
top=0;
for(int i=1;i<=n;i++) {
if(k[i]) continue;
int cnt=0,tmp=i;
while(!k[tmp]) k[tmp]=true, cnt++, tmp=p[tmp];
s[top++]=cnt;
}
memset(f,0,sizeof f);
f[0][0][0]=1;
while(top--)
for(int i=a;~i;i--)
for(int j=b;~j;j--)
for(int t=c;~t;t--) {
if(i>=s[top]) (f[i][j][t]+=f[i-s[top]][j][t])%=MOD;
if(j>=s[top]) (f[i][j][t]+=f[i][j-s[top]][t])%=MOD;
if(t>=s[top]) (f[i][j][t]+=f[i][j][t-s[top]])%=MOD;
}
return f[a][b][c];
}
int main() {
scanf("%d%d%d%d%d",&a,&b,&c,&m,&MOD);
n=a+b+c;
LL ans=0;
for(int i=1;i<=m;i++) {
for(int j=1;j<=n;j++) scanf("%d",p+j);
ans+=calc();
}
for(int j=1;j<=n;j++) p[j]=j;
ans+=calc();
printf("%lld\n",ans*f_pow((LL)m+1,(LL)MOD-2)%MOD);
return 0;
}