We hope you enjoyed the contest! Thank you for participating! This is our first official round on Codeforces, so we would be happy to hear your feedback in the comments and in the mini-survey below.
Идея: EzikBro
Prove that the number $$$2$$$ is not an ideal generator. What other numbers are not ideal generators for the same reason?
Prove that the number $$$3$$$ is an ideal generator. What is the difference from the previous case?
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
if (n % 2 == 1) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
return 0;
}
Идея: gravitsapa
What is the minimum cost that can be achieved?
How many non-zero digits should be in the remaining number for the cost to be minimal?
How can we ensure that the number has as many leading zeros as possible?
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
int n = s.size();
bool met_positive = false;
int cnt_zero = 0;
for (auto i = n - 1; i >= 0; --i) {
if (s[i] != '0') {
met_positive = true;
} else if (met_positive) {
cnt_zero++;
}
}
cout << n - (cnt_zero + 1) << '\n';
}
return 0;
}
Идея: pskobx
$$$y$$$ is always divisible by $$$x$$$.
What happens if $$$k \geq 2$$$?
If $$$k \geq 2$$$ and $$$x \geq 2$$$, then $$$y$$$ will never be prime. Are there any edge cases with $$$k = 1$$$ or $$$x = 1$$$?
#include <bits/stdc++.h>
using namespace std;
bool is_prime(int x) {
if (x <= 1) {
return false;
}
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
void solve() {
int x, k;
cin >> x >> k;
if (k > 1 && x > 1) {
cout << "NO";
} else if (k == 1) {
cout << (is_prime(x) ? "YES" : "NO");
} else {
cout << ((k == 2) ? "YES" : "NO");
}
}
int main() {
int tests;
cin >> tests;
while (tests--) {
solve();
cout << '\n';
}
}
Идея: fstilus
How does the answer change when $$$n$$$ is decreased?
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, q;
cin >> n >> q;
while (q--) {
string type;
cin >> type;
if (type == "->") {
int x, y;
cin >> x >> y;
x--, y--;
long long num = 0;
for (int i = n - 1; i >= 0; --i) {
int cur = 1 << i;
if (!(x & cur) && !(y & cur))
num ^= 0ll << (2 * i);
if ((x & cur) && (y & cur))
num ^= 1ll << (2 * i);
if ((x & cur) && !(y & cur))
num ^= 2ll << (2 * i);
if (!(x & cur) && (y & cur))
num ^= 3ll << (2 * i);
}
cout << num + 1 << '\n';
}
else {
long long num;
cin >> num;
num--;
int x = 0, y = 0;
for (int i = n - 1; i >= 0; --i) {
long long cur = 3ll << (2 * i);
if ((num & cur) >> (2 * i) == 0)
x ^= 0 << i, y ^= 0 << i;
if ((num & cur) >> (2 * i) == 1)
x ^= 1 << i, y ^= 1 << i;
if ((num & cur) >> (2 * i) == 2)
x ^= 1 << i, y ^= 0 << i;
if ((num & cur) >> (2 * i) == 3)
x ^= 0 << i, y ^= 1 << i;
}
cout << x + 1 << ' ' << y + 1 << '\n';
}
}
}
return 0;
}
Идея: Boodoochai
How to check if a solution exists for a fixed $$$x$$$?
We can use binary search.
#include <bits/stdc++.h>
using namespace std;
vector<int> nums(2e5 + 5, 0);
bool check(vector<int>& v, int k, int m)
{
int cnt = 0;
int cur_mex = 0;
for (int i = 0; i < v.size(); i++) {
if (v[i] <= v.size() + 1) {
nums[v[i]] = 1;
}
while (nums[cur_mex]) {
cur_mex++;
}
if (cur_mex >= m) {
cnt++;
for (int j = 0; j < min(m + 1, (int)v.size() + 2); j++) {
nums[j] = 0;
}
cur_mex = 0;
}
}
for (int j = 0; j < v.size() + 2; j++) {
nums[j] = 0;
}
return cnt >= k;
}
void solve()
{
int n, k;
cin >> n >> k;
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
int l = 0;
int r = 1e9;
while (r - l > 1) {
int m = (r + l) / 2;
if (check(v, k, m)) {
l = m;
} else {
r = m;
}
}
cout << l << '\n';
}
signed main()
{
int t;
cin >> t;
for (int i = 0; i < t; i++) {
solve();
}
return 0;
}
2093F - Hackers and Neural Networks
Идея: _icy_
We need to ensure that we construct the array $$$a$$$. Therefore, when interacting with the neural network, we should consider the worst-case scenario.
Read the previous hint. How many operations of the first type applied to the $$$i$$$-th neural network do we need to obtain at least one correct word (i.e., one such that $$$b_{i, j} = a_j$$$)?
If we have obtained at least one correct word from the neural network, then, since we are considering the worst-case scenario, it has already placed all incorrect words in the array $$$c$$$.
Read the previous hint and its answer. Which neural network is optimal to start with? How many blanks will there be in the array $$$c$$$ after interacting with this neural network?
It is optimal to start with the neural network that has the most correct words. There will be no blanks in the array $$$c$$$.
There are currently no blanks in the array $$$c$$$, but there are incorrect words in some positions. How many operations are needed to replace one incorrect word with a correct one?
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m;
cin >> n >> m;
vector<string> a(n);
for (auto &i: a) {
cin >> i;
}
vector<vector<string>> b(m, vector<string>(n));
for (auto &ar: b) {
for (auto &s: ar) {
cin >> s;
}
}
vector<bool> exists(n);
int x = 0;
for (int i = 0; i < m; i++) {
int cnt = 0;
for (int j = 0; j < n; j++) {
if (a[j] == b[i][j]) {
cnt++;
exists[j] = true;
}
}
x = max(x, cnt);
}
if (all_of(exists.begin(), exists.end(), identity())) {
cout << n + 2 * (n - x) << "\n";
} else {
cout << "-1\n";
}
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Идея: EzikBro
Let the shortest segment have boundaries $$$l$$$ and $$$r$$$. What can be said about the indices $$$i$$$ and $$$j$$$ for which the maximum value of the expression $$$a_i \oplus a_j$$$ is achieved?
The operation $$$x \oplus y$$$ on int32 numbers can be represented as the XOR of two binary strings of length $$$32$$$. What can be said about the comparison of numbers? In what case does the expression $$$x \oplus y = z$$$ hold? And $$$x \oplus y \gt z$$$?
How can we use a trie on binary strings to find the nearest index $$$j$$$ such that $$$a_i \oplus a_j = k$$$?
#include <bits/stdc++.h>
using namespace std;
const int LOG_X = 29;
struct node {
int children[2] { -1, -1 };
int last = -1;
};
int find(const vector<node>& trie, int value, int border) {
int res = -1;
int current = 0;
bool ok = true;
for (int position = LOG_X; ok && position >= 0; position--) {
int x_bit = (value >> position) & 1;
int k_bit = (border >> position) & 1;
auto& children = trie[current].children;
if (k_bit == 1) {
if (children[x_bit ^ 1] != -1) {
current = children[x_bit ^ 1];
} else {
ok = false;
}
} else {
if (children[x_bit ^ 1] != -1) {
res = max(res, trie[children[x_bit ^ 1]].last);
}
if (children[x_bit] != -1) {
current = children[x_bit];
} else {
ok = false;
}
}
}
if (ok) {
res = max(res, trie[current].last);
}
return res;
}
void add(vector<node>& trie, int value, int index) {
int current = 0;
trie[current].last = max(trie[current].last, index);
for (int position = LOG_X; position >= 0; position--) {
int x_bit = (value >> position) & 1;
if (trie[current].children[x_bit] == -1) {
trie[current].children[x_bit] = trie.size();
trie.push_back(node());
}
current = trie[current].children[x_bit];
trie[current].last = max(trie[current].last, index);
}
}
void solve() {
int n, k;
cin >> n >> k;
int ans = n + 1;
vector<node> trie(1);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
add(trie, x, i);
int y = find(trie, x, k);
if (y != -1) {
ans = min(ans, i - y + 1);
}
}
if (ans == n + 1) {
cout << "-1\n";
} else {
cout << ans << '\n';
}
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}








