Ahoj, Codeforces!

We are excited to invite you to participate in Codeforces Round 945 (Div. 2), which will start on May/17/2024 17:35 (Moscow time).

The problems are authored by TimDee and prvocislo.

This round will be rated for participants whose rating is below $$$2100$$$. Participants with higher ratings are encouraged to participate out of the competition.

You will be given $$$6$$$ problems and $$$2$$$ hours to solve them. The scoring distribution is bellow. One of the problems will be **interactive**. Please read this blog to get familiar with this type of problems.

We would like to thank everyone, who made this round happen:

**You**for participating in this round! I hope you will enjoy the problems!satyam343 for great coordination and discussion of the problem ideas!

KAN for providing suggestions to help us make the round better

MikeMirzayanov for the amazing Codeforces and Polygon platforms!

Alexdat2000 for Russian translations

Our loyal army of testers: A_G, Adam_GS, errorgorn, Dominater069, valeriu, atodo, Ritwin, MridulAhi, NemanjaSo2005, Sanat, tyr0Whiz yuvrajKharayat, 18o3, Phantom_Performer, mesanu, tushaar9876, madlogic, callmepandey, Scythe, htetgm, Blagoj, Non-origination, Abhishek_Srivastava and ✩₊ myvaluska ₊✧ , for pointing out mistakes and helping us improve the round!

Last but not least, I, prvocislo, would like to thank atodo and misof for inspiring me to start with problem-setting.

Scoring distribution: $$$500-1000-1500-2000-2250-3000$$$

Good luck, have fun and see you on Friday! ✩₊˚

**Upd: Congratulations to the Winners!**

**Div 2**:

2) Sxy_Limit

3) LofiGirl

4) prins

5) SXWZ-Queenie

**Div 1+2**:

1) BucketPotato with first AK!

2) gamegame

3) hitonanode

4) abc864197532

5) peti1234

**Upd2**: Editorial is out!

As a tester, I enjoyed testing this round. Also, satyam343 orz

Can you briefly explain, what testers do?

They just enjoy testing the round, as he had said.

So they solve all problems before the contest? Is this testing? If it is then it's quite cool!

Pretty much. You do the round before it is held and give feedback to problem authors. You can also upsolve and give suggestions and feedback to authors even after testing. Basically helping with problems as it's much easier to make good problems when you have opinion from a lot of people.

It's what you mention — with some goals; broadly, there are two types of testers:

- those that give the contest and opine on the overall quality (difficulty gradient/topic bias etc)

- those that scrutinize the problems in detail, evaluating all aspects of the problem; its difficulty, constraints, wording, test-quality, some go the extra mile and test to make sure that poor solutions don't pass (though I believe this falls on part of the setter)

At times, there is also an overlap of the two; after evaluating the contest as a whole, a tester can also individually scrutinize problems in detail.

Most ImportantlyIt helps farm contribution points 😌

satyam343 orz

As a

loyaltester, I wish you all the best!Huge fan sir.

Hi! As a newbie tester I would encourage everyone to take part in this round and I would like to thank all the people who took part in organizing this round — especially prvocislo ! :D

Well done prvocislo, I am looking forward to Friday :-)

As a tester, I tasted so you could enjoy. GLHF!

As a tester, prvocislo TimDee and satyam343 put a ton of effort into this round. ORZ

As a tester, I confirm!

As a participant,I will confirm after contest!

As a participant, I will tell you which problems are easy for beginners like me.

LET'S GOOOOOOOOOOOOOOOOOOO

As a tester, problems are:

SpoilerAs a tester, I hope the participants will enjoy solving the problems as much as I did. Also, satyam343 orz.

romanticforces

