The 2024 CCPC Online Contest
https://codeforces.com/gym/105336
B - 军训 II
排序后肯定是最优解,方案数就是能排成有序序列的个数
#include <bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using i64 = long long;
using i128 = __int128;
#define int i64
using vi = vector<int>;
using pii = pair<int, int>;
const int inf = 1e9, INF = 1e18;
const i64 mod = 998244353;
struct mint {
i64 x;
mint(i64 x = 0) : x(x) {};
mint &operator=(i64 o) { return x = o, *this; }
mint &operator+=(mint o) { return (x += o.x) >= mod && (x -= mod), *this; }
mint &operator-=(mint o) { return (x -= o.x) < 0 && (x += mod), *this; }
mint &operator*=(mint o) { return x = (x * o.x) % mod, *this; }
mint &operator^=(int b) {
mint w = *this, ret(1);
for (; b; b >>= 1, w *= w) if (b & 1) ret *= w;
return x = ret.x, *this;
}
mint &operator/=(mint o) { return *this *= (o ^= (mod - 2)); }
friend mint operator+(mint a, mint b) { return a += b; }
friend mint operator-(mint a, mint b) { return a -= b; }
friend mint operator*(mint a, mint b) { return a *= b; }
friend mint operator/(mint a, mint b) { return a /= b; }
friend mint operator^(mint a, i64 b) { return a ^= b; }
i64 val() {
x = (x % mod + mod) % mod;
return x;
}
};
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int n;
cin >> n;
vi a(n);
for (auto &i: a) cin >> i;
ranges::sort(a, greater<>());
int res = 0;
for (int cnt = n, sum = accumulate(a.begin(), a.end(), 0); auto i: a) {
cnt--, sum -= i;
res += cnt * i - sum;
}
cout << res;
vector<mint> fact(n + 1);
fact[0] = 1;
for (int i = 1; i <= n; i++)
fact[i] = fact[i - 1] * i;
mint ret(2 - (a.back() == a.front()));
a.push_back(-1);
for (int lst = -1, cnt = 0; auto i: a) {
if (lst != i) {
ret *= fact[cnt], cnt = 1, lst = i;
} else {
cnt++;
}
}
cout << " " << ret.val();
return 0;
}
D - 编码器-解码器
\(f[i][l][r]\)表示\(S_i\)可以匹配\(T[l,r]\)的方案数,然后按照题目的意思进行\(O(N^4)\)的转移就好了。
#include <bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using i64 = long long;
using i128 = __int128;
#define int i64
using vi = vector<int>;
using pii = pair<int, int>;
const int inf = 1e9, INF = 1e18;
const i64 mod = 998244353;
struct mint {
i64 x;
mint(i64 x = 0) : x(x) {};
mint &operator=(i64 o) { return x = o, *this; }
mint &operator+=(mint o) { return (x += o.x) >= mod && (x -= mod), *this; }
mint &operator-=(mint o) { return (x -= o.x) < 0 && (x += mod), *this; }
mint &operator*=(mint o) { return x = (x * o.x) % mod, *this; }
mint &operator^=(int b) {
mint w = *this, ret(1);
for (; b; b >>= 1, w *= w) if (b & 1) ret *= w;
return x = ret.x, *this;
}
mint &operator/=(mint o) { return *this *= (o ^= (mod - 2)); }
friend mint operator+(mint a, mint b) { return a += b; }
friend mint operator-(mint a, mint b) { return a -= b; }
friend mint operator*(mint a, mint b) { return a *= b; }
friend mint operator/(mint a, mint b) { return a /= b; }
friend mint operator^(mint a, i64 b) { return a ^= b; }
i64 val() {
x = (x % mod + mod) % mod;
return x;
}
};
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
string s, t;
cin >> s >> t;
int n = s.size(), m = t.size();
vector f(n, vector(m + 1, vector(m + 1, mint())));
for (int i = 0; i < m; i++)
if (s[0] == t[i]) f[0][i][i] = 1;
for (int i = 1; i < n; i++)
for (int l = 0; l < m; l++)
for (int r = l; r < m; r++) {
f[i][l][r] = f[i - 1][l][r] * 2;
if (l == r) {
if (s[i] == t[l]) f[i][l][r] += 1;
} else {
if (s[i] == t[l]) f[i][l][r] += f[i - 1][l + 1][r];
if (s[i] == t[r]) f[i][l][r] += f[i - 1][l][r - 1];
for (int k = l; k < r; k++)
f[i][l][r] += f[i - 1][l][k] * f[i - 1][k + 1][r];
for (int k = l + 1; k < r; k++)
if (s[i] == t[k])
f[i][l][r] += f[i - 1][l][k - 1] * f[i - 1][k + 1][r];
}
}
cout << f[n - 1][0][m - 1].val();
return 0;
}
E. 随机过程
最大点数就是使得每一层尽可能的填满。
对于期望点数,同一层的点是相互独立的。每层能够出现的点有\(26^i\)种,对于某一种点,被选到的概率就是\(\frac{1}{26^i}\),那么选不到的概率就是\(1 - \frac{1}{26^i}\),我们要选择\(n\)次,\(n\)次都选不到的概率是\((1- \frac{1}{26^i})^ n\),所以这个点被选中的概率就是\(1 - (1- \frac{1}{26^i})^ n\)。这层的点共有\(26^i\)个,因此总的期望就是\(26^i(1 - (1- \frac{1}{26^i})^ n)\),共\(m\)层,所以答案就是
#include <bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using ui32 = uint32_t;
using i64 = long long;
//#define int i64
using vi = vector<int>;
using pii = pair<int, int>;
const i64 mod = 998244353;
struct mint {
i64 x;
mint(i64 x = 0) : x(x) {}
mint &operator=(i64 o) { return x = o, *this; }
mint &operator+=(mint o) { return (x += o.x) >= mod && (x -= mod), *this; }
mint &operator-=(mint o) { return (x -= o.x) < 0 && (x += mod), *this; }
mint &operator*=(mint o) { return x = (i64) x * o.x % mod, *this; }
inline mint &operator^=(int b) {
mint w = *this;
mint ret(1);
for (; b; b >>= 1, w *= w) if (b & 1) ret *= w;
return x = ret.x, *this;
}
mint &operator/=(mint o) { return *this *= (o ^= (mod - 2)); }
friend mint operator+(mint a, mint b) { return a += b; }
friend mint operator-(mint a, mint b) { return a -= b; }
friend mint operator*(mint a, mint b) { return a *= b; }
friend mint operator/(mint a, mint b) { return a /= b; }
friend mint operator^(mint a, int b) { return a ^= b; }
i64 val() {
x = (x % mod + mod) % mod;
return x;
}
};
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int n, m;
cin >> n >> m;
const mint b(26), one(1);
mint sum;
for (int i = 0, x = 1, t = 26; i <= m; i++) {
sum += x;
x *= t;
if (x > n) x = n, t = 1;
}
mint res;
for (int i = 0; i <= m; i++) {
mint t = b ^ i;
res += t * (one - ((one - (one / t)) ^ n));
}
cout << sum.val() << " " << res.val();
return 0;
}
J. 找最小
首先我们可以求出原本的异或和\(A,B\),再求出\(c_i = a_i \oplus b_i\)
此时我们就可以考虑用\(c_i\)选择一些数去异或,看能否使得答案减小。
我们贪心的从高位开始操作。
如果当前位\(A,B\)都是\(1\),能操作就操作。
如果当前位都是\(0\),不需要任何操作。
如果当前位的较大数是\(1\),我们可以考虑操作。
#include <bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using ui32 = uint32_t;
using i64 = long long;
//#define int i64
using vi = vector<int>;
using pii = pair<int, int>;
#define lowbit(x) ( x & -x )
struct Basis {
vector<ui32> B;
Basis() {
B = vector<ui32>();
}
void insert(ui32 x) {
for (auto b: B)
x = min(x, b ^ x);
if (x == 0) return;
for (auto &b: B)
b = min(b, b ^ x);
B.push_back(x);
return;
}
ui32 find(ui32 x) {
for (auto i: B) {
int y = i;
while (lowbit(y) != y) y -= lowbit(y);
if (x == y) return i;
}
return 0;
}
};
void solve() {
int n;
cin >> n;
vector<ui32> a(n), b(n);
ui32 A = 0, B = 0;
for (auto &i: a) cin >> i, A ^= i;
for (auto &i: b) cin >> i, B ^= i;
ui32 res = max(A, B);
Basis basis;
for (int i = 0; i < n; i++)
basis.insert(a[i] ^ b[i]);
for (ui32 i = (1 << 30); i > 0; i >>= 1) {
if (A < B) swap(A, B);
if (((A & i) == 0) and ((B & i) == 0)) continue;
if (A & i) {
ui32 x = basis.find(i);
A ^= x, B ^= x;
}
}
res = min(res, max(A, B));
cout << res << "\n";
return;
}
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int T;
cin >> T;
while (T--)
solve();
return 0;
}
K - 取沙子游戏
这道题目是赛时先写出SG函数,然后找规律发现。
- \(n\)为奇数先手必胜,因为只要先手拿\(1\)后手就只能拿\(1\)
- \(k \ge n\)先手必胜,因为先手可以一步全部拿完。
- 剩下的情况通过找规律发现,对于\(n\)找到最大的\(t\)满足\(t | n\) 且\(t\)为 \(2\)的整次幂,那么\(t \ge k\) 先手必胜
原因赛时没想出来
#include <bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using i64 = long long;
using i128 = __int128;
#define int i64
using vi = vector<int>;
using pii = pair<int, int>;
const int inf = 1e9, INF = 1e18;
void solve() {
int n, k;
cin >> n >> k;
if (n & 1) {
cout << "Alice\n";
} else if (k >= n) {
cout << "Alice\n";
} else {
int p = 1;
while (n % (p * 2) == 0) p *= 2;
if (k < p) cout << "Bob\n";
else cout << "Alice\n";
}
}
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int T;
cin >> T;
while (T--)
solve();
return 0;
}
赛后看了题解,题解总结出来一条规律就是\(\mathrm{lowbit}(n) \le k\)先手必胜。
如果\(\mathrm{lowbit}(n) \le k\),先手可以取一个\(\mathrm {lowbit}(n)\),然后后手一定无法一步取完,这种情况其实和 Nim 博弈很类似。道理上来说我打表找到的规律实际上是殊途同归了
#include <bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using i64 = long long;
using i128 = __int128;
#define int i64
using vi = vector<int>;
using pii = pair<int, int>;
const int inf = 1e9, INF = 1e18;
#define lowbit(x) ( x & -x )
void solve() {
int n, k;
cin >> n >> k;
if (lowbit(n) > k) cout << "Bob\n";
else cout << "Alice\n";
return;
}
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int T;
cin >> T;
while (T--)
solve();
return 0;
}
L - 网络预选赛
#include <bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using i64 = long long;
using i128 = __int128;
#define int i64
using vi = vector<int>;
using pii = pair<int, int>;
const int inf = 1e9, INF = 1e18;
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<string> g(n);
for (auto &i: g) cin >> i;
int res = 0;
for (int i = 0; i + 1 < n; i++)
for (int j = 0; j + 1 < m; j++) {
if (g[i][j] == 'c' and g[i][j + 1] == 'c' and g[i + 1][j] == 'p' and g[i + 1][j + 1] == 'c')
res++;
}
cout << res;
return 0;
}