The 2024 ICPC Asia East Continent Online Contest (II)

The 2024 ICPC Asia East Continent Online Contest (II)(A、C、E、F、G、H、I、J、K、L)

A - Gambling on Choosing Regionals

最坏情况下强队和自己都去同一站,此时选取最小的赛站, $rank$ 越小。按照能力值排序即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>

using i64 = long long;

void solve()
{
int n, k;
std::cin >> n >> k;
int mn = n;
for (int i = 1; i <= k; i++) {
int v;
std::cin >> v;
mn = std::min(mn, v);
}

std::vector<std::pair<std::pair<int, std::string>, int>> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i].first.first >> a[i].first.second;
a[i].second = i + 1;
}
std::sort(a.rbegin(), a.rend());

std::map<std::string, int> mp;
std::vector<int> ans(n + 1);
int pre = 0;
for (int i = 0; i < n; i++) {
auto [f, id] = a[i];
ans[id] = pre;
if (mp[f.second] != mn) {
ans[id]++;
}
if (mp[f.second] != mn) {
mp[f.second]++;
pre++;
}
}
for (int i = 1; i <= n; i++) {
std::cout << ans[i] << "\n";
}
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);

int t = 1;
// std::cin >> t;
while (t--)
solve();

return 0;
}

C - Prefix of Suffixes

对于每次加入一个 $s_i$ ,我们只需要记录有哪些当前后缀 $s[j…i] = s[1…i - j + 1]$ 即后缀等于前缀,那么 $newans = lastans + a[i] * (\sum b[j])$ 。

这时我们讨论哪些后缀等于前缀。

对于新加入进来的 $s_i$ ,如果 $s_1=s_i$ ,那么这就是一个新的后缀等于前缀。我们将 $b_i$ 加入集合。

对于一个原来已经在集合中的 $b_j$ ,那么 一定有 $s[j…i-1] = s[1…i - j]$ 。此时如果 $s_{i-j+1} = s_i$ ,那么 $s[j…i] = s[1…i - j + 1]$ ,$b_j$ 依旧在集合中,反之则从集合中移除。

我们使用 $kmp$ ,得到 $border$ 数组,这时我们暴力的从 $x-1$ 开始向前跳,找出上述中不满足要求的位置,并将之从集合中去除。 但是这种做法在最差情况下时间复杂度为 $O(n^2)$ 。

我们注意到暴力跳会重复许多种情况如 $aaaaa$ ,我们可以知道跳到 $4, 3, 2, 1$ 时我们的下一个字符都是 $a$ ,一定合法,那么我们便不需要重复遍历。我们加上 $fail_x$ 表示通过 $border$ 一直向前跳到第一个位置 $y$ 且 $s_{x + 1} != s_{y + 1}$ ,起到一个类似于路径压缩的作用。这时我们每次向上通过 $fail$ 链跳一次,一定可以找到 $s_{i-j+1} != s_i$ 我们将之从 集合移除,由于每个位置只会进入一次集合,所以每个位置只会被移除一次,时间复杂度 $O(n)$ 。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <bits/stdc++.h>

using i64 = long long;

void solve()
{
int n;
std::cin >> n;
i64 ans = 0;
std::vector<i64> s(n + 1);
std::vector<i64> a(n + 1), b(n + 1);
std::cin >> s[1] >> a[1] >> b[1];
ans = a[1] * b[1];
std::cout << ans << "\n";
i64 sum = b[1];
std::vector<int> fail(n + 1);
std::vector<int> to(n + 1);
for (int i = 2, j = 0; i <= n; i++) {
std::cin >> s[i] >> a[i] >> b[i];
s[i] = (s[i] + ans) % n;
while (j && s[j + 1] != s[i])
j = fail[j];
if (s[j + 1] == s[i])
j++;
fail[i] = j;
if (s[i] == s[1]) {
sum += b[i];
}
if (s[fail[i - 1] + 1] == s[i]) {
to[i - 1] = to[fail[i - 1]];
} else {
to[i - 1] = fail[i - 1];
}
int pos = i - 1;
while (pos) {
if (s[pos + 1] != s[i]) {
int p = to[pos];
while (pos != p) {
sum -= b[i - pos];
pos = fail[pos];
}
} else {
pos = to[pos];
}
}
ans = ans + a[i] * sum;

std::cout << ans << "\n";
}
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);

