AtCoder Beginner Contest 318 Ex Count Strong Test Cases

洛谷传送门

AtCoder 传送门

首先做一些初步的观察:A 和 B 的解法是对称的,所以 A 对的方案数等于 B 对的方案数。同时若 A 和 B 同时对则每个置换环环长为 \(1\),方案数为 \(n!\)

所以,若设 A 对的方案数为 \(x\),那么答案为 \(n!^2 - (x - n!) - (x - n!) - n! = n!^2 + n! - x\)。所以转化为算 \(x\)

A 对当且仅当每个置换环的最大边刚好是编号最小的点的出边。设确定 \(p_i\) 后环长分别为 \(l_1, l_2, \ldots, l_m\),那么安排边权的方案数为 \(n! \prod\limits_{i = 1}^m \frac{1}{l_i}\),其中 \(n!\) 可以放到最后乘。

那么设环的 EGF 为 \(\hat F(x)\),有:

\[\hat F(x) = \sum\limits_{i \ge 0} \frac{(i - 1)!}{i \times i!} = \sum\limits_{i \ge 0} \frac{1}{i^2} \]

其中 \((i - 1)!\) 为长度为 \(i\) 的圆排列方案数。

设答案的 EGF 为 \(\hat G(x)\),有标号的组合对象拼接,可得:

\[\hat G(x) = \sum\limits_{i \ge 0} \frac{\hat F(x)^i}{i!} = \exp(\hat F(x)) \]

那么 \(x = n!^2 [x^n] \hat F(x)\)

时间复杂度 \(O(n \log n)\)

code
// Problem: Ex - Count Strong Test Cases
// Contest: AtCoder - THIRD PROGRAMMING CONTEST 2023 ALGO(AtCoder Beginner Contest 318)
// URL: https://atcoder.jp/contests/abc318/tasks/abc318_h
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 1000100;
const ll mod = 998244353, G = 3;

inline ll qpow(ll b, ll p) {
	ll res = 1;
	while (p) {
		if (p & 1) {
			res = res * b % mod;
		}
		b = b * b % mod;
		p >>= 1;
	}
	return res;
}

ll n, iv[maxn], fac[maxn];

typedef vector<ll> poly;

int r[maxn];

inline poly NTT(poly a, int op) {
	int n = (int)a.size();
	for (int i = 0; i < n; ++i) {
		if (i < r[i]) {
			swap(a[i], a[r[i]]);
		}
	}
	for (int k = 1; k < n; k <<= 1) {
		ll wn = qpow(op == 1 ? G : qpow(G, mod - 2), (mod - 1) / (k << 1));
		poly pw(k);
		pw[0] = 1;
		for (int i = 1; i < k; ++i) {
			pw[i] = pw[i - 1] * wn % mod;
		}
		for (int i = 0; i < n; i += (k << 1)) {
			for (int j = 0; j < k; ++j) {
				ll x = a[i + j], y = pw[j] * a[i + j + k] % mod;
				a[i + j] = (x + y) % mod;
				a[i + j + k] = (x - y + mod) % mod;
			}
		}
	}
	if (op == -1) {
		ll inv = qpow(n, mod - 2);
		for (int i = 0; i < n; ++i) {
			a[i] = a[i] * inv % mod;
		}
	}
	return a;
}

inline poly operator * (poly a, poly b) {
	a = NTT(a, 1);
	b = NTT(b, 1);
	int n = (int)a.size();
	for (int i = 0; i < n; ++i) {
		a[i] = a[i] * b[i] % mod;
	}
	a = NTT(a, -1);
	return a;
}

inline poly mul(poly a, poly b) {
	int n = (int)a.size() - 1, m = (int)b.size() - 1, k = 0;
	while ((1 << k) <= n + m + 1) {
		++k;
	}
	for (int i = 1; i < (1 << k); ++i) {
		r[i] = (r[i >> 1] >> 1) | ((i & 1) << (k - 1));
	}
	poly A(1 << k), B(1 << k);
	for (int i = 0; i <= n; ++i) {
		A[i] = a[i];
	}
	for (int i = 0; i <= m; ++i) {
		B[i] = b[i];
	}
	poly res = A * B;
	res.resize(n + m + 1);
	return res;
}

poly inv(poly &a, int m) {
	if (m == 1) {
		poly res;
		res.pb(qpow(a[0], mod - 2));
		return res;
	}
	poly b = inv(a, m >> 1), c(m), res(m);
	for (int i = 0; i < m; ++i) {
		c[i] = a[i];
		if (i < (m >> 1)) {
			res[i] = b[i] * 2 % mod;
		}
	}
	c = mul(c, mul(b, b));
	for (int i = 0; i < m; ++i) {
		res[i] = (res[i] - c[i] + mod) % mod;
	}
	return res;
}

inline poly inv(poly a) {
	int n = (int)a.size() - 1;
	int t = __lg(n + 1);
	if ((1 << t) < n + 1) {
		++t;
	}
	poly b(1 << t);
	for (int i = 0; i <= n; ++i) {
		b[i] = a[i];
	}
	b = inv(b, 1 << t);
	b.resize(n + 1);
	return b;
}

inline poly der(poly a) {
	int n = (int)a.size() - 1;
	poly res(n);
	for (int i = 1; i <= n; ++i) {
		res[i - 1] = a[i] * i % mod;
	}
	return res;
}

inline poly itg(poly a) {
	int n = (int)a.size() - 1;
	poly res(n + 2), I(n + 2);
	I[1] = 1;
	for (int i = 2; i <= n + 1; ++i) {
		I[i] = (mod - mod / i) * I[mod % i] % mod;
	}
	for (int i = 1; i <= n + 1; ++i) {
		res[i] = a[i - 1] * I[i] % mod;
	}
	return res;
}

inline poly ln(poly a) {
	int n = (int)a.size() - 1;
	poly res = itg(mul(der(a), inv(a)));
	res.resize(n + 1);
	return res;
}

poly exp(poly &a, int m) {
	if (m == 1) {
		poly res;
		res.pb(1);
		return res;
	}
	poly b = exp(a, m >> 1);
	b.resize(m);
	poly c = ln(b), d(m);
	for (int i = 0; i < m; ++i) {
		d[i] = (a[i] - c[i] + mod) % mod;
	}
	d[0] = (d[0] + 1) % mod;
	poly res = mul(b, d);
	res.resize(m);
	return res;
}

inline poly exp(poly a) {
	int n = (int)a.size() - 1;
	int t = __lg(n + 1);
	if ((1 << t) < n + 1) {
		++t;
	}
	poly b(1 << t);
	for (int i = 0; i <= n; ++i) {
		b[i] = a[i];
	}
	b = exp(b, 1 << t);
	b.resize(n + 1);
	return b;
}

void solve() {
	scanf("%lld", &n);
	iv[1] = fac[0] = 1;
	for (int i = 1; i <= n; ++i) {
		fac[i] = fac[i - 1] * i % mod;
	}
	for (int i = 2; i <= n; ++i) {
		iv[i] = (mod - mod / i) * iv[mod % i] % mod;
	}
	poly a(n + 1);
	for (int i = 1; i <= n; ++i) {
		a[i] = iv[i] * iv[i] % mod;
	}
	a = exp(a);
	ll ans = fac[n] * fac[n] % mod * a[n] % mod;
	ans = (fac[n] * fac[n] % mod + fac[n] - ans * 2 % mod + mod) % mod;
	printf("%lld\n", ans);
}

int main() {
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

posted @ 2024-05-10 12:42  zltzlt  阅读(26)  评论(0编辑  收藏  举报