Hi everybody,

This Sunday there will be a 16th Moscow Team Olympiad, high school students competition based in Moscow that is an elimination contest for All-Russian Team Olympiad. This contest is prepared by Moscow Olympiad Scientific Committee that you may know by Moscow Open Olympiad, Moscow Olympiad for Young Students and Metropolises Olympiad (rounds 327, 342, 345, 376, 401, 433, 441, 466, 469, 507).

Round will be held at 10:05 UTC on Sunday and will last for 2 hours. Each division will have 6 problems.

Problems are prepared vintage_Vlad_Makeev, Glebodin, Andreikkaa, qoo2p5, mingaleg, Flyrise, cdkrot, achulkov2, grphil, Sehnsucht, Aphanasiy, Sender, DebNatkh, GreenGrape under my supervision with great help of GlebsHP, meshanya, Endagorion, Zlobober and Helen Andreeva.

Thanks to cdkrot for the round coordination and statement translation, and also thanks for MikeMirzayanov for systems codeforces and polygon, which was used to prepare problems of this olympiad.

Good luck everybody!

**UPD1:** The scoring distribution will be:

500 — **1000** — **1000** — 1500 — 2000 — 2500 for div. 1.

500 — 1000 — 1500 — **2000** — **2000** — 2500 for div. 2.

**UPD2:** Editorial

**UPD3:** Winners:

Div. 1:

Div. 2:

will it be rated for any division?

There is a separate round for div1 and div2.

Unfortunately clashes with GP of Korea.

where is the contest "GP of Korea"?

unfourtunately that's on my school time :(

On Sunday? :o

Yes, in arabic countries the firday is a day-off, not the sunday

Lol, this comment chain is a copy of this one from the first Moscow Team Olympiad held on Codeforces.

Guess somethings never change xD

i wish there a country where every day is off the school :P

And Hii Bau :D

hi sguu

not only Arabic countries , in Iran the friday is a day-off.

In many Chinese senior high schools, Saturday and Sunday are school time. ( >﹏<。)～

very real

A Russian Olympiad clashing with open cup, why am I not surprised?

We're going to write it tomorrow official and my team is really scared of Mr.GreenGrape in problemsetters

Perfect time for Chinese users!

%%%

Perfect time for Korean users, as the contest is in 7PM, and they can't take today's GP!

GL&HF

hope not a mathforces

Really bad time for US users :(

Just go back to sleep after the contest :)

Really good time for Chinese users :)

Perfect time for Chinese users.Thank you Codeforces!

The site has slowed down significantly.

In Div 2 problem D, the time limit is very strict. Python solution not passing pretest 8 even after all possible optimisations.

Div2 D pretest 13?

Yes, this one killed me too

Mine too gave WA on 13, using DFS. Bypassed it by using BFS. Probably recursion stack size was the issue.

the same experience as yours,but my bfs was hacked because i didn't use priority_queue.

I too got WA, solved using DFS!

Interactive Problem and Binary Search again.

What were the hacks on Labyrinth about?

InputOutputfor those who used a single queue.

UPD: It seems that not everyone used a single queue failed on this test.Then my D will fail systest

Careless boy

I am using single queue but i pass this test

Same situation in my case.

Lol?

I used a single queue and passed systests in 140ms.

Could you elaborate on why was that supposed fail?

Your solution works cause you're checking for a better answer on

Which is pretty much the same thing as removing the if and making sure you visit the node with the least amount of horizontal moves as possible (i.e., by using a priority queue and sorting by increasing number of horizontal moves)

CSAcademy saved me on E with the geometry tool.

yeah i managed to hack the Grandmaster last minute!!!

After one hour of rocket science, I realised that answer for C is.

`sort(s.begin(), s.end()); cout<<s;`

So the correct answer for

`asasas`

is`aaasss`

, isn't it`asasas`

better? First one has 8 palindrom substrings and the second one has 12.First one also has 12

First one has:

a

a

a

s

s

s

aa

aa

ss

ss

aaa

sss 12 in total.

Now I understand, thank you all. :)

No, first one too has 12 palindromic substrings..

`'a - 3 times, aa - 2 times aaa - 1 time, total - 6`

and similarly 6 for

`sss`

so, 12It's 12 for both. Single character is also a palindromic substring.

what is the correct approach to this problem?

It is a correct approach, which can be proven.

Basically the idea is that if you consider any palindrome, its both ends must have same character.

This implies natural bound on the number of palindromes having end of character c occuring

xtimes:x(x+ 1) / 2.One can see, that sorted string reaches that bound exactly.

