And here is the Editorial!
We hope you liked the problems. Thanks everyone!
\[^-^]/
Don't forget to praise the sun!
Tutorial
Tutorial is loading...
Solution
#include <iostream>
using namespace std;
void solve() {
int n;
cin >> n;
int cnt[10] = {};
bool f = 0;
for (int i = 0; i < n; i++) {
int dig;
cin >> dig;
cnt[dig]++;
if (cnt[0] >= 3 && cnt[1] >= 1 && cnt[2] >= 2 &&
cnt[3] >= 1 && cnt[5] >= 1 && !f) {
cout << i + 1 << endl;
f = 1;
}
}
if (!f) cout << 0 << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
}
Tutorial
Tutorial is loading...
Solution
#include <iostream>
#include <algorithm>
using namespace std;
void solve() {
int n, x;
cin >> n >> x;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
reverse(a, a + n);
int ans = 0;
for (int i = 0, cnt = 1; i < n; i++, cnt++) {
if (a[i] * cnt >= x) {
ans++;
cnt = 0;
}
}
cout << ans << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
}
Tutorial
Tutorial is loading...
Solution
#include <iostream>
using namespace std;
void solve() {
int n;
cin >> n;
if (n % 2 == 0) {
cout << -1 << endl;
return;
}
for (int i = n; i > 0; i--) {
cout << i << ' ';
}
cout << endl;
}
int main() {
int t = 1;
cin >> t;
while (t--)
solve();
}
Tutorial
Tutorial is loading...
Solution
#include <iostream>
using namespace std;
void solve() {
long long n, m, k, l, r, mid;
cin >> n >> m >> k;
l = 0, r = m;
while (l + 1 < r) {
mid = (l + r) / 2;
if ((m / (mid + 1) * mid + m % (mid + 1)) * n >= k) {
r = mid;
} else {
l = mid;
}
}
cout << r << endl;
}
int main() {
int t = 1;
cin >> t;
while (t--) {
solve();
}
}
Tutorial
Tutorial is loading...
Solution
#include <iostream>
using namespace std;
const int MAXN = 10000001;
bool prime[MAXN];
void solve() {
int n, ans = 0;
cin >> n;
for (int i = 2; i <= n; i++) {
if (prime[i]) {
ans += n / i;
}
}
cout << ans << endl;
}
int main() {
for (int i = 0; i < MAXN; i++) prime[i] = 1;
prime[0] = prime[1] = 0;
for (int i = 2; i * i < MAXN; i++) {
if (!prime[i]) continue;
for (int j = i * i; j < MAXN; j += i)
prime[j] = 0;
}
int t;
cin >> t;
while (t--) {
solve();
}
}
Tutorial
Tutorial is loading...
Solution
#include <iostream>
using namespace std;
const int MAXN = 2010;
const int MOD = 998244353;
string s[MAXN];
int dp[MAXN][MAXN][2];
long long sdp[MAXN][MAXN][2];
int n, m, d;
long long getsum(int x, int y1, int y2, int f) {
long long res = sdp[x][y2][f];
if (y1) res -= sdp[x][y1 - 1][f];
return res;
}
int get(int i, int j, int f) {
if (s[i][j] != 'X') return 0;
long long res = 0;
if (i == n - 1) res++;
if (!f) {
res += getsum(i, max(0, j - d),
min(m - 1, j + d), 1);
res -= dp[i][j][1];
}
if (i < n - 1) {
res += getsum(i + 1, max(0, j - d + 1),
min(m - 1, j + d - 1), 0);
}
return res % MOD;
}
void solve() {
cin >> n >> m >> d;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
for (int i = n - 1; i >= 0; i--) {
for (int f = 1; f >= 0; f--) {
for (int j = 0; j < m; j++) {
sdp[i][j][f] = dp[i][j][f] = get(i, j, f);
}
for (int j = 1; j < m; j++) {
sdp[i][j][f] += sdp[i][j - 1][f];
}
}
}
long long ans = 0;
for (int j = 0; j < m; j++) {
ans += dp[0][j][0];
}
cout << ans % MOD << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
}
Tutorial
Tutorial is loading...
Solution
#include <iostream>
#include <vector>
using namespace std;
int s, k, p;
vector<bool> was1, was2;
void bfs() {
vector<int> v;
for (int i = 0; i < s; i++) {
if (was1[i])
v.push_back(i);
}
int q = 0;
while (q < v.size()) {
int x = v[q++];
int y = x + p * k;
if (y >= 0 && y <= s && !was1[y]) {
was1[y] = 1;
v.push_back(y);
}
}
}
void solve() {
cin >> s >> k;
if (s % k == 0) {
cout << k << endl;
return;
}
if (s > k * k) {
cout << max(1, k - 2) << endl;
return;
}
was1.resize(s + 1);
was2.resize(s + 1);
for (int i = 0; i <= s; i++) {
was1[i] = was2[i] = 0;
}
p = 1;
was1[k] = 1;
while (1) {
bfs();
if (was1[s]) {
cout << k << endl;
return;
}
k = max(k - 1, 1);
p *= -1;
for (int i = 0; i <= s; i++) {
was2[i] = 0;
}
for (int i = 0; i < s; i++) {
if (was1[i]) {
if (i + p * k >= 0 && i + p * k <= s) {
was2[i + p * k] = 1;
}
}
}
swap(was1, was2);
}
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
}








