Hello Codeforces!
I am pleased to invite you all to participate in Codeforces Round 970 (Div. 3), which will start on Sep/01/2024 17:35 (Moscow time).
The format of the event will be like any Div. 3 rounds:
6-8 tasks;
ICPC rules with a penalty of 10 minutes for an incorrect submission;
12-hour phase of open hacks after the end of the round (hacks do not give additional points)
after the end of the open hacking phase, all solutions will be tested on the updated set of tests, and the ratings recalculated
by default, only "trusted" participants are shown in the results table.
I encourage participants with a rating of 1600+ not to create new accounts but to participate unofficially.
Only trusted participants of the third division will be included in the official standings table. This is a forced measure for combating unsporting behavior. To qualify as a trusted participant of the third division, you must:
- take part in at least five rated rounds (and solve at least one problem in each of them),
- do not have a point of 1900 or higher in the rating.
Regardless of whether you are a trusted participant of the third division or not, if your rating is less than 1600 (or you are a newcomer/unrated), then the round will be rated for you.
Also, it will be the first round with unrated register. If you already registered as rated participant you can change registration type here.
I would like to thank
MikeMirzayanov for creating Codeforces and Polygon and testing the round.
Vladosiya for beautiful coordination and helping in preparation.
FBI, JuicyGrape and Esestree for not only testing but also discussion of the problems.
gmusya, employed, chen_zexing, Mr.Pie, XYZ_Herry, gvancak, Layk, CLown1331 and aboodsalman04 for testing and useful feedback.
Good luck!
UPD:
Editorial has been published.