I bet there will be lots of FST (failed system test) on Div1.B/ Div2.D, since lots people are using queue instead of priortiy queue, which will results in TLE. Though, it is not easy to hack them :(

Deque is enough, thought

Wait. I don't understand. Wouldn't the same element be put into queue for more than one time? Just like SPFA? Does anyone want to explain that to me?

Usual 0-1bfs — deque + array that indicates visited cells

You won't visit cell with better cost than first visit.

First time (using 0-1bfs) — cell will be visited with L left moves and R right moves, all other times it will be L+k and R+k where k>=0

P.S. I hope it understandable enough

So the cost will be different if you move vertically instead of horizontally, right? which means the cost in your queue can be not monotomous.

No, it will always be monotonous.

The idea, is to push a vertex at back of deque if the weight is 1, and at the front of deque if weight of edge is 0. Since, there can be atmost two levels of weight in queue, it will always be monotonous.

My submission

You can read about it here

Oh! Use deque, then it is always monotonous! Such a neat application of deque! I got it. Thank you so much!

I didn't think of why you use a deque instead of queue. Now I get it. Thank you!

I am eblan.

whats the correct approach for div2 d?

0-1 bfs

I have done same but got TLE in pretest 8

you used map, but it is slow here

So how to speed up my solution? using unordered_map ?

Use 2d array

Thank you very much Numb! :)

so why does bfs fail ??

usual bfs will find (8;3) with 5 lefts and 2 rights and won't go to (8;4)

If we were to use dijkstra's instead, do we sort the items in the priority queue based on the number of left moves, or the number of right moves, or does it not matter? And also, do we sort it in ascending or descending order?

the number of left plus right moves, ascending order (firstly take the least)

By 0-1 bfs do you mean we will have to take n*m*4 states.I was doing the same.But I could not submit the code because of a bug I had in my code.But my code passes the test you have given above.

Why 4nm?

Because n*m states for each cell and 4 states for from which direction i entered to that cell.Otherwise if i take n*m states then it was not unique enough and creating conflict.

Why do you need the information "from which direction i entered to that cell"?

Yeah.The extra state is not needed.My code is giving correct answer without it.Thank you.

ignore

I had RE 8 on D problem because of pragma

pragmaI think it can be a problem of new servers. MikeMirzayanov, can it be fixed?

I also got RE 8.

I too got RTE on pretest 8

how to solve F?

Let

dp(i) be the maximum length of a journey starting at positioni.Then,

dp(i) ≥xiff there exists suchjthati+x≥j,dp(j) ≥x- 1 ands[j,j+x) matches eithers[i,i+x) ors[i+ 1,i+x+ 1).You can get different and solutions with the following observation:

There exists an optimal journey such that these conditions hold:

1) The maximal length of a string in the journey is .

2) The difference in lengths of two consecutive strings in the journey will always be 1.

Mine didn't pass but maybe someone passed with such a solution.

We believe shouldn't pass.

Try thinking about solution :).

It passes in 2.6 seconds. Turns out using a suffix array to help finding the groups of suffixes that have lcp >= i makes it a lot faster. http://mirror.codeforces.com/contest/1063/submission/44310989

My solution is . Not sure if it will pass.

I am going to assume the string to be reversed.This way the strings in a journey are going to be increasing in length.

We can prove that there always exists an optimal answer when

t_{i}has lengthi. Now, letdp_{i}be the largest possible length, such that the last string ends at positioni. Note that if we can have a valid lengthlending ati, we can also have a valid lengthl- 1, so it makes sense to define such a dp state.We can binary search on

dp_{i}. For a lengthl, the second last string is either same ass_{i - l + 1... i - 1}or the same ass_{i - l + 2...i}. We find the rightmost such string which doesn't intersect the last string, and check if this string could be used according to the dp state.Finding the rightmost possible second last string can be done using persistent segment tree in . Basically, we have a range on the rank in suffix array the secondlast string can belong to, and a maxmium possible rightmost index for the secondlast string as the constraints.

It won’t :(

Try to replace binary search with some monotonic observation.

Passed with small optimizations. Sad that both the accepted solutions have worse complexity.

Time limit for Problem D is too strict :(

Not at all. It can be solved in O(nm).

my solution is also O(nm) but i am using map which give me TLE on test 8

So your map don't have a log?

I am using map<pair<int,int>,pair<int,int>> which is very slow When I get the idea why my solution is slow I am very late

Each access is

O(log(n)) time, which seem to be timing out under the current constraints. Instead, try using a 2D array of pairs, so that the time required for each access is constant.Thanks! after replacing map with 2d array i passed the time limit but I got wrong answer on test 33. Do you have any idea why my solution is incorrect on test 33 http://mirror.codeforces.com/contest/1064/submission/44324094

How to solve Div1 D?

Think of solution.

My solution for Div2 D is failing on test 13. Could anybody help me where I am making a mistake? Thanks!!

The time limit is too strict, possibly due to recursion and stack size.

As far as I understand you did a dfs and checked the conditions and you dont visit the same place twice. However consider this

.....

.*.**

.x.**

x is where you are initially.

lets say you can go left at most once and right at most twice.

Then if you go left and go up twice and go right twice you will mark (0,2) as visited and when you come there again from (2,1) -> (2,2) -> (1,2) -> (0,2) you wont continue since it (0,2) is visited however you went right only once and if you continued you could go right.

Expected answer is 9. right?

Thanks bro i got my mistakeSo what's the correct approach to solve it using DFS? Thanks for pointing out the mistake though.

It's very hard to (if possible) because dfs doesn't have properties of shortest paths.

Ok Thanks that makes sense.

What’s pretest 10 in div 2 E?

Probably something like 1 black and 29 white.

Yeah I was getting wrong answer on pretest 10

n= 30, jury mostly assigns colors randomly.However there is a small twist: in all of jury tests (except for sample) if it is possible to assign a color, such that it makes impossible to separate points later jury will always do that :).

Div 2 Problem C was tricky. I can't prove solution correctness. Can anyone give me a proof ?

answer is just sorted

s. ifN_{x}is number of occurrences ofxins. then number of palindrome substrings because first and last letter of palindrome must be same. on the other hand this number is achieved whensis sorted.I am trying to understand this formula you stated.

is number of ways to choose substring so that it starts and ends with

xShouldn't it be (nx) * (nx — 1) / 2 then ?

Number of ways of picking 2 things out of nx. (Combinations)

substring can have only 1 character

Oh thanks!

Got the same formula after accounting for substrings of length 1 (adding Nx to my formula)

I am still not able to understand this formula. The no. of palindromes in a string of length n having all characters equal to c will definitely be n * (n + 1) / 2 but how does that justify that summing over this expression over the frequencies of all chars in a string having more than one character gives an upper bound on the no. of palindromes that we can have in a permutation of the string ?

see this comment

That helped. Thanks. :)

gag forces?

With new Judging infrastructure, I hope System testing would be a bit faster.

So that i upsolve after that :)

C and D combined made this the worst problemset in my opinion I've seen so far in codeforces.

C was binary search. Nothing else, just binary search, just hidden behind a silly problem statement about lines. There's lots of complaints about every interactive problem being binary search and then we get new problems like this :/

D was a math problem emphasizing exactly what I hate about most math problems. You just have to make a trivial observation (either n or the number of rounds is small), and then calculate stuff. Of course, the calculation isn't interesting either, It's just super annoying case handling.

The especially annoying thing about D was that the last person can take just one candy, even if they have a sweet tooth. That adds nothing to the creative part of solving the problem, and around doubles the amount of case handling. Why was that there?

In my recent memory I don't remember any problem worse than either of these, and now we got two of them on the same round :/

I 100% agree. Missed that sweet tooth — one candy case for 30 minutes during contest...

Well, while I will agree that D had hard technichal part, I not very much agree with your complaints on C.

You just needed to come up with cute binarysearch-like construction and the implementation is very short. If the problem was obvious to you -- just get AC in 5 mins and move on!

For me div1C was not trivial, and had a lot of challenges, not just "implement binary search". I don't think the geometrical construction was obvious at all.

In problem D Div 2, why dfs not working ?? thanks

(edited) dfs won't work, because you can end up in a square with "wastefully expended left/right moves". Meaning, you could end up in the same square later with fewer left/right moves. What do you do at this point? If you reprocess the square and start doing dfs again from it, you will TLE for sure. If you don't reprocess the square, you might get WA if later on you fail to reach some square because you had wastefully expended left/right moves earlier.

edit 2: or maybe dfs will work if you can do dfs in such an order that you never wastefully expend left/right moves?

thanks, baobab. I have found the mistake :)

Consider you started from vertex 'X' and at some point you are at vertex 'Y'

DFS ensures that you visit every vertex only once so you won't come to vertex 'Y' again.But as the graph formedmight be cyclici.e. there can be multiple ways to reach a vertex 'Y' from 'X' andthere can be a possibility that there is some shorter path to 'Y' that you'll miss.PS: Dijkstra :)Also: BFS

Dijkstra can be fast enough on C++ but on Java it apparently wasn't: https://mirror.codeforces.com/contest/1064/submission/44311014

I got trolled by div2 C lol :/ Wrote 80 lines of buggy code...

My solution in python:

Oh sorry, I meant div2C. My solution for B was the same idea.

Test 40 Div2D distroyed so many dreams :(

There goes my hopes of entering div1 :(

wat is hope?

weak pretests in div2 D

The Div1 problem E about lasers is beautiful in my opinion, thanks to the authors!

You are very welcome :)

I guess that my solution to F has complexity

O(n^{2.5})... I implementedO(n^{1.5}·log(n)), but it turned out to be slower and more memory-eating, so I resubmitted older brute force, which passed with max time lower than one second.WOW, and my solution failed!

OK, OK, sorry, it's

O(n^{1.5}), just noticed some amortization.I GOT TLE IN TEST 10,IN DIV 2 C MY SOLUTION time complexity in o(nlogn) but still it got timed out. is it due to slow input output?

Actually it could be O ( N ). Flash sort!

Can somebody please help me finding why my code for DIV2 D gives a run time error?? CODE

The difficulty curve was.. 98.9%, 75.2%, 73.7%, 5.9%, 2.1%, 0.4% (percentage of people who solved each problem)

Perhaps codeforces should consider including problems solved by between 6% and 73% of participants???

Impossible

It was a bit... unexpected to us.

We held a presolving of the moscow team olympiad, from which the problems are. And most of the teams got accepted all problems except problem corresponding to Div1F.

So I was even worrying whether our Div1D and Div1E are a bit too easy.

maybe the next time the presolvers group should contain not only red and orange coders, not only ROI prizers and ROI winners?

What's Test 27 in Div2 D?

Can someone explain why Div2 D Test 40 failed for me??

44306721

Just go left, straight up and down, then go right. You can visit all the free cell. 0-1 bfs can solve this problem. Because moving up and down costs 0, you should push one colomn into the queue at the same time when visiting a new cell.

but what is the issue with normal bfs even in normal bfs i am trying to find the node closest to a popped node so that the nearest nodes are reached first.

Normal bfs just visits the nodes as quick as possible. This means it may go left and right for several times and reach the limits. But it is possible that existing a path which is longer than the first one but turn left or right less. In this situation, bfs will visit a node that already been visited, and won't push the new node into the queue, leaving some of the nodes not visited. My code

Thanks GGAutomaton understood :)

How to solve Div2 E??

Use binary search! Put x somewhere to the middle (from 1 to 999999999) and fix it (x=3, for example) and try (3,0) (save the color). Next try (3,500000000). If these two point have the same color next point you should try is (3, 750000000) if colors are different next point should be (3, 250000000). Beware of edges cases: one with all points with the same color and when another one only one color is different 44313158 (very bad code, sorry)

Can someone look into my solution for DIV 2 D problem 44308344 and tell me where I am going wrong. Thanks :)

How would you guys come up with the solution for problem C, Div 2? And how would you test it to make sure that every time you sort the string’s characters you get maximum number of palindromes?

Sixth sense!

I manually checked it for random strings of length 10

I was trying something on paper and I suddenly saw that for

`aaaaab`

the count is much higher than its other permutations, once I realised that I could prove it easily as :suppose you are counting palindromes with

`a`

as it ends, now if you insert anything in between two`a's`

, that may decrease count by 1. Hence all occurences of a character must be together.where is the editorial?

DIV1 B:

Suppose, system tests are weak.

This solution based on simple BFS and single queue (not BFS-0-1) 44319324 passed all the tests.

UPD: tests are not weak, my solution differs from BFS in visiting vertexes twice and more times.

I think this solution is right. It has right answer on my hack

This is equivalent to SPFA (or Bellman-Ford with queue). It is a correct shortest path algorithm, and although its worst case complexity is

O(VE), practically it's very fast for most graphs.SPFA can be hacked by specially constructed graphs, but I don't know whether such graph exists given the restrictions of this problem. Also, since SPFA is not popular outside China, non-Chinese problem setter usually don't make anti-SPFA tests.

Disclaimer: My solution used the same algorithm.

Thanks for the information, didn't know neither about SPFA neither about Bellman-Ford. There is a reason to read something.

Finally, found the difference between my solution and simple BFS: in my case one vertex can be visited once again, if there is better path found.

I've just known that some of the solutions implement BFS using deque instead of a queue, which allows them to keep the queue monotonous. Then the complexity is the same as a standard BFS —

O(V+E), which is correct. However, I do think those solutions using queue is hackable, but there might not be anti-SPFA tests.Div2 D Time depends a lot on Compiler, same solution in c passed and c# TLE

It's possible to pass Div2 D using shortest path if you use a faster heap in C++... http://mirror.codeforces.com/contest/1064/submission/44318340