Does CF allow someone who cheated (https://codeforces.net/blog/entry/120675) and never showed remorse for his actions to author contests? What I want is not that he shouldn't author contests ever, what I want is for him to apologize publicly and say that what he did was wrong, and maybe return the 2 TON he got from cheating.

dramaforces

MikeMirzayanov Can you please tell why no action was taken against him despite him cheating in many contests using alt accounts, and he never apologized or showed remorse for his actions?

The perception among the top contestants is generally that using alts is not a bad thing, and I kind of agree with it (although Mike does not given that zh0ukangyang got banned, but that was probably a special case anyway, but I digress). It has a caveat though: You should never, and I repeat never, use two accounts in the same contest (obviously), which he has violated several times, and even worse than that, he has used two accounts in a contest with prizes, therefore effectively taking away one contestant's 2 TON.

Of course, you could argue: "Well 2 TON is chump change, who the hell cares?", and I agree yes, this sort of mindset does have a place in certain situations, but not in this one. Why? Well for starters zh0ukangyang got banned even though all he did was take 3000 measly internet points from other participants, so it stands to reason that a person who takes 2 TON should have some action taken against him. Also, without all of that stuff, like cheating = bad???? hello??? a person who cheats should not get away scot free, like that's what I believe, maybe other people might believe something different, but yknow I've spoken my piece I guess

Using alts is still cheating anyway, cos you're still taking some rating from other people. If you want to be against cheating, be against it fully.

Yes, that is true. I personally don't have any alts, but I can see a ton of reasons why someone could create alts, the most frequent of which is that "I have only an hour to participate in contest, but I still want to participate". If CF introduced a feature like Atcoder where you can participate unrated, that would be great, and would probably reduce the use of alts by a lot.

As a tester, I tested!

CM Pandey

World Telecommunication Day !

as a participant i won't participate if problem C Or D is interactive

as another participant I don't think this is a wise decision. If you don't attempt harder interactive problems you will not improve at them.

As a tester, I enjoyed testing this round.

As a tester I am afraid to say that I'm no longer a specialist to make this round special

Why is the time changed? I remember it started from 17:00 UTC+8

As a tester, the problems are of high quality and I would recommend the participants to read all of them.

It was my first time testing a round, I really enjoyed it, all the best for it!!!!

Hopefully, the problem statements will be short & precise just like the announcement! :)

one of the problem will be

interactiveis it rated?

Ok cheater

did u said it to me?

Not you sir

This will be my birthday contest :)

Happy birthday! I hope you will enjoy the round :D

As a participant, I wonder why the time has been changed. It would start at 17:05 UTC+8 on May 16th when I registered.

Same, the original time just barely didn't collide with my uni, the new one does >:(

Pray for Expect :3

Plz.start on 17:05(UTC+08) :<

GL&HF&high rating!

Thank you all for organizing this. Hope i can manage to solve up to problem C.

Lovers made me contest, Tim is fall in love with prvocislo. This contest will be a very romantic

Congratulations prvocislo, looking forward to your round :D

romantic forces 🤡

I wish everyone could reach their destination by this contest.Best of luck all the participants.

Today's contest in "Calendar" was scheduled: 15:05. I got ready and saw that: 20:35. Anyway I'm ready!!!

A > B,C

A for me was in reading the constrain after 2 hours of trying to casework it

Sad

Receiving clarifications for A about 7-9 mins into the contest after 1 failed attempt to answer the "original" problem is pretty underwhelming.

Bonus points for MASSIVE confusion after the first submission failed on pretest 1.

.

Anyone proved the validity of C?

I thought of a greedy solution, check if it's possible to maximize at indices $$$(3, 5, \dots, n-1)$$$ or $$$(2, 4, \dots, n-2)$$$, then take the case with better score.

I skipped the sequence having a[i]=1

As long as $$$1$$$ is not one of the chosen elements to be local maximums, then such construction will lead to values $$$\geq n+2$$$ for local maximums and $$$\leq n+1$$$ for other values.

I see. This claim seems to assure that my solution is correct (yet slightly redundant as excluding $$$1$$$ is done only at the final comparison). Thank you.

Just choose the case that ignore 1 cuz for example if n=8

a-> ..8 1 7.. p-> ..1 8 2.. res -> ..9 9 9 ..

which not valid solution..

