# # Educational Codeforces Round 94 (CF1400) 题解

## # Problem A - String Similarity (opens new window)

### # 题解

#include <iostream>

using namespace std;

int main() {
int t;
cin >> t;
while (t--) {
int n;
string s, ans;
cin >> n >> s;
for (int i = 0; i < n; ++i)
ans.push_back(s[2 * i]);
cout << ans << endl;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

## # Problem B - RPG Protagonist (opens new window)

### # 题解

#include <cstdio>
#include <iostream>

using namespace std;

template <typename T> void read(T &x) {
x = 0;
char c = getchar();
T sig = 1;
for (; !isdigit(c); c = getchar())
if (c == '-')
sig = -1;
for (; isdigit(c); c = getchar())
x = (x << 3) + (x << 1) + c - '0';
x *= sig;
}

class Solution {
public:
void solve() {
int p, f, cs, cw, s, w;
read(p), read(f), read(cs), read(cw), read(s), read(w);
int ans = 0;
if (s > w)
swap(s, w), swap(cs, cw);
for (int i = 0; i <= cs && i * s <= p; ++i) {
int p1 = p - i * s;
int pw = min(p1 / w, cw);
int cw1 = cw - pw;
int fs = min(f / s, cs - i);
int f1 = f - fs * s;
int fw = min(f1 / w, cw1);
ans = max(ans, i + pw + fs + fw);
}
printf("%d\n", ans);
}
};

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
read(t);
while (t--) {
Solution solution = Solution();
solution.solve();
}
}
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

## # Problem C - Binary String Reconstruction (opens new window)

### # 题目描述

• 如果$w_{i-x}=1$$w_{i+x}=1$，则$s_i=1$$i-x$$i+x$需要是合法的下标）
• 否则$s_i=0$

### # 题解

#include <iostream>

using namespace std;

class Solution {
public:
void solve() {
string s;
int x;
cin >> s >> x;
int n = s.size();
string w(n, '1');
for (int i = 0; i < n; ++i)
if (s[i] == '0') {
if (i >= x)
w[i - x] = '0';
if (i + x < n)
w[i + x] = '0';
}
for (int i = 0; i < n; ++i)
if (s[i] == '1') {
bool ok = false;
if (i >= x && w[i - x] == '1')
ok = true;
if (i + x < n && w[i + x] == '1')
ok = true;
if (!ok) {
cout << -1 << endl;
return;
}
}
cout << w << endl;
}
};

int main() {
int t;
cin >> t;
while (t--) {
Solution solution = Solution();
solution.solve();
}
}
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

## # Problem D - Zigzags (opens new window)

### # 题解

#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;

template <typename T> void read(T &x) {
x = 0;
char c = getchar();
T sig = 1;
for (; !isdigit(c); c = getchar())
if (c == '-')
sig = -1;
for (; isdigit(c); c = getchar())
x = (x << 3) + (x << 1) + c - '0';
x *= sig;
}

class Solution {
public:
void solve() {
int n;
read(n);
vector<int> a(n), cnt(n + 1);
for (int i = 0; i < n; ++i)
read(a[i]), cnt[a[i]]++;
ll ans = 0;
for (int i = 1; i <= n; ++i) {
int &t = cnt[i];
if (t >= 4)
ans += (ll)t * (t - 1) * (t - 2) * (t - 3) / 24;
}
for (int i = 3; i < n; ++i) {
vector<int> l(n + 1), r(n + 1);
ll cnt = 0;
for (int j = 0; j < i; ++j)
r[a[j]]++;
for (int j = 0; j < i; ++j) {
if (a[j] == a[i])
ans += cnt;
else {
cnt -= l[a[j]] * r[a[j]];
l[a[j]]++, r[a[j]]--;
cnt += l[a[j]] * r[a[j]];
}
}
}
printf("%lld\n", ans);
}
};

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
read(t);
while (t--) {
Solution solution = Solution();
solution.solve();
}
}
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

## # Problem E - Clear the Multiset (opens new window)

### # 题目描述

1. 将一段连续区间减$1$，要求操作前区间所有数都大于$0$
2. 将某个位置置为$0$

### # 题解

#include <iostream>
#define N 5005

int n, a[N];

int dfs(int l, int r, int b) {
if (l == r)
return a[l] > b;
int lo = 1e9;
for (int i = l; i <= r; ++i)
lo = std::min(lo, a[i]);
int cl = -1, cr = -1;
int tot = lo - b, cnt = 0;
for (int i = l; i <= r; ++i) {
if (a[i] == b) {
if (cr != -1)
tot += dfs(cl, cr, lo);
cl = -1, cr = -1;
} else {
cnt++;
if (cl == -1)
cl = i;
cr = i;
}
}
if (cr != -1)
tot += dfs(cl, cr, lo);
return std::min(tot, cnt);
}

int main() {
std::cin >> n;
for (int i = 0; i < n; ++i)
std::cin >> a[i];
std::cout << dfs(0, n - 1, 0);
}
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

## # Problem F - x-Prime Substrings (opens new window)

### # 题解

• 放弃$i+1$，最小值加一。
• $i+1$，串在AC自动机上的位置移动到$nxt=nodes[j].children[s_{i+1}]$。如果$nxt$不为终点，则可以进行这一转移，最小值不变。

