At ABC453 题解
_TwilightSnow_ · · 题解
[ABC453E] Team Division
小清新计数题。
设数组
然后,枚举人数
那么就做完了,记得开 long long。
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, s, t) for (int i = s; i <= t; i++)
#define pre(i, s, t) for (int i = s; i >= t; i--)
const int N = 2e5 + 2, MD = 998244353;
int n, a[N], b[N], c[N], fct[N] = {1}, invf[N], res;
int power(int x, int k) {
int res = 1;
for (; k; k >>= 1, x = x * x % MD)
if (k & 1) res = res * x % MD;
return res;
}
int C(int x, int y) { return fct[y] * invf[x] % MD * invf[y - x] % MD; }
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
rep(i, 1, n) {
int l, r; cin >> l >> r;
a[l]++, a[r + 1]--, b[n - r]++, b[n - l + 1]--;
if (max(l, n - r) <= min(r, n - l)) c[max(l, n - r)]++, c[min(r, n - l) + 1]--;
}
rep(i, 1, n) fct[i] = fct[i - 1] * i % MD;
invf[n] = power(fct[n], MD - 2);
pre(i, n, 1) invf[i - 1] = invf[i] * i % MD;
rep(i, 1, n - 1) {
a[i] += a[i - 1], b[i] += b[i - 1], c[i] += c[i - 1];
if (a[i] + b[i] - c[i] == n && a[i] - c[i] <= i && i <= a[i]) (res += C(i - a[i] + c[i], c[i])) %= MD;
}
return cout << res, 0;
}