The 2023 ICPC Asia Jinan Regional Contest

The 2023 ICPC Asia Jinan Regional Contest(A、B、D、E、G、H、I、K)

A - Many Many Heads

维护一个栈,顺序遍历序列种每一个括号,如果此时栈非空,且栈顶括号种类等于该括号,则弹出栈顶,否则将该括号压入栈中。这样我们可以得到一个合法序列,并且我们注意到,每一个右括号都放在了可以放到的最左边的位置。

对于形如 $()(),() $ 这样的序列,一定可以形成另一种序列 $ (()),(([]))$ 。即如果有两个相同的括号对,那么一定可以形成多种合法序列。

因此我们对刚才形成的合法序列遍历一次,每次如果当前括号为右括号且栈顶不是对应的左括号,那么可以知道栈顶一定存在合法括号对,我们一直弹出括号对直至栈顶为对应左括号,此时检查弹出段是否有能够形成另一种序列。

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
#include <bits/stdc++.h>

using i64 = long long;

void solve()
{
int n;
std::string s;
std::cin >> s;
n = s.size();
int s1 = 0, s2 = 0;
std::string ss = "";
std::stack<int> st;
for (int i = 0; i < n; i++) {
if (s[i] == ']' || s[i] == '[') {
if (st.empty() || st.top() == 2) {
ss += '[';
st.push(1);
} else {
ss += ']';
st.pop();
}
} else {
if (st.empty() || st.top() == 1) {
ss += '(';
st.push(2);
} else {
ss += ')';
st.pop();
}
}
}

for (int i = 0; i < n; i++) {
if (ss[i] == '(' || ss[i] == '[') {
if (ss[i] == '(') {
st.push(1);
} else {
st.push(2);
}
} else {
if (!st.empty() && (st.top() == 3 || st.top() == 4)) {
std::vector<int> c(3);
while (!st.empty() && st.top() != 1 && st.top() != 2) {
st.pop();
int t = st.top();
c[t]++;
st.pop();
}
if (c[1] >= 2 || c[2] >= 2) {
std::cout << "No\n";
return;
}
}
if (ss[i] == ')') {
st.push(3);
} else {
st.push(4);
}
}
}

std::vector<int> c(3);
while (!st.empty()) {
st.pop();
int t = st.top();
c[t]++;
st.pop();
}
if (c[1] >= 2 || c[2] >= 2) {
std::cout << "No\n";
return;
}

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

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

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

return 0;
}

B - Graph Partitioning 2

对于 $k \leq \sqrt{n}$ ,直接进行树上背包,状态设为两维,第一维 $i$ 表示当前节点,第二维 $j$ 表示包含当前节点未被切开的大小,转移为 $dp[u][i + j] = dp[u][i + j] + dp[u][i] * dp[v][j]$ 。时间复杂度为 $O(n\sqrt{n})$ 。

对于 $k > \sqrt{n}$ ,我们可以知道最后切分的连通块数小于 $\sqrt{n}$ ,同样进行树上背包,第二维记录以及切出去的块大小为 $k$ 的块数。对于当前节点,是否作为一块大小为 $k$ 或 $k + 1$ 的连通块切分进行讨论。

我们知道这个节点只能被切出去一次,我们设定两个状态 $dp[i][j][0]$ 表示没有切,$dp[i][j][1]$ 表示已经切分。

我们在子节点有剩余,且子节点和父节点的剩余恰好为 $k$ 或 $k + 1$ 时进行切分从而维护只切分一次。

  • 若切分大小为 $k + 1$ , 此时有 $dp[u][i + j][1] = dp[u][i+j][1] + dp[u][i][0] * dp[v][i][0]$
  • 若切分大小为 $k$ , 此时有 $dp[u][i + j + 1][1] = dp[u][i+j][1] + dp[u][i][0] * dp[v][i][0]$

同时注意到若子节点没有剩余,则依然符合当前节点只切分一次,所以有:

  • $dp[u][i + j][1] = dp[u][i + j][1] + dp[u][i][1] * dp[v][j][0]$ 。