#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;
const int INF = 0x3f3f3f3f;

struct Node {
int fail = 0, children[9]{};
bool match = false;
};

string s, t;
int x, dp[1005][5005];
set<string> xprime;
vector<int> suffix = {0};
vector<Node> nodes;

void generate_xprime() {
if (suffix[0] == x) {
xprime.insert(t);
return;
}
for (int i = 1; i <= 9 && i + suffix[0] <= x; ++i) {
t.push_back(i + '0');
for (int &j : suffix)
j += i;
suffix.emplace_back(i);
bool ok = true;
for (int &j : suffix)
if (j != x && x % j == 0) {
ok = false;
break;
}
if (ok)
generate_xprime();
suffix.pop_back();
for (int &j : suffix)
j -= i;
t.pop_back();
}
}

void build_aca() {
nodes.emplace_back(Node{});
for (const string &str : xprime) {
int curr = 0;
for (const char &c : str) {
if (!nodes[curr].children[c - '1']) {
nodes[curr].children[c - '1'] = nodes.size();
nodes.emplace_back(Node{});
}
curr = nodes[curr].children[c - '1'];
}
nodes[curr].match = true;
}
queue<int> q;
for (const int &u : nodes[0].children)
if (u)
q.push(u);

while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = 0; i < 9; ++i) {
int &v = nodes[u].children[i];
if (v) {
nodes[v].fail = nodes[nodes[u].fail].children[i];
q.push(v);
} else
v = nodes[nodes[u].fail].children[i];
}
}
}

int main() {
cin >> s >> x;

// Step 1: Enumerate all x-prime strings
generate_xprime();

// Step 2: Build Aho-Corasick automaton with x-prime strings
build_aca();

// Step 3: Dynamic programming
memset(dp, 0x3f, sizeof(dp));
int n = s.size(), m = nodes.size(), ans = INF;
dp[0][0] = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
if (dp[i][j] == INF)
continue;
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1);
int nxt = nodes[j].children[s[i] - '1'];
if (!nodes[nxt].match)
dp[i + 1][nxt] = min(dp[i + 1][nxt], dp[i][j]);
}
for (int j = 0; j < m; ++j)
ans = min(ans, dp[n][j]);
cout << ans;
}

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

## # Problem G - Mercenaries (opens new window)

### # 题目描述

$N$个雇佣兵（$N\leq3\times10^5$），每个人要求组团的人数在$[L_i,R_i]$之间。另外有$m$$m\leq20$）条规则，规定了$A_i$$B_i$不能一起组团。求组建雇佣兵团的方法数（模$998244353$）。

### # 题解

#include <bitset>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
#define MAXN 300005
#define MAXK 41
#define MOD 998244353

using namespace std;
typedef long long ll;

template <typename T> void read(T &x) {
x = 0;
char c = getchar();
T sig = 1;
for (; !isdigit(c); c = getchar())
if (c == '-')
sig = -1;
for (; isdigit(c); c = getchar())
x = (x << 3) + (x << 1) + c - '0';
x *= sig;
}

ll fac[MAXN], rev[MAXN];

ll fexp(ll x, ll y) {
ll ans = 1;
while (y) {
if (y & 1)
ans = ans * x % MOD;
x = x * x % MOD;
y >>= 1;
}
return ans;
}

ll C(int n, int k) {
if (n < 0 || n < k || k < 0)
return 0;
return fac[n] * rev[k] % MOD * rev[n - k] % MOD;
}

ll pre[MAXK][MAXN];

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
read(n), read(m);
fac[0] = 1, rev[0] = 1;
for (int i = 1; i <= n; ++i) {
fac[i] = fac[i - 1] * i % MOD;
rev[i] = fexp(fac[i], MOD - 2);
}
vector<pair<int, int>> p(n), c(m);
vector<int> start(n + 1), end(n + 1);
for (int i = 0; i < n; ++i) {
read(p[i].first), read(p[i].second);
start[p[i].first]++;
end[p[i].second]++;
}
int cnt = 0;
vector<int> can(n + 1);
for (int i = 1; i <= n; ++i) {
cnt += start[i];
can[i] = cnt;
cnt -= end[i];
}
for (int i = 0; i < m; ++i) {
read(c[i].first), read(c[i].second);
c[i].first--, c[i].second--;
}
for (int k = 0; k <= 2 * m; ++k) {
pre[k][0] = 0;
for (int i = 1; i <= n; ++i)
pre[k][i] = (pre[k][i - 1] + C(can[i] - k, i - k)) % MOD;
}
ll ans = 0;
for (int i = 0; i < (1 << m); ++i) {
bitset<32> bs(i);
int t = bs.count();
int sig = (t & 1) ? -1 : 1;
int l = 1, r = n;
set<int> chosen;
for (int j = 0; j < m; ++j)
if (bs.test(j)) {
chosen.insert(c[j].first);
chosen.insert(c[j].second);
}
for (auto x : chosen) {
l = max(l, p[x].first);
r = min(r, p[x].second);
}
int k = chosen.size();
l = max(l, k);
if (l > r)
continue;
ll tot = (pre[k][r] - pre[k][l - 1] + MOD) % MOD;
ans = (ans + sig * tot + MOD) % MOD;
}
printf("%lld", ans);
}
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