Thanks to everyone who participated in this round! I hope you enjoyed the contest.
2200A - Eating Game
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (auto &i: a) cin >> i;
int mx = 0, ans = 0;
for (int i : a) mx = max(i, mx);
for (int i : a) ans += i == mx;
cout << ans << '\n';
}
signed main() {
int t = 1;
cin >> t;
while (t--) solve();
}
2200B - Deletion Sort
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (auto &i: a) cin >> i;
if (is_sorted(a.begin(), a.end())) cout << n << '\n';
else cout << "1\n";
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
cin >> t;
while (t--) solve();
}
2200C - Specialty String
What if you delete characters instead of replacing them with $$$*$$$?
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
string s;
cin >> n >> s;
for (int i = 0; 2*i < n; i++) {
for (int j = 1; j < size(s); j++) {
if (s[j] == s[j-1]) {
s.erase(j-1, 2);
}
}
}
cout << (s.empty() ? "YES\n" : "NO\n");
}
signed main() {
int t = 1;
cin >> t;
while (t--) solve();
}
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
string s;
cin >> n >> s;
stack<int> st;
for (char c : s) {
if (st.size() && c == st.top()) st.pop();
else st.push(c);
}
cout << (st.empty() ? "YES\n" : "NO\n");
}
signed main() {
int t = 1;
cin >> t;
while (t--) solve();
}
2200D - Portal
What invariants does $$$p$$$ have?
Consider the elements in between the portals and the elements outside of them.
What invariant does each of these two regions have?
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, x, y;
cin >> n >> x >> y;
x--, y--;
vector<int> a, b;
for (int i = 0; i < n; i++) {
int j;
cin >> j;
if (i <= x || i > y) a.push_back(j);
else b.push_back(j);
}
// solve
if (!b.empty()) rotate(b.begin(), min_element(b.begin(), b.end()), b.end());
int m = b.empty() ? -1 : b[0];
auto it = a.begin();
while (it != a.end() && *it < m) it++;
a.insert(it, b.begin(), b.end());
for (int i = 0; i < n; i++) {
cout << a[i] << " \n"[i == n-1];
}
}
signed main() {
int t = 1;
cin >> t;
while (t--) solve();
}
2200E - Divisive Battle
What elements can the operation not be used on?
$$$1$$$ and prime numbers.
What first moves ensure that Alice will win?
Making a small prime number appear in the array after a number with a larger prime factor.
If Alice can win, then she can ensure victory on her first move.
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
int primebase(int x) {
set<int> s;
for (int i = 2; i*i <= x; i++) {
while (x % i == 0) {
s.insert(i);
x /= i;
}
}
if (x > 1) s.insert(x);
if (s.size() > 1) return -1;
if (s.size() == 0) return 1;
return *s.begin();
}
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (auto &i: a) cin >> i;
// solve
vector<int> b(n);
for (int i = 0; i < n; i++) b[i] = primebase(a[i]);
if (is_sorted(all(a))) {
cout << "Bob\n";
} else if (*min_element(all(b)) == -1) {
cout << "Alice\n";
} else if (is_sorted(all(b))) {
cout << "Bob\n";
} else {
cout << "Alice\n";
}
}
signed main() {
int t = 1;
cin >> t;
while (t--) solve();
}
2200F - Mooclear Reactor 2
Try to solve the problem without shop particles.
Fix the number of energy-generating particles $$$k$$$. Which particles can Bessie use?
She can use all particles with $$$y\geq k-1$$$.
Of the particles she can use for some $$$k$$$, which should Bessie use?
The $$$k$$$ particles with the largest $$$x$$$ values.
Process $$$k$$$ in decreasing order.
If Bessie has to use a shop particle, which of her own particles should she replace with it?
The energy-generating particle with smallest $$$x$$$.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> bool smin(T &a, const T b) { return b < a ? a = b, 1 : 0;}
template<class T> bool smax(T &a, const T b) { return a < b ? a = b, 1 : 0;}
void solve() {
int n, q;
cin >> n >> q;
vector<array<int, 2>> a(n);
for (auto &i : a) cin >> i[1] >> i[0];
// solve
sort(a.rbegin(), a.rend());
int ptr = 0;
multiset<int> s;
ll mx = 0, cur = 0;
vector<ll> pref(n+1);
for (int k = n; k >= 0; k--) {
while (ptr < n && a[ptr][0] >= k) {
cur += a[ptr][1];
s.insert(a[ptr][1]);
ptr++;
}
while (s.size() > k + 1) {
cur -= *s.begin();
s.erase(s.begin());
}
smax(mx, cur);
pref[k] = (s.size() <= k) ? cur : cur - *s.begin();
}
for (int k = 1; k <= n; k++) smax(pref[k], pref[k-1]);
// queries
while (q--) {
int x, y;
cin >> x >> y;
cout << max(mx, pref[y] + x) << '\n';
}
}
signed main() {
int t = 1;
cin >> t;
while (t--) solve();
}
2200G - Operation Permutation
Convert each - operation to + and / operation to x.
Fix the permutation. For each +y operation, by how much would the answer change if it was removed?
The answer would reduce by the product of $$$y$$$ and the x operations that come after it in the permutation.
How can you calculate the expected value of the product x operations that come after +y over all permutations?
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9+7;
ll mpow(ll a, ll b) {
ll r = 1;
while (b) {
if (b & 1) {
r *= a;
r %= mod;
}
b >>= 1;
a *= a;
a %= mod;
}
return r;
}
ll inv(ll a) {
return mpow(a, mod-2);
}
const int maxn = 5000;
ll facts[maxn+1] = {1};
void precomp() {
for (int i = 1; i <= maxn; i++) {
facts[i] = facts[i-1] * i % mod;
}
}
void solve() {
int n;
ll x;
cin >> n >> x;
vector<ll> mults;
ll adds = 0;
for (int i = 0; i < n; i++) {
char c;
ll v;
cin >> c >> v;
if (c == '-') adds += mod - v;
else if (c == '+') adds += v;
else if (c == '/') mults.push_back(inv(v));
else mults.push_back(v);
}
adds %= mod;
// solve
int m = mults.size();
vector<ll> dp(m+1);
dp[0] = 1;
for (auto &b : mults) {
x *= b;
x %= mod;
for (int i = m; i >= 1; i--) {
dp[i] += dp[i-1] * b;
dp[i] %= mod;
}
}
ll S = 0;
for (int i = 0; i <= m; i++) {
S += dp[i] * facts[i] % mod * facts[m-i] % mod * inv(facts[m]) % mod;
S %= mod;
}
S *= inv(m+1);
S %= mod;
// answer
ll ans = x + adds * S;
ans %= mod;
cout << ans << "\n";
}
signed main() {
precomp();
int t = 1;
cin >> t;
while (t--) solve();
}
Solve for $$$n \leq 2\cdot 10^5$$$.
2200H - Six Seven
Find a recursive definition for special numbers.
When can $$$x \bmod{42}$$$ tell us if $$$x$$$ is special?
Whenever $$$42\nmid x$$$.
If $$$42\mid x$$$, how can we determine if $$$x$$$ is special?
$$$x$$$ will be special if and only if $$$\frac{x}{42}$$$ is special.
Iterate on the number of operations $$$\bmod{42}$$$.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll inf = LLONG_MAX / 2;
ll f(const vector<ll>& a) {
if (a.empty()) return 0;
if (a.back() == 1) return 5;
int n = a.size();
int mod6 = a[0] % 6;
vector<ll> groups[7];
for (ll x : a) {
if (x % 6 != mod6) return inf;
groups[(x - mod6) / 6 % 7].push_back(x);
}
ll ans = inf;
for (int r = 0; r < 7; r++) {
ll k = (42 - 6*r - mod6) % 42;
vector<ll> next;
for (ll x : groups[r]) next.push_back((x + k) / 42);
ll rec = f(next);
if (rec >= inf) continue;
ans = min(ans, k + 42 * rec);
}
return ans;
}
void solve() {
int n;
cin >> n;
vector<ll> a(n);
for (auto &x : a) cin >> x;
sort(a.begin(), a.end());
ll res = f(a);
if (res >= inf) cout << "-1\n";
else cout << res << '\n';
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
cin >> t;
while (t--) solve();
}