int t = 1;
// std::cin >> t;
while (t--)
solve();

return 0;
}

E - Escape

如果可以在原地停留, $Sneaker$ 到达一个点要比所有的杀戮机器人早。

所以我们预处理出来杀戮机器人到达每个点的最早时间。

然而 $Sneaker$ 和杀戮机器人都不能停留在原地,所以 $Sneaker$ 和杀戮机器人要恰好同时到达一个点。注意到可以在相邻边反复横跳,让杀戮机器人到达一个点的时间 $+2$ 。所以我们处理出一个点奇数时间的最小值和偶数时间的最小值。这部分我们可以见一个点 $u$ 扩展为两个点 $u, u+n$ ,这样到达 $u + n$ 的最小时间就是到达点 $u$ 的奇数时间的最小值,到达 $u$ 的最小时间就是偶数时间的最小值。

最后从节点 $1$ $bfs$ 即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <bits/stdc++.h>

using i64 = long long;

constexpr int INF = 1E9;

void solve()
{
int n, m, d;
std::cin >> n >> m >> d;
std::vector<std::vector<int>> adj(2 * n + 1);
for (int i = 1; i <= m; i++) {
int u, v;
std::cin >> u >> v;
adj[u].push_back(v + n);
adj[u + n].push_back(v);
adj[v].push_back(u + n);
adj[v + n].push_back(u);
}

int k;
std::cin >> k;
std::vector<int> p(k);
for (int i = 0; i < k; i++) {
std::cin >> p[i];
}
std::vector<int> dist(2 * n + 1, INF);
std::queue<int> q;
std::vector<int> vis(2 * n + 1);
for (int i = 0; i < k; i++) {
dist[p[i]] = 0;
q.push(p[i]);
vis[p[i]] = 1;
}

while (q.size()) {
auto t = q.front();
q.pop();
for (auto v : adj[t]) {
if (!vis[v] && dist[t] < d) {
vis[v] = 1;
dist[v] = dist[t] + 1;
q.push(v);
}
}
}

std::vector<int> vis1(2 * n + 1), pre(2 * n + 1);
std::vector<int> dist1(2 * n + 1, INF);
q.push(1);
vis1[1] = 0;
dist1[1] = 0;
while (q.size()) {
auto t = q.front();
q.pop();
for (auto v : adj[t]) {
if (!vis1[v] && dist1[t] + 1 < dist[v]) {
vis1[v] = 1;
dist1[v] = dist1[t] + 1;
q.push(v);
pre[v] = t;
}
}
}

if (dist1[n] == INF && dist1[2 * n] == INF) {
std::cout << "-1\n";
} else {
int mn = 0, p = 0;
if (dist1[n] <= dist1[2 * n]) {
mn = dist1[n];
p = n;
} else {
mn = dist1[2 * n];
p = 2 * n;
}
std::cout << mn << "\n";
std::vector<int> path;
while (p != 1) {
path.push_back(p);
p = pre[p];
}
path.push_back(1);
for (int i = mn; i >= 0; i--) {
if (path[i] > n) {
path[i] -= n;
}
std::cout << path[i] << " \n"[i == 0];
}
}
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);

int t = 1;
std::cin >> t;
while (t--)
solve();

return 0;
}

F - Tourist

按照题意模拟即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <bits/stdc++.h>

using i64 = long long;

void solve()
{
int n;
std::cin >> n;
std::vector<int> a(n + 1);
i64 sum = 1500;
for (int i = 1; i <= n; i++) {
std::cin >> a[i];
}
for (int i = 1; i <= n; i++) {
sum += a[i];
if (sum >= 4000) {
std::cout << i << "\n";
return;
}
}
std::cout << "-1\n";
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);

int t = 1;
// std::cin >> t;
while (t--)
solve();

return 0;
}

G - Game