时间复杂度为 $O(n\sqrt{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
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
#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>;

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

if (sqrt(n) * 2 >= k) {
std::vector<int> sz(n + 1);
std::vector<std::vector<Z>> dp(n + 1, std::vector<Z>(k + 2));
auto dfs = [&](auto self, int u, int fa) -> void {
sz[u] = 1;
dp[u][1] = 1;
for (auto v : adj[u]) {
if (v == fa) {
continue;
}
self(self, v, u);
for (int i = std::min(k + 1, sz[u]); i; i--) {
if (dp[u][i] != 0) {
Z p = dp[u][i];
dp[u][i] = 0;
for (int j = 0; j + i <= k + 1; j++) {
dp[u][i + j] = dp[u][i + j] + p * dp[v][j];
}
}
}
sz[u] += sz[v];
}
dp[u][0] = dp[u][k] + dp[u][k + 1];
};
dfs(dfs, 1, 0);
std::cout << dp[1][0] << "\n";
} else {
int m = n / k;
std::vector<int> sz(n + 1);
std::vector<std::vector<std::vector<Z>>> dp(n + 1, std::vector<std::vector<Z>>(m + 1, std::vector<Z>(2)));
auto dfs = [&](auto self, int u, int fa) -> void {
sz[u] = 1;
dp[u][0][0] = 1;
if (k == 1) {
dp[u][1][1] = 1;
}
for (auto v : adj[u]) {
if (v == fa) {
continue;
}
self(self, v, u);
for (int i = sz[u] / k; i >= 0; i--) {
if (dp[u][i][0] != 0 || dp[u][i][1] != 0) {
Z p = dp[u][i][0], q = dp[u][i][1];
dp[u][i][0] = 0;
dp[u][i][1] = 0;
for (int j = 0; j <= sz[v] / k; j++) {
int s = ((sz[u] - i * k) % (k + 1)) + ((sz[v] - j * k) % (k + 1));
if (s > k + 1) {
continue;
}
if (s == k + 1) {
dp[u][i + j][1] = dp[u][i + j][1] + p * dp[v][j][0];
} else if (s == k) {
if (((sz[v] - j * k) % (k + 1)) != 0) {
dp[u][i + j + 1][1] = dp[u][i + j + 1][1] + p * dp[v][j][0];
}
dp[u][i + j][0] = dp[u][i + j][0] + p * dp[v][j][0];
} else {
dp[u][i + j][0] = dp[u][i + j][0] + p * dp[v][j][0];
}
if (((sz[v] - j * k) % (k + 1)) == 0) {
dp[u][i + j][1] = dp[u][i + j][1] + q * dp[v][j][0];
}
}
}
}
sz[u] += sz[v];
}
for (int i = 0; i <= sz[u] / k; i++) {
dp[u][i][0] += dp[u][i][1];
}
};
dfs(dfs, 1, 0);
Z ans = 0;
for (int i = 0; i <= m; i++) {
if ((n - i * k) % (k + 1) == 0) {
ans += dp[1][i][0];
}
}
std::cout << ans << "\n";
}
}

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

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

return 0;
}

D - Largest Digit