Good luck to everyone! How were you able to set a round as a specialist though?
One round ago I was pupil)
coaches don't play
Bcoz now a Specialist was once a Candidate master and probably future master
as a tester give me contribution
As a tester, wish me luck! ..... (●'◡'●)
good luck (●'◡'●)
As a tester, I cried because how beautiful the problems are
Pa_sha [user:Vladosiya][user:MikeMirzayanov] Why are people with a popular history of cheating like him are allowed to test? He will leak the problems even before the contest is conducted!
bros a self-proclaimed tester
He is not a tester. I don't know why he wrote that he is. All testers are in the blog
You can ascend to the heavens immediately.
my friend told me once that kids started using codeforces , I didn't believe him at first , but now I do
Unrated registration! Too bad I can't test that when I'm usually a rated participant! (since I'm >=1600)
it will soon be in div1/2 too
qp
Will not be able to give this contest :( as I have to travel to college tomorrow at same time.
my first unrated div3 :O
I have a question — youre only a specialist but you are able to make a content. I have heard that you have to be a master to be able to do that. Can someone explain?
You have to be master or a writer in the past, as blog with rules say
As a tester, the problems were great! GL everyone
The comment below literally has +18. STOP TESTERISM!
as a tester I hope that yall have fun in this contest!
experts who registered as specialists wya
darn they put me out of competition
I once again hope I can AK this round ^_^. Good luck to you all as well!
Interesting fact, this round is rated for the author. As a tester, I encourage him to participate and get $$$\infty$$$ delta.
Return expert?
I'm back
First time participating unofficially, hope the problems will be good, and good luck to other contestants! ^^
Oh god give me power to solve atleast 2 + questions this time :) I love you all who ever reading it
Good luck and have fun! I am goona up to pupil
i hope i dont drop to newbiew
I am very much excited for today's contest, but as usual, more than 2000 cheaters are also ready
MikeMirzayanov attention plz there are lots of cheaters
I am hoping for a positive delta and changing the color of my username
and I did it
This Div3G is like div4D lol
ahh it seems i got it, but i just couldnt implement it. good job
Don't just change the problem statement mid contest,who tested this?
Are points reduced for compilation error also on submission. I forgot to select the correct language before uploading in 2 questions. :(
no reduction in points in error/wrong in 1st testcase
Problem F really made me angry thought about it over and over again with no conclusion, the third testcase did not make sense and I just can't think straight anymore from the anger I had of this failure. is the modulo increasing the result somehow? I just don't remember modulo could do something like this, am i missing something about probability? wish I started with other problem I would have had the chance probably. I got the same result for testcase 1 and 2 from the solution, but the third one made me want to throw my laptop
It is not increasing it, the answer for the third testcase is a fraction so it is the $$$numerator \cdot modular inverse(denominator) (mod 10^{9} + 7)$$$.
$$$\big(a\cdot b^{-1}\big)\bmod p = \big(a\cdot b^{p-2}\big)\bmod p$$$ for prime $$$p$$$.
You can check this blog about modular arifmetic. Particulary, about modular division, which is pretty common.
Problem F be like:
C++: Wrong Answer on test 4
Python: Accepted
So true. I don't know what is the issue with my c++ code, I handled all the overflows and yet it keeps giving me WA Test 4
Not all. You had overflow
when n = 2e5, n * (n — 1) / 2 overflows
Have you tried $$$\texttt{long long}$$$?
FOR REAL I had submitted two C solutions which got
Wrong Answerat test 5. At that point just gave up and used my beloved Python.is my solution for E hackeable?
I just hacked it
Can someone simplify on what needs to be done in
Problem FSakurako's Boxplease.answer = $$$\sum_{i = 1}^{n} \sum_{j = i + 1}^{n} \frac{2 \cdot a_i \cdot a_j}{n \cdot (n - 1)}$$$
modulo $$$10^9 + 7$$$ of course.
Thank you for the reply. Isn't the denominator
(n*(n-1))/2?Yes it is, sorry, I edited it.
There are $$$\frac{n(n - 1)}{2}$$$ possible pairs. Since we want to obtain the expected value, we know that we want to multiply the product of each pair by its probability. Clearly, each pair $$$(a_i, a_j)\, /\, i \lt j$$$ has the same probability of occurring. This means that the probability of each pair is $$$p = \frac{1}{\frac{n(n - 1)}{2}}$$$. Now then, we can see that the final result will have the form $$$pa_1a_2 + pa_1a_3 + ...$$$. So, we can extract the $$$p$$$s and obtain $$$p(a_1a_2 + a_1a_3 + ...)$$$. Here, we know what $$$Q = p^{-1}$$$. At this stage, you must choose a method to calculate $$$Q^{-1} \mod 10^9 + 7$$$. This can be achieved by any number of methods using the modular multiplicative inverse. The value obtained from this we will multiply with the calculated value of the numerator $$$P$$$ to obtain the final result.
On the other hand, we must find a way to efficiently calculate $$$a_1a_2 + a_1a_3 + ...$$$. This can be done by simply calculating $$$s = \sum_{j = 1}^{i - 1}$$$ and adding $$$sa_i$$$ to the numerator for each $$$2 \leq i \leq n$$$.
Hope this helps.
PS: Please do be careful with the modulos.
Thank you for taking the time to respond in detail.
Both (.-__-. and fisher199) your comments were helpful, and I have solved that problem. Thank you.
I added hints for H. Sakurako's Test on cfstep.com
why none of testers tried to check if F is solvable by chat-GPT ?
Why are you using Chat-GPT during the contest? What is even the point of solving a problem with GPT?
first of all, its the author's fault to not check if it's solvable by gpt or not, i can imagine that at least half of the submissions were made by the help of chat GPT (6k is a lot for a typical Div3 F)
F was set way too high IMO. It was easier than both D and E.
imagine using ChatGPT. using it should be banned because it can sometimes solve template problems regardless of their difficulty.
couldn't implement G although exactly know what I'm going to do
compute gcd of all element, cnt x % g, find first element not appear,
this can be done by binary search right? 😭
for n>1, you can set the array to 0, gcd, 2*gcd, 3*gcd, ..., (n-1)*gcd. I believe this will be enough for you to be able to solve it now.
Not quite. Compute gcd, let it be G. Then you can always make the array to be of the form
0, G, 2G, 3G ... (n - 1)G. Then finding the kth MEX is O(N).Damn, I had no idea how to solve G. How did you notice the GCD?
read how to find GCD using euclidean algorithm
Actually fun fact. This is very similar to the most recent Div 2C, which has a quite similar use of the GCD concept. I immediately thought of using GCD because of how recent that problem was.
In fact, G was made before it, so I didn't take it's idea. But yeah, it is somehow similar.
Funnily enough I solved it in contest.
I got the idea almost at once thanks to this problem.
find smallest positive integer x out of set of integer
allow to add and subtract each other unlimited times. gcd naturally comes into mind.
F was easier than D/E lol.
Yea but I dont know how to inverse modulo if not im spec by now
You can use fermat's little theorm.
$$$a^{p-1} \equiv 1 \pmod p \implies a \cdot a^{p-2} \equiv 1 \pmod p.$$$
So the inverse of $$$a$$$ is $$$a^{p-2}.$$$
Extended Euclidean Algorithm also works.
what is the idea for E when n%2 !=0 ?
Iterate over each letter to remove and maintain a count of each character at even and odd positions. Notice that the removal of a character will flip the parities of indices in front of it.
How to make sure that correct letter is being removed?
You don't have to, just try each one.
I just iterated from 1 to n and checked if i delete the current element in string , how many operations would i need to do in order to obtain alternating string and stored minimum of all those operations . For doing it in time limit , I stored the count of occurence of alphabets at odd and even positions at each index.
You can check this out , there may be better approaches but this came to me
https://mirror.codeforces.com/contest/2008/submission/279203992
To calculate the number of operations required for an arbitrary string s, you need to find the maximum count of a character at odd indices and another count for even indices, then subtract both from the size of the string, because that's how many characters you need to change. Also, notice how removing a character at some index i flips the parity of all characters in front. Moreover, if you remove a character at index i and flip the parity of a character at index i+1, then that character will have the same parity for the removals of characters at indices < i. Knowing this, going down from i=n while i>=0 you only need to update the counts related to the characters at index i and i+1, where s[i] is the removed character
We will try removing ith character for all $$$ i $$$ such that $$$ 1 \lt = i \lt = n $$$. with some precomputation, at each $$$ i $$$ we can calculate the most frequent character at even positions and odd positions in $$$ pref_{0\cdot\cdot\cdot i-1} $$$. You can do the same for $$$suff_{i+1 \cdot\cdot\cdot n} $$$. During the precomputation you can also store the characters which occur the most in prefixes and suffixes respectively. Then it is easy to observe that whatever balanced string we'll have will contain either of the 4 characters we just stored (most frequent even from prefix, most frequent odd from suffix) and (most frequent odd from prefix, most frequent even from suffix). (After writing some cases you can observe that after the removal $$$ith$$$ character, the parities of suffix flip). and that's it. you can check my submission
I couldn't get AC during the contest but I just had to add
ans = 1e8. feeling super sad rn :(Was G somewhere related to gcd of whole array if yes then why my solution is giving wrong
if no please help me in solving it
upd: got it just it just an extra equal to symbol
Could someone please identify what is overflowing in my F solutions?
Probably this line:
explain why people use prefix sum to solve H please :(
That's a very standard trick. It even appeared in a recent CF Problem : Med-imize.
I added more details here
I precalculated the answer for each x by binary search. Since $$$a_i \leq n$$$, each x splits the numbers int $$$\frac{n}{x}$$$ patterns of $$$0, 1, 2, ..., x - 1, x$$$, I used prefix sums to avoid iterating over that.
My submission: 279218027
Time complexity: $$$O(n log^2{n})$$$
wonderful set of problems! i managed to do my first two problems :)
congrats! Long way to go!
This is my best CF round — I am top 2000!!!
Will you release an Editorial/Tutorial?
Yes. We will try to do it as soon as possible
neal Congrats for actual Rank 1!
Funny how even the top 10 in ranklist people cannot code G correctly.... authors, testers do better. Wrong models really shouldnt be happening.
Here is my solution link, written in Simplified Chinese.
The solution of G before constrain modification is included.
I made the same error as std in G :D
Am I the only one stucking at implement of E?
I believe my first solution for H is hackable. I imagine O(n^2) in the worst case, but it's hard to analyze the complexity.
If someone is interested in hacking, take it)
Basically instead of doing a binary search I'm trying to calculate current result as +1/-1 from the prev result (in the loop). So if we have tests where the result sequence is 0->n->0... then my solution is quadratic.
https://mirror.codeforces.com/contest/2008/submission/279227245
The problemset needs to be rearranged, C<B F<D&E!!
The hard part about problem C is proving the simplest solution runs without the risk of TLE. Which I think not many people did.
this could be as easy as a div4, anyway good contest
From my deepest point in my heart i hate cheaters
you're a cheater, bro, rating matters more bro, no one cares about your self righteeousness get answer from chat gpt or telegram oa group to get good rating, and placement
What are you saying
why are you saying you hate cheaters?
Who does?
https://mirror.codeforces.com/contest/2008/submission/279214387
Does it help to not get banned? =)))
Someone help me. I don't understand codeforces at all. I don't understand divisions, ratings, trusted players, unrated contestant....I read some cf articles.
First time participated in this Div 3 contest, solved like 3 problems(A,B,C). Why still it shows unrated player?
Uncheck the "show unofficial" checkbox and you'll get a normal standing.
please explain how you got this answer to test 3 from example in F. Why 8 isnt the answer?
i mean there's a whole paragraph telling you to take the answer modulo M. basically when you divide two numbers, you will multiply by the denominator's mod inverse. which is equal to X^(MOD-2)
The sum (numerator) is $$$85$$$ while the denominator is $$$10$$$.
$$$85/10$$$ is $$$8.5$$$, not $$$8$$$
here you are required to print the answer to the mod of $$$10^9 + 7$$$ Since you can't use mod on a float value, you need to use mod inverse.
note that $$$ (a / b) \% m \neq (a \% m) / (b \% m)$$$
instead
$$$(a / b) \% m = (a \% m) * (b^{-1}) \% m$$$, here $$$b^{-1}$$$ denotes the mod inverse of $$$b$$$.
To calculate the mod inverse of a number $$$b$$$ for the mod of $$$m$$$ you can use this formula:
$$$(b ^ {m - 2}) \% m$$$
This can be calculated efficiently in $$$O(log(m))$$$ using binary exponentiation
This only works if the mod is prime
I know that pE is only prefix sum with odd/even index. But, I don't know how to write it efficiently. My sol is fucking LONG and still having some bugs.
Do I get a penalty for wrong submission on the problem which was not finally accepted
No
Though I wasn't cloudflared during the contest, I still spent more time loading pages than solving problem in ABCD. Perhaps I need to open all problem pages next time.
Is this solution to B fine? https://mirror.codeforces.com/contest/2008/submission/279237030
Lol I missed this condition during contest — R = C = sqrt(n)
Consider a 4*16 rectangle. 1074688
can u also verify my B submission 279084385
Your submission seems correct
In problem B, since the string is always made from a perfect binary matrix, is it sufficient to check if the count of ones equals to 4 * (r — 1)?
This submission is failing on some test case. Problem G
https://mirror.codeforces.com/contest/2008/submission/279239206
Can anyone tell me why this is happening.
For this case:
You can operate on the array like this
$$$[6,10,15]\to[6,10,5]\to[1,10,5]\to\dots\to[1,0,2]$$$
Fixed it, Thanks
TL for G is actually insane, how is this TLE? 279241104
It has an integer overflow, because
ris initialized with unnecessarily large number.Suppose $$$k=10^9$$$ and you have some distinct elements, then the answer is obviously larger than $$$10^9$$$. in this case, when
midis 1E9,good(mid)will returntrue, and thereforelbecomes 1E9. Becauseris still 2E9,(l + r)overflows.See the fixed version: 279244131.
ah, just realize overflow issue causes loop forever thanks.
How can I delete a comment?
hope Cheaters will be identified....
performed very bad :( should have taken mod of q also
279181406
Can some one please highlight the error in this solution?
I used this formula (Numer/Denom) where,
Numer = sum of (A[i]*(sum(A)-A[i])); for i=1 to n;
Denom = n*(n-1);
n * (n-1) can overflow before it's modded
That's not the case since I use #int long long
Anyways I got my silly error:
In my code, Correct:
int val = (A[i]*((cSum[n]-A[i]+MOD)%MOD)+MOD)%MOD;
Wrong:
int val = (A[i]*(cSum[n]-A[i])+MOD)%MOD;
Can anyone please tell me why this solution gives tle and this one does not?
In the first one (for the reason I can't understand), instead of counting the numbers in the range [a,b],I pushed all those number in a vector and printed the size of that vector.
vector push_back needs to resize the whole vector occasionally, but it all adds up into the time taken to run the whole program
Thanks man but can you explain it in a bit detail if it's okay for you!
Sure, you can check the Complexity of push_back in this link
It says
Constant (amortized time, reallocation may happen). If a reallocation happens, the reallocation is itself up to linear in the entire size.So that's exactly what happen. You can avoid that reallocation by using the .reserve() function to allocate upfront, or the best thing to do in this case is just not using a vector at all.
Got it! Thanks a ton
Prefixforces
any one has idea how to solve E with dp I don't get how it's tagged as dp
misleading tag? Some people consider prefix/suffix sums to be dp
Here is some feedback from me. The issue on G was unfortunate, but it can't be helped. However, even without that, I think some problems were also a bit too typical and some things could have been improved.
squaredwasn't very clear initially without the last sentence.Hello can you hack my H which ig has tc of O(n(logn)^3)
279221504
Hi. Thanks for the feedback. I appreciate it and I will consider it in the next contests. Here is what I can say about some of this:
A: There is a rule for Div3 that constrains in the first task should be small. I think it is made for solution like $$$2^{a+b}$$$ to pass, for the programmers who only start competetive programming.
D,F: I think you are right, but here is reasons I put it in the contest. There is not a lot of tasks, where expected value or dividing permutation on cycle are in a some simple way, so when I first saw task expected value it was to hard for me even with tutorial. So, I wanted some more easier tasks to be here on at least some topics. I understand that there is a lot of sites, books where you can learn this, but when I was only starting CP I didn't know any of that.
H: To be honest, I thought about it, but here is reasons I didn't want to do these version:
This is why there is queries.
One more time thanks for your feedback. I don't have anything to say about B and C, because probably you are right.
I can see your reasons behind the other things, but I still don't get about A. If that was your intention, then the constraints should have been even lower, because many $$$2^{a+b}$$$ did NOT actually pass and got hacked or FST. It could've been also done by guaranteeing distinct test cases, but even that was not the case.
So the only reason I can think why it's so small is just because it's a 4A problem, and I've seen this quite a few times on other Div. 4/3 A problems. It really feels like these constraints are not decided based on actual solutions that it wants / doesn't want to pass, and rather just want to be 'somewhat small'. I understand why the coordinators want to include every possible case in one test, but I honestly don't see why the number of test cases cannot be something like $$$1000$$$ in these problems with larger constraints. The only thing it would do is to take just tens of milliseconds more for any viable solution that is not exponential. If they actually intended to allow any viable exponential solution, then the constraint should be like only 1 digit. For now they're neither of these cases.
I have registered as rated in this content but still my rating has not changed in it.
yea, the testing isnt finished i think
Can anyone tell me why this code get WA in B?
1.n must be a square number, like 4,9,16,... 2-(1) n=4 will be yes. 2-(2) n>4, if s contain '0' will be yes, otherwise no.
Any bug in this logic?
I fix it, thanks.
I submitted solutions for 6 out of 8 problems, and now it shows that only the 1st is accepted, and the rest are in red or in the queue.
That's because System Testing is currently going on. Wait for it to get over and you're attempts will be back to normal.
at first I was so amazed by the fact that its you first contest and you submitted 6/8 problems kinda cool. then I wanted to look your submission and when I saw you submissions of problem A, you took two inputs in array (alright great for beginner) but then your submissions got drastically changed with maturity of variable selection and formatting. I was having mixed thoughts in my mind until I looked at E's Submission and I got reminded of a random comment. Well you know I know what I am talking about but CF is all about fun, please enjoy!
When do we get our rating score at Codeforces Round Div 3?
just started and already cheating yeah GL
Moderators I would like to report this guy almighty_xd with submission of E 279219069 . Vladosiya Pa_sha
Why the rating changes are so low? can someone explain if they know?
So lucky to have E passed in 1999 ms. 279208099
Can someone please tell me whats the problem with my solution for Problem F?
https://mirror.codeforces.com/contest/2008/submission/279375562
Added a
+ modin your code. Now, it passes. 279398988For problem B I can't understand how just count of 1s equal to perimeter condition is passing all tests 279233678. For s = 111111110 the above approach should say it is a beautiful matrix but the representation doesn't look like that.
1 1 1
1 1 1
1 1 0
Lol yeah, that solution doesn't look correct. Very interesting that it passed.
Guessed dufficulty
A — 800
B — 1000
C — 900
D — 1300
E — 1700
F — 1600
G — 2000
H — 2200
F is 1300 at most, you only need modulo inversion to do that
fair ig, but has lower solve rate than D so.