省流:根据容斥原理,不应该省流。
总分 100 + 100 + 100 + 0 = 300,符合预期。
T1 综合类大学
开场读错两次题目糖丸了。
根据容斥原理 简单进制问题。
#include <bits/stdc++.h>
#define dor(x, s, t) for(int x = s; x <= t; x++)
using namespace std;
int n, m, k, a[1005], o, ans;
int wei (int x) {
int kk = (1 << 20), cnt = 0;
while(kk >= 1) {
if (x >= kk) {
x -= kk; cnt++;
}
kk /= 2;
}
return cnt;
}
int main() {
cin >> n >> m >> k;
dor(i, 1, m) cin >> a[i];
cin >> o;
dor(i, 1, m) {
int P = (o | a[i]);
if (wei(P) >= k) ans++;
}
cout << ans;
return 0;
}T2 与或差
根据容斥原理 可以知道 (A | B) + (A & B) = A + B
于是就变成了一道极其简单的小学二年级和差问题。
#include <bits/stdc++.h>
#define int long long
#define dor(x, s, t) for(int x = s; x <= t; x++)
using namespace std;
int t, huo, yu, cha, he;
signed main() {
cin >> t; while(t--) {
cin >> yu >> huo >> cha;
he = huo + yu;
cout << (he + cha) / 2 << " " << (he - cha) / 2 << "\n";
}
return 0;
}T3
根据容斥原理 可以在确定 v 的情况下求出是否合法。
于是二分。
#include <bits/stdc++.h>
#define int long long
#define dor(x, s, t) for(int x = s; x <= t; x++)
using namespace std;
int cnt1, cnt2, x, y;
bool check(int w) {
int s1 = w / x, s2 = w / y, s3 = w / (x * y);
s1 -= s3; s2 -= s3;
int s4 = w - s1 - s2 - s3;
if (max((cnt1 - s2), 0ll) + max((cnt2 - s1), 0ll) <= s4) return 1;
return 0;
}
signed main() {
cin >> cnt1 >> cnt2 >> x >> y;
int l = 0, r = 1e18;
while(l < r) {
int mid = (l + r) / 2;
if (check(mid)) r = mid;
else l = mid + 1;
}
cout << l;
return 0;
}T4
根据容斥原理 通过观察发现显然 DP,推演可以得出 O(1) 转移的公式。
但是 wzj 没有考虑奇偶数导致的差别,所以保龄了。
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 1e9 + 7;
int n, a, b, k, ans, dp[2][10010];
void add(int x, int y, int k) {
dp[1][x] = (dp[1][x] + k) % MOD, dp[1][y + 1] = (dp[1][y + 1] - k + MOD) % MOD;
}
signed main() {
cin >> n >> a >> b >> k;
dp[0][a] = 1;
for (int i = 1; i <= k; i++) {
for (int j = 1; j <= n; j++) {
if (!dp[0][j]) continue;
int dis = abs(j - b), l = max(1ll, j - dis + 1), r = min(n, j + dis - 1);
add(l, j - 1, dp[0][j]);
add(j + 1, r, dp[0][j]);
}
for (int j = 1; j <= n; j++) dp[0][j] = (dp[0][j - 1] + dp[1][j]) % MOD, dp[1][j] = 0;
}
for (int i = 1; i <= n; i++) ans = (ans + dp[0][i]) % MOD;
cout << ans;
return 0;
}总结
根据容斥原理 可得下图:
