Hi, Codeforces!

Codeforces Round #553 (Div. 2) will be held on Apr/18/2019 18:35 (Moscow time). Round will be **rated** for second division (rating below 2100). As usual, participants from the first division can participate in the contest out of competition.

My gratitude to arsijo and KAN for coordinating the round, Markellonchik (special thanks for the help in preparing one of the tasks), mohammedehab2002, Jeel_Vaishnav for testing, and also 300iq for the idea and preparation of one of the tasks, aleks5d and isaf27 for testing it, and of course MikeMirzayanov for Codeforces and Polygon systems.

In this round you will help the residents of the Kingdom of Kremland. I strongly recommend you to read the statements of ALL tasks (and of course, try to solve them).

Good luck!

**UPD:** Scoring distribution: 500-750-1250-1750-2250-2750.

**UPD:** Editorial

**UPD:** Thank you for your participation in this round! Congratulations to the winners!

**Div. 2**

how much problems are required to solve in division 2 to get expert rating

You should solve ABC in 30 minutes

thanks man

N what bout specialist? What should be done 2 reach this rating?

i think that you should solve A,B very fast, or A,B,C a bit fast.

I think solving ABC super fast is not primary goal to go 1600+, instead try to solve ABCD in 2 hours.

Ok

Those feelings when you wait for the contests to read the tasks and not solve them

Sorry if my question is silly, will it be rated for Div3 participants as well?

Of course!

Thanks! :)

I wish you good luck! You did well yesterday, in your first contest!

Thanks, it was a good contest!

6 problems?

Can div3 pupils improve their rating by solving A problem?

Depends on how fast you are, where in pupil range you are, and of course how hard the tasks are, but generally I don't think so. You may try solving at least A and B, which I think in most case will give rating improvements.

BTW. I don't think that there is a big gap between Div.2 AB and Div.3 AB. Difficulty gap between 2 different div2 contests are actually larger (in my opinion, but problem rating data also kina shows similar results.)

`I strongly recommend you to read the statements of ALL tasks (and of course, try to solve them)`

I have seen a similar thing here. I will read all the tasks only if you say so. :P

But still, any updates on 0 IQ Challenge?:)

flood of contests, thanks MikeMirzayanov

I wish positive rating for every participant :)This is not possible :)

i hope to got an expert grade after this contest!

How much should I solve 2 get specialist rating?

maybe 3? i guess

Will (Forethought Future Cup — Elimination Round) be a rated contest? Can any one tell me, please.

Yes.

Expecting this round to be full of awesome questions with strong pretests!!!

with good pretests uninteresting hack))

Hear This before contest and you will RISE by 100

Well good luck for today's contest..Its time to sit back and watch Game of Thrones..

Score distribution?