$a + b$ 的值域区间为 $[l_a + l_b,r_a+r_b]$ 。所以当区间长度大于等于 $10$ ,个位数一定会出现 $9$ ,区间小于 $10$ ,直接暴力枚举。

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 l1, r1, l2, r2;
std::cin >> l1 >> r1 >> l2 >> r2;
if (r1 - l1 + 1 + r2 - l2 >= 10) {
std::cout << 9 << "\n";
} else {
int ans = 0;
for (int i = l1 + l2; i <= r1 + r2; i++) {
int p = i;
while (p) {
ans = std::max(ans, p % 10);
p /= 10;
}
}
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 - I Just Want… One More…

建源 $S$ 向二分图左侧每个点连流量为 1 的边,汇 $T$ 从二分 图右侧每个点连流量为 1 的边,原图的匹配数就是新图的最大流。

先求出新图最大流以及对应的残余网络,添加一条边能够提高匹配数当且仅当新的残余网络上源 $S$ 和汇 $T$ 联通。因此 求出源 S 能到达左侧多少点,右侧多少点能到达汇 T,相乘即为答案。

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
#include <bits/stdc++.h>

using i64 = long long;

constexpr int maxn = 2E5 + 10, maxm = 6E5 + 10;

template <typename cost_t> struct MaxFlow {

const cost_t INF = std::numeric_limits<cost_t>::max() / 2;
int h[maxn], e[maxm], ne[maxm], idx;
cost_t f[maxm], incf[maxn];
int q[maxn], cur[maxn];
int V, S, T;

MaxFlow(int v, int s, int t)
{
for (int i = 0; i <= v; i++)
h[i] = -1;
idx = 0;
V = v, S = s, T = t;
}

void add(int a, int b, cost_t c)
{
e[idx] = b, f[idx] = c, ne[idx] = h[a], h[a] = idx++;
e[idx] = a, f[idx] = 0, ne[idx] = h[b], h[b] = idx++;
}

bool bfs()
{
int hh = 0, tt = 0;
for (int i = 0; i <= V; i++) {
incf[i] = -1;
}
q[0] = S, incf[S] = 0, cur[S] = h[S]; //
while (hh <= tt) {
int t = q[hh++];
for (int i = h[t]; ~i; i = ne[i]) {
int ver = e[i];
if (incf[ver] == -1 && f[i]) {
incf[ver] = incf[t] + 1; // 分层
cur[ver] = h[ver]; // 弧优化
if (ver == T) {
return true;
}
q[++tt] = ver;
}
}
}
return false;
}

cost_t find(int u, cost_t limit)
{
if (u == T) {
return limit;
}
cost_t flow = 0;
for (int i = cur[u]; ~i && flow < limit; i = ne[i]) {
cur[u] = i;
int ver = e[i];
if (incf[ver] == incf[u] + 1 && f[i]) {
cost_t t = find(ver, std::min(f[i], limit - flow));
if (!t) {
incf[ver] = -1;
}
f[i] -= t, f[i ^ 1] += t, flow += t; // 更新网络
}
}
return flow;
}

cost_t dinic()
{
cost_t res = 0, flow = 0;
while (bfs()) {
while (flow = find(S, INF)) {
res += flow;
}
}
return res;
}
};

void solve()
{
int n, m;
std::cin >> n >> m;
int S = 0, T = 2 * n + 1;
MaxFlow<int> flow(T, S, T);
std::set<std::pair<int, int>> s;
for (int i = 1; i <= m; i++) {
int u, v;
std::cin >> u >> v;
if (!s.count({u, v})) {
s.insert({u, v});
flow.add(u, v + n, 1);
}
}
for (int i = 1; i <= n; i++) {
flow.add(S, i, 1);
}
for (int i = 1; i <= n; i++) {
flow.add(i + n, T, 1);
}
flow.dinic();

i64 s1 = 0, s2 = 0;
std::queue<int> q, qq;
q.push(S);
std::vector<int> vis(2 * n + 2);
vis[S] = 1;
while (q.size()) {
auto t = q.front();
q.pop();
if (t >= 1 && t <= n) {
s1++;
}
for (int i = flow.h[t]; ~i; i = flow.ne[i]) {
int v = flow.e[i], w = flow.f[i];
if (w == 0) {
continue;
}
if (vis[v]) {
continue;
}
vis[v] = 1;
q.push(v);
}
}

qq.push(T);
vis.assign(2 * n + 2, 0);
vis[T] = 1;
while (qq.size()) {
auto t = qq.front();
qq.pop();
if (t >= n + 1 && t <= 2 * n) {
s2++;
}
for (int i = flow.h[t]; ~i; i = flow.ne[i]) {
int v = flow.e[i], w = flow.f[i];
if (w == 1) {
continue;
}
if (vis[v]) {
continue;
}
vis[v] = 1;
qq.push(v);
}
}
std::cout << s1 * s2 << "\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 - Gifts from Knowledge

对于第 $j$ 列和第 $m-j + 1$ 列总共 $1$ 的数量超过两个,无解。

假设存在两个 $1$ ,一个位于第 $i$ 行,一个位于第 $j$ 行,则有两种情况。

  • 位于同一列,则 $i,j$ 的选择情况相反
  • 位于不同列,则 $i,j$ 的选择情况相同

介绍两种建图方式:

  • 对于一张 $2*n$ 个点的图,其中点 $1\leq i\leq n$ ,表示不选了第 $i$ 行,点 $i + n$ 表示不选第 $i$ 行。若 $i,j$ 选择情况相同,则连边 $(i,j )\ (i + n, j + n)$ ,相反则连边 $(i,j + n )\ (i + n, j)$ 。若出现 $i, i + n$ 在同一个连通块,则无解,最终形成 $c$ 块连通块。方案数为 $2^{c/2}$ 。使用并查集维护连通块。
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
#include <bits/stdc++.h>

using i64 = long long;

struct DSU {
std::vector<int> p, siz;
DSU(int n) : p(n + 1), siz(n + 1, 1)
{
std::iota(p.begin(), p.end(), 0);
}

int find(int x)
{
return p[x] == x ? x : p[x] = find(p[x]);
}

bool same(int x, int y)
{
return find(x) == find(y);
}

bool merge(int x, int y)
{
x = find(x), y = find(y);
if (x == y)
return false;
siz[x] += siz[y];
p[y] = x;
return true;
}
int size(int x)
{
return siz[find(x)];
}
};

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;
}
} else {
while (b > 0) {
if (b & 1) {
res = res * a % mod;
}
a = a * a % mod;
b >>= 1;
}
}
return res;
}