若 $V_{Alice} >= V_{Bob}$ ,则可以让 $Bob$ 先赢下 $0 - (V_{Alice} / V_{Bob} - 1)$ 次,此时 $Alice$ 赢一次 ,依然可以保证赢下比赛。

所以赢得概率为 $p_0 + p_0p_1 + …+ p_0p_1^{(V_{Alice} / V_{Bob} - 1)}$ ,可以使用等比数列求和。

若 $Bob$ 赢了 $V_{Alice} / V_{Bob}$ ,则 $V_{Alice} = V_{Alice} % V_{Bob}$ 。则 $V_{Alice} < V_{Bob}$。

若若 $V_{Alice} < V_{Bob}$ ,则 $Alice$ 必须先赢下 $V_{Bob} / V_{Alice}$ 次,此时 $V_{Bob} = V_{Bob} % V_{Alice}$ 赢一次 。则 $V_{Alice} >= V_{Bob}$ 。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <bits/stdc++.h>

using i64 = long long;

template <class T> constexpr T power(T a, i64 b)
{
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}

constexpr i64 mul(i64 a, i64 b, i64 p)
{
i64 res = a * b - i64(1.L * a * b / p) * p;
res %= p;
if (res < 0) {
res += p;
}
return res;
}
template <i64 P> struct MLong {
i64 x;
constexpr MLong() : x{}
{
}
constexpr MLong(i64 x) : x{norm(x % getMod())}
{
}

static i64 Mod;
constexpr static i64 getMod()
{
if (P > 0) {
return P;
} else {
return Mod;
}
}
constexpr static void setMod(i64 Mod_)
{
Mod = Mod_;
}
constexpr i64 norm(i64 x) const
{
if (x < 0) {
x += getMod();
}
if (x >= getMod()) {
x -= getMod();
}
return x;
}
constexpr i64 val() const
{
return x;
}
explicit constexpr operator i64() const
{
return x;
}
constexpr MLong operator-() const
{
MLong res;
res.x = norm(getMod() - x);
return res;
}
constexpr MLong inv() const
{
assert(x != 0);
return power(*this, getMod() - 2);
}
constexpr MLong &operator*=(MLong rhs) &
{
x = mul(x, rhs.x, getMod());
return *this;
}
constexpr MLong &operator+=(MLong rhs) &
{
x = norm(x + rhs.x);
return *this;
}
constexpr MLong &operator-=(MLong rhs) &
{
x = norm(x - rhs.x);
return *this;
}
constexpr MLong &operator/=(MLong rhs) &
{
return *this *= rhs.inv();
}
friend constexpr MLong operator*(MLong lhs, MLong rhs)
{
MLong res = lhs;
res *= rhs;
return res;
}
friend constexpr MLong operator+(MLong lhs, MLong rhs)
{
MLong res = lhs;
res += rhs;
return res;
}
friend constexpr MLong operator-(MLong lhs, MLong rhs)
{
MLong res = lhs;
res -= rhs;
return res;
}
friend constexpr MLong operator/(MLong lhs, MLong rhs)
{
MLong res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MLong &a)
{
i64 v;
is >> v;
a = MLong(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MLong &a)
{
return os << a.val();
}
friend constexpr bool operator==(MLong lhs, MLong rhs)
{
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MLong lhs, MLong rhs)
{
return lhs.val() != rhs.val();
}
};

template <> i64 MLong<0LL>::Mod = i64(1E18) + 9;

template <int P> struct MInt {
int x;
constexpr MInt() : x{}
{
}
constexpr MInt(i64 x) : x{norm(x % getMod())}
{
}

static int Mod;
constexpr static int getMod()
{
if (P > 0) {
return P;
} else {
return Mod;
}
}
constexpr static void setMod(int Mod_)
{
Mod = Mod_;
}
constexpr int norm(int x) const
{
if (x < 0) {
x += getMod();
}
if (x >= getMod()) {
x -= getMod();
}
return x;
}
constexpr int val() const
{
return x;
}
explicit constexpr operator int() const
{
return x;
}
constexpr MInt operator-() const
{
MInt res;
res.x = norm(getMod() - x);
return res;
}
constexpr MInt inv() const
{
assert(x != 0);
return power(*this, getMod() - 2);
}
constexpr MInt &operator*=(MInt rhs) &
{
x = 1LL * x * rhs.x % getMod();
return *this;
}
constexpr MInt &operator+=(MInt rhs) &
{
x = norm(x + rhs.x);
return *this;
}
constexpr MInt &operator-=(MInt rhs) &
{
x = norm(x - rhs.x);
return *this;
}
constexpr MInt &operator/=(MInt rhs) &
{
return *this *= rhs.inv();
}
friend constexpr MInt operator*(MInt lhs, MInt rhs)
{
MInt res = lhs;
res *= rhs;
return res;
}
friend constexpr MInt operator+(MInt lhs, MInt rhs)
{
MInt res = lhs;
res += rhs;
return res;
}
friend constexpr MInt operator-(MInt lhs, MInt rhs)
{
MInt res = lhs;
res -= rhs;
return res;
}
friend constexpr MInt operator/(MInt lhs, MInt rhs)
{
MInt res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MInt &a)
{
i64 v;
is >> v;
a = MInt(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a)
{
return os << a.val();
}
friend constexpr bool operator==(MInt lhs, MInt rhs)
{
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MInt lhs, MInt rhs)
{
return lhs.val() != rhs.val();
}
};

template <> int MInt<0>::Mod = 998244353;

template <int V, int P> constexpr MInt<P> CInv = MInt<P>(V).inv();

constexpr int P = 998244353;
using Z = MInt<P>;

template <typename T> T qpower(T a, i64 b, T init = T(), int mod = 0)
{
T res = init;
if (!mod) {
while (b > 0) {
if (b & 1) {
res = res * a;
}
a = a * a;
b >>= 1;
}
}
return res;
}

void solve()
{
int x, y;
std::cin >> x >> y;
Z p, q, s;
std::cin >> p >> q >> s;
s = p + q;
p = p / s;
q = q / s;
Z c = 1;
Z ans = 0;
while (x && y) {
if (x >= y) {
int cnt = x / y;
x %= y;
ans += c * (p - p * qpower<Z>(q, cnt, 1)) / (1 - q);
c *= qpower<Z>(q, cnt, 1);
} else {
int cnt = y / x;
y = y % x;
if (y == 0) {
cnt--;
y += x;
}
c *= qpower<Z>(p, cnt, 1);
}
}
std::cout << ans << "\n";
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);

int t = 1;
std::cin >> t;
while (t--)
solve();

return 0;
}

H - Points Selection

对于询问 $query(a, b, c)$ 为真,那么 $query(\ge a, \ge b, c)$ 一定为真。

从小到大枚举 $a$ 的值,再从小到大按照 $b$ 的值,依次加入每个点,维护 $f_c$ 表示当前加入点集合中最小的 $b$ 满足集合中纵坐标小于等于 $b$ 的点可以满足 $query(a, b, c)$ 为真。

当加入一个点 $(x, y, w)$ ,有更新:$f_{(c+ w)\ mod\ n} = min(f_{(c+ w)\ mod\ n},max(f_c, y))$ 。

所以当 $y \ge max(f)$ ,这个点就没有加入的必要。

由于数据随机。近似认为加入 $logn$ 个点后,可以填满整个 $f$ 数组。因此 $f$ 数组的最大值的期望值等于所有加入点的纵坐标的第 $O(logn)$ 小值。

所以加入第 $k$ 个点的纵坐标 $y < max(f)$ 的概率为 $O(logn/k)$ ,所以只需要更新 $O(\sum_{k = 1} ^{n}logn/k) = O(log^2n)$ 次 $f$ 数组。每次直接暴力更新 $f$ 数组即可。

对于每一个 $a$ ,我们对于数组 $f$ 求解,对于 $f_c$ 有 $b \ge f_c$ ,$query(a, b, c)$ 为真。所以其贡献为 $c\sum_{b =fc}^{n}$ 。每次更新数组是同时维护即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <bits/stdc++.h>

using i64 = long long;
using ui64 = unsigned long long;

void solve()
{
int n;
std::cin >> n;
std::vector<std::pair<std::pair<int, int>, int>> p;
for (int i = 1; i <= n; i++) {
int x, y, w;
std::cin >> x >> y >> w;
p.push_back({{x, y}, w});
}
std::sort(p.begin(), p.end());
std::vector<int> f(n, n + 1);
f[0] = 0;
int mx = n + 1;
ui64 sum = 0;

auto insert = [&](int id) -> void {
auto [point, w] = p[id];
auto [x, y] = point;
if (y >= mx) {
return;
}
std::vector<int> f1(n);
for (int i = 0; i < n; i++) {
int ni = (i + w) % n;
f1[ni] = std::min(f[ni], std::max(f[i], y));
}
std::swap(f, f1);
mx = 0;
sum = 0;
for (int i = 0; i < n; i++) {
mx = std::max(mx, f[i]);
}
for (int i = 1; i < n; i++) {
sum = sum + (ui64)(n + 1 - f[i]) * (n + f[i]) / 2 * i;
}
};

ui64 ans = 0;

for (int i = 1, j = 0; i <= n; i++) {
while (j < n && p[j].first.first == i) {
insert(j);
j++;
}
ans += sum * i;
}
std::cout << ans << "\n";
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);

int t = 1;
// std::cin >> t;
while (t--)
solve();

return 0;
}