No interactive problem today :(

No extra registration?

I am not able to submit my codes. I am getting an error message saying "You have already submitted the exact code before" even when I haven't made a single submission.

add anything... like a space or anything to that code somewhere

I tried changing the codes, it still gives the same message.

Can i register now after the contest started?

Solution for D?

Sort by $$$(a_{i}-b_{i})$$$

Got this algorithm right away, but pretest 12 wouldn't run fast enough with my Python code... Tweaked it for efficiency as much as I could, and eventually it got past 12 but gave me a time limit error on 13.

You have to select Python 3 instead of PyPy 3 as the language

Ironically, I was using PyPy because I thought it was faster...

And yup, works perfectly when submitted in Python 3. Sigh.

It is just the input that is slow with PyPy. You can get it accepted if you take care of that: 52988802

Notice that $$$a_i(j-1)+b_i(n-j)=j(a_i-b_i) + n b_i - a_i$$$. So, what matters is the value $$$a_i - b_i$$$.

:( Late

Dissatisfaction be rewritten as $$$j(a_i-b_i)+b_in-a_i$$$. Thus, we are trying to order $$$i$$$'s to minimize the sum of $$$j(a_i-b_i)$$$. Just sort them in decreasing order of $$$a_i-b_i$$$ and that should be the minimum. You can use an exchange argument to prove it.

https://pastebin.com/tmHW66Yh

Here's my solution, if you want the code, but if you want an explanation, I can give that as well.

Take this input: 10 5 10 12 4 31 45 20 55 30 17 29 30 41 32 7 1 5 5 3 15

The correct order from left to right is sorting from largest to smallest in terms of Ai-Bi: 30 17 (30-17 = 13) 41 32 (9) 12 4 (8) 7 1 (6) 5 5 (0) 29 30 (-1) 5 10 (-5) 3 15 (-12) 31 45 (-14) 20 55 (-35)

If in doubt, read the problem statement and bring out your calculator.

For contests like these, you should get my script which sorts problems by number of submissions :)

Here

Can someone give me a hint on D?

Hint: What's the formula for number of components in a tree?

Ah, that's a hint for E actually. :D

I was wondering...

Expand the formula for dissatisfaction

Got it. Thanks

since ans = summation(a[i]*j + b[i]*(n-j)),so we have to assign positions such that for each i if a[i] > b[i] start filling it from left and from right otherwise ,but since j factor is there ,so more the (a[i]-b[i]),put it most left..

Nice problems!

Any hint for E?

Hint: What's the formula for number of components in a forest?

idk

Number of Vertices — Number of Edges

a forest with V vertices and E edges has V-E components?

First Compress the array by merging consecutive indices with same value in one component.

Consider $$$dp[r] = \sum_{l = 1}^{r} f(l, r) $$$.

Now, we have $$$dp[r] = (\text{number of elements with value r}) * r + dp[i - 1] - \text{number of edges that occur when we add elements with value r} $$$.

the last term can be found by going through elements with value r, and checking its neighbours, If the neighbour's value is less than r, say x, it means for l <= x there will be an added edge. hence you add x to the last term in dp.

Let's consider the starting of any component. Let it start at position i. Now, this is a starting position iff a[i — 1] does not belong to the range [l, r] and a[i] belongs to the range [l, r]. So we'll calculate the number of ranges [l, r] s.t. a[i] is inside this range and a[i — 1] is outside this range. The sum of the number of possible ranges over all i will give you the answer.

Hacks for A and B?

I did one successful hacking attempt in B, and the testcase was:

The only possible answer is

`TAK: C = [2, 1]`

, but I found a submission which is assuming that $$$C$$$ is non-decreasing, so I could hack this solution.what's about test 4 in C?

strictly greater than zero or greater than zero, same things?

Yup

Got wrecked on problem C because of

`double`

precision in C++. I used`double`

everywhere but when I switch to`long double`

it gives another result. Both results are remarkably close to the expected answer on pretest 3. Can someone help me on this one ?On pretest 3:

- Expected answer: 761141116

- Answer I get using

`double`

everywhere: 761034963- Answer I get using

`long double`

everywhere: 761141203Why are you using doubles? Just use ints/long longs and modulo 1e9+7 as you go.

I added the results by "chunks" like it was explained in the problem, so the chunks could get over the maximum long long value in C++

Yeah but just calculate the chunks as their values mod 1e9+7...

You should not be using floating point numbers in a problem like this, where the exact answer can be found using integers.

Solution: It is enough to calculate the sum of values in the range [1..M], because then the sum of values in the range [L..R] can be found doing a subtraction. To find it, we can recreate the process in which numbers are written in the blackboard to find how many odd and even numbers where added. Something like this:

Once you know how many odd and even numbers are the in the black board, you can calculate two sums of the form 1+3+5+... and 2+4+6..., which are easy arithmetic sums (use a formula). In addition, remember that you must perform the operations modulo 10^9+7.

Hope it helps.

Thanks for the answer. I basically tried this implementation, but I thought that

`sum_to_numodd`

could exceed the maximum long long value if`r`

is very big. Looks like I was wrongWell, think about it. The number r fits in a long long, and r = num_odd+num_even (all positive), so those fit as well.

I'm afraid I don't understand. Surely r fits in a long long, but the sum of all even integers from 2 to r doesn't. That's why I think I needed double

edit: well it turns out I have not done your implementation. Sorry for the misunderstanding

Ok, fine then.

Just to clarify in case someone else is also confused: num_odd is the number of odd numbers written in the board, not their sum. Their sum can be easily calculated using the formula for the sum of an arithmetic progression.

After reading B:

Me: OK, randomized algo should work

Inner Me: But there's a possibility it will fail

Me: Very low though

Inner Me: So, you are saying you never had something happened to you that was totally unexpected?

Me: Ok Ok. I will write a dp solution.

Conclusion: Got Hacked 2 minutes before the end of the contest. Then resubmitted a randomized solution anyway because there was not enough time for debugging

Approach for E ?

I haven't solved it But I have some idea with stack、、wait for help too.

Number of connected components $$$f(l, r) = 1 + (\text{number of cut edges}) - (\text{number of cut vertices})$$$

You can calculate total number of cut edges and cut vertices in $$$O(n)$$$ each.

thx

can you please elaborate a little more?

$$$\sum_{l=1}^{n} \sum_{r=l}^{n} f(l,r) = \sum_{l=1}^{n} \sum_{r=l}^{n} (1 + (\text{number of cut edges}) - (\text{number of cut vertices}))$$$ $$$ = \frac{n(n+1)}{2} + \sum_{l=1}^{n} \sum_{r=l}^{n} ((\text{number of cut edges}) - (\text{number of cut vertices}))$$$ $$$ = \frac{n(n+1)}{2} + \sum_{\text{for each edge}} (\text{how many cut occurred on this edge?}) - \sum_{\text{for each vertex}} (\text{how many cut occurred on this vertex?})$$$

Let $$$p = \text{min}(a_{i}, a_{i+1}), q = \text{max}(a_{i}, a_{i+1})$$$

When $$$p \lt l$$$ or $$$r \lt q$$$ then the edge will be cut.

Cut count of edge $$$i$$$ ~ $$$i+1$$$ is equivalent to $$$\frac{1}{2}((n-p)(n-p+1) + (q-1)q - (q-p-1)(q-p))$$$

When $$$l \le r \lt a_{i}$$$ or $$$a_{i} \lt l \le r$$$ then the vertex will be cut.

Cut count of vertex $$$i$$$ is equivalent to $$$\frac{1}{2}((a_{i}(a_{i}-1) + (n-a_{i})(n-a_{i}+1))$$$

A simpler way of thinking about this problem: for each element a[i] count the number of ways to choose (l, r), such that a[i] is the last element in its component.

if n=3 S(l=1 to 3)S(r=l to 3)= 1+2+3+2+3+3 How you derive n*(n+1)/2 If im wrong, correct me.

$$$\sum_{l=1}^{n} \sum_{r=l}^{n} 1 = \sum_{l=1}^{n} (n-l+1) = n^{2} - \frac{1}{2}n(n+1) + n = \frac{n(n+1)}{2}$$$

It's $$$1$$$, not $$$l$$$.

Any ideas about Test Case 8 in problem B?

very poor pretests

How to solve div2 B? what I did is that I maintained a cnt[] array and I didn't registered a number if is has been entered more than once. After this I took maximum element from each row and computed the answer. (I now know this approach is wrong) Any additions here?

The idea look like dp

dp[510][1024]

dp[i][j] means The preceding i-line scheme for forming number “j”

and dp[510][1023] can be optimized to dp[1024]

Basically if a row contains at least two different numbers then there is a solution. If not any row contains two differents numbers, then just compute bitwise xor of a_(i,1) for i in range (0,n). If it's 0 then the answer is

`NIE`

, else you have your solutionOh no... it really was THAT simple!!! Thanks, brurrito.

observation: if a != b, a ^ b > 0 (in this problem i used bruteforce).

fix a row that has > 1 unique value (if there's any, but if none than you can pick any row).

let k be the fixed row, after that you xor any value of a[i][0] excluding that row (that is a[0][0] ^ a[1][0] ^ ... ^ a[n — 1][0] ^ a[k][0]), let the value val.

now you try all possible value x in this row k, if any val ^ x > 0, then you found the answer (n — 1 column 1 and 1 column in the fixed row).

My submission: 52983517

Carry a cell from each row, as your wish... if the xor of all values equal 0, then just try change any of those values from any row..if it is not possible, then NIE

Any tips for B?

If x is not y, then (x xor a) is not (y xor a).

Randomly assign one number from each row. If the xor of all the assigned numbers is greater than 0,the problem ends there, else changing any one number among the chosen ones will change the xor. Try replacing each number in the matrix and when the xor is not equal to zero, that's one solution

I'm sorry. I didn't realize you asked for tips and not the solution

Well, you can consider each bit separately (there are $$$\leqslant 10$$$ bits). Now for $$$\operatorname{xor}$$$ to be non-zero you need an odd number of $$$1$$$'s. If the answer is 'yes' for at least one bit, then you have found a solution.

Look at it as a dynamic programming problem.

`N <= 500, A_{i,j} <= 1023`

Try them all and save the answer.Spoiler - dp statesThe dp states are : dp[row][current_number]

Time complexity and my sub`O(N^2 * MAXA_{i,j])`

My sub: 52973863I don' t know how to use latex or markdown in comments, sorry for bad formatting.

thanks for your help!

Carry a cell from each row, as your wish... if the xor of all values equal 0, then just try change any of those values from any row..if it is not possible, then NIE

What is the difference between greater than zero & strictly greater than zero ?

My solution for pE was :

First, calculate the numbers of the interval that each vertex will leave in the tree.

Second, minus the numbers of the interval for each pair (i,i+1) that there is an edge between them.

Very very weak pretests on problem B.

Could I add another "very"?

My solution was 'sorting' the array and I was printing the modified indices and somehow that passed the pretests. In, normal occasions this solution should not even pass the samples.

How to solve E ?

https://codeforces.com/blog/entry/66576?#comment-506224

One of the worst contests IMO. Very Unbalanced.

Agree that they should have swapped C and D instead, but I think many of the problems require nice insight (like B, D and E).

I think this contest was better for the higher rated participants in Div 2. But for people like me and below, it was hard to get that insight.

Ok, i don't want to be "that guy", but someone has to say it. I think that there were at least 2 coders who participated in this contest from the same account. And yes, i am referring to the user square1001. Time of submissions B and D are < 3 min , and between C and E is only 5 min. While i think that it is possible for someone to get the idea for them in a few minutes + reading the statement, i really doubt that they can code like this. And anyway, if you are that good, why take order B,D,C,E? Why not straight E,D,C,B? Also, coding styles seems to differ, so yeah, definitely something to look into, beloved admins.

Please do not doubt — I am pretty surprised and happy that I won this round, and I was lucky that ecnerwala participated 30 minutes after the contest begins (if he participated from first, I think I lost).

So, why did I take order B, D, C, E, A, F? I did pretty strategical.

- First, opened problem B because I thought that opening problem B is lighter for servers than opening problem A. Then solved in ~3 minutes. It was not so difficult.

- Second, opened problem C. But updating standings every ten seconds, I found out that problem D is already solved by someone! Hence, I immediately moved to problem D. I have came up with the solution in ~15 seconds, and the implementation was easy, so I solved in like ~2.5 minutes.

- Then, re-thought problem C. Implementation was not so difficult, but my code bugged so it took ~6 minutes to solve.

- In this time, the solver of E existed, so I moved to problem E. I had came up with the idea of "we should only care about difference in position $$$i$$$ and $$$i+1$$$ (+ two sides)", 1 seconds after I read the problem statement completely. The implementation was easy, so I could solve in 5 minutes.

- Then, I moved A and solved easily. I had a pretty difficult time in solving F, because my matrix library is really slow (but the TL is 1 second), so I coded without using own library.

I am very happy to win in such contest, in contrast to last contest taking 30 minutes in easy Div.2 B.

P.S. You said that the coding style differs. What pair of problems are you pointing?

I think code for D can be written in 1 min. And if you get the idea straight then it's an easy problem. Just read A. Statement and so many conditions, but still people have submitted in <3 min. Where i found A hard than D to code and understand. So writing code in 2-3 min for D is fine. Even problem E is too easier, if you know the logic. Just count of cut edges and cut vertices. Thinking may take 3-4 minutes. But code can be definitely written in 1 min. So I don't see any kind of cheating. You just go through other good coders' submissions, most of them have solved D, E in <= 5 min.

Got my first successfully hack and first system test failed on PB lol

What was your approach on last problem F?

You can change the problem into weighted graph that has $$$4Hn$$$ vertices and up to $$$16 \times 4Hn$$$ vertices. Use

binary jump(I don't exactly know the formal name of these kind of techniques) to calculate after $$$k$$$ moves in this graph.For your information, $$$nHr = n+r-1Cr$$$.

I couldn't code my idea in last 20 minutes.

not)) this is just boolshit))