Are we just gonna ignore the hacks on E on n(logn)² solutions?
Sadly
314556699 mine passes
But why is it not working as they have mentioned that the sum of n across all test cases does not exceed 2⋅105 . So, n(logn)2 ~ 6⋅107.
Mine passed with $$$O(n\times log^2(n))$$$, the set solution does not pass due to big constant factor.
I think you mean $$$6 \cdot 10^7$$$, but note that
set's constant factor is extremely huge.Is it the same for std::map as well?
They should be almost identical.
map is probably twice slower
Not really. The slow part of
setandmapis to manage the elements (which is the element itself forset, and the key formap) and they work the same way for both. The value ofmapis only an additional element attached to each node of the binary tree, which should take only trivial time.But my submission with std::map receive TLE,with std::set receive AC
Your AC submission took 1968 ms, which is just 32 ms away from TLE. Execution time is unstable, so the same code can sometimes get just 1968 ms but far exceed 2000 ms, or just take 1800 ms depending on the server's condition.
There might be a tiny difference, but it should be really small.
You are right.During the writing of the code I was very confused about O(nlog2n) could AC. After submission I reacted that it would be better to use vector .But when I got Ac,I didn't pay attention to it again,I fst while some of my friend got ac with set,that would be a shame for me.
Mine passed within 1999ms, lol.
Can you share some resources to learn more about
setandunordered_set. I want to know how they work internally and understand the terms like "constant factor" and "collisions" like you mentioned.I don't specifically have good resources, but I can provide you some basic ideas and keywords to search for.
setis typically (at least in GCC) implemented as Red–black tree. This is needed to ensure that every insertion / deletion / search takes $$$\mathcal{O}(\log{n})$$$ time no matter what operations are performed with any order. However, this effort to guarantee the time complexity involves "tree rotation" which changes the structure of the tree by moving around some nodes, and this means it has to deal with a lot of pointers and many random accesses happen, so these operations are "heavy" — in other words, its constant factor is huge.unordered_setis a simple hash table and in GCC it uses remainder of the keys as the index, it is very prone to hash collision without proper adjustment to the hash functions. You many want to read this and this to see how they are attacked.bro, could you please tell me why is my solution 338262399 for 2093G - Shorten the Array getting TLE or is there any way to improve it
mine passes , i used python sets
Yes python is running in 1/3 rd time even by using dictionary
Because Python's
setanddictroughly correspond to C++'sunordered_setandunordered_map, notsetandmap.For problem
F, can someone rewrite the problem statement in a simpler way?You can perform two kinds of operations. Operation 2, i.e, replace any index (1 to n) with a blank is a definite operation, the hackers can choose the index to turn into a blank. However, for operation 1, you can only choose a neural network (1 to m), and any random BLANK index converts to its corresponding string.
So, we first remove the randomness by choosing the vector with most commonality (comm) with target. So, we definitely have the correct elements in comm positions, if we choose the neural network n times, and definitely the wrong elements in n-com positions. Now, we use 2 operations per (n-com) positions to convert to blank, and then fill it with a string of our choice. Remember, its only random if there are multiple blanks.
Thanks, much appreciated.
D. In the fourth possible position, isn’t it y > 2^(n-1) instead of <?
Fixed
bro on E, I did an n*log(n) solution and it got TLE, like tf??? 314610668, (in the contest it pased but in system testing it got TLE on test 32)
don't use unordered_map, it can be hack to $$$O(n)$$$
also if you use map you will get TLE on test 16(guess why I know it!), in this problem you should use an array and only process the number which <= n
I first tried using ordered set during virtal contest for tracking missing numbers, which was supposed to take an O(n log n) check, for overall time O(n*(log n)^2), but then I used bool array like the editorial, but only reached test 26.
As I know, $$$O(n\log^2n)$$$ algorithm can't pass this problem because map/set is VERY SLOW
UPD: I check your code and found your rollback of array
doneis incorrect, you should only rollback the numbers you used(you can use a vector to storage the numbers that you used)Now, I tried the bool array like the editorial, passed only a LITTLE more tests, but still FAILED on test 26.
vector<bool> done(n+1, false);done[min(n+1, a[i])] = true;out of bounds
also, never use
vector<bool>. This may lead to some unexpected errors like:See the solution that failed test 26 which also uses a bool array. Can you give me a little modified version of my code that gets AC pls. One solution is still getting tested, but I pray that it gets through test 26 at least, if not AC.
seems the newest version is right(I am not sure)
AC. OMG! I'm so excited. Thank YOU SO MUCH for your efforts to help ME solve this HUGE persistent ISSUE. I love you.
You are too welcome :)
We'll give a huge shoutout to SYCu for the efforts of getting through huge roadblocks, analyzing every single line, and requesting changes to help me get AC. Real appreciation, helpful and good. So MANY TLE's later, YOU DID NOT GIVE UP! It took TWELVE submissions, just for ONE, SINGLE, problem.
Nah, my code passed with O(N * (log(N) ^ 2)) time complexity
The run-time is 1999 milliseconds
i think if we use unordered map and only process for numbers less than n it will still work though i am not certain about how collisions work in unordered map
While practicing on Codeforces, I usually prefer set/map over unordered_set/unordered_map, since they tend to perform better when there are many duplicates. But in this contest's E, although my solution got accepted during the contest, it got TLE on test 67 during system testing, possibly due to extra cases added during hacks. Surprisingly, after replacing set with unordered_set, it passed. I'm confused because I thought set/map handled duplicates better, so I'm not sure why unordered_set worked here. Could you help me please!
Unordered set/map is faster than ordered because of being unsorted. The reason they get TLE is due to collisions in hash value. This happens on very specific prime numbers. Since the constraints of the problem were low, it was very hard to produce such collisions. This is why unordered passed. A suggestion is that whenever you use unordered map/set, use a custom hash to avoid collisions
Using unordered_map can make you TLE. While you can access in O(1), the rehash when collapse cost O(n)
Careful $$$O(n\log^2n)$$$ implemetation is about 10x slower that vector but passes easily: 314616665. It doesn't matter ordered or unordered set, because inserted elements are fixed 0..x and cannot be crafted to break hash. Proof in cpp: 314732121
yes, the complexity may increase in some test cases in maps and sets , so what we can do here is , we can keep a check alternatively
if(mid>2e5+1) return 0; (because any how no more than 2e5 elements can be there so we can eliminate large test cases(passed for me
I think the editorial is not in English.Anyone facing the same issue?
Fixed. Thanks!
Can anyone help me out why my solution failed on E, isnt the time complexity is $$$O(n (\log n)^2)$$$ why would it fail? link
Also the editorial is not in english, please do the needful. Kogut_Ivan
https://en.cppreference.com/w/cpp/container/set/clear : Linear in the size of the container, i.e., the number of elements.
So worst case complexity is when $$$k=\sqrt n$$$, $$$O(n^{3/2}\log n)$$$
thanks, much appreciated.
Strange that it fails on test with k=1, but without clear its ok: 314738392. And unordered set is noticeably faster 314738680
I don't know what to say. After 1h30min, I was close to a +100 delta. But F felt overwhelming due to the problem statement, and I couldn't solve G despite giving it my all. Now, having FSTed E, I just feel sad. I'll look into it, thanks a lot for your time and effort, it really means a lot.
This was one of the best Div3 experiences I've had, even though the outcome wasn't in my favor.
Can you please rejudge my solution for E, as some people's code is rejudged again after getting TLE initially, so please rejudge mine as I was at 500 rank before testing. Plsss!!!!
Example: https://ibb.co/jvZTtsT8 (Accepted) https://ibb.co/v44cYddP (TLE)
Pls help Kogut_Ivan
Even code of the user whose initially got TLE and later got accepted (whoami793389) is using n*logn*logn approach.
Strange round, difficult D but easy enough E. Still thanks for the tasks and the new experience
What is wrong in this submission ??
How did i get TLE? my submission: https://mirror.codeforces.com/contest/2093/submission/314619983
Can someone try uphacking my E? It passes in 1999ms
Does E have any $$$O(N)$$$ solutions ? I thought about some greedy picking but the case
$$$n = 10$$$, $$$k = 3$$$
$$$a = 1, 0, 0, 1, 1, 1, 0, 0, 0, 2$$$
is a good counterexample.
Should we overlook the innovative approaches applied to E within n(log n)² solutions? These techniques merit a closer examination for their potential to optimize performance and address complex computational challenges effectively.
Just disappointed with the E verdict even after writing the solution for O(n log^2 n) time complexity.
As a rule of thumb, if you can optimize better always do, if you can use a visited array or a frequency array over set/map, always do.
Bad Problem Statement for F
For problem E, anyone who gets TLE because of using the unordered map could try using a custom hash. This code comes from here.
I got FST on G with this lol
Oh so gp hash table would be faster in anyway?
Yeah i guess
Are you sure you didn't reverse D-E-F?
D. what number will be in the cell at the x-th row and y-th column;
x-th row and y-th column? blasphemy.
swap D and F please i started from F and then did E then moved to D and was flabbergasted and left the contest (because i was hungry also).
Can someone help me with my submission 314773349 for D ?
Hi, can anyone explain me why my code get TLE. Its $$$O(nlog(n))$$$ 314779695 not even $$$nlog^2$$$
your 'r' is 1e9, try with r=n. (I too got TLE.. T_T)
so nlog(1e9) vs nlog(2*1e5)
Actually $$$r=n$$$ also can't pass the problem, please look my submission 314735689
hm, hey, isn't s.count(x) is logn? ig your code is not nlogn? its n(logn)^2
you can see my code, I used unordered_set,s.size() and "r=n" 314734844
:: okay he used set, I didn't notice earlier
Actually your solution is $$$O(n\log a\log n)$$$ not $$$O(n\log n)$$$.
Binary search $$$O(\log a)$$$, one check $$$O(n\log n)$$$ because of set.
for G you can also do two pointers on a Trie
Could you explain your approach?
hopefully this explanation isn't so horrible:
denote a set $$$S$$$, notice that $$$\max(a \circ b)$$$ for two elements $$$a, b \in S$$$ is increasing when we add elements to $$$S$$$ and decreasing when we remove elements.
also given a set $$$S$$$ you can use a Trie to find an element $$$x \in S$$$ such that $$$a \circ x$$$ is maximized for a given $$$a$$$.
this enables you to do two pointers, you move the right pointer if the greedy algorithm stated above returns $$$x$$$ such that $$$s_r \circ x \lt k$$$ and insert $$$s_r$$$ into to the Trie, else we remove $$$s_l$$$ and move the left pointer, the complexity sums up to $$$O(n \lg max(a_i))$$$ or $$$O(32 \cdot n)$$$
not my code: 314691768
isn't it O(n * 32), not O(nlogn)?
getting TLE in test case 4 in problem D with this recursive solution....can anyone help me
I looked into your code. The main issue was in how you calculated the quadrants—your offset logic wasn’t dividing the matrix correctly, which led to unnecessary recursive calls. Heres the fixed solution : 314840363.
P.S. Try to use long long for every variable where large values are expected. Otherwise, always make sure to check whether a sum or product could exceed the int limit. Even if your logic was correct, you would have still received a penalty for calculating
total = len * len, sincelenwas an int.There's yet another "solution" for G: 314828750.
The last 2 trie problems in Div3 mentioned AVX in their editorial, so I had to mention it again...
Do you know why my submission got FST? does the custom hash have such a big constant? 314614738
I have replaced your
unordered_mapwithgp_hash_tableand it got an AC: 314839995.Tysm, should I always use gp hash table instead of unordered map?
I think so, when
gp_hash_tabledidnt get an AC, I usually usecc_hash_tableinstead. Both of these are faster thanunordered_map.This happened because SO many solutions used an unordered_map or unordered_set and they are very good targets to hack because normally they take $$$O(1)$$$ but in the hash collisions, they can take $$$O(n)$$$. So many people tried to hack these solutions and got successful, so AC solutions during the contest get TLE's when retested after hacks, even when your solution was not targetted. This is extremely disappointing for so many contestants who passed the pretests but after the hacking phase, boom! So many TLE's you can't imagine. I'm not lying, just go to HACKS then Successful hack. You will so many TLE hacks on just problem E. I first tried using normal set during a virtual contest for safety with bin-search 314714290, which was supposed to take $$$O(nlog^2n)$$$ and that is around $$$7 \times 10^7$$$ operations and pass, but I got TLE sadly. Then, I tried bool array like the editorial and after some time, I finally cracked it in $$$O(n log n)$$$ time in just 140 ms 314722830. I think you guys should use unordered maps or sets when and only when you are very sure about the hash collisions (even if pretests pass) and also, for safety, have a normal map or set solution, which are very unlikely to get hacked, unless the $$$log n$$$ factor is really bothering.
D is a great problem to learn recursion better.
I solved it iteratively lol.
I am having trouble finding the case of TLE in my code for problem G. Could anyone help me out?
314875046
I sorted the issue. Got to know that using objects(pointers) instead of flatenning the trie was the issue. Here's the corrected code if anyone wishes to see:
My O(n lg n) solution is failing, is it due to my CP template?
Editorial for question C says "It is also possible to precompute prime numbers" but since the X was up to 10^9, precomputing and storing 10^9 values isn't possible, so how can that be done?
Maybe "precompute" is not the best word. They mean to manually check the repunit numbers (i.e. $$$1, 11,\ldots, 111111111$$$) and hardcode the solution.
Fun fact, after $$$11$$$, the next repunit prime has 19 ones: https://oeis.org/A004023
You dont need to use Sieve of Eratosthenes, see here Sieve of Eratosthenes is used to find all the primes in a certain range but in this question you dont need all the primes in a range instead what you need is simply to find a certain number is prime or not
for that you can iterate all the divisors till square root of the number and check if there is a divisor other than 1 and the number itself
Here you can check this code
here lets say your number is n so i am just checking if a number greater than 1 can devide n.
I really liked the contest, solved five problems by myself in the contest time, and after that learned new ideas and solved the rest of the problems by peeking into the video tutorial (learned about a trie in G and that you can use the binary search method to find the value in question in E). So for me the contest was very educational.
But I'm writing this post to express my unpopular among these comments opinion about problems D and F. For me these problems were exactly what I've always liked about the contests in the past — when the problem can be solved just by coming up with an idea and clear coding instead of pasting one of the miriad data structures or reusing ideas from a similar problem that you've seen before. I like it when you don't need to have such specific knowledge in order to solve a problem.
So I personally would like to see more problems like D and F, even though I can see that a lot of people had trouble with them for some reason.
Why is my solution of F with persistent trie TLE 314962660? but if I use another binary search method, it got ac 314958098. it seems that, for any test case, the two kinds of binary search call
queryfunction the same times? it's weird, can anyone tell me why my first binary seach is bad.why am i getting TLE on test case 26 for problem E . while the logic of my code is similar to that of editorial. Anyone help please .
my submission : 315084707
any suggestions please.
Instead of min(m+1, n+1), only unmarked those elements you just traversed. Here's your correct version of code: 315233613
If you’re getting
WA2 (291st numbers differ)on problem G, the bug is probably in how you pick the maximum index. You need to take the maximum over all indexes for cases where k′ᵢ = 0 and xor′ᵢ = 1.Can someone help me with this code..i am getting WA again and again 320445368
In C, if number is even then it always not a prime expect.
This is my solution for B :
Can somebody explain where am i doing it wrong ?
E submission my solution is based on counts of a and b . and after seeing others submission i feel i think too question oriented not in a general way.
how to improve that???
Why is my solution got wrong for problem G, I have taken all the case