I - Strange Binary

对于二进制最低位两位都是 $0$ ,则无解。

对于二进制一位为 $1$ ,如果他的高一位也是 $1$ ,那么答案中设为 $1$,如果高一位是 $0$ ,即原二进制中为 $01$,那么答案中则可以设置为上一位减去下一位,即 $1, -1$ ,同时注意到若更高一位也为 $0$ ,即 $001$ ,那么可以设置为 $1, -1, -1$ ,以此类推。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <bits/stdc++.h>

using i64 = long long;

void solve()
{
int n;
std::cin >> n;
if ((n & 1) == 0 && (n >> 1 & 1) == 0) {
std::cout << "NO\n";
return;
}

std::vector<int> ans(32);
for (int i = 31, cnt = 0; i >= 0; i--) {
if (n >> i & 1) {
if (cnt) {
ans[i] = -1;
} else {
ans[i] = 1;
}
cnt = 0;
} else {
cnt++;
if (cnt == 1) {
ans[i] = 1;
} else {
ans[i] = -1;
}
}
}
if ((n & 1) == 0) {
ans[0] = 0;
}
std::cout << "YES\n";
for (int i = 0; i < 32; i++) {
std::cout << ans[i] << " \n"[i % 8 == 7];
}
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);

int t = 1;
std::cin >> t;
while (t--)
solve();