Your approach is clean and compact!

Let $$$z$$$ be the number of zeroes in the array. Consider the block of first $$$z$$$ bits: array is sorted iff it's all zero.

Let $$$dp[i][u]$$$ be the probabilty that after $$$i$$$ steps we will have $$$u$$$ ones in the first $$$z$$$ bits. Then the answer will be $$$dp[k][0]$$$.

Starting from $$$dp[i][u]$$$ there are three possibilities after the next step:

$$$u$$$ didn't change. Either we swapped something inside a block (there are $$$z \cdot (z - 1) / 2 + (n - z) \cdot (n - z - 1) / 2$$$ ways to do that), or we took same bits from different blocks: $$$u \cdot (n - z - u)$$$ ways to take two ones and $$$(z - u) \cdot u$$$ ways to take two zeroes.

$$$u$$$ increased by one. This can only happen if we took a $$$0$$$ from the first block and a $$$1$$$ from the second. There are $$$(z - u) \cdot (n - z - u)$$$ ways to do that.

$$$u$$$ decreased by one. Then we took $$$1$$$ from the first block and $$$0$$$ from the second. There are $$$u^2$$$ ways to do that.

You can easily change the calculation of this DP to backward and then use matrix exponentiation.

how to solve B?

Carry a cell from each row, as your wish... if the xor of all values equal 0, then just try change any of those values from any row..if it is not possible, then NIE

