WC模拟(1.10) T2 Permutation

Permutation

题目背景:

1.10 WC模拟T2

分析:结论推理 + 状态分析

 

首先,这个题·····如果找找规律,40分是很好拿的,听说再找找规律连100分都能拿了,现在我们来考虑有理有据的证明,首先我们先dfs一遍,求得在钦定的情况下,有多少奇环,偶环,奇链,偶链,显然如果有奇环,直接不合法,偶环可以直接忽略,直接记录奇链的个数和偶链的个数,显然,奇链的个数如果是奇数也是不行的(对于一个单独的点,也算作一个奇链),现在考虑,如何连接,假设,我们定义f[x][y]表示有x个奇链,y个偶链的好的方案,我们考虑对于一个偶链y该如何连接,如果这个偶链连接了另一条偶链,那么很显然的偶链个数减一,奇链个数不变,如果连接了另一条奇链,那么还是偶链个数减一,奇链个数不变,如果连接自己形成一个偶环,依然偶链个数减一,奇链个数不变,那么我们就发现,对于一个偶链,各种连接都是等价行为,那么我们可以得到f[x][y] = f[x][y - 1] * (x + y),那么答案就变成了f[x][0] * (x + 1) * (x + 2) *  * (x + y),考虑计算f[x][0]即,只有x条奇链的方案数,比较简单的方法打个表就会发现是小于x的奇数之积的平方,再次有理有据的证明,考虑对于一个奇链a,它先连接除它自己以外的任意一条奇链b,然后b再选择除b自己以外的一条奇链c连接,考虑,如果b连回a,那么出现一个偶环,奇链个数减二,如果b连的不是a,那么奇链个数依然减少2,那么我们就可以知道f[x][0] = f[x - 2][0] * (x - 1)2ab的连接都有x - 1种选择,乘法原理即可,所以直接算一遍就好了,复杂度O(n)

 

Source:

/*
	created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
#include <ctime>
#include <bitset>

inline char read() {
	static const int IN_LEN = 1024 * 1024;
	static char buf[IN_LEN], *s, *t;
	if (s == t) {
		t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
		if (s == t) return -1;
	}
	return *s++;
}

///*
template<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = read(), iosig = false; !isdigit(c); c = read()) {
		if (c == -1) return ;
		if (c == '-') iosig = true;	
	}
	for (x = 0; isdigit(c); c = read()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
	if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
	*oh++ = c;
}

template<class T>
inline void W(T x) {
	static int buf[30], cnt;
	if (x == 0) write_char('0');
	else {
		if (x < 0) write_char('-'), x = -x;
		for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
		while (cnt) write_char(buf[cnt--]);
	}
}

inline void flush() {
	fwrite(obuf, 1, oh - obuf, stdout);
}

/*
template<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = getchar(), iosig = false; !isdigit(c); c = getchar())
		if (c == '-') iosig = true;	
	for (x = 0; isdigit(c); c = getchar()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int MAXN = 100000 + 10;
const int mod = 998244353;

int n, cnt;
int p[MAXN];
bool unable[MAXN], vis[MAXN], is_c;
int cnt1, cnt2;

std::vector<int> edge[MAXN];

inline void add_edge(int x, int y) {
	edge[x].push_back(y);
}

inline void dfs(int cur) {
	if (vis[cur]) {
		is_c = true;
		return ;
	}
	vis[cur] = true, cnt++;
	for (int p = 0; p < edge[cur].size(); ++p)
		dfs(edge[cur][p]);
	vis[cur] = false;
}

inline void read_in() {
	R(n);
	for (int i = 1; i <= n; ++i) {
		R(p[i]), unable[p[i]] = true;
		if (p[i] != 0) add_edge(i, p[i]);
	}
}

inline void solve() {
	for (int i = 1; i <= n; ++i)
		if (!unable[i]) {
			is_c = false, cnt = 0, dfs(i);
			if (is_c) {
				if (cnt & 1) {
					std::cout << "0", exit(0);
				}
			} else {
				if (cnt & 1) cnt1++;
				else cnt2++;
			}
		}
	if (cnt1 & 1) std::cout << "0", exit(0);
	int ans = 1;
	for (int i = 1; i <= cnt1; i += 2) ans = (long long)ans * i % mod * i % mod;
	for (int i = 1; i <= cnt2; ++i) ans = (long long)ans * (cnt1 + i) % mod;
	std::cout << ans;
}

int main() {
	freopen("permutation.in", "r", stdin);
	freopen("permutation.out", "w", stdout);
	read_in();
	solve();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值