return 0;
}

J - Stacking of Goods

若第 $i$ 个此时在第 $j$ 个上方,产生的贡献是 $v_i - c_i * W + v _j - c_j * (W + w_i)$ ,若第 $i$ 个在第 $j$ 个下方,产生的贡献为 $v_i-c_i * (W + w_j) + v_j-c_j*W$ 。此时贡献越小越好,化简得若 $w_i/c_i>w_j/c_j$ ,则第 $i$ 个在第 $j$ 个上方,反正在下方。排序即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <bits/stdc++.h>

using i64 = long long;

void solve()
{
int n;
std::cin >> n;
std::vector<std::pair<i64, i64>> a(n + 1);
i64 ans = 0;
for (int i = 1; i <= n; i++) {
i64 w, v, c;
std::cin >> w >> v >> c;
ans += v;
a[i] = {w, c};
}
auto f = [&](std::pair<i64, i64> x, std::pair<i64, i64> y) -> bool {
i64 res = x.first * y.second - x.second * y.first;
return res < 0;
};
std::sort(a.begin() + 1, a.end(), f);
i64 sum = 0;
for (int i = n; i > 0; i--) {
ans -= a[i].second * sum;
sum += a[i].first;
}
std::cout << ans << "\n";
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);

int t = 1;
// std::cin >> t;
while (t--)
solve();

return 0;
}

k - Match

对于形如 $a \bigoplus b >= k$ ,我们从高位向低位枚举。

$f(A, B, k)$ 表示 $A$ 集合和 $B$ 集合进行 $A_i \bigoplus B_j ≥ k$ 匹配的答案,返回一个数组,依次 表示 $0, 1, 2, 3,…, n$ 个匹配的方案数。

假设 $k$ 这一位的权值为 $0$,那么所有 $A_i$ , $B_j$ 只要当前位不同就可以进行匹配。 我们把 $A$ 按当前位为 $0$ 或 $1$ 分类成 $A_0$, $A_1$,$B$ 同理分成 $B_0$, $B_1$,先算出 $f(A_0, B_0, k)$, $f(A_1, B_1, k)$, 剩下的匹配在 $A_0$, $B_1$ 和 $B_1$, $A_0$ 中都可以任意匹配,可以组合数 DP 转移。转移如下:

我们令 $f(A_0, B_0, k) = f_0$ ,$f(A_1, B_1, k) = f_1$ ,那么 $f_0[i]$ 表示从 $A_0, B_0$ 里面分别取出 $i$ 个且匹配的合法方案数。$f_1[j]$ 同理。我们设从 $f_0$ 里面取 $i$ 个,$f_1$ 里面取 $j$ 个,同时我们从剩下的 $A_0$ 里面取出 $u$ 个,剩下的 $B_1$ 里面取出 $u$ 个并两两任意匹配,从剩下的 $A_1$ 里面取出 $v$ 个,剩下的 $B_0$ 里面取出 $v$ 个并两两任意匹配。那么匹配方案有:

$dp[i+j+u+v] = f_0[i] * f_1[j] * C(cnta_0 - i, u) * C(cntb_1-j, u) * u!* C(cnta_1 - j, v) * C(cntb_0 - i, v) * v!$ 。

直接四重枚举即可,我们可以发现这类似于树上背包。复杂度 $O(n^4)$ 。

假设 $k$ 这一位的权值为 $1$,那么只有 $f(A_1, B_0, k)$ 和 $f(A_0, B_1, k)$ 有解,将二者最高位抹去继续递归即可。

时间复杂度为 $O(n^4 )$ 。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include <bits/stdc++.h>

using i64 = long long;

template <class T> constexpr T power(T a, i64 b)
{
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}

