题解:CF1799G Count Voting
思路
限制太多,无法计数,考虑反演。
设
问题转化为求解
决策人对人很难,考虑决策票对人(可以看作票是有组别的,票的组别就是它投给的人的组别,票不能和同组的人配对),这样就是一个配对问题,可以看作排列计数。注意这样相当于给投一个人的票强制定序了,最后需要除法原理。
设
考虑枚举每一个组别去 dp 算贡献,设
细致分析一下,至此
code
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
namespace fast_IO{
#define ld cin
#define jyt cout
}using namespace fast_IO;
#define REP(i, l, r) for (int i = l; i <= r; ++i)
#define PER(i, l, r) for (int i = l; i >= r; --i)
#define clean(name) memset(name, 0, sizeof(name))
#define memst(name, n) memset(name, 0, (n + 1) * sizeof(name[0]))
// #define int long long
constexpr int N = 200 + 7;
constexpr int P = 998244353;
namespace JoKing {
int n, m, q, a[N], b[N], S[N], T[N], lsh[N]; ll fac[N], inv[N], f[N], dp[N][N], Calc, Res;
inline long long qpow(long long x, long long y) {long long R = 1; for (; y; (x *= x) %= P, y >>= 1ll) if (y & 1ll) (R *= x) %= P; return R;}
inline ll C(int x, int y) {return fac[x] * inv[y] % P * inv[x - y] % P;}
signed main() {
ld >> n, dp[0][0] = Calc = fac[0] = 1;
REP(i, 1, n) fac[i] = fac[i - 1] * i % P;
inv[n] = qpow(fac[n], P - 2);
PER(i, n, 1) inv[i - 1] = inv[i] * i % P;
REP(i, 1, n) ld >> a[i], (Calc *= inv[a[i]]) %= P;
REP(i, 1, n) ld >> b[i], lsh[i] = b[i];
sort(lsh + 1, lsh + n + 1), m = unique(lsh + 1, lsh + n + 1) - lsh - 1;
REP(i, 1, n) b[i] = lower_bound(lsh + 1, lsh + m + 1, b[i]) - lsh, ++S[b[i]], T[b[i]] += a[i];
REP(i, 1, m) {
q += min(S[i], T[i]);
REP(j, 0, q) REP(k, 0, min(j, min(S[i], T[i])))
(dp[i][j] += dp[i - 1][j - k] * C(S[i], k) % P * C(T[i], k) % P * fac[k]) %= P;
}
REP(i, 0, q) f[i] = dp[m][i] * fac[n - i] % P * Calc % P;
REP(i, 0, q) (Res += (i & 1 ? -1ll : 1ll) * f[i]) %= P;
(Res += P) %= P, jyt << Res << '\n';
return 0;
}
}
signed main() {
#ifdef WYY
freopen("files/code.in", "r", stdin);
freopen("files/code.out", "w", stdout);
freopen("files/code.err", "w", stderr);
#endif
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
JoKing::main(); return 0;
}