Even built-in priority_queue worked fine for me. Here is my solution.

simply use deque and it passes in tenth of your time.

How to solve Div2 F? Can this problem be solved with binary search？

Can anyone tell me why i am getting runtime error on pretest 8 44315444 Please help i am not getting the mistake

Hey, ch_egor MikeMirzayanov cdkrot, my submission 44312315 gave TLE during contest, but identical submission 44320638 gives AC now. Can anything be done about it?

Nothing can happen now. Same thing has happened with me twice, but they don't re-evaluate our code after the contest. See the codes 37815677, 37820871. They are identical but one gave TLE during system testing while the other one passed after the contest. I also posted about it in a comment, but no one even cared to reply. I also messaged Mike on CF, but he also just ignored the message. Here is the link for the discussion.

Dude, your identical submission also nearly TLE'd. So the TLE during the contest seems reasonable. There is no reason to judge your solution again.

shorest solution I ever encountered:

31086861

probably it is shortest for div 2 C?

26869940

did you write a script to find that?

no

i just remember

Cool :)

what is this c

ugh

Is it possible to get the complete input for test #10 in div2D? I'm really curious what I did wrong in #44322132 and can't get it...

Well nevermind, found my mistake.

BFS with one queue is fine for div2 D: 44322392

Thank you for pretests, GreenGrape

Joke’s on you, but I was involved only in d1F.

You stole my rating and now you’re trying to steal my contribution

Funny, its the first time i have seen author trolling the participant xD

So much math.

Test 40 for Div2-D be likeHow to solve Div 2 B i didn't able to find the approach

Brute force solution for

afrom 0 to 20 and try to find a connection between a and the answerThis is a good tip in general, it's way easier to prove if something in particular works (or doesn't) rather than finding the solution itself

just observe that since

If i'th bit of a is 1, then if i'th bit of x is 0 or 1, in both cases i'th bit of is 1, and we are safe.

But if i'th bit of a is 0, then i'th bit of x must be 0, i'th bit of will be 2,i.e, greater than 0. which will make greater than a.

hence the answer is

But in case when ith bit of a=0 and x=0,

will also give 0 which is equal to a right?

Yes, that's the point, if i'th bit of a is zero then i'th bit of x must be zero.

I made little mistake in my statement, edited that now !!

DIV1 B:

Suppose, system tests are weak.

Solutions: 44298986 44296043 Test:

Solutions have different answers: "1" and "2"("1" is correct answer).

In Div2 C,Can Anyone explain why the sorted String is the best answer.

See this link: http://mirror.codeforces.com/blog/entry/62440?#comment-463988

Thanks

Hello, could you throw a link to the analysis of this round

Nice Contest overall! Thanks Codeforces.

T̶h̶e̶r̶e̶'̶s̶ ̶p̶e̶o̶p̶l̶e̶ ̶a̶s̶k̶i̶n̶g̶ ̶f̶o̶r̶ ̶t̶h̶e̶ ̶e̶d̶i̶t̶o̶r̶i̶a̶l̶.̶ ̶T̶h̶i̶s̶ ̶i̶s̶ ̶t̶h̶e̶ ̶e̶d̶i̶t̶o̶r̶i̶a̶l̶.̶ ̶I̶ ̶g̶u̶e̶s̶s̶ ̶t̶h̶e̶ ̶a̶u̶t̶h̶o̶r̶ ̶f̶o̶r̶g̶o̶t̶ ̶t̶o̶ ̶l̶i̶n̶k̶ ̶i̶t̶ ̶h̶e̶r̶e̶?̶ ̶

̶h̶t̶t̶p̶s̶:̶/̶/̶c̶o̶d̶e̶f̶o̶r̶c̶e̶s̶.̶c̶o̶m̶/̶b̶l̶o̶g̶/̶e̶n̶t̶r̶y̶/̶6̶2̶4̶5̶5̶

I was busy and came home a few minutes ago.

Sorry I didn't realize editorial and announcement authors were different people

I'm getting TLE when I'm not using if(visited[x][y]) continue; // for checking whether the node is visited before or not here is my code link https://ide.geeksforgeeks.org/bcROnzM4qJ

It's possible for BFS (0-1 variant too) to have repeats of same element in (de)queue, so this is actually to be expected.

For example, try drawing a "triangle" graph (three node cycle) to convince yourself. You will see that two of the nodes repeat.

## Where are the winners?

can anyone help me with div2. D i tried floodfill but it got WA on test case 5

my approach now is to BFS but i get WA in test case 40 https://mirror.codeforces.com/contest/1064/submission/44454781

See my comment here,it may help you

How to solve div 2 F? Will there be tutorial?