If one of them have better score, then the smaller-score one will have the element '1' inside it. The fact is that every elements will be at least n + 1 after the transformation. So if the we try to maximize the position having the element 1, it couldn't be the local maximum.

Take the one with n or if n is on the edge, take the one that is further away from n.

If you take the one with n, worst case, you have to maximize 1, 2, 3, ... n in order. To 1 let's match up n in ans permutation and to 2 let's match up n — 1 and so on and so on. The minimum sum we can get is n + 1, n + 1, n + 1 ...

To be sure they are the local maximums, the worst case scenario of the neighbors are that they are n — 1, n — 2, n — 3 ... to n — 1 let's match up 1 in our ans permutation, to n — 2 let's match up 2 to n — 3, 3 ... and we see that our neighbors are n, n, n in the worst case. Since n + 1 > n, this works.

There is similar logic when n is on the edge.

Since it is even you can always achieve the optimal number of local maximums in an array. The only thing you need to watch out for is to make element N, a local maximum, and if you do that it can be proven that every element that is supposed to be a local maximum will be >= N+1 and every other element < N+1. Just add 1, 2, 3, 4.... to local mins in increasing order so at worse that would be (N-1)+1, (N-2)+2 ... you can see that each is at most N, and do the reverse for local maxs 1+N, 2+(N-1) ... each is >= N+1.

good round

is it just me or was C kinda tough to implement?

It was very easy to implement, the observation was the difficult part imo.

dang I must've overcomplicated it then

Take a look at my submission, I see your code got very messy. 261386987

Thanks broski. It seems like we had the same greedy strat but this one is much more concise.

PTSD

Almost toughest Div2. BCD are too hard for their positions and D is the strangest problem of my CP journey...

Lol I thought I was the only one thinking B was odd. Counting prefixes bit-by-bit should be C at least.

I didn't count prefixes. I just split the numbers into bits and found the length of the longest sequence of zeroes in the resulting arrays.

Exactly, just sort the indices of elements with each bit and take the max difference between them, no need for prefixes or binary search or anything similar.

Ah, I see. I must have done dumb things under pressure. XD

Still, bitworking for B does seem overboard a little bit. Maybe 1250 is a more proper score for it.

I get what you're saying, but nothing compares to having an incorrect russian A statement to me. Wasted 10 mins because of it, more than i spent on B.

That part, I could empathize with (though I escaped it since the English ver did justice). Kinda sorry for your bad luck there.

Same lol, I used Sparse Table and Binary Search. Ig I really over killed it.

Out of curiosity, what makes D so strange in your opinion?

It feels fairly normal for a CF interactive problem to me.

The interaction and the output format is very weird. What I expected: I can ask $$$l, r$$$ and the answer will be $$$f(l, r)$$$. (I know that's not interesting because $$$f(l, l)=$$$ $$$a_l$$$) And the output should be the array or some natural proterty of that.

By the way nice problem.

Straightforward thinking leads $$$O(n \log n)$$$ (but maybe with small constant) queries, and try to cut down into $$$O(n)$$$ leads back-to-back wrong ideas.

And splitting $$$2n$$$ into $$$n+n$$$ feels bad because asking $$$?$$$ $$$1$$$ $$$m \times i$$$ ( $$$1 \le i \le n$$$ , $$$m$$$ is a (fixed) integer) leads easily shortage of one of $$$n$$$. The actual solution is finding maximum and bruteforcing with some upperbounds, but later one costs $$$\le n$$$ queries is very unnatural I feel.

Maybe the unnatural situation of the problem also calls my feeling.

I think so too, i would say the less number of solves is partly due to the un intuitive solution and setting of the problem

Why does it feel unnatural to try and split the latter $$$n$$$ into checking $$$\frac{n}{k}$$$ options that each require $$$k$$$ queries? Or did I misunderstand something?

When I don't know the solution, fixed $$$k$$$ looks like shackle rather than advantage so I get lost, and I found the fact your figured almost after write my code. (Noticing after is easy but solve from $$$n/k \times k = n$$$ is hard and I feel the solution is just happened to work well or something that)

How to solve C?

1.Divide the array into halves based on even and odd index.

Find which half has 1 (this is the half without maxes), you could do half that has n is maxes, alse.

Sort both halves.

Sort sides with peaks. Put n/2 + 1 to n in this half. Smallest number in OG array gets largest number in new array.

Same as above with array without peaks but with 1 to n/2.

Print

I understand it. The peaks are all taken as>=n+2, and the rest are taken as<=n+1,is it?.

yes

ok,thank you for your explanation

Proud of myself by solving C with proving its validity, not by guessing

how problem B was solved?

got 180 score on A. couldn't solve anything else. bye bye pupil! :sob:sob:sob

Same. I'm yet to come up with a solution to it, in fact :(

my hardest div.2 till now

Happy to solve B, BS op!

can anyone explain the approach for problem-C ?

1.Divide the array into halves based on even and odd index.

Find which half has 1 (this is the half without maxes), you could do half that has n is maxes, alse.

Sort both halves.

Sort sides with peaks. Put n/2 + 1 to n in this half. Smallest number in OG array gets largest number in new array.

Same as above with array without peaks but with 1 to n/2.

Print

please don't give spoiler

upd : got AC, we only need check first n / k multiples of a_max

You're going in the correct direction.

If you want a (somewhat spoiler-ish) hint:

HintHow large of a multiple of $$$a_{max}$$$ can $$$m$$$ actually be for a valid solution.

Thank's! will see the hint after AC :)

Worst A ever!!! A > B

A: I guessed we should greedily remove (1, 1) from 2 highest score until there is 0/1 positive score left.

B: I guessed it's binary searchable.

C: I guessed only considering position 2 and 3 is sufficient.

D: What should I guess? XD

First I thought D is binary search, but it's actually not. Ad-hoc seems fit to D.

D: Guess that finding the max is important and then think how to use it :P

I guessed that we needed to reduce the amount of values i needed to check

If you try solving B in O(n log n) the solution is very pretty, no guesses needed.

You can take a look at my solution https://codeforces.net/contest/1973/submission/261355440

You just look for the maximum distance between numbers with the i-th bit on, for every bit.

Author please tell me why interactor not giving maximum element in the array for sample test case 3 for the following code.

code...

Every time it's giving 0 as maximum, but surely it will give some positive

I think that you may need to read something after you give an answer, because the interactor in this problem will return 1 or -1 after you give an answer. I also WA in pretest 1 and got confuse but after I figure it out and read one more thing after giving my answer, I passed the pretest.

Shit, due to this missed AC

got TLE for problem B

how to solve D?

Use n guesses to find largest element in array. Can be done by guessing ? n s*n for s in range n to 1. s is the largest number in array

If m exists m must be a multiple of s. m must also be a maximum of s/k, because anything greater cannot exist as m=s/k, divide array into k equal parts if all elements in array are s.

Can brute force with those 2 observations. n/k solutions to check. Each solution takes at most k queries.

Find the maximum element $$$mx$$$ in $$$a$$$ by making queries $$$(1,n*n), (1,n*(n-1)),\dots$$$. If you get $$$r=n$$$ on query $$$(1,n*b)$$$ then $$$mx=b$$$.

Now since this maximum element is present in at least one of the $$$k$$$ subarrays, $$$mx$$$ must divide $$$m$$$. Also, if $$$m=b*mx$$$, every subarray must have length at least $$$b$$$. So we only need to check multiples of $$$mx$$$ upto $$$\frac{n}{k}mx$$$. In each candidate for $$$m$$$, just query $$$k$$$ segments starting from position $$$1$$$ and check if these segments cover the array.

This takes $$$n$$$ queries to find $$$mx$$$ and another $$$n$$$ queries to check all values of $$$m$$$.

Damn, I figured this one out during contest but forgot to replace $$$m$$$ with $$$b \cdot mx$$$ and thought it's $$$O(n^2)$$$ queries :(

you'll need to find the max, it is easy to find in $$$n-1$$$ queries (iterate over multiples of n)

once, you've got max (call it $$$a_{max}$$$) then iterate over multiples of $$$a_{max}$$$, note that $$$m \leq a_{max} \times n$$$, candidate $$$m$$$ is a multiple of $$$a_{max}$$$

Problem C, I've tried some special case line n = 6 Assumpt P = {6 1 4 2 3 5} so optimal Q should be = {1 4 5 3 6 2} but not Q = {1 6 3 5 4 2}

and otherwise with P = {6 3 2 4 1 5} So i come up with solving two case but my idea seems to be wrong somehow idk. Can sb help me with this 261394519

It was the most fun Div.2 ever, thank you!

Happy you liked!

thanks a lot! I'm really glad you enjoyed the problems!

Man, this was a hard contest. Took 51 minutes to solve AB.

Also, why is this wrong for C? I did the same idea as everyone else. Calculate max score for odd indices (3, 5, ..., n — 1) and for even indices (2, 4, ..., n — 2) and then take max.

Nvm I got it now. We have to ignore the last element in odd case and first element in even case.

-1e9 downvotes loading

Mannn.. wasted 20 min on a corner case in problem A. and wasted almost 1 hr on a simple mistake in problem C.. as well as 4 wrong submissions, giving a heavy penalty... But overall the questions were amazing.

I hope you learned from this contest...

C is too bad

Why do you think so?

Finally i have reached master

I would also appreciate if anyone could give me some advice on how to grow from here. I am looking for things like something that you felt you had to change in order to go from M to GM and really anything except for just practice.

Can I be your slave? Plz?

Would you be so kind to enlighten me on what that entails?

Nice reaching master in less than 2 years, any advice for us newbies, kiyotaka?

Just enjoy cp

"and really anything except for just practice."

My easy solution for A:

Spoilerhow does it always work

There can be at most $$$\frac{p1 + p2 + p3}{2}$$$ draws between 3 players. But if the total score of any two people smaller than $$$\frac{p1 + p2 + p3}{2}$$$ (for example, $$$p1 + p2 < \frac{p1 + p2 + p3}{2}$$$) then there can only be at most $$$p1 + p2$$$ draws between player 3 and (player 1 & player 2).

I don't know how to explain it more clearly :(

Your explanation was pretty clear! Very nice solution.

You can apply standart algorithm: $$$s=a+b+c$$$ and answer is $$$\left\lfloor \frac{s}{2} \right\rfloor$$$ or $$$s-c$$$ if $$$s-c\leq c$$$

(p2+p3) and (p3+p1) makes no sense since the numbers are sorted (p1+p2) makes sense if C was bigger than(a+b) but how did you come up with (p1+p2+p3)/2 can you please tell ?

(p1+p2+p3)/2 is literally the case when every match ends with a draw.

Oh, I didn't notice that the numbers are sorted. Each game increases $$$(p1+p2+p3)$$$ by $$$2$$$, which means the maximum number of games played is $$$\frac{p1+p2+p3}{2}$$$.

How can we write math variables and equations with special font and symbols in our comment?

The expressions use LaTeX. You can check out this quick guide.

It's best to press "Preview" before commenting to make sure that your written syntax is correctly rendered.

$$$Thanks$$$ $$$a$$$ $$$lot$$$

Terrible problems.

The assumption of finding the maximum value of the array in problem D was nice.

Terrible contest...

Finally I find my lucky contest lol :>

Waiting for another even luckier contest to push me to CM :>

hey can u please reply to dm

I'm newbie in here, can someone explain why in the problem A test case 1 => 3, 4, 5 the answer is 6. If i count manually i just can make the max of draw is 5. With the following operation:

I cant find if the max draw is 6 T_T

A & C draw $$$2$$$ times, B & C draw $$$3$$$ times, A and B draw $$$1$$$.

Oh! stupid me :) Thx for the answer, very understandable