Such a fast editorial. Nice!!

Everything so fast...system testing + editorial + rating update...

Finally reached

Candidate Master. It's sad that I submitted my contest proposal when I was Expert.. But whatever, I am speechless and so happy.the problem C can scare, but in fact easy, the most difficult, thing is to keep track of the overflow, but the python decides everything))

https://codeforces.com/contest/1151/submission/52983378

Any hint about C ?

If you want the whole solution, you can read the editorial. If you just want a hint, try to think of a formula by which you can solve this. It's similar to calculating the sum of the first

nnumbers, you just have to change it a little bit. :)And the statement,

`I strongly recommend you to read the statements of ALL tasks (and of course, try to solve them)`

makes sense :P.Spent way too much time on observing patterns to arrive at an $$$O(\log{N})$$$ solution. The excitement of reading a math problem had got the best of me.

order of this contest should be A,D,C,B,E,F

I can offer other order A,D,B,C,F,E

I found c easier than b,b was implementation heavy

I got a message that says my submission 52964771 coincides with sandrik398631‘s 52963443. They look really similar. But the problem A is too simple and my coding style is somewhat similar to sandrik398631. How can I appeal? All my submissions are skipped. Is it because of of this?

Can B solved by DP?

Yes, B can be solved by DP. You can maintain a DP of size 510*1024, Here dp[i][j], i is the row number and j is the xor of numbers. So, all dp[i][j] values will be 1, if we can have xor j at an ith row, otherwise, the value will be 0. If the value is non zero, store the parent i.e. the element in (i-1)th row which xor with current element will give the xor j.

