2179A - Blackslex and Password
Author: IceBorworntat
Think about the indices that have the same remainder after division by $$$x$$$.
Consider the subsequence formed from the indices that have the same remainder after division by $$$x$$$. There are $$$x$$$ such subsequences. Each subsequence contains distinct characters, so the maximum length of such subsequences is $$$k$$$. As such, the minimum string length that cannot be constructed is $$$n = kx+1$$$.
Time complexity: $$$\mathcal{O}(1)$$$
t = int(input())
for _ in range(t):
k, x = [int(i) for i in input().split()]
print(k*x+1)
2179B - Blackslex and Showering
Author: spycoderyt
First observe that it is always helpful to remove a floor. Now the question becomes which floor to remove.
You can simply simulate the process of removing any possible floor. To do this efficiently, compute the current time taken and consider the change to the time taken if a floor is removed. If we remove floor $$$i$$$ where $$$i \neq 1$$$ and $$$i \neq n$$$, then the differences $$$|a[i] - a[i-1]|$$$ and $$$|a[i+1] - a[i]|$$$ are removed and the difference $$$|a[i-1] - a[i+1]|$$$ is added. If $$$i = 1$$$ or $$$i = n$$$, only the adjacent difference is removed.
Time complexity: $$$\mathcal{O}(n)$$$
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void test_case_run() {
int n;
cin >> n;
ll a[n+1];
for (int i = 1; i <= n; i++) cin >> a[i];
ll sum = 0;
for (int i = 2; i <= n; i++) sum += abs(a[i]-a[i-1]);
ll ans = min(sum - abs(a[2]-a[1]), sum - abs(a[n]-a[n-1]));
for (int i = 2; i < n; i++) {
ans = min(ans, sum - abs(a[i+1]-a[i]) - abs(a[i]-a[i-1]) + abs(a[i+1]-a[i-1]));
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) test_case_run();
return 0;
}
2179C - Blackslex and Number Theory
Author: omsincoconut
Making everying zeros works often.
Sometimes, it's better to make all numbers something else that's not zero.
Following Hint 2, if we don't make everything zero, then what's our target number?
Sort the array from small to large.
We can mod every number in the array by itself, and this would require $$$k = a_1$$$.
For a larger value of $$$k$$$, we cannot modify the value of $$$a_1$$$, so every number must be made into $$$a_1$$$. We can mod $$$a_i$$$ with $$$a_i - a_1$$$, using $$$k = a_2 - a_1$$$.
The answer is thus $$$\max(a_1, a_2 - a_1)$$$.
Time complexity: $$$\mathcal{O}(n)$$$ or $$$\mathcal{O}(n \log n)$$$ depending on implementation.
for test_case in range(int(input())):
n = int(input())
a = sorted([int(_) for _ in input().split()])
print(max(a[0], a[1]-a[0]))
2179D - Blackslex and Penguin Civilization
Author: omsincoconut
Ignore the lexicographically minimum condition first. Can you find any permutation that maximizes $$$S(p)$$$? Can you explain why the permutation maximizes $$$S(p)$$$?
There's an easy permutation that maximizes $$$S(p)$$$.
There are at most $$$2^{n-i}$$$ prefixes of the permutation that has the popcount of the bitwise AND be at least $$$i$$$.
For a popcount of a prefix to exceed $$$i$$$, you'll need to fix $$$i$$$ bits to be all ones, so there are only $$$n-i$$$ bits left to do other things to.
Read all hints. Now, look at this sentence in Hint 3.
"For a popcount of a prefix to exceed $$$i$$$, you'll need to fix $$$i$$$ bits to be all ones."
We will fix the $$$i$$$ lowest-value bits, and iterate the other bits lexicographically minimally.
Here's an example for $$$n=3$$$:
1 1 1 = 7
0 1 1 = 3
0 0 1 = 1
1 0 1 = 5
0 0 0 = 0
0 1 0 = 2
1 0 0 = 4
1 1 0 = 6
For a clean implementation, note that iterating the unfixed bits is basically iterating even numbers.
Time complexity: $$$\mathcal{O}(2^n)$$$.
t = int(input())
for _ in range(t):
n = int(input())
print(2**n - 1, end = ' ')
for i in range(1, n+1):
for j in range(0, 2**i, 2):
print(j*2**(n-i) + 2**(n-i) - 1, end = ' ')
print()
2179E - Blackslex and Girls
Author: spycoderyt
Denote the party that is supposed to win a district as the 'designated winner.' A minimum population $$$p_i$$$ for each district and a strict majority condition creates a lower bound on the number of votes from the designated winner. What is this lower bound?
The lower bound is $$$\lfloor \frac{p_i}{2} \rfloor + 1$$$.
What happens if at least one pair of districts has a different designated winner?
What happens if that is not true; i.e all districts have the same designated winner?
Consider the process of constructing a solution, even though we will not explicitly construct it in the code. First note that if $$$x+y \lt \sum p_i$$$, then it is impossible.
Then if $$$x+y \geq \sum p_i$$$, we start off by filling each district with the lower bound number $$$\lfloor \frac{p_i}{2} \rfloor + 1$$$ of votes from the designated winner. If we run out of votes from either party then the answer is no. After that we have two cases:
Case 1: if there is at least one pair of districts with different designated winners, we can arbitrarily fill (with any color) each district up to its minimum capacity $$$p_i$$$, and then allocate the rest of the votes to a district where that vote's party is supposed to win in. Therefore we only need to check if the lower bound on designated winner votes can be achieved.
Case 2: if all districts have the same designated winner, sometimes there are too many votes from the 'losing' party for a solution to exist. The minimum votes the winning party must have over the losing party is $$$n$$$, because we need a strict majority in every district, which corresponds to a vote difference of at least 1 in each district. WLOG let $$$x$$$ be the votes from the winning party and $$$y$$$ be the votes from the losing party, and we simply check if $$$x \geq y + n$$$.
Time complexity: $$$\mathcal{O}(n)$$$
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void test_case_run() {
int n;
ll x, y;
cin >> n >> x >> y;
string s;
cin >> s;
s = '.'+s;
ll p[n+1];
for (int i = 1; i <= n; i++) cin >> p[i];
if (accumulate(p+1, p+n+1, 0LL) > x+y) {
cout << "NO\n";
return;
}
if (s == "." + string(n, '0') || s == "." + string(n, '1')) {
if (s == "." + string(n, '1')) {
swap(x, y);
}
ll x_need = 0;
for (int i = 1; i <= n; i++) {
x_need += p[i]/2 + 1;
}
if (x < x_need || x < y+n) {
cout << "NO\n";
} else {
cout << "YES\n";
}
return;
}
ll x_need = 0, y_need = 0;
for (int i = 1; i <= n; i++) {
if (s[i] == '0') {
x_need += p[i]/2 + 1;
} else {
y_need += p[i]/2 + 1;
}
}
if (x >= x_need && y >= y_need) cout << "YES\n";
else cout << "NO\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) test_case_run();
return 0;
}
2179F - Blackslex and Another RGB Walking
Author: omsincoconut
Solve for a line graph.
Solve for a tree.
Does the exact same thing work? Why?
In the first run, use breadth-first search to find the distance of each vertex from vertex $$$1$$$. Color a vertex red if the distance is dividsible by $$$3$$$, green if the remainder of the division by $$$3$$$ is $$$1$$$, and blue if the remainder of the division by $$$3$$$ is $$$2$$$.
Note that if two vertices are connected by an edge, then their distances differ by no more than $$$1$$$. In fact, the distances differ by exactly $$$1$$$ for a bipartite graph.
Consider two adjacent vertices has the same distance. Connect the shortest paths from the two vertices to vertex $$$1$$$ with the edge connecting the two vertices. We can see that an odd cycle would form.
In the second run, move anywhere if every neighbor have the same color. Otherwise, note that
If the neighbors are red and green, then B is at a blue vertex, and should move to any green vertex.
If the neighbors are green and blue, then B is at a red vertex, and should move to any blue vertex.
If the neighbors are blue and red, then B is at a green vertex, and should move to any red vertex.
Time complexity: $$$\mathcal{O}(n+m)$$$
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
void test_first() {
int n, m;
cin >> n >> m;
vector<vector<int>> edge(n+1);
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
edge[a].push_back(b);
edge[b].push_back(a);
}
vector<int> dist(n+1, INF);
dist[1] = 0;
queue<int> bfs;
bfs.push(1);
while (!bfs.empty()) {
int a = bfs.front();
bfs.pop();
for (int b : edge[a]) {
if (dist[b] > dist[a] + 1) {
dist[b] = dist[a] + 1;
bfs.push(b);
}
}
}
for (int i = 1; i <= n; i++) cout << "rgb"[dist[i]%3];
cout << "\n";
}
void test_second() {
int dv;
string color;
cin >> dv >> color;
bool fr = false, fg = false, fb = false;
for (char c : color) {
if (c == 'r') fr = true;
if (c == 'g') fg = true;
if (c == 'b') fb = true;
}
if ((int)fr + (int)fg + (int)fb == 1) {
cout << 1 << "\n";
return;
}
char target;
if (fr && fg) target = 'g';
if (fg && fb) target = 'b';
if (fb && fr) target = 'r';
for (int i = 0; i < dv; i++) {
if (color[i] == target) {
cout << i+1 << "\n";
return;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string player;
int t;
cin >> player >> t;
if (player == "first") while (t--) test_first();
else while (t--) {
int q;
cin >> q;
while (q--) test_second();
}
return 0;
}
2179G - Blackslex and Penguin Migration
Author: omsincoconut
Think about corners.
Think about non-opposite corners.
Think about cells furthest from another cell.
The idea is that distance from two non-opposite corners uniquely determine the position.
Note that the answer can be rotated or flipped and still be correct. Let us assume the two corners are at position $$$(1, 1)$$$ and position $$$(1, n)$$$. Let the distance from the two corners be $$$d_1$$$ and $$$d_2$$$. Assume that the cell is on $$$(x, y)$$$. Then, we can write
We can solve the system of equations to have
Start from an arbitrary cell, and query the distance from that cell to every other cell, using $$$n^2$$$ queries. Any cell furthest is a corner.
Next, from that corner, query to every other cell, using $$$n^2$$$ queries. We now get the distances from one corner.
Now, notice that the non-opposite corners would have a distance of $$$n-1$$$ from the corner we have right now. There will be $$$n$$$ candidate corners. Choose one arbitrary candidate and query the distance from this candidate to all other candidates, using $$$n$$$ queries. Any furthest cell is a desired non-opposite corner.
Lastly, query from the new-found corner, using $$$n^2$$$ queries. We now have sufficient information to answer.
Query count: $$$3n^2 + n - \mathcal{O}(1)$$$. This analysis did not account for small constant optimizations like not querying the same pair twice and not querying the distance to self.
Time complexity: $$$\mathcal{O}(n^2)$$$
#include <bits/stdc++.h>
using namespace std;
int query(int i, int j) {
cout << "? " << i << " " << j << endl;
int ret;
cin >> ret;
return ret;
}
void test_case_run() {
int n;
cin >> n;
// Find the first corner
int dist1[n*n+1];
for (int i = 1; i <= n*n; i++) dist1[i] = query(1, i);
int corner1 = 1;
for (int i = 1; i <= n*n; i++) if (dist1[i] > dist1[corner1]) corner1 = i;
// Query distance from first corner
int dist_corner1[n*n+1];
for (int i = 1; i <= n*n; i++) {
dist_corner1[i] = query(corner1, i);
}
// Find second corner
vector<int> corner2_candidates;
for (int i = 1; i <= n*n; i++) {
if (dist_corner1[i] == n-1) corner2_candidates.push_back(i);
}
int dist_findcorner2[corner2_candidates.size()];
for (int i = 0; i < corner2_candidates.size(); i++) {
dist_findcorner2[i] = query(corner2_candidates[0], corner2_candidates[i]);
}
int corner2_index = 0;
for (int i = 0; i < corner2_candidates.size(); i++) {
if (dist_findcorner2[i] > dist_findcorner2[corner2_index]) corner2_index = i;
}
int corner2 = corner2_candidates[corner2_index];
// Query distance from second corner
int dist_corner2[n*n+1];
for (int i = 1; i <= n*n; i++) {
dist_corner2[i] = query(corner2, i);
}
// Find position
/*
d1 = |x-1|+|y-1| = x+y-2
d2 = |x-1|+|y-n| = x-y+n-1
d1 + d2 = 2x+n-3 => x = (d1+d2-n+3)/2
d1 - d2 = 2y-n-1 => y = (d1-d2+n+1)/2
*/
int ans[n+1][n+1];
for (int i = 1; i <= n*n; i++) {
int d1 = dist_corner1[i], d2 = dist_corner2[i];
int x = (d1+d2-n+3)/2;
int y = (d1-d2+n+1)/2;
ans[x][y] = i;
}
cout << "!" << endl;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) cout << ans[i][j] << " ";
cout << endl;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) test_case_run();
return 0;
}
This problem is solvable in $$$3n^2$$$ queries. Try it.
2179H - Blackslex and Plants
Author: NortGlG
Solve the easier version of this problem first:
- Let $$$f(x)$$$ be the value of the least significant bit of $$$x$$$.
Find the conditions needed to be satisfied to make the value of $$$f(x)$$$ become $$$2^y$$$ for any non-negative integer $$$y$$$.
$$$f(x) = 2^y$$$ if and only if $$$y$$$ is the largest non-negative integer that makes $$$2^y$$$ divide $$$x$$$.
What other things can we imply from that observation ?
If $$$2^y$$$ divides $$$x$$$, then $$$2^0, 2^1, 2^2, \ldots, 2^{y-1}$$$ will divide $$$x$$$ too. Use this fact to solve this version of the problem.
For any non-negative integer $$$y$$$, this equation is always true:
From this equation, the value of $$$f(x) - 1$$$ can be represented as the sum of $$$2^{a-1}$$$ for each positive integer $$$a$$$ that $$$2^a$$$ divides $$$x$$$, that is:
Which can be rearranged as:
You can see that every term of this sum can be calculated independently because the term $$$2^b$$$ exists because $$$2^{b+1}$$$ divides $$$x$$$.
For each watering operation on range $$$[l,r]$$$, you need to perform this action:
- For $$$a = 1, 2, 3, \ldots, \log n$$$; increase the water amount by $$$2^{a-1}$$$ in every position $$$i$$$ where $$$2^a$$$ divides $$$(i - l + 1)$$$, and also increase the water amount in every position in range $$$[l, r]$$$ by $$$1$$$, too.
These operations can be handled using prefix sum arrays separately in each $$$a$$$.
Time complexity: $$$\mathcal{O}((n + q)\log n)$$$.
Read Hint 1 first.
You can extend the idea from the easier version to solve the actual task.
Since the value of the least significant bit can be represented using the sum of $$$2^0, 2^1, 2^2, \ldots$$$, the only thing left to consider is the coefficient of those values in each position.
For watering operations that cover the range $$$[l, r]$$$, you can perform the following update to the coefficient array:
- For all $$$l \leq i \leq r$$$, add $$$i$$$ to the $$$i$$$-th position.
- For all $$$l \leq i \leq r$$$, subtract $$$l-1$$$ from the $$$i$$$-th position.
Which is basically the same as increasing the coefficients value in each $$$l \leq i \leq r$$$ by $$$i - l + 1$$$.
These operations can be implemented using prefix sum arrays separately in each $$$a$$$, too.
Time complexity: $$$\mathcal{O}((n+q)\log n)$$$.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 200200;
int qs1[20][N];
ll qs2[20][N];
ll ans[N];
void solve() {
int n, q;
cin >> n >> q;
int sz = 31 - __builtin_clz(n);
for (int i = 1;i <= n;i++) ans[i] = 0;
for (int j = 0;j <= sz;j++) {
for (int i = 0;i <= n;i++) qs1[j][i] = 0, qs2[j][i] = 0;
}
while (q--) {
int l, r;
cin >> l >> r;
int sz = 31 - __builtin_clz(r - l + 1);
for (int j = 0;j <= sz;j++) {
int k = (r - l + 1) >> j;
qs1[j][l + (1 << j) - 1]++;
qs2[j][l + (1 << j) - 1] += l - 1;
qs1[j][min(n + 1, l + ((k + 1) << j) - 1)]--;
qs2[j][min(n + 1, l + ((k + 1) << j) - 1)] -= l - 1;
}
}
for (int i = 0;i <= sz;i++) {
for (int j = 1;j <= n;j++) {
if (j >= (1 << i)) {
qs1[i][j] += qs1[i][j - (1 << i)];
qs2[i][j] += qs2[i][j - (1 << i)];
}
ans[j] += 1ll * qs1[i][j] * j * (1 << max(0, i - 1));
ans[j] -= 1ll * qs2[i][j] * (1 << max(0, i - 1));
}
}
for (int i = 1;i <= n;i++) {
cout << ans[i] << ' ';
}
cout << '\n';
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int q;
cin >> q;
while (q--) {
solve();
}
return 0;
}