void solve()
{
int n, m;
std::cin >> n >> m;
std::vector<std::string> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
DSU dsu(2 * n);
std::vector<std::vector<int>> b(m);
for (int j = 0; j < (m + 1) / 2; j++) {
for (int i = 0; i < n; i++) {
if (a[i][j] == '1') {
b[j].push_back(i);
}
if (a[i][m - 1 - j] == '1') {
b[j].push_back(i);
}
}
if (b[j].size() > 2) {
std::cout << "0\n";
return;
}
if (b[j].size() == 2) {
if (b[j][0] == b[j][1]) {
continue;
}
int x = b[j][0] + 1, y = b[j][1] + 1;
if (a[b[j][0]][j] == a[b[j][1]][j]) {
dsu.merge(x, y + n);
dsu.merge(x + n, y);
} else {
dsu.merge(x, y);
dsu.merge(x + n, y + n);
}
}
}
for (int i = 1; i <= n; i++) {
if (dsu.same(i, i + n)) {
std::cout << 0 << "\n";
return;
}
}

std::set<int> s;
for (int i = 1; i <= 2 * n; i++) {
s.insert(dsu.find(i));
}

int ans = qpower<i64>(2, s.size() / 2, 1, 1E9 + 7);
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;
}
  • 对于一张 $n$ 个点的图。若 $i,j$ 选择情况相同,则连边 $(i,j,0)$ ,相反则连边 $(i,j,1)$ 其中 $0,1$ 表示边权。设每个点点权为 $v$ ,则若出现 $vi$ 异或 $vj$ 不等于路径上边权的异或和,则无解。最终形成 $c$ 块连通块。方案数为 $2^{c}$ 。使用并查集维护连通块。
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
#include <bits/stdc++.h>

using i64 = long long;

constexpr int mod = 1E9 + 7;

void solve()
{
int n, m;
std::cin >> n >> m;
std::vector<std::string> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
std::vector<std::vector<int>> b(m);
std::vector<std::vector<std::pair<int, int>>> adj(n + 1);
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
if (a[i][j] == '1') {
b[j].push_back(i);
}
if (a[i][m - 1 - j] == '1') {
b[j].push_back(i);
}
}
if (b[j].size() > 2) {
std::cout << "0\n";
return;
}
if (b[j].size() == 2) {
if (b[j][0] == b[j][1]) {
continue;
} else {
int x = b[j][0], y = b[j][1];
if (a[x][j] == a[y][j]) {
adj[x + 1].push_back({y + 1, 1});
adj[y + 1].push_back({x + 1, 1});
} else {
adj[x + 1].push_back({y + 1, 0});
adj[y + 1].push_back({x + 1, 0});
}
}
}
}
int flag = 1;
int ans = 1;
std::vector<int> vis(n + 1), c(n + 1, -1);
auto dfs = [&](auto self, int u, int fa, int val) -> void {
vis[u] = 1;
c[u] = val;
for (auto [v, w] : adj[u]) {
if (vis[v]) {
if (c[u] ^ c[v] != w) {
flag = 0;
}
continue;
}
self(self, v, u, val ^ w);
}
};

for (int i = 1; i <= n; i++) {
if (!vis[i]) {
ans = ans * 2 % mod;
dfs(dfs, i, 0, 0);
}
}
if (!flag) {
ans = 0;
}

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 - Basic Substring Structure

令 $g(i) = lcp(s, suf(s,i))$ ,我们算出每个 $g(i)$ 对于每个位置修改会出现的贡献。

假设设修改的位置为 $t$

  • 若 $1 \leq t \leq g(i)$ 或者 $i \leq t \leq i + g(i)$ ,此时对于其贡献为从 $0$ 开始的等差数列。例如 $g(i)$ 为 $5$ ,则对 $i$ 的贡献为 $0$ ,对 $i + 1$ 的贡献为 $1$ ,对 $i + 2$ 的贡献为 $2$ 等。同理对 $1$ 的贡献为 $0$ ,对 $2$ 的贡献为 $1$ 等等。
  • 若修改位置恰好是 $g(i) + 1$ 或者 $i + g(i) + 1$ ,二分出修改为特定数字后 $lcp(s, suf(s, i))$ 。
  • 其余情况,对其位置的贡献都是 $g(i)$ 。