Please look at code for more clear representation.

Thanks shreyansh08

I have done following in problem C. Function calculates the sum in set till number n.

ev — next even sequence start od — next odd sequence start

then for each iteration, I calculate start and end of that sequence and calculate their sum by AP. It is giving wrong answer on test case 1 1e18. Is there an overflow ? I cannot find the bug in function calc.

code link

CODEI have written a C solution that I would consider well documented and can be easily understood by anyone. It might not be the shortest or cleverest solution, but I believe it's nice to have it this way. Please comment where this solution can be improved. Thanks in advance.

I noticed that a lot of D solution only sort using the key

`a-b`

, and I wonder why it is acceptable. Rearrangement of the dissatisfaction value $$$a_i(j-1) + b_i(n-j)$$$ gives $$$(j-1)(a_i-b_i) + b_i(n-1)$$$. To minimize the sum of all dissatisfaction, we would assign small $$$j$$$'s with large $$$(a_i-b_i)$$$, when the position of the student does not affect the term $$$b_i(n-1)$$$. What I does not understand is that when various students share the same $$$(a_i-b_i)$$$, the value of $$$b_i$$$ itself should make a difference. However, it doesn't seem to be the case. Am I missing something?EDIT: After thinking more carefully, I realize that it really doesn't matter. Their dissatisfaction value would be different, but we are not trying to arrange them in order of dissatisfaction anyway.Congratulations to the winner！

The link to the editorial redirects to home page. Anyone else facing this issue?

Can someone make links to the editorial and to the contest announcement in the contest page? They do not exist for me here: https://codeforces.com/contest/1151