constexpr i64 mul(i64 a, i64 b, i64 p)
{
i64 res = a * b - i64(1.L * a * b / p) * p;
res %= p;
if (res < 0) {
res += p;
}
return res;
}
template <i64 P> struct MLong {
i64 x;
constexpr MLong() : x{}
{
}
constexpr MLong(i64 x) : x{norm(x % getMod())}
{
}

static i64 Mod;
constexpr static i64 getMod()
{
if (P > 0) {
return P;
} else {
return Mod;
}
}
constexpr static void setMod(i64 Mod_)
{
Mod = Mod_;
}
constexpr i64 norm(i64 x) const
{
if (x < 0) {
x += getMod();
}
if (x >= getMod()) {
x -= getMod();
}
return x;
}
constexpr i64 val() const
{
return x;
}
explicit constexpr operator i64() const
{
return x;
}
constexpr MLong operator-() const
{
MLong res;
res.x = norm(getMod() - x);
return res;
}
constexpr MLong inv() const
{
assert(x != 0);
return power(*this, getMod() - 2);
}
constexpr MLong &operator*=(MLong rhs) &
{
x = mul(x, rhs.x, getMod());
return *this;
}
constexpr MLong &operator+=(MLong rhs) &
{
x = norm(x + rhs.x);
return *this;
}
constexpr MLong &operator-=(MLong rhs) &
{
x = norm(x - rhs.x);
return *this;
}
constexpr MLong &operator/=(MLong rhs) &
{
return *this *= rhs.inv();
}
friend constexpr MLong operator*(MLong lhs, MLong rhs)
{
MLong res = lhs;
res *= rhs;
return res;
}
friend constexpr MLong operator+(MLong lhs, MLong rhs)
{
MLong res = lhs;
res += rhs;
return res;
}
friend constexpr MLong operator-(MLong lhs, MLong rhs)
{
MLong res = lhs;
res -= rhs;
return res;
}
friend constexpr MLong operator/(MLong lhs, MLong rhs)
{
MLong res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MLong &a)
{
i64 v;
is >> v;
a = MLong(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MLong &a)
{
return os << a.val();
}
friend constexpr bool operator==(MLong lhs, MLong rhs)
{
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MLong lhs, MLong rhs)
{
return lhs.val() != rhs.val();
}
};

template <> i64 MLong<0LL>::Mod = i64(1E18) + 9;

template <int P> struct MInt {
int x;
constexpr MInt() : x{}
{
}
constexpr MInt(i64 x) : x{norm(x % getMod())}
{
}

static int Mod;
constexpr static int getMod()
{
if (P > 0) {
return P;
} else {
return Mod;
}
}
constexpr static void setMod(int Mod_)
{
Mod = Mod_;
}
constexpr int norm(int x) const
{
if (x < 0) {
x += getMod();
}
if (x >= getMod()) {
x -= getMod();
}
return x;
}
constexpr int val() const
{
return x;
}
explicit constexpr operator int() const
{
return x;
}
constexpr MInt operator-() const
{
MInt res;
res.x = norm(getMod() - x);
return res;
}
constexpr MInt inv() const
{
assert(x != 0);
return power(*this, getMod() - 2);
}
constexpr MInt &operator*=(MInt rhs) &
{
x = 1LL * x * rhs.x % getMod();
return *this;
}
constexpr MInt &operator+=(MInt rhs) &
{
x = norm(x + rhs.x);
return *this;
}
constexpr MInt &operator-=(MInt rhs) &
{
x = norm(x - rhs.x);
return *this;
}
constexpr MInt &operator/=(MInt rhs) &
{
return *this *= rhs.inv();
}
friend constexpr MInt operator*(MInt lhs, MInt rhs)
{
MInt res = lhs;
res *= rhs;
return res;
}
friend constexpr MInt operator+(MInt lhs, MInt rhs)
{
MInt res = lhs;
res += rhs;
return res;
}
friend constexpr MInt operator-(MInt lhs, MInt rhs)
{
MInt res = lhs;
res -= rhs;
return res;
}
friend constexpr MInt operator/(MInt lhs, MInt rhs)
{
MInt res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MInt &a)
{
i64 v;
is >> v;
a = MInt(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a)
{
return os << a.val();
}
friend constexpr bool operator==(MInt lhs, MInt rhs)
{
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MInt lhs, MInt rhs)
{
return lhs.val() != rhs.val();
}
};

template <> int MInt<0>::Mod = 998244353;

template <int V, int P> constexpr MInt<P> CInv = MInt<P>(V).inv();

constexpr int P = 998244353;
using Z = MInt<P>;

struct Comb {
int n, mod;
std::vector<int> f, inf;
Comb(int n, int mod) : f(n + 1), inf(n + 1)
{
this->n = n;
this->mod = mod;
init();
}

int qmi(int a, int k, int p)
{
int res = 1;
while (k) {
if (k & 1)
res = (i64)res * a % p;
a = (i64)a * a % p;
k = k >> 1;
}
return res;
}

void init()
{
f[0] = 1;
inf[0] = 1;
for (int i = 1; i <= n; i++)
f[i] = (i64)f[i - 1] * i % mod;
inf[n] = qmi(f[n], mod - 2, mod);
for (int i = n - 1; i; i--)
inf[i] = (i64)inf[i + 1] * (i + 1) % mod;
}

i64 C(int a1, int b1)
{
if (a1 < 0 || b1 < 0 || a1 < b1)
return 0;
return ((i64)f[a1] * inf[b1] % mod) * inf[a1 - b1] % mod;
}
};

void solve()
{
int n;
i64 k;
std::cin >> n >> k;
Comb comb(n, P);
std::vector<i64> a(n), b(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
for (int i = 0; i < n; i++) {
std::cin >> b[i];
}

auto get = [&](int x, int y) -> std::vector<Z> {
std::vector<Z> res(n + 1);
for (int i = 0; i <= std::min(x, y); i++) {
res[i] = res[i] + (Z)comb.C(x, i) * comb.C(y, i) * comb.f[i];
}
return res;
};

auto mul = [&](std::vector<Z> &a, std::vector<Z> &b) -> std::vector<Z> {
std::vector<Z> res(n + 1);
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n - i; j++) {
res[i + j] = res[i + j] + a[i] * b[j];
}
}
return res;
};

auto calc = [&](auto self, int p, std::vector<i64> &a, std::vector<i64> &b) -> std::vector<Z> {
std::vector<Z> res(n + 1);
res[0] = 1;
if (a.size() == 0 || b.size() == 0) {
return res;
}
std::vector<i64> a0, a1, b0, b1;
for (auto v : a) {
if (v >> p & 1) {
a1.push_back(v);
} else {
a0.push_back(v);
}
}
for (auto v : b) {
if (v >> p & 1) {
b1.push_back(v);
} else {
b0.push_back(v);
}
}

int ca0 = a0.size(), ca1 = a1.size();
int cb0 = b0.size(), cb1 = b1.size();
if (k >> p & 1) {
std::vector<Z> res1, res2;
if (p > 0) {
res1 = self(self, p - 1, a0, b1);
res2 = self(self, p - 1, a1, b0);
} else {
res1 = get(a0.size(), b1.size());
res2 = get(a1.size(), b0.size());
}
res = mul(res1, res2);
return res;
} else {
std::vector<Z> res1, res2;
if (p > 0) {
res1 = self(self, p - 1, a0, b0);
res2 = self(self, p - 1, a1, b1);
} else {
res1 = get(a0.size(), b0.size());
res2 = get(a1.size(), b1.size());
}
res[0] = 0;
for (int i = 0; i <= ca0; i++) {
for (int j = 0; j <= ca1; j++) {
int sz1 = std::min(ca0 - i, cb1 - j);
int sz2 = std::min(ca1 - j, cb0 - i);
for (int u = 0; u <= sz1; u++) {
for (int v = 0; v <= sz2; v++) {
res[i + j + u + v] = res[i + j + u + v] + res1[i] * res2[j] * comb.C(ca0 - i, u) * comb.C(cb1 - j, u) * comb.f[u] * comb.C(ca1 - j, v) * comb.C(cb0 - i, v) * comb.f[v];
}
}
}
}
return res;
}
};

std::vector<Z> ans = calc(calc, 59, a, b);

for (int i = 1; i <= n; i++) {
std::cout << ans[i] << "\n";
}
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);

int t = 1;
// std::cin >> t;
while (t--)
solve();

return 0;
}

