【2022 省选训练赛 Contest 18 C】C(容斥)
作者:互联网
C
题目链接:2022 省选训练赛 Contest 18 C
题目大意
搭积木,有 n 行 m 列。
告诉你从正面和侧面长的样子,问你有多少种搭积木的方式使得满足要求。
思路
考虑先转换题意。
可以变成问你有多少个 \(n*m\) 的二维矩阵,使得每行和每列的最大值固定。
不难想到一个容斥的方法,考虑具体实现。
然后你会发现你可以按高度从小到大依次解决。
每次就处理一个高度 \(h\)。(就作为上限的高度)
然后你就处理对应的格子,然后你会发现它是某几行某几列。
然后你就可以容斥,枚举有多少行多少列没有达到它高度的上限。
然后就是 \((-1)^{i+j}(h+1)^{unboom}h^{boom}\binom{cnta}{i}\binom{cntb}{j}\) 这个东西。
然后每个加起来就是这个高度的答案,每个高度的答案乘起来就是我们要的答案啦。
代码
#include<cstdio>
#include<algorithm>
#define ll long long
#define mo 1000000009
using namespace std;
const int N = 200 + 10;
int n, m, xl[N << 1], numa[N << 1], numb[N << 1];
int a[N], b[N], cnt;
ll jc[N], inv[N], ans;
ll C(ll n, ll m) {
return jc[n] * inv[m] % mo * inv[n - m] % mo;
}
ll ksm(ll x, ll y) {
ll re = 1;
while (y) {
if (y & 1) re = re * x % mo;
x = x * x % mo; y >>= 1;
}
return re;
}
int main() {
jc[0] = 1; for (int i = 1; i < N; i++) jc[i] = jc[i - 1] * i % mo;
inv[0] = inv[1] = 1; for (int i = 2; i < N; i++) inv[i] = inv[mo % i] * (mo - mo / i) % mo;
for (int i = 1; i < N; i++) inv[i] = inv[i - 1] * inv[i] % mo;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), xl[i] = a[i];
for (int i = 1; i <= m; i++) scanf("%d", &b[i]), xl[n + i] = b[i];
sort(xl + 1, xl + n + m + 1); cnt = unique(xl + 1, xl + n + m + 1) - xl - 1;
for (int i = 1; i <= cnt; i++) {
for (int j = 1; j <= n; j++) if (a[j] == xl[i]) numa[i]++;
for (int j = 1; j <= m; j++) if (b[j] == xl[i]) numb[i]++;
}
ans = 1;
for (int i = 1; i <= cnt; i++) {
ll lup = 0, rup = 0, an = 0;
for (int j = i + 1; j <= cnt; j++) lup += numa[j], rup += numb[j];
for (int x = 0; x <= numa[i]; x++)
for (int y = 0; y <= numb[i]; y++) {
ll now = ((x + y) & 1) ? mo - 1 : 1;
ll unboom = (numa[i] - x) * rup + lup * (numb[i] - y) + (numa[i] - x) * (numb[i] - y);
ll boom = numa[i] * rup + lup * numb[i] + numa[i] * numb[i] - unboom;
(now *= ksm(xl[i] + 1, unboom) * ksm(xl[i], boom) % mo * C(numa[i], x) % mo * C(numb[i], y) % mo) %= mo;
an = (an + now) % mo;
}
ans = ans * an % mo;
}
printf("%lld", ans);
return 0;
}
标签:Contest,省选,18,mo,高度,++,int,inv,jc 来源: https://www.cnblogs.com/Sakura-TJH/p/16065492.html