Thanks for the contest. It was nice one
Can you please put the codes on the blog? I can't visit the solution links.
I liked the problems, Thanks for the good contest.
But E was a blocker for me, couldn't come up with a valid approach :(
I felt that problem E looks like a typical optimization problem that could be find on mathematical optimization class.
Very good contest and fast editorial
Please put the codes on the blog
Why codes?
The editorial was clear, I can't understand why you want the codes.
:(
Thanks for the contest and fast editorial, the problems were quite interesting, maybe just E could have been swapped with F. Overall the contest felt good!
My roommate was playing games and shouting while I was solving problems, which distracted me so much that even though I had fully figured out Problem D, it still took me 30 minutes to write the code. But I seem to have no idea at all about Problem E. :<
Anyway, it was a great contest. I feel like I need to practice more. Orz
ngl idgaf
Then why did you bother commenting?
Catch you bear big.
Wow, pretty coincidential :>
none of the implementation link is accessible.
Great Contest ! I'll need to work on Bit Manipulations now :)
look up CPH "Bit Manipulation" section, was a gamechanger for me
Okay will try that ! Thanks
Couldn't even solve C. I don't even know what went wrong for me in this contest.
Problem G: (Challenge)
After finding first corner, now we have N candidate corners. the farthest candidate from the arbitrary point (initial random point) would be the corner which we are looking for. And those distances from initial point are already computed.
So we can find 2 non-opposite corners in $$$2n^2$$$ queries, thus solving the problem in $$$3n^2$$$ queries
Corner Case: If the first random picked point is at the corner. (max distance = $$$2N-2$$$) then we can use this arbitrary point as the corner, and proceed with the editorial solution, would cost $$$2n^2 + n$$$ queries
Submission: 354860434
Consider when the "arbitrary point" from the start is a corner itself, so the distance of every "candidates" to the "arbitrary point" will be $$$n-1$$$, which is indistinguishable.
Update: Yes, I implemented this approach and failed miserably.
If initial point is corner, then we can completely skip step 2 (since we have all distances from a corner) and proceed with the normal solution
Thanks for the contest. Though it went pretty bad for me, I learnt a lot.
I was stuck on E while trying to find a construction (I have no idea why I was trying to construct a solution) but anyways, I reached a way to construct a solution that I want to share.
Let $$$sumX$$$ be the sum of array $$$x$$$ for any array $$$x$$$. It is trivial that there is no solution if $$$sumP \gt x + y$$$. Otherwise, we know that $$$sumP \leq x + y$$$. Now, consider the following approach of iterating over the string if $$$s_i$$$ is equal to $$$1$$$ put $$$b_i = p_i$$$, otherwise put $$$a_i = p_i$$$. Now, we are sure that the second and third conditions are satisfied. We will try to satisfy the first one without messing with the other two.
The main observation is that since $$$sumP \leq x + y$$$, we have atmost one correct codition of $$$sumA \gt x$$$ or $$$sumB \gt y$$$ (since $$$sumA + sumB = sumP$$$ and $$$sumP \leq x + y$$$). Let us focus on one of them and the other one will be solved similarly because of the similarity. Let $$$sumA \gt x$$$. We will try to fix it while maintating $$$sumB \leq y$$$. Now, we can check all indices where $$$s_i$$$ is equal to $$$0$$$ and we know that $$$a_i \gt b_i$$$. So, we can reduce the value of $$$a_i$$$ and add the difference to $$$b_i$$$ while still not reaching $$$a_i$$$ and $$$sumA \geq x$$$. (you can ask yourself now if this can violate the condition of $$$sumB \leq y$$$ but the answer is no because we know that $$$sumA + sumB = sumP$$$). If we can not fix the condition $$$sumA \gt x$$$ using this approach then we will never can.
If the condition is fixed so we now have $$$sumA \leq x$$$ and $$$sumB \leq y$$$. The last part of the algorithm is easy by trying to add the difference anywhere in both arrays and it is easy to see that this will not violate any of the three conditions.
Submission: 354831958
Brother, our ways of thinking are very similar, but I keep using WA2.354843307
Let b[0] = 4 and a[1] = 11 and it works
Thank you so much for your help. I have found a minor error in my code.
Same construction but I had to burn my brain while stress testing for over half an hour to get AC
Is there a typo in Hint 2 of Problem D? It says $$$p_i = 2^n-i+1$$$, but shouldn't it be $$$p_i = 2^n-i-1$$$?
Fixed
Thanks for the contest and i really liked the problems..Need to work more on my number theory
Solution to challenge of G.:
First we ask $$$\mathrm{dist}(1, i)$$$ for all $$$2 \leq i \leq n^2$$$.
If $$$1$$$ is on the corner, then some $$$\mathrm{dist}(1, i)=2n-2$$$ and we do not need to recalculate distance, so we can just find the other corner by brute force. This takes time $$$2n^2 + n$$$.
If $$$1$$$ is not on the corner, then after asking all distances from one corner $$$x$$$, the distance of at least one corner $$$c$$$ on the diagonal $$$d$$$ with $$$\mathrm{dist}(d_i, x) = n - 1$$$ must be maximum to $$$1$$$, and any other cell on the diagonal can't reach the maximum. So we can find this corner without need of queries. This takes time $$$3n^2$$$.
So the total complxity is $$$\min(2n^2 + n, 3n^2) = 3n^2$$$.
Code: 354857075
P.S. I wonder if this problem can be solved by $$$2n^2 + o(n^2)$$$ queries.
I believe there exists a solution with max 2.5 n^2 queries (there might exist an edge case i didnt consider):
You start with your initial point 1 and do n^2 queries dist(1,i), then find a Corner x and do our n^2 queries from there. By your construction we can already find another Corner y from this. Then, by the distances from 1 and x, we can determine pairs (i,j) that are on known positions ("The intersections of the circles"), but we dont know which of the two is at which position. For each pair we determine one of the points positions by asking its distance from y. From this we can determine both positions of the pair then.
There can be many intersections instead of $$$2$$$. Please note that the distance used in this problem is Manhattan distance.
sorry, wrong approach
The parity only helps you when finding the corner point. However to answer the exact position you actually need something like the distance from two corners to every other grids, so optimization in finding corners seems doesn't work.
I think the bottleneck is the first $$$n^2$$$ queries, as some worst case is $$$1$$$ on the grid $$$(1, 2)$$$ where we will lose tons of information in first $$$2n^2$$$ queries following the strategies above, causing that we have to make another $$$O(n^2)$$$ queries.
you are right, thanks
D can also be implemented by sorting the binary strings of the numbers from 0 to 2^n — 1 by the follwoing rules
if we compare string a & string b
the string with more continuous 1s in the suffix comes first
if both string have the same, then the string whose decimal value is smaller comes first (for lexi-min).
so 2^n — 1, has all 1s, comes first always.
Consider two numbers say 10111 and 00111 both these strings have 3 continuous 1s inn the suffix, we pick 00111 because (7 < 23).
This ensures we maintain max popcount for as long as possible in the prefixes of the resulting permutation.
Submission: 354826764
same solution gg
not that im complaining but, why are there so many communication/interactive problems in codeforces rounds nowadays? are they untouchable for AI or is it just that they're more interesting?
personally they're much more interesting
Nowadays, AI can solve interactive and communication too.
I tested Problem F on DeepSeek and it can solve the problem.
that's unusual, i tried some CSES interactive problems with free deepseek a month ago and it was stumped
Ok initially I thought H would be hard to implement, but the code was surprisingly short
I accidentally got $$$3n^2$$$ + $$$n/2$$$ queries for G
Another approach for D, There was a pattern hidden in it. first number = (1<<n)-1, hence the adder here is (1<<(n+2)), add this number and insert this into the answer array this it reaches or exceeds (1<<n). Next number = (1<<n-1)-1, repeat the process. Stop when the answer array size is (1<<n). 354825334
I came up with another approach for H:
Let lsb(x) be the position of the least significant bit in x. It is possible to see that lsb(x-y) (with x > y) is simply the first position at which x and y differ.
The answer for plant i is the sum of (i-l+1) * (1<<lsb(i-l+1)) for each interval covering i, which can be computed this way: (i+1) * sum(1<<lsb(i-l+1))-sum(l * lsb(i-l+1)). (sum(f(l)) is the sum of f(l) over all intervals covering i).
Note that i+1 > l so we can use what's written above, then for each interval covering i we only need to know the first position at which it differs from (i+1) in order to compute lsb(i+1-l).
This can be done using a trie. In each node we need to store the number of active intervals having next bit 0,1 and the sum of l for all active intervals having next bit 0,1. This way, when computing the answer for i, we can visit the trie following the nodes based on i+1 and keep track of sum(1<<lsb(i-l+1)) and sum(l*lsb(i-l+1)).
Thanks for the approach.
Code for this approach:361336544
I spent a long time trying to understand the statement of problem A, and eventually gave up. Seeing how many people solved it in a very short time, I guessed that it was probably just a simple formula. I inferred an answer from the samples, submitted it, and surprisingly got AC.
Problem B was very straightforward, nothing much to say.
Problem C had very good samples that gave a lot of hints, and I more or less wrote the solution directly based on them.
Problem D was the most interesting one for me — the difficulty felt just right, and after figuring it out, it didn’t feel that boring like others.
Problem E, on the other hand, felt quite uninteresting. I missed one corner case that should output NO, so I didn’t pass it. After reading the editorial, I’m even more convinced that this problem wasn’t very enjoyable.
Problems A to E took all of my time, so I didn’t get a chance to look at the more interesting interactive problems later.
As a non-native English speaker, I somewhat dislike the forced and awkward character background stories in the statements. To me, they are purely distracting. Also, there’s no need to try to make all problems look like parts of a single story — they really aren’t.
Could someone explain D's solution to me? I understand up to hint 2, but I think it is a little ambiguous on what "i" is. Additionally, the python implementation seems like it doesn't follow the above solution consistently.
In particular, what does "[fixing] the i lowest-value bits" mean?
Sorry for the duplicate variable use. $$$i$$$ in hint 2 is the index in the permutation. $$$i$$$ in hint 3 is the prefix AND popcount. Fixing the $$$i$$$ lowest bits refer to how the last $$$i$$$ bits are all 1.
Oh, I think I understand now. Thank you!
I don't know what happened to me in this game, but I couldn't even solve the D task because of stupid mistakes. But the round was interesting.
Could someone please tell me why my solution is wrong:
https://mirror.codeforces.com/contest/2179/submission/354888747
This didn't pass during the contest, and I didn't pay attention to it. Right before I went to sleep, I got an idea, and I thought that was the mistake. It made it better, sure, but it's still wrong
Binary contesttt
problem C went over me completely, B was straightforward....
for problem C, in the sorted array, if a[1]=1, the the answer should be a[2] isnt it ? please correct me if I am wrong Edit-got the mistake, was confued bw modding and divind to make all equal :(((
if the answer is a[2], you can't find x that a[2] mod x == 1
Bit difficult according to DIV3
In problem C, there is much intresting thing i have found is that the pattern i see in the sample testcases which are basically sort the array which is given in the input and take the maximum of first value and the difference between second and first value after sorting the array, i do not know why this works but i see the pattern and it works too.
yeah it is correct.
Reasoning: the answer has to be >= than the smallest element in the array(in case of equality we can make every element zero).
If the answer is greater than that , then all elements have to become equal to the smallest element after all the operations are done. Now, lets assume x is the smallest element and y is some element bigger than x, then the condition for y to become x is : y = (k)(q) + x where k>x. This k can be maximized when q is minimum i.e. q=1 (q!=0 bcz all elements are distinct) which yields k = y-x.
Now , this value of y-x is smallest when y is the second smallest element in the array.
There must be something wrong with the prob c!!i couldn't even pass the sample in the contest, then i tried my thought then it turned out to be right, that's infrustrating!!!
wait what contest was yesterday and it is showing me that editorial was uploaded 2 days ago! is this only for me?
The contest editorial is written before the contest but not published.
oh okay got it
In problem-E
if one of the party run out of votes , for example say party-A we can dump some voters from party-A to party-B (only where s[i] = '1') we can check if we have enough ones to dump enough voters for A to B such that sumA become equal to x
now for any remaining y, we can arbitrarily increase voters for B, if number of ones > 0 in S
-- same can be said for party-B running out of votes -- if both party run out of votes, x + y — (sumA + sumB) becomes negative , then ans is not possible even with dumping logic
i would like to know , where i am wrong, this problem ruined my contest 354850547
Hello everyone. Thank you for editorial!
my first contest! i'm 411 now yay!
I think for the problem F, the solution will work for non-bipartite graphs as well. Can someone give me a counter-example where it fails?
There is a typo in the editorial for G, the "solving the system of equations" shows the wrong final equations.
Fixed
H can be solved with a square root algorithm by directly simulating larger powers of 2 and using dynamic programming on smaller values. https://mirror.codeforces.com/contest/2179/submission/354987709
This is a really nice alternative method. I found it much easier to understand than the editorial; thanks for sharing!
very mathematical contest liked it
Guys, for problem E, why
is the lower bound?
For each district, the winning party must have more than half of the total votes.
oh, the winning one, got it, thanks
Can anyone please explain problem D's solution in a simpler way? Especially the given implementa.tion.
It would be really helpful to me if someone can explain Problem E Solution because i am not able to understand it properly, the winning condition i got but is it necessary to have like that pi/2 + 1 . and also how are we going to fill the remaining votes and what will be the approach to do that
i couldnt understand problem H
problem D I think hint 2 is wrong ?
fixed
In problem F, why did we restrict ourself to biparate? I don't know for sure but I solved using some approach thats similar maybe but I feel it will work for non-biparate as well..
Edit: Sorry I got it!
It is to avoid the case where two neighbouring nodes have the same color hence avoiding conflict among choices
Problem H (Challenge):
Solve the modified problem where you're given a parameter $$$k$$$ and instead of $$$x\cdot \text{lsb}(x)$$$ you do $$$x\cdot k^v$$$ with $$$v=\max_{i\geq 0}{i\cdot[k^i | x]}$$$.
For a clean implementation, note that iterating the unfixed bits is basically iterating even numbers.
can you please explain this a little bit more as I'm not getting your Implementation.
So, we will fix $$$i$$$ set suffix bits, then iterate the $$$n-i$$$ prefix bits. When iterating the prefixes from lexicographical minimum, you would be iterating all nonnegative integers with $$$n-i$$$ bits from small to large. But, the last bit of the $$$n-i$$$ prefix bits must be 0, as otherwise we’d have $$$ \gt i$$$ set bits, so we’re iterating over only the even integers.
Ok thanks I got it, I will also add my explanation to help people in the future.
Let's say that $$$n = 7$$$, and the current $$$i$$$ that we fixed is 3.
Which means that we have something like this:
_ _ _ _ 1 1 1Here we fixed the 3 bits on the right, and we want to iterate and get all the numbers that have these exact three bits on the right. Instead of going through all the possible numbers and checking which one has these 3 fixed bits, we can iterate over all numbers only up to $$$2^{n-i}$$$ and left shift each one of them by $$$i$$$ and then add $$$2^i - 1$$$ to it.
But we don't want to have something like
x x x 1 1 1 1where the 4th bit is 1 because we already dealt with this case when $$$i$$$ was 4, so if we only use even numbers, we guarantee that this 4th bit will be 0.