等差数列可以使用差分维护。

时间复杂度 $O(nlogn)$ 。

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
#include <bits/stdc++.h>

using i64 = long long;

std::vector<int> zFunction(const std::vector<int> &s)
{
int n = s.size();
std::vector<int> z(n + 1);
z[0] = n;
for (int i = 1, j = 1; i < n; i++) {
z[i] = std::max(0, std::min(j + z[j] - i, z[i - j]));
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) {
z[i]++;
}
if (i + z[i] > j + z[j]) {
j = i;
}
}
return z;
}

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 = 1E9 + 7;

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

constexpr int P = 1E9 + 7;
using Z = MInt<P>;

constexpr Z p = 998244353;

void solve()
{
int n;
std::cin >> n;
std::vector<int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
auto z = zFunction(a);
std::vector<Z> pp(n + 1), s(n + 1);
pp[0] = 1;
for (int i = 1; i <= n; i++) {
pp[i] = pp[i - 1] * p;
s[i] = s[i - 1] * p + a[i - 1];
}

auto get = [&](int l, int r) -> Z {
Z res = (s[r] - s[l - 1] * pp[r - l + 1]);
return res;
};

std::vector<i64> cnt(n + 3);
for (int i = 2; i <= n; i++) {
int r = std::min(z[i - 1], i - 1);
cnt[2] += 1;
cnt[r + 1] -= r;
cnt[r + 2] -= 1 - r;
cnt[i + 1] += 1;
cnt[i + z[i - 1]] -= z[i - 1];
cnt[i + z[i - 1] + 1] -= 1 - z[i - 1];
}

for (int i = 1; i <= n; i++) {
cnt[i] += cnt[i - 1];
}

for (int i = 1; i <= n; i++) {
if (z[i - 1] + 1 <= i - 1) {
cnt[z[i - 1] + 1] += z[i - 1];
cnt[i] -= z[i - 1];
}
if (i + z[i - 1] <= n) {
cnt[i + z[i - 1]] += z[i - 1];
}
}
for (int i = 1; i <= n; i++) {
cnt[i] += cnt[i - 1];
}

std::vector<std::map<int, i64>> sum(n + 1);
for (int i = 1; i <= n; i++) {
int ne = i + z[i - 1];
if (ne == n + 1) {
continue;
}
if (z[i - 1] + 1 < i) {
int l = 0, r = n - ne;
while (l < r) {
int mid = l + r + 1 >> 1;
if (get(z[i - 1] + 1 + 1, z[i - 1] + 1 + mid) == get(ne + 1, ne + mid)) {
l = mid;
} else {
r = mid - 1;
}
}
sum[z[i - 1] + 1][a[ne - 1]] += 1 + l;
}

{
int l = 0, r = std::min(n - ne, i - 2);
while (l < r) {
int mid = l + r + 1 >> 1;
if (get(z[i - 1] + 1 + 1, z[i - 1] + 1 + mid) == get(ne + 1, ne + mid)) {
l = mid;
} else {
r = mid - 1;
}
}
if (l == i - 2 && ne + i - 1 <= n && a[z[i - 1]] == a[ne + i - 1 - 1]) {
l++;
int ll = 0, rr = n - (ne + i - 1);
while (ll < rr) {
int mid = ll + rr + 1 >> 1;
if (get(z[i - 1] + 1 + i - 1 + 1, z[i - 1] + 1 + i - 1 + mid) == get(ne + i - 1 + 1, ne + i - 1 + mid)) {
ll = mid;
} else {
rr = mid - 1;
}
}
l += ll;
}
sum[ne][a[z[i - 1]]] += l + 1;
}
}

i64 ans = 0;
for (int i = 1; i <= n; i++) {
i64 mx = 0;
for (auto [x, y] : sum[i]) {
mx = std::max(mx, y);
}
ans += (cnt[i] + n + mx) ^ 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 Sorting

从左向右,若这个位置上的数不等于下标,设为 $a_i$ ,我们向右找到比这个数小的数且下标最大的数 $a_j$ ,排序后,可知 $i, i+ 1$ ,位置上一定为 $i, i + 1$ ,每次操作一定能排序好两个数,$n/2$ 次一定可以排序完整个序列。

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
#include <bits/stdc++.h>

using i64 = long long;

void solve()
{
int n;
std::cin >> n;
std::vector<int> a(n + 1), o(n + 1);
for (int i = 1; i <= n; i++) {
std::cin >> a[i];
o[a[i]] = i;
}
std::vector<std::pair<int, int>> ans;

for (int i = 1, lo = 1, r = 1; i <= n; i++) {
r = std::max(r, i);
int mx = 0;
for (int j = lo; j < a[i]; j++) {
if (o[j] > r) {
mx = std::max(mx, o[j]);
}
}
if (mx == 0) {
continue;
}
r = mx;
ans.push_back({i, mx});
std::sort(a.begin() + i, a.begin() + mx + 1);
lo = a[i] + 1;
}
std::cout << ans.size() << "\n";
for (auto [l, r] : ans) {
std::cout << l << " " << r << "\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 - Rainbow Subarray

双指针,得到以第 $i$ 个数为左区间的最长数组,右区间单调增大。

我们将序列减去下标得到新的序列 $b$ ,问题等价于将一段数通过操作变成同一个数。我们知道将一段变成他的中位数时操作次数最小。

维护两个树状数组,一个维护数量 $fenc$ ,一个维护总和 $fens$ 。通过 $fenc$ 找出中位数,可以在 $O(logn)$ 内求出操作需要的最小次数。

注意到值域较大,需要离散化。

时间复杂度 $O(nlogn)$ 。

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
#include <bits/stdc++.h>

using i64 = long long;

template <typename T> struct Fenwick {
int n;
std::vector<T> tr;

Fenwick(int n) : n(n), tr(n + 1, 0)
{
}

int lowbit(int x)
{
return x & -x;
}

void modify(int x, T c)
{
for (int i = x; i <= n; i += lowbit(i))
tr[i] += c;
}

void modify(int l, int r, T c)
{
modify(l, c);
if (r + 1 <= n)
modify(r + 1, -c);
}

T query(int x)
{
T res = T();
for (int i = x; i; i -= lowbit(i))
res += tr[i];
return res;
}

T query(int l, int r)
{
return query(r) - query(l - 1);
}

int find_first(T sum)
{
int ans = 0;
T val = 0;
for (int i = std::__lg(n); i >= 0; i--) {
if ((ans | (1 << i)) <= n && val + tr[ans | (1 << i)] < sum) {
ans |= 1 << i;
val += tr[ans];
}
}
return ans + 1;
}

int find_last(T sum)
{
int ans = 0;
T val = 0;
for (int i = std::__lg(n); i >= 0; i--) {
if ((ans | (1 << i)) <= n && val + tr[ans | (1 << i)] <= sum) {
ans |= 1 << i;
val += tr[ans];
}
}
return ans;
}
};

void solve()
{
int n;
i64 k;
std::cin >> n >> k;
std::vector<int> a(n + 1);
std::vector<int> b;
for (int i = 1; i <= n; i++) {
std::cin >> a[i];
a[i] -= i;
b.push_back(a[i]);
}
std::sort(b.begin(), b.end());
b.erase(std::unique(b.begin(), b.end()), b.end());
int m = b.size();
std::map<int, int> mp;
for (int i = 0; i < m; i++) {
mp[b[i]] = i + 1;
}
std::vector<int> o(n + 1);
for (int i = 1; i <= n; i++) {
o[i] = mp[a[i]];
}

Fenwick<int> fenc(m);
Fenwick<i64> fens(m);
auto check = [&](int x) -> bool {
fenc.modify(o[x], 1);
fens.modify(o[x], a[x]);
int sum = fenc.query(m);
int mid = (sum + 1) / 2;
int id = fenc.find_first(mid);
i64 prec = fenc.query(id);
i64 sfc = sum - prec;
i64 pres = fens.query(id);
i64 sfs = fens.query(m) - pres;
if (sfs - sfc * b[id - 1] + prec * b[id - 1] - pres <= k) {
return true;
}
fenc.modify(o[x], -1);
fens.modify(o[x], -a[x]);
return false;
};
int ans = 0;
for (int i = 1, j = 0; i <= n; i++) {
while (j < n && check(j + 1)) {
j++;
}
ans = std::max(ans, j - i + 1);
fenc.modify(o[i], -1);
fens.modify(o[i], -a[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;
}

The 2023 ICPC Asia Jinan Regional Contest
http://example.com/2024/09/20/The-2023-ICPC-Asia-Jinan-Regional-Contest/
作者
muxingchengfeng
发布于
2024年9月20日
许可协议