L - 502 Bad Gateway

最优策略一定是选择一个 $c\in[1,t]$ ,不停重置,直至重置到一个小于等于 $c$ 的数就直接减到 $0$ 。

设重置到小于等于 $c$ 的数的概率为 $p = c/t$ ,由几何分布可知重置到小于等于 $c$ 的期望步数为:$E = p + 2p(1-p) + 3p(1-p)^2 + …. = 1/p$ 。

到达小于等于 $c$ 后会先减去 $1$ ,之后就是每步减去 $1$ ,这部分的期望步数:$E = (c-1)/2$ 。

所以总期望步数为:$(c-1)/2 + t/c$ 。这是一个对勾函数,在 $sqrt(2t)$ 出取最小值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <bits/stdc++.h>

using i64 = long long;

void solve()
{
i64 n;
std::cin >> n;
i64 c = sqrt(2 * n);
i64 p1 = c * c - c + 2 * n, q1 = 2 * c;
i64 d = std::gcd(p1, q1);
p1 /= d, q1 /= d;
c++;
i64 p2 = c * c - c + 2 * n, q2 = 2 * c;
d = std::gcd(p2, q2);
p2 /= d, q2 /= d;
if (p1 * q2 <= p2 * q1) {
std::cout << p1 << " " << q1 << "\n";
} else {
std::cout << p2 << " " << q2 << "\n";
}
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);

int t = 1;
std::cin >> t;
while (t--)
solve();

return 0;
}

The 2024 ICPC Asia East Continent Online Contest (II)
http://example.com/2024/09/26/The-2024-ICPC-Asia-East-Continent-Online-Contest-II/
作者
muxingchengfeng
发布于
2024年9月26日
许可协议