版本比较
【学习笔记】Min-Max 容斥
正文
增加 15 行删除 15 行
4 行未改动567891011121341 行未改动555657585960616263646566676869707172737475767778798029 行未改动1101111121131141151161171181191201211221231241251261271281293 行未改动1331341351361371381391401412 行未改动4 行未改动$$\max_{i\in S}a_i=\sum_{T\subseteq S,T\ne \varnothing}(-1)^{|T|+1}\min_{i\in T}a_i$$$$\min_{i\in S}a_i=\sum_{T\subseteq S,T\ne \varnothing}(-1)^{|T|+1}\max_{i\in T}a_i$$以上式为例。考虑计算一个数对答案的贡献,设其为第 $k$ 大,那么其作为最小值贡献了$$\sum_{i=0}^{k-1}(-1)^i\dbinom{k-1}{i}$$次。根据组合数知识,当且仅当 $k=1$ 时上式为 $1$,否则为 $0$,即只有最大值贡献了一次。41 行未改动```cpp#include <bits/stdc++.h>#define pc(i) __builtin_popcount(i)using namespace std;const int N = 20;const double eps = 1e-10;double f[1 << N];int main() {ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);int n, s; cin >> n; n = 1 << n;for (int i = 0; i < n; i++) cin >> f[i];for (int i = 1; i < n; i <<= 1)for (int j = 0; j < n; j++)if (j & i) f[j] += f[j ^ i];double ans = 0;for (int i = 1; i < n; i++)if (1 - f[n - 1 ^ i] > eps)ans += ((pc(i) & 1) ? 1 : -1) / (1 - f[n - 1 ^ i]);if (ans < eps) cout << "INF\n";else printf("%.10lf\n", ans);return 0;}```**[P4707 重返现世](https://www.luogu.com.cn/problem/P4707)**29 行未改动int res = 1;while (b) {if (b & 1) res = res * a % P;a = a * a % P, b >>= 1;} return res;}const int N = 1005, K = 15, M = 1e4 + 5;int f[2][K][M], a[N];signed main() {ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);int n, k, m; cin >> n >> k >> m;for (int i = 1; i <= n; i++) cin >> a[i];k = n - k + 1; f[0][0][0] = 1;for (int i = 1; i <= n; i++) {int x = i & 1, y = x ^ 1;memset(f[x], 0, sizeof(f[x]));for (int j = 0; j <= k; j++)for (int p = 0; p <= m; p++) {f[x][j][p] = f[y][j][p]; if (p < a[i] || !j) continue;3 行未改动int ans = 0;for (int i = 0; i <= m; i++)ans = (ans + f[n & 1][k][i] * qpow(i)) % P;cout << ans * m % P << '\n';return 0;}```**练习**2 行未改动4 行未改动567891011121341 行未改动555657585960616263646566676869707172737475767778798029 行未改动1101111121131141151161171181191201211221231241251261271281293 行未改动1331341351361371381391401412 行未改动4 行未改动$$\max_{i\in S}a_i=\sum_{T\subseteq S,T\ne \varnothing}(-1)^{|T|+1}\min_{i\in T}a_i$$$$\min_{i\in S}a_i=\sum_{T\subseteq S,T\ne \varnothing}(-1)^{|T|+1}\max_{i\in T}a_i$$以上式为例。考虑计算一个数对答案的贡献,设其为第 $k$ 大,那么其作为最小值贡献了$$\sum_{i=0}^{k-1}(-1)^i\dbinom{k-1}{i}$$次。根据组合数知识,当且仅当 $k=1$ 时上式为 $1$,否则为 $0$,即只有最大值贡献了一次。41 行未改动```cpp#include <bits/stdc++.h>#define pc(i) __builtin_popcount(i)using namespace std;const int N = 20;const double eps = 1e-10;double f[1 << N];int main() {ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);int n, s; cin >> n; n = 1 << n;for (int i = 0; i < n; i++) cin >> f[i];for (int i = 1; i < n; i <<= 1)for (int j = 0; j < n; j++)if (j & i) f[j] += f[j ^ i];double ans = 0;for (int i = 1; i < n; i++)if (1 - f[n - 1 ^ i] > eps)ans += ((pc(i) & 1) ? 1 : -1) / (1 - f[n - 1 ^ i]);if (ans < eps) cout << "INF\n";else printf("%.10lf\n", ans);return 0;}```**[P4707 重返现世](https://www.luogu.com.cn/problem/P4707)**29 行未改动int res = 1;while (b) {if (b & 1) res = res * a % P;a = a * a % P, b >>= 1;} return res;}const int N = 1005, K = 15, M = 1e4 + 5;int f[2][K][M], a[N];signed main() {ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);int n, k, m; cin >> n >> k >> m;for (int i = 1; i <= n; i++) cin >> a[i];k = n - k + 1; f[0][0][0] = 1;for (int i = 1; i <= n; i++) {int x = i & 1, y = x ^ 1;memset(f[x], 0, sizeof(f[x]));for (int j = 0; j <= k; j++)for (int p = 0; p <= m; p++) {f[x][j][p] = f[y][j][p]; if (p < a[i] || !j) continue;3 行未改动int ans = 0;for (int i = 0; i <= m; i++)ans = (ans + f[n & 1][k][i] * qpow(i)) % P;cout << ans * m % P << '\n';return 0;}```**练习**2 行未改动