Great Problemset and fasttt editorial, loved D and E. AksLolCoding orz orz orz orz
Solution to G bonus
Instead of doing DP, model the multiplication as terms in polynomial coefficients $$$(1+b_1\cdot x)(1+b_2\cdot x)(1+b_3\cdot x)...(1+b_m\cdot x)$$$
We can see that the coefficient of $$$x^k$$$ in the $$$dp(m,k)$$$ in the DP described in the editorial
Calculating this from left to right still takes $$$O(m^2)$$$ time. But you can instead just pair up the linear terms to get quadratic terms, then pair up the terms to get quartic terms, and so on. This takes $$$O(m \log^2(m))$$$ time instead.
Can you explain what is S in terms of probability and why we multiply everything by 1/(m+1)
We are trying to calculate the expected value (EV) of the final value of the expression. To do this, by linearity of expectation, we can calculate the expected value of each additive term separately and sum them all to obtain the final answer.
At the end of an arbitrary expression, an additive term $$$y$$$ will have the form $$$y \cdot X$$$, where $$$X$$$ is the product of all multiplicative terms to the right of $$$y$$$. There may be many possible values for $$$X$$$, but only its expected value matters to us.
Let $$$S$$$ denote $$$\mathbb{E}[X]$$$, the expected value of $$$X$$$. The contribution to the answer for each additive term $$$y$$$ is, therefore, $$$S.y$$$. We now compute $$$S$$$.
Again, by linearity of expectation (with a small twist), we have: $$$ S = \sum_{k=0}^{m} \Pr(\text{there are } k \text{ multiplicative terms to the right of } y) \cdot \mathbb{E}[\text{product if there are } k \text{ multiplicative terms to the right of } y]. $$$
Each probability term is $$$\dfrac{1}{m+1}$$$ due to randomness, and each expected product term corresponds to the $$$\text{dp}/\text{choose}$$$ component.
That's how the editorial finds $$$S = \frac{1}{m + 1} \sum_{k = 0}^{m} dp[m][k]/ \binom{m}{k} $$$
If you, as I did until yesterday, struggle with comprehending Expected Values and it's manipulations that were used, i recommend you watch Errichto 's lecture on YouTube
thanks bro, really appreciate that
So this solution requires 3 mod NTT?
This is be a valid solution for MOD=998244353 right?? is there a method to perform accurate polynomial Multiplication for MOD=1e9+7?? (FFT gave WA)
Maybe you need 3 mod NTT: convolution_mod_1000000007 (Which calculate the ans with three different MOD and merge them with CRT(Chinese Remainder Theory) to get ans in MOD=1e9+7)
Link seems to be broken, care to send it again?
revised
Thanks, Something new, will look into it
My first contest and I could actually solve smtg:)
all the best
Great contest! Loved the C and D problems. Can F be solved using knapsack algorithm? I am just learning DP algorithms and thought that the problem is similar to knapsack, correct me if I am wrong.
Yes I think you can use DP here (knapsack one) but the issue is that it will TLE and MLE both,It is O(n^2) and since constraint are 2*10^5, 2 state dp would also fail, u can optimize this to 1 state DP array so MLE can be cleared but TLE cant be helped i guess.
Let dp[i]=max answer if we select pairs such that the min y is equal to i.
Then answer for dp[i] can be calculated by using a priority queue (I'm not stating my implementation here, but basically for dp[i] I'm finding the max sum of x values for pairs of type (x,y) with y>=i)
Then the problem says we have to find the answer for each pair in 'b' if we had that pair. So I consider 2 cases:
1) we have the pair, but we don't use it: Then answer is simply max(dp[i]) as nothing changes.
2) We may use it: Let's say the pair is (X,Y). If I include this pair in my array a, then dp[i] for i>Y won't change. But dp[i] for i<=Y may change. Because,
(i) it may be possible that the min values out of the selected i values (for some dp[i]) may be less than X
(ii) it may be possible that dp[i] was calculated with us having selected less than i values, which means there's room for more elements to add.
So I just maintain some values in order to find the max for each (X,Y).
But it gives WA on TC2, so I'm wrong somewhere. But idk where, it seems alright to me.
In C, I thought that matching characters will have different effect based on positions only to realise after the contest that it doesn't, cool problems
It was my first contest and it was actually good, I solved 2
I just 3 problems,I will study more.
Thank you for the editorial! Isn't code 1 for C incorrect?
We erase from
s, but comparejton(the initial length), so we will inevitably get out of bounds -> erase will crash if we were to call it. We should compare tos.size()instead.O(n) solution for C
As explained in the editorial, it is always optimal to delete a pair when you find one. You can avoid looping through the string multiple times by mantaining a stack with all currently undeleted characters. Loop through characters in the string from start to end. When you encounter character $$$c$$$ in the string, if it matches the top element of the stack, $$$t$$$, then $$$c$$$ and $$$t$$$ will eventually become adjacent after a bunch of deletions, so remove $$$t$$$ from the stack. Otherwise, add $$$c$$$ to the stack. If the stack is empty after looping through the string, it's possible to turn the string into stars.
Isn't answer is YES for the case "aaabb" like first select i=0 and j=2 set all a's to * then select i=3 and j=4 and set all b's to * so it is possible to convert string s to all *
but your as well as editorial code gives answer as NO
No you can't choose $$$i=0$$$ and $$$j=0$$$ at the start because the question says $$$i, j$$$ must satisfy $$$s_k = *$$$ for all $$$i \lt k \lt j$$$. You might have misread the question to say that you set $$$s_k = *$$$ for all $$$i \lt k \lt j$$$
The question is asking about deleting pairs anyways, so it's always impossible when $$$n$$$ is odd
thanks , I misread that part
AksLolCoding In tutorial of E, shouldn't it be "If bi is non-decreasing, then Bob will win."?
Fixed
i felt E was easier than D probably because D involved heavy implementation or rather i overcomplicated its implementation :) but it was a great contest
Hey, swayamsn123. I think you might have complicated. Breaking array in two parts made the problem simple. You can check this solution of mine and let me know if it's better or worse than yours.369796694
you macros look good
instead of caps you made them lower case
Nice problems!
For G bonus: I suppose $$$O(n^{\log_2(3)})$$$ can pass with some minor optimizations.
I have an idea for an $$$o(n\log{n})$$$ solution for F, which is just improvising $$$O(n\log{n})$$$ parts with $$$o(n\log{n})$$$ parts, e.g. replacing standard sorting with radix sorting, and using e.g. a vEB tree to manage the set. Even though I'm not sure it'll run better since $$$O(n\log(n))$$$ with a simple heap is already 100ms.
F was pretty good, not some boring DP like the last few rounds, wasn't brutal but hard enough for me to think.
Great ProblemSet
The problems are really excellent. I will compete Div.3 next time.
bruh in B i got hyperfixated on max and min values to find some pattern from them that i didnt think any element would work :(( solved c in 5 mins remained stuck at B for 45 mins. Good contest
I got stuck at last one. I KNEW I SHOULDVE DIVIDED THE x+k IM BADDDDD
Question D is really interesting!
Really loved it. Great questions,lots for me to learn.Thank you for all who contributed to this.
Great contest
AksLolCoding for editorial of problem F shouldn't it be a MIN heap instead considering we'll remove the element with the least energy from the heap
Fixed
The problem E is so hard
Hi, I've tried to explain a little bit simpler, simulation-based approach here, hope that helps!
Yes, Thank you for help, i put you +1. +1 put my please
For problem 2, I guess the answer should be 2 for every testcases except for an empty list because what you can do is just Sort the array and then reverse it.
Damn, couldn't figure out D on my own.
Solved! the hints made the problem trivial, at least conceptually. Implementation was still tricky...364873482
A little bit different solution for Task E: -
I used a simulation-based thought process, focusing on the explicit sequences each player tries to construct: -
We know that: -
Alice wants to make the array unsorted. Her goal is to create a state where $$$A_i \gt A_j$$$ for some $$$i \lt j$$$.
Bob wants to keep the array sorted. His goal is the exact opposite, ensuring $$$A_i \le A_{i+1}$$$ for all valid $$$i$$$.
Because both players play optimally, for a composite number $$$C$$$ with prime factors as $$$p_1 \le p_2 \le \dots \le p_k$$$ is changed in a specific arrangement, by alternately distributing the prime factors into a left and right partition (and reversing the right), the split naturally $$$S$$$ is increasing first, then decreasing. Mathematically, this is like a bitonic sequence that looks like this: —
Combining these observations, we can construct the final modified sequence by replacing every composite number with their respective expanded sequences.
Once we create this fully expanded sequence, our final check is very simple: If the final modified sequence is non-decreasing, Bob wins. Otherwise, Alice wins. There's also a base condition that if the initial sequence is non-decreasing in the first place, then Bob wins.
I hope this helps! :)
Submission Link
But for every element alice can't start first then how is this possible for every element. S=(p2,p4,…,ppeak,…,p3,p1)
for instance: 16 10 25 if alice choose 16 first, next bob choose 10. So, Definitely alice need to choose 10 and split like 5*2, then only alice can win.
you’re correct about the part that alice must choose to split 10, and that’s exactly why she would never start with 16 as it’s the sub-optimal choice, simply because 16’s a perfect power of 2, similar to how 25 is a perfect power of 5!
simply put, the simulation can be seen as finding a particular composite number, whose factorisation can be made non-decreasing, since one such number is enough to guarantee alice’s victory, the rest of the simulation isn’t actually necessary.
moreover, it doesn’t make our answer incorrect at any point simply because of the fact that if there exists a winning move for alice, she can do that move first and then play the entire game with bob just for the sake of it (simulation), in case there is no winning move for her then again the simulation can’t generate a winning position and hence bob wins!
For problem D, consider this input
1 11 2 8 2 1 5 3 6 5 3 5 4 6 3
The code in the tutorial gives 2 1 3 6 5 3 5 5 4 6 3 However, I think the actual output should be 2 1 3 5 5 3 6 5 4 6 3 Can someone clarify if I'm thinking correctly? Nvm, the input is invalid. Sorry in advance
I think the testcase that you've mentioned is invalid, since it's explicitly stated that the given sequence is a permutation i.e. all values are unique and are between 1 and N inclusive (where N is the size of the sequence).
You're right. Thx a lot
your input is not correct should be permutation
Lol just noticed that. Thx a lot.
wait this is actually a really good editorial. AksLolCoding orz
I agree
I was doing the contest and I had a question about problem C. In the solution logic, we delete characters in pairs, but if we have a case like "llml", what happens? In theory, the answer should be YES, but when running the algorithm it gives NO, someone can explain to me?
The answer is no because when there is no more movements all characters should have be changed to * (eliminated), in your example, the only two scenarios are the following:
llml -> **ml -> game ends, not all characters are*->AksLolCoding lostllml -> *lm* -> game ends, not all characters are*->AksLolCoding lostno,
llml -> *ml*is invalidMy mistake, you are right, the 1st scenario is the only possible
364648115 submission, how and why would someone ever write Check is alaready decreasing in such a nice way. huge cheating AI sus https://codeforces.me/contest/2200/submission/364648115 this young bright mind solved B and C but was too smart to do a stupid q like A, cudnt do wow had 3 WA on test case 1?????? https://codeforces.me/submissions/Zero-zaber/contest/2200
Really liked G problem! Such a great one, sadly got accepted only after round:( I bet for better time complexity FFT might be helpful
You can even use the exp-log trick for an $$$O(n \log(n))$$$ solution. Actually E has the time complexity of $$$O(n \log^2(V))$$$. I forgot that the number $$$x$$$ only needed a prime base and not being a prime itself.
isn't A is B and B is A for problem D code ? please correct me if wrong .
Guys, support our flash mob and go to the "банда мопсоу" organization, and also put this picture as your avatar. Thanks to everyone who took part
Problem B is like https://youtube.com/shorts/DxeTYF5dsUU?si=MNHkl8Y9I7fV6aND
for the problem C: lets assume that our string is special and indexes are 1,2,3,...,n. Then we can pair these indexes such that for any pair (i,j) that they selected in some operation. (which means s[i]=s[j]). Then for any pair (a,b) and (c,d) its impossible to be like a<c<b<d. That explains why the order of the operation does not matter since our string should be concatenation of some even length palindrome strings.
Can someone explain to me why are we iterating over m (mod 42) ? I don't get the intuition behind it.
...A lot of cool solutions for C, I just thought of it as an Even Palindrome problem. Used 2 adjacent pointers(i,j) and if they were equal then converted both to '*' & started deacreasing i and increasing j, if string[i/j] is a '*' then just ignore it and decrease/increase i/j respectively till the j > string.size().
When will these questions be rated ?
In H, why are you including those numbers in the next recursion depth, which are already special at this level? Does this not fail to minimize the number of operations?
A Slight Different Approach for Problem C using recursion and string size
369425890
Hi Codeforces! Problem E 2200E - Divisive Battle. Please do check out this
O(N)solution, 369918055, of mine which uses pre-computation by adding some variation in Sieve of Eratosthenes. Feedback is highly appreciated!In the solve of problem D, the line "while (it != a.end() && *it < m) it++;" should be revised. It should be "*it <= m" instead of "*it < m".
AksLolCoding thankyou for D & E