Problem D O(1)
void solve(){
}
int main(){
ios::sync_with_stdio(false); cin.tie(0); int t=1; cin>>t; while (t--) { solve(); }}
wcnb
what's that?
"wc" means "fxxk", and "nb" means "very impressive".
I think I've found 4 Chinese users so far
so what?
How did you deduce the necessary formula?
To minimize the length of the longest bench: Distribute the k students as evenly as possible across the n rows. max = ceil(k/n) Within each row, distribute the students as evenly as possible across the m seats. It is like seperating "max" number of objects(pigeons) using "m — max" seperators, i.e "m — max + 1" holes so ans = ceil(max/m — max + 1)
wcnb
F has a nice explanation
can you please help me.
In F while selecting second hold on same row, it is done
dp[i][j][f]= dp[i][j][f]+∑j+dy=j−d dp[i][y][1]−dp[i][j][1]
But why here dp[i][y][1]? shouldn't it be dp[i][y][0]? why we take another hold if we already took 2?
If we used dp[i][y][0], it would mean we are still in the state where only one hold is selected, which contradicts the fact that we are selecting the second hold at y.
We need to transition to the state where two holds are used, which is captured by dp[i][y][1].
Praise the sun! Solaire posture
In G, I wasn't able to prove the fact where the answer will always be $$$k$$$ or $$$k - 2$$$ for $$$s \ge k^2$$$, so I am posting my solution for the same.
Main thing to note is that for each value of power $$$x$$$, we only need to consider $$$x$$$ different residues modulo $$$x$$$, and particularly for each turn around step we only need to consider the maximum $$$x$$$ values we can reach if we are travelling forwards or the minimum $$$x$$$ values if we are travelling backwards. Running complexity is $$$O(k^2)$$$ for each valid $$$k$$$ resulting in overall complexity of $$$\begin{equation} \sum_{i=1}^{k} i^2 = O\left(\frac{k(k+1)(2k+1)}{6}\right) \end{equation}$$$
Edit: My Submission — 312461704
What is the reason behind of making k <= 1000 and sum of k <= 2000 in G? I just didn't write the code because I have never wrote an n^3 algorithm for n <= 1000. And yes it has very nice constant factor and in practice works very fast. But why couldn't u make it like k <= 800, sum <= 800, any unintended solutions that pass under smaller constraints?
When it comes to this kind of question it is safe to guess the conclusion, we are almost certain that not all $$$k$$$ will be traversed, and ultimately there must be very few valuable $$$k$$$, see my submission 312426354, where we note that the run time is very short.
Edit: I think this problem may be related to the harmonic series, which has only about $$$\log{k}$$$ valid numbers.
Cool contest!
In fact there are two ways to approach the problem $$$F$$$: either by counting ways to reach the current 'X' index from the neighboring 'X' indices or vice versa, counting ways to reach the neighboring 'X' indices from the current. The first approach can be implemented with prefix sums and the second via a difference array.
Yes, if we consider where the dp state can transition to, we can use the difference array to solve it.
In problem G I just "felt" the algorithm wouldn't run slow, then I submitted the code and got AC.
Us
ok
guess~
Interesting G.
Using derivatives to prove the $$$\Theta$$$ of G may be more intuitive&simple (⑅˃◡˂⑅)
Thanks for a great contest!!!
orz
As per my current rating, of 1076, If I have solved questions till E, how much will the rating change ?
0 as your solutions will be skipped for copying from chat gpt lol
intuition might be of your own but the implementation is completely done by gpt, solve less but when you do, fully understand and implement on your own, it's just more satisfactory, also I have nothing against you so chill ^_^
Yup, thanks mate understood your point, will keep that in mind.
Someone report this guy
Problem D
Problem D O(1)
Can G problem be solved in $$$O(K^2log(K))$$$
My Solution: Submission
Consider that $$$s \lt k*k$$$
Can any one explain another approach for problem f ?
in d part for the binary search suose you wrote while(l<=r) you get TLE error ,can someone tell me the calculation why ?
you are not changing the l to mid+1 or changing r to mid-1, that is the issue. you are just writing l and r = mid.
gotcha
can anyone please explain me in problem-G; when s=10 and k=4 how is the answer 2
Some how I find C is the most difficult one among A-F, I have read the editorial, but can not I find a way to come up with solution, why people can relate to the fact that even length can not be constructed in the first place then gradually come up with the solution?
Don't forget to praise the moon!
very easy to understand (for me at least), thank you
O(k^2logk) solution for G.
For convenience, we will only consider the round of jumping to the right here. The turn that jumps to the left is symmetrical. If the current jump distance is k0, then for each modulo result of k0, we only need to record the smallest position. Because smaller positions can always jump to larger positions. Next, consider turning at each position. These positions, such as d, d+k0, and d+2k0, must form a cyclic interval in the form of k0-1. What we need to do is to take the maximum value of the cyclic interval for the arithmetic sequence. It can be achieved using a suitable data structure.
I implemented a similar solution with same complexity. 312517599
My idea was to maintain all visitable points as continuous ranges $$$[lx,rx]$$$, it can be easily shown that count of such intervals is atmost k, and after updating for every turn, atmost 1 new range (near boundary) gets added to my datastructure.
Processing K intervals for each turn (k , k-1 , ... 1) with sorting yeilds the same time complexity
If last range is of form $$$[lx , S]$$$ then this turn's power is the maximal possible answer.
Problem-F is wrong, you have not calculated the possibility of Igor climbing from level x to level x+2,x+3,....x+n directly, If Igor can't skip any levels while climbing from one level to another you should have clearly mentioned this in problem statement ;(
I think I have a proof that my solution for G (which is basically the same as the editorial solution, but with dynamic programming instead of BFS) works in $$$O(k^2)$$$ per test case.
Let $$$x$$$ be the maximum integer such that $$$s \ge x \cdot k$$$. In other words, $$$x$$$ is $$$\frac{s}{k}$$$, rounded down.
Each value of Gleb's power is processed in $$$O(x \cdot k)$$$, and I claim that after we decrease Gleb's power by $$$O(\frac{k}{x})$$$, we will find the answer. So, if this claim is true, it works in $$$O(x \cdot k \cdot \frac{k}{x}) = O(k^2)$$$.
Let's prove this claim. Initially, Gleb is at point $$$0$$$ and has power $$$k$$$. Let's consider the points where he can be after we decrease his power by $$$2$$$. If he moves $$$i$$$ times to the right, makes a U-turn, moves $$$i$$$ times to the left, and makes a turn again, he will be in point $$$i$$$. And the maximum possible value of $$$i$$$ is $$$x$$$, so, after we decrease Gleb's power by $$$2$$$, he can be in any point from $$$[1, x]$$$ (and possibly some other points we haven't considered yet).
Using a similar argument, we can prove that after we decrease his power by $$$2$$$ again, Gleb can be in any point from $$$[2, 2x-1]$$$, and so on. So, after $$$2i$$$ power decreases, there is a segment of length very close to $$$i \cdot x$$$ in which every point is reachable by Gleb with his current power. As soon as this segment's length becomes equal or greater than his current power $$$k-2i$$$, there will be at least one point $$$p$$$ in this segment such that $$$p \bmod (k-2i) = s \bmod (k-2i)$$$, so the point $$$s$$$ will be reachable with current power. If we take $$$i = \frac{k}{x}$$$, the length of the segment will be very close to $$$x \cdot \frac{k}{x} = k$$$.
I wonder why after Gleb decrease his power by 4, he can only be in any point from $$$[2, 2x - 1]$$$ instead of $$$[2, 2x]$$$
As given that $$$s ≥ x * k$$$, then $$$(s - 2x) ≥ x * (k - 2)$$$, he should have the chance to move another $$$x$$$ times
In that case, then after $$$k / x$$$ turn, We should at least get from $$$[k / x, k]$$$, and the power should be $$$k - 2 * k / x$$$, we may not seem to prove Gleb can get to every place from $$$[0, k - 2 * k / x - 1]$$$, am i making some mistakes somewhere?
Yeah, you are right, it looks like it is possible to move another $$$x$$$ times after we decrease the power by $$$2$$$, so the segment will be $$$[2, 2x]$$$ after decreasing the power by $$$4$$$. So after $$$2i$$$ decreases, our segment will be $$$[i, i \cdot x]$$$, and as soon as $$$i$$$ becomes at least $$$2 \cdot \frac{k}{x-1}$$$, there will be at least $$$k$$$ consecutive points we can be in.
These points don't have to start from $$$0$$$, since any segment of $$$k$$$ consecutive points contains all remainders modulo $$$k$$$.
I'm truly dissapointed by the testcases on G , if I would have got a wrong answer I would have changed the range from 0 to n instead of 1 to n.
I got TLE on system testing for problem E just because of j=1. How?! (j=2 got AC)
Should I change my language? 312564077
There is no way 63% of people that solved problem A, can actually solve problem C. I find problem C from this contest (division 3 contest) to be much harder than problem A from the previous division 1 contest (Codeforces Round 1012). The mentioned problem A was solved only by 935 persons, this problem C was solved by 15434 persons. Also, not to mention the difference in ello/rating between people that participated in the division 1 contest vs the rating of the people that participated in the division 3 contest. (In other words, the persons with higher rating can't solve the easier problem, but persons with lower rating can solve the harder problem).
Div. 1 A is supposed to be harder than Div. 3 C. In this case, I think that A from 1012 Div. 1 is much harder than 1013 Div. 3 C. Furthermore, Div. 2 contests have way more participation than Div. 1 contests, so the early problems are going to have more solves.
if you say that: this C problem is much easier (for you) than the mentioned div 1 A problem.
Than how can you logically solve it (without guessing a pattern) ?
Div 1 A is actually very easy, you just find a prime number around n/2, than you add numbers in the sequence/permutation in such a way that: every 2 numbers from permutation the average stays equal with the prime number that you found (around n/2). This problem has a clear logic, you don't need to guess any pattern or use any advanced mathematics.
In Div. 3 C it is pretty clear to prove that n must divide 1+2+...+n, so n must be odd. Divisibility is not advanced mathematics. And reversing a permutation is much easier to find than the more advanced algorithm of sum pairing needed for Div. 1 A.
I have no idea why i need to prove "n must divide 1+2+...+n".
Even if i guess the fact that correct permutations exist only when n is odd, i still have no idea how you can construct a correct permutation without guessing the correct permutation/pattern (without clear logic).
i found 3 different patterns that work, but i have no idea why any of them work.
pattern 1: https://mirror.codeforces.com/contest/2091/submission/312558837
pattern 2: https://mirror.codeforces.com/contest/2091/submission/312559065
pattern 3: put n in the center of the permutation, put all odd numbers in the left of n (inside the permutation), put all even numbers in the right of n (inside the permutation).
https://mirror.codeforces.com/contest/2091/submission/312559632
Let
a[i]be the index of valueiin the permutation. Then valueiwill be in the correct spot of the cyclic shift(i-a[i]) % nto the right. Then, the problem requires that each value of(i-a[i]) % nbe distinct. One easy way to do this is to have $$$(i-a[i])=-i \pmod{n}$$$, which means that $$$a[i]=2i \pmod{n}$$$ (which is your first pattern). Obviously, this only works in distinct values for odd $$$n$$$.Praise the sun!
How to implement prefix sum in memoization? Anyone help. 312470925
In F while selecting second hold on same row, it is done
dp[i][j][f]= dp[i][j][f]+∑j+dy=j−d dp[i][y][1]−dp[i][j][1]
But why here dp[i][y][1]? shouldn't it be dp[i][y][0]? why we take another hold if we already took 2?
I think its just:
$$$dp[i][j][1] = \sum_{y=j-dx}^{j+dx} dp[i][y][0] - dp[i][j][0]$$$
Praise the sun!
Can someone tell me why we define a and b as:
a=gcd(a,b)⋅x, b=gcd(a,b)⋅y for some x and y Where does this come from
$$$a$$$ is some number. Also $$$b$$$ is some number
The problem requires us to work with $$$\text{gcd}(a,b)$$$
What does it mean for both $$$a$$$ and $$$b$$$ to have a number, say $$$g$$$, as their greatest common divisor? It must be that:
So this means that:
where $$$g$$$ is $$$\text{gcd}(a, b)$$$
As a side note, I don't know about you, but I personally found it more intuitive to start my thinking from these three facts
Thanks!
for problem E , the sum of n overall test cases isnt required to be bounded we can realize that for each number x it has number_of_distinct_primes(x) relations under it which inspires a dp solution where
dp[i] = dp[i — 1] + number_of_distinct_primes(i);
312619387
also another solution for problem G
let dp[i][j] denote the following
for a movement with same parity as k the minimum reachable cell such that its index equals j mod i
for a movement with different parity from k the maximum reachable cell such that its index equals j mod i
ans is the first time a movement with the same parity as K has s % movement is reachable
transitions are kind of hard to explain honestly but i'll leave a submission and if maybe i've time later i'll explain them
312626019
complexity o(k * k * log(k))
dp[i] = dp[i — 1] + number_of_distinct_primes(i);
what is the reasoning behind this equation?
for the constraint to be correct the larger number must be the smaller number * prime
for 2 numbers a , b the larger is equal to the upperbound n
now the problem is for a number n how many numbers a (less than n) such that the constraint is satisfied u can basically subtract 1 prime from the prime factors and the other number becomes a , subtracting the same prime twice gives similar a so we only subtract distinct primes
For problem E, I was able to pre-compute the answer of all n up to 1e7. However, the test case limit was up to 1e3, gives enough time to have a loop over all prime which is not required with pre-compute. So, even if the test cases are 1e7, it would have worked.
My solution: https://mirror.codeforces.com/contest/2091/submission/312405661
\[^-^]/
4 line solution for F (except for template)
Using lazy segment tree
https://mirror.codeforces.com/contest/2091/submission/312526600
For Problem E n=5 how is ans 4?
for b = 2, a = 1 is only solution such that F(a, b) = 2 for b = 3, a = 1 is only solution such that F(a, b) = 3 for b = 4, there is no solution for b = 5, a = 1 is only solution such that F(a, b) = 5 b from 6 is > 5 since it's > n
So ans should be 3?
How about $$$a=2$$$, $$$b=4$$$? It is literally in the problem statement also
Can anyone tell me why Binary Search has been mentioned in the tag of Problem F? I get it uses Prefix Sum along with dp states but where have we used binary search ??
2091G - Gleb and Boating I solved it in O(k^2 log k). Let's consider the number m (=k-2*j), let's get a dp of size m, where in the index i there will be a maximum or minimum position (depending on where we are going at the moment: left or right) that can be reached and which has a remainder i modulo M. Thus, we will iterate through all q from k to m and update dp, if at some point we have dp[s%m] ==s, then m suits us. Let's call this algorithm for m as check(m) -> true/false. If we consider all the numbers k, k-2, k-4, ..., and for them check(k), check(k-2), check(k-4), ... then false will go first, then true. We'll do a bin search for them and find the answer. If everything is false, we'll output 1.
My solution: 319579708
in F in last fomrula
dp[i][j][f]=dp[i][j][f]+∑j+dxy=j−dxdp[i+1][y][0]
as given in qn. for vertically we can go one by one up only. then how we directly going above
Each subsequent hold is not lower than the previous one; At least one hold is used on each level (i.e., in every row of the rectangle);
can someone explain me how did thy get k=pi−i (mod n) from (i+k) mod n=pi in question C. I mean how did they simplify (i+k) mod n ?????????????????????? and what goes where ?
.
Problem D O(1):
D: O(1)
My thinking process for problem E is slightly different than the editorial, but using the same fact anyway.
Case $$$a = 1$$$: $$$lcm(a, b) = b$$$ and $$$gcd(a, b) = 1$$$, thus if $$$b$$$ is prime, it's a valid pair;
Case $$$a \gt 1$$$ and $$$gcd(a, b) = 1$$$: $$$lcm(a, b) = ab$$$ and gcd(a, b) = 1. But $$$ab$$$ cannot possibly be prime. Thus, pairs of coprime numbers are invalid.
Case $$$a \gt 1$$$ and $$$gcd(a, b) = k \ne a$$$: This is a case where $$$a$$$ and $$$b$$$ are non-coprime, and $$$b$$$ is not a multiple of $$$a$$$. We prove by contradiction. Assume $$$\frac{lcm(a, b)}{k} = prime$$$, then $$$lcm(a, b) = \frac{a \cdot b}{k} = k \cdot prime$$$. Let $$$m = \frac{a}{k}$$$ and $$$n = \frac{b}{k}$$$, then $$$\frac{km \cdot kn}{k} = k \cdot prime$$$, and we have $$$m \cdot n = prime$$$. This is a contradiction. Thus, we proved that for a pair of non-coprime numbers with a gcd smaller than $$$a$$$, the statement is invalid.
In the end, we are left with only the case where $$$1 \lt a \lt b$$$ and $$$gcd(a, b) = a$$$. It's obvious to pick $$$b = a \cdot prime$$$.
For implementation, I first generated all the primes up to 1e7, and used binary search to search for the number of primes that can be used to obtain $$$b$$$ while not exceeding $$$n$$$. This is my submission.
I used the following approach for Problem D:
Idea:
Please notice that if
kbenches have to be distributed across n rows, atleast one of them will haveceil(k/n)desks.Now, let the
ithrow containceil(k/n)desks. Letx = ceil(k/n).Notice that this row initially had
mempty spots. Now our task is to choose the locations of desks in such a way that the length of longest bench is minimised. If we calculate this minimal length,l, this will be our answer, because all other rows will have<= ceil(k/n)desks.Please notice that
m - xspots will remain empty in the row.Our goal is to use these empty spots (gaps) to create discontinuities.
So, if
m - xgaps are there, a group ofxcontinuous desks can be divided intom - x + 1smaller parts of continuous desks.Now the largest of them will have
ceil (x / (m - x + 1))desks, which is the answer to the problem.Please notice that this formula can be transformed to
floorasfloor(m / (m - x + 1)), which has been used in the code below.