Is it just me who solve A with a naive dp? Can't even come up with any solutions now xD

same here

I don't like to guess T_T

Sooo fast rating updation!!!

Hi all!

Can someone help me figure out what happened with my B? I solved it using C++20 in my machine (the tests where matching) then I submitted my code to CF and got WA on test case 1. I looked at the results on the submission and one test was different from what I got locally.

Then I switched to C++17 on codeforces, submitted my code again and got AC.

What might be the problem? What I think is weird is that I was also using c++20 locally and there was no problems.

AC submission (c++17): 261375967 WA submission (c++20): 261374878

Bro, I got TLE on case 1. Although, running well in my pc. Don't know why!!! Finally, I have managed to get accepted.

This C solution seems simpler than the other ones I've seen. Just take some set of nonadjacent (n-2)/2 elements to make as peaks such that none of the taken ones are equal to 1. Then, you can force those positions in the array a to have values >= n + 2, while forcing all other positions to have values <= n + 1, which guarantees that those become peaks.

got skill-issued, and somehow increased rating lol

How to solve B?

A bit tougher than usual Div2, but the problems were amazing.

I felt B > C, especially proving that binary search works in B has taken a lot of time.

BUCKETPOTATO ORZ

I solved A in like 1hr and B in next 30 min. B without binary seach.

