bzoj1004 [HNOI2008]Cards Burnside 引理+背包

题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=1004

题解

直接 Burnside 引理就可以了。

要计算不动点的个数,那么对于一个长度为 \(x\) 的循环,必须全部是红色、蓝色、绿色三种。

所以显然可以 DP。令 \(dp[i][j][k]\) 表示前 \(i\) 个循环,\(j\) 张牌选了红色,\(k\) 张牌选了蓝色,剩下的选了绿色的方案数。背包转移就可以了。

最后记得要比 \(m\) 个置换多算一个 \(f_i = i\) 的排列,也就是不动的情况。


代码如下,时间复杂度 \(O(mn^3)\)

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
	int f = 0, c;
	while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
	x = c & 15;
	while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
	f ? x = -x : 0;
}

const int N = 60 + 7;

int n, nr, nb, ng, m, P;
int fa[N], sz[N], siz[N], s[N], dp[N][N][N];

inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
inline void merge(int x, int y) {
	x = find(x), y = find(y);
	if (x == y) return;
	if (sz[x] < sz[y]) std::swap(x, y);
	fa[y] = x, smax(sz[x], sz[y] + 1), siz[x] += siz[y];
}

inline int smod(int x) { return x >= P ? x - P : x; }
inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
inline int fpow(int x, int y) {
	int ans = 1;
	for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
	return ans;
}

inline int DP() {
	memset(dp, 0, sizeof(dp));
	dp[0][0][0] = 1;
	int i = 0;
	for (int g = 1; g <= n; ++g) {
		if (find(g) != g) continue;
		++i;
		s[i] = s[i - 1] + siz[g];
//		dbg("***** i = %d\n", i);
		for (int j = 0; j <= std::min(s[i], nr); ++j)
			for (int k = 0; j + k <= s[i] && k <= nb; ++k)
				if (s[i] - j - k <= ng) {
					if (j >= siz[g]) sadd(dp[i][j][k], dp[i - 1][j - siz[g]][k]);
					if (k >= siz[g]) sadd(dp[i][j][k], dp[i - 1][j][k - siz[g]]);
					if (s[i] - j - k >= siz[g]) sadd(dp[i][j][k], dp[i - 1][j][k]);
//					dbg("dp[%d][%d][%d] = %d, siz[g] = %d\n", i, j, k, dp[i][j][k], siz[g]);
				}
	}
	return dp[i][nr][nb];
}

inline void work() {
	for (int i = 1; i <= n; ++i) fa[i] = i, sz[i] = siz[i] = 1;
	int ans = DP();
	for (int k = 1; k <= m; ++k) {
		for (int i = 1, x; i <= n; ++i) read(x), merge(i, x);
		sadd(ans, DP());
	}
	ans = (ll)ans * fpow(m + 1, P - 2) % P;
	printf("%d\n", ans);
}

inline void init() {
	read(nr), read(nb), read(ng);
	n = nr + nb + ng;
	read(m), read(P);
}

int main() {
#ifdef hzhkk
	freopen("hkk.in", "r", stdin);
#endif
	init();
	work();
	fclose(stdin), fclose(stdout);
	return 0;
}
posted @ 2019-09-23 15:27  hankeke303  阅读(112)  评论(0编辑  收藏  举报