Why do I suck at A kind of problems.

That was a hard one, anyway I enjoyed it.

Pretest Exactly Maintest WOW!

Very nice round!

A little difficult, but absolutely a nice contest and high quality problems.

'B' was quite tough compared to usual div-2.

Great problems!

Any approach to solve this problem?I am stuck

You are given a tree of 'N' vertices, rooted at vertex '0'. You assign a random number in the range '[0, M — 1]' to each of the vertices.

Let us define 'heap-like' as the property that: for all vertices 'v' in the subtree of any vertex 'u' of the tree, the random number assigned to vertex 'u' is less than or equal to the random number assigned to vertex 'v'.

Find the probability that the resulting tree is 'heap-like'.

The answer should be found modulo 10^9 + 7. Formally, let M = 10^9 + 7. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q !≡ 0 (mod M). Output the integer equal to p * (q^-1) mod M. In other words, output such an integer x that 0 <= x < M and x * q ≡ p (mod M).

For Example : Let 'N' = 3, edges = [ [ '0, 1' ], [ '0, 2' ] ], 'M' = 3. Random number assignments that result in a 'heap-like' tree are [ 0, 0, 0 ], [ 0, 0, 1 ], [ 0, 1, 0 ], [ 0, 1, 1], [ 0, 0, 2 ], [ 0, 2, 0 ], [ 0, 2, 1 ], [ 0, 1, 2 ], [ 0, 2, 2 ], [ 1, 1, 1 ], [ 1, 2, 1 ], [ 1, 1, 2 ], [ 1, 2, 2 ], [ 2, 2, 2 ]. The total number of possible assignments is '27' and the '14' assignments above result in a 'heap-like' tree. Thus, the probability of a heap-like tree is '(14 / 27) % (10^9 + 7) = 185185187'.

Hello there! I had a funny little solution for B which involving Segment Trees.

My code : 261728503

Solution :Let

ansbe the answer we have gotten. Notice that this that OR of all a[i] to a[i+ans-1] must be equal to the OR of a[0]...a[ans-1].We iterate through all the indices, in the array and let

kbe the answer we have gotten uptil now. Two cases may arise :**Case 1 : ** The OR of

a[i]toa[i+k-1]= OR ofa[0] to a[k-1]This means k is good for this i, and we can go to the next position**Case 2 : ** The OR or

a[i]toa[i+k-1]!= OR ofa[0]toa[k-1This means thatkhas to be incremented. Notice that if we increasekby 1, we also need to decrementiby 1, I am not really sure about why I just got the intution of doing it, because we need to check one more case?And

ican never go negative because eventually ifi=0, we get the base case and it has to be trueAll these queries can be managed with a Segment Tree.

Hope you understand the solution, if you have any

queriesfeel free to ask, and I'll try to help if possible.I think problem D,E are both clever and interesting.

really cool round! Problems have quite short and nifty codes and the ideas are really cool. well done on setting D and E (especially D)