Hello! Codeforces Round #581 (Div. 2) will start on Aug/20/2019 17:35 (Moscow time). The round is rated for everyone whose rating isn't greater than 2099.

The problems were invented and prepared for you by danilz1806, baumanec and sorry_marymarine. Thanks to Anton arsijo Tsypko for the coordinating the round. Thanks to the testers for testing and giving advice:

- Ariel juckter Garcia;
- Anton antontrygubO_o Trygub;
- Mikhail PikMike Piklyaev;
- Mriganka Basu mbrc Roy Chowdhury;
- Tiago Figueiredo tfg Gonçalves;
- Noam Noam527 Gutterman;
- Stefan stefdasca Dascalescu;
- Khaleel Adhami Al-Adhami;
- Rahul Rahul Goswami;
- Mohamed MohamedAhmed04 Ahmed;
- an incognito tester;
- Bencil BencilSharpeniro Sharpeniro;
- Luca LucaSeri Seritan;
- Alexey dalex Dergunov;
- Zain ZieiN Alabedeen Ali.

You will be given 5 problems and 2 hours to solve them. The score distribution ~~will be announced later~~ is **500-750-1250-(1500+750)-2500**.

The round is over! Congratulations to the winners:

1. 79brue

2. sinus_070

3. baluteshih

4. xpporzwzl

5. shahaliali1382

you forgot Numb

to you he is mister master

for uno i'm not

Mr orange, I was your alt for half a year, and you didn't asked me to test your contest?

Yes because why should i ask myself for testing my round dude

since you didn't liked Twice we disbanded, so you are not my alt anymore!

Yout better say thanks to Mike MikeMirzayanov Mirzayanov for the amazing platforms Codeforces и Polygon if you want your round to be rated)))

thanks to Mike MikeMirzayanov Mirzayanov for the amazing platforms Codeforces и Polygon

15 peoples are tester for contest. <3 I thinks this round don't need hacking. I'm afraid =))))))

The great Mr. Master Numb has already predicted this.

it's not hard to predict this

All the best for your 1st contest :)

Bencil Sharpeniro orz

That army of 15 testers...

The blue dude Ziein is Spiderman XD

.

Problem A: The first line contains a single binary number s (

0 ≤ s< 2^100)without leading zeroes.Okay guys. Well played (^_^)

Me: -5, Testers: 1e5

string contains only a '0' isn't counted as one with leading zeros .. a leading zeros string is the one with length more than 1 and contains leading zeros

Actually string 01 also has leading zero:)

sorry, fixed it

Thank you all for your efforts. We wish we had an extra lifesaver 10 minutes as in #580 :)

Get ready to become newbie

Thank you for your support :)

hello sorry_marymarine,

i am predicting this to be great contest,, because i know for facts that BencilSharpeniro is a very good problem testing man,

last time he giving me the problems, i felt like it was a good problems, so i having the faith in this contest and qualities of this contest,,,..,,..

good luck and high rating mr master, rajeshwar~~

Of course. I can assure you that the grammar for problem A is on point. I ran it through Grammarly just for you bentaji <3.

So, grammarly servers has the problems in its logs. Any grammarly devs here?

Hope the statement will be short and easy to understand. Thanks :)

Good luck and high rating to all :)

I am so psyched.

Desire for becoming Candidate Master tonight!! :)

Good luck! Hoping to get master

You won't

thanks mr master

orz you can tell the future

Me too!!! :)

Thank you, "spsk spsk"

there is a problem with account link ? "The problems were invented and prepared for you by danilz1806, baumanec " both link to ur account

He owns half of the master accounts on Codeforces

Are you him?

I refuse to give information to the penguin intelligence agency.

but they share the same link with the same profile

i mean when u press on "danilz1806" its gonna open "sorry_marymarine" profile

sorry for bad english :|

When you change handle, your old handle links to your account iirc.

Nice idea to overcome downvotes u got b4.

Please fix long testing queue for pretests during contests. It's really annoying to wait for results during contest . Thanks

Questions made are excellent but I would suggest to improve the testing queues during submissions. I can understand it is a complex job to do given that many submissions are made in a single instant but if this fault is cleared, then people will be more motivated to take part in such thrilling contests. :)

Please read previous comments before writing one.

I don't know how to tell you, because I'm only a pupil.

Tiat is so cute! (Tiat is the girl in the picture)

I will become expert after the contest :))

woooo！You are so great that you will become expert！

Is it rated guys??

Coz, I know Nothing.

It is only rated for women

Can you PLEASE share your algorithm to identify female coders?

So that means I will get +215

With such army of tester, I expect super strong pretest.

quantity =/=> quality

although, it's good to be optimistic.

A has heuristics solution and the actual solution is div1E

When I finished 2 Ds, I felt scary. But when I saw my comment here, no more fear.

Hoping to get the last point to be a Candidate Master!! :)

About 20 people trying to make a contest for us. How lucky we are. :D

The Final Testing'll be directly After The Contest or After 12 hours of Contest end :) ?

15 peoples are tester for contest. <3 I thinks this round don't need hacking. I'm afraid =))))))

I just solved some problems and gave some advice to make statements better, I didn't try to submit wrong or tricky solutions. Most of other testers too, I think.

<3

For me you are Mr Master.

clashes with Mineski VS Na'Vi :(

why I can't get registered

over time

what it mean

Where is the link to ask a question? I think I rember that on every problem page there was such a link, but can not find it. :/ Is it gone? (Maybe caused by to much stupid questions ;)

In the problems page.

BINARYFORCES!!!

The word "subsequnce" appears 7 times in the problem statement of D1 and D2

haha

How many time subsequence appear as a subsequence in the problem statement?

maybe I got it. my bad

Ask a question

Better dont discuss problems while the contest is still running.

Uh, why does solving D2 not auto solve D1? RIP penalty. ;_;

When did it ever do that

In my dreams and imaginations -- Pretty much what happens after one engages to work in the industry and cares about user experience. Kappa

What is test case 13 for C?

My score has

plungedto532Calc Pro XD

You gotta be kidding me :(

How did that happen? What is the logic behind it?

He made a lot of wrong hacks.

I hope this is the first and last time i ever see 998244853 in a contest.

What is the problem with 998244853?

It's not 998244353.

The algorithm NTT often modulo 998244353,and 998344853 looks like 998244353

I apologize for that eyes are not needed in coding competitions but Ctrl-C+V. I have thought the number was just that number.

Agreed, I prefer 993244853 instead.

Does anyone know why I TLE on pretest 13 of prob. C. I'm pretty sure my algorithm runs in O(n^3 + m) worst case which should fit in the bounds.

Nice task E, thanks)

What was the solution for E?

dp or mathematical derivation

How to solve C ??

I tried greedy, but I got WA on 5th. Never mind, I had a bug. It is a greed algorithm. O(n^3) (Floyd–Warshall) + O(m) linear search get we erase b in this scenario (a — b — c). If dist[a][c] == dist[a][b] + dist[b][c] then we can erase b, otherwise he stays in array.

I was trying to figure out the condition which can be used to remove element from p(i). But found nothing. :(

Condition to erase p[i] :

There is a j and a k such that

j<i<kanddist[p[j]][p[k]]=k-j. Dist[x][y] is the shortest path in the graph betwen x and yYou can apply Greedy algorithm. For each triplet, (

left,current,right), if you can go fromleft-->currentand fromcurrent-->right,but you can't directly go fromlefttoright, then we can removecurrent. This ensures that the shortest path fromlefttorightincludescurrent. Hence, it retains the original sequence.You can use a stack to process every triplet.

The time complexity is

O(m + n*n).Code

Edit --> It can't be solved without using Floyd Warshall

You can make a Floyd-Warshall to calculate the APSP,then you just need to search the sequence (starting with the first number) the number that is farther away and that the difference in positions is the minimum path between those two, then you keep it and go to that one and repeat it until you reach the end,O(N*M)

I submited D1 without any testing or even compiling in last 30s an it passed, tests are probablu awful for this one.

Bonus for E. Solve it in $$$O(n)$$$

I think problem E would be much easier if the constraints are set to fit $$$O(n)$$$, e.g. $$$n \leq 100000$$$, because one need not consider an $$$O(n^2)$$$ approach instead, which could simplify thinking.

Upd: Ignore, got it.

In div2E, did we have to use the fact that number of sequences whose maximal prefix sum is k is equal to product of catalan numbers($$$C_{x_{i}}$$$) such that sum of $$$x_{i}$$$ is n-k)?

E can be solved in O(n+m) 59161848

to solve this you need to calculate C(n, r) in O(n) preprocess and O(1) query

Maybe I should call this round...

UnsuccessfulSubmissionForces.

Problem A and C are really tricky.

Anybody want to explain D? The last example does not make sense to me.

0111001100111011101000

Answer should start with 000100... but the example says 001100...

Why? Which condition would not be satisfied if the answer starts with 000100...?

Any subsequences, not only consecutive.

Consider subsequence in [3,6]. The longest non-decreasing in input is 2, but your example is 3.

In 001100 the longest non-decreasing subsequence is 4 In 000100 the longest non-decreasing subsequence is 5

But in input string the longest non-decreasing subsequence s[1:6] = 4, not 5!

You're considering sub-string(s) . For 't' we have to consider sub-sequences.

If you know Catalan number's formula's proof, you could solve E with complexity O(n)

Can you explain it more clearly ? Thank you !

You can read this and use the trick which is presented in proof. You can calculate : Number of array that has max-prefix greater or equal to x

Do you have any other problems that use the same technique? :)

https://codeforces.com/contest/26/problem/D

This is basically the same though.

Nice problem D

how to solve?

We turn the array over, then we consider zeros and ones at each step and say, if zeros are less or equal then ones, then we put zero in a new line

why would that work?

because if the number of zero is smaller than the number of one than you turn this number(1) into 0 can effect the number of zero ,it adds 1.but the number of one doesn't change.

UPD:and you didn't change the biggestGood idea!

how you know it won't change the LIS for all the substrings?

What is the trick in p.A?

I am a new to hacking. Is hacking allowed only during the contest?

In normal rounds yes, but in the extended ICPC rules you get 12 hours of open hacking after the contest ended.

Feels really great when you finish debugging C 40s after the contest ended :(

What about problem C? Is it Floyd–Warshall algorithm?

BFS for each vertex works well too.

You can apply Greedy to solve it in

O(m + n*n ).Explanation

I don't understand problem C.

What " and p is one of the shortest paths passing through the vertexes v1, …, vk in that order." means?

It means, that there is no strictly shorter path than p passing through all vertexes v1, ..., vk in that order.

And i should find the shortest sequence of vertices that this path 'P' pass through?

Whether yes or no, Could you explain more about what should the output be?

Yes.

Since $$$v$$$ is a subsequence of $$$p$$$, I will define another sequence, $$$a$$$, such that for all $$$i$$$ in range $$$[1, k]$$$ $$$p_{a_i} = v_i$$$ and $$$a$$$ is ascending.

You should output a sequence $$$v_1, v_2, \ldots , v_k$$$ such that:

For example in test

The proper answer is

because:

I got it, thank you very much.

B was very interesting but had nice proof

First competition so I have a few questions. - When do we get our rating? - How do you read the score distribution? I'd be grateful if anyone could answer.

The ratings need like an hour or so, sometimes more.

Score distribution? There is a link to "Standings".

Actually, the first problem was the hardest

I would not say the hardest, although clearly harder than B for me

I suppose the first two problems' placement could be swapped

who can tell me the thinking process about Pro.C,never solve the pro.c since came here

And the meaning of "The main characters have been omitted to be short."

That means "Let's skip the little useless story and go straight to the problem"

What's wrong with this solution? Problem A

1- Read the user input as a decimal value in a variable

`s`

2- Output the round up value of

`log4(s)`

for exemple

`100000000`

is`256`

in base 10 and`log4(256)`

is`4`

because`power(4,4) = 256`

and

`101`

is`5`

and the round up of`log4(5)`

is`2`

and so on ...With numbers up to 2^256 I think using that might be not precise enough.

I think the logic is okay the problem is in my implementation I'm using C++ and I used a variable

`s`

as`uint64_t`

so I can only store`64 bits`

so when the user input is more than`64 bits`

I got problems so I have to use a type where I can store`100 bits`

if someone know a type in C++ where I can store more than`100 bits`

please helpstring *_*

I can't use string in log fonction xD

you can... If you think about it the length of the string is very closely tied with the log2 value

I don't think you need a log function here, it would work on smaller bits(when you are converting to decimal) but for 200 bits its not possible to convert it into decimal, that's why string is used, with some check(even/odd) for the indices, you can get the answer.

check this solution : 581 DIV 2 A

typedef __int128 lll;

Compilation error!

program.cpp:8:5: error: '__uint128_t' was not declared in this scope

I can't use __uint128_t in codeforces even it's works nice on my computer

I tried the same solution using string and it didn't work for me https://codeforces.com/contest/1204/submission/59172843

Could someone help me figure out what is wrong in my logic in question C. 59182371

Even I am facing same problems. Would appreciate if someone could explain it.

I mean, they don't need to be adjacent to have a shortest path that goes around the 'inter' in your code.

After experimenting with various test cases, I finally came up with one that your code fails on. I hope this will help you better in understanding where you went wrong

4

0101

0010

0001

0000

4

1 2 3 4

The expected answer is of length 3 , but your code outputs a sequence of length 2.

Hint: Suppose we have a sequence (a,b,c,d). We take`a`

into our path. We cannot reach`c`

from`a`

, hence we neglect`b`

. Now, we cannot reach`d`

from`c`

, hence we also neglect`c`

. This is where you went wrong. Since we've already deleted`b`

, we should check the condition on the triplet (a,c,d).15 testers and even the basic test cases are not covered for problem C. A simple path graph of length 5 is enough to hack a guy. Why were all the basic cases not covered?

http://codeforces.com/contest/1204/submission/59167944

When testing, testers mostly gave feedback on the problems, not the tests. Also, all testers who solved C did so without running into this test case.

And just because a single case wasn't covered doesn't mean all basic cases weren't covered. A lot of edge cases were covered, for example:

`2 0100 2 1 2`

System tests and hacks are a part of Codeforces, it's really hard to avoid and check every single case.

Please be considerate of the authors for taking the time to put the round together and writing the problems. It's a shame this happened to many participants.

Best of luck in the future.

dude, the humans' idiotism, retardness and stupidity don't have limits.

I can understand people posting weird-ass solutions which could pass sometimes, but there should have been a path graph test case in there somewhere.

See the glass as half full: you have the opportunity to hack someone.

Will you provide the table of most hacks?

it's quite strange, but i couldn't open cf on time when todays contest has started. after one hour of useless attempts to open cf i tried to change my wifi connect to another one. and suddenly it opened. wifi connect which couldn't open cf, opened any other website except cf. what's the matter?

What was the intended solution for D?

what is the problem with s.size() and (ll)s.size()? s.size() gives wrong answer but (ll)s.size() gives correct answer.

With (ll)s.size() https://codeforces.com/contest/1204/submission/59182674

With s.size() https://codeforces.com/contest/1204/submission/59158562

s.size() returns size_t type integer, where size_t is an unsigned integral data type. So, it can't be negative.

s.size() has type size_t, if say size is 1 and you subtract 2 from it, it won't go to -1, it wraps around, see this : size_t in reverse for loop

using ll ensures proper conversion. :)

I face this problem many times in codeforces only. I don't face this problem in any other sites like codechef etc.

Why 1e8 operations doesn't fit to 2 sec in C, when 1e9 fit to 1 sec in Codeforces? Even when I make 1e7 for get WA I get TLE can anyone explain?

shit I wrote

instead

get AC in 0.6 s

Test case 2 in problem C. I don't understand why not true??? May be Im wrong??? Help me. Thanks you so much <3

there is an arc from 3 to 1

sorry :< i thought 4 cases is the same

I am a newbie now. I want tips how to become a legendary grand newbie. Help is highly appreciated

"legendary grand newbie"??I'm confused!!!

I am confused in test case 3 of problem C. Why the solution of test case 3 in Problem C can't be 6 1 2 3 3 2 1. Even if 1 is deleted from the sequence , shortest path from 3 to 3 is of distance 2 only.

In the problem statement it is clearly mentioned that — "Any two consecutive numbers should be distinct"

Miss that adjacent elements on the array v must be different

That's rally quite strage, that this army of testers didn't crash my solution in D2 (I did it after the end of the contest), in the worst sitation I have O(n^2), but on max systemtest my solution got only 30 ms.

Sorry, for my poor english

https://codeforces.com/contest/1204/submission/59180412

dude, the humans' idiotism, retardness and stupidity don't have limits. i can't foresee which bad solutions can you invent

But you must

but i can't.

**But I thing this test is rather simple max_test (10000*("110")+20000*("10")+10000*("100")) or many others where there arу many non-decreasing sub-segments in a minimal partition, that's not soo hard to constract this test

If I were the author of the solution, I would be really offended by your comment.

Most people in "army of testers" (at least me) just wanted to solve the round beforehand and didn't work on wrong solutions.

I thought that one of the most important thing is to try to build good maxtest. It's not quastion for you, it's a quastion for organisator of testers' work. And there are some hacks of task C with really simple tests too(n=4 and m=4)!

what a smooth contest!

No announcement and also fast testing.

every thing about this contest is really good, but the best thing is sorry_marymarine'comments on this post lol

I solved problem C in O(m) without using Floyd-Warshall and without computing shortest paths. Here is my code: 59178907

I also applied a similar logic. Although it cleared all the test cases (still does), but there is a big flaw in the logic. After dry running it a couple of times, I came up with this test case which fails with your code.

4

0100

0010

0001

0000

4

1 2 3 4

This is a linear chain. The answer should be of length 2. But your code produces an answer of length 3. The test cases for this question are too weak.

Test #4 in problem D1 Why isn't

0001000100001000101000the answer? Maybe I didn't get the problem right, but I don't see values oflandrwhich can fail the test.I've even cheched it for every [l;r] in program.

First 6 characters "011100" has a good subsequence of size at most 4 while "000100" has size 5

Why is the size of second good subsequence is 5? How does it look? Isn't it "0001" for "000100"?

"00000"

it's a subsequence not a substring.

I got it now, thanks!

solution for problem D2. So fun =))

+262 good contest

+8 rating next contest =)) good luck

Finally reached blue :)

E can be visualised as a path counting problem in an $$$n$$$ by $$$m$$$ grid. Where going up is +1 and right is -1. And can be solved in $$$O(n+m)$$$.

Let $$$F(i)$$$ be the number of paths from $$$(0, 0)$$$ to $$$(n, m)$$$ that satisfy each point $$$(x, y)$$$ in the path satisfies $$$x - y \leq i$$$.

Answer is summation of $$$(i . (F(i)-F(i-1)))$$$ for $$$i$$$ in $$$1$$$ to $$$n$$$.

$$$F(i)$$$ can be found by counting all the bad paths for $$$i$$$ and subract it from total paths $$$(C(m+n,m))$$$. A bad path is a path where there exists atleast one vertex that is of the form $$$(y+i, y)$$$, in other words, the path meets the line $$$x-y=i$$$. It can be seen that the bad paths, given $$$i$$$ have a bijection with paths from $$$(0,0)$$$ to $$$(m+i+1, n-i-1)$$$. (If we swap the number of up and right moves from the first point of intersection with $$$x-y=i$$$). Number of bad paths is $$$C(n+m, m+i+1)$$$.

So $$$F(i)$$$ is just $$$max(C(n+m,m)-C(n+m,m+i+1), 0)$$$

Also, feels good to be on the blog for the first time :)

Ccan't be solved without usingFloyd Warshall(equivalently, BFS from all vertices)Going through the solutions of problem

`C`

, I have observed a lot of people (including me), solve the question without using Floyd Warshall and deleting vertices in a greedy manner. Although the solution passes all the system test cases, I believe it is wrong.People have used 2 approaches for greedily deleting the vertices.

Approach 1:: Checking every window of size 3 without modifying the array.This approach fails for this test case.

4

0101

0010

0001

0000

4

1 2 3 4

Expected 1 3 4

Output 1 4

Approach 2:: Checking every window of size 3 (while actually deleting elements using stack).This approach fails for this test case. 4

0100

0010

0001

0000

4

1 2 3 4

Expected 1 4

Output 1 2 4

sorry_marymarine Can you add these test cases to the problem ?

I fail in C because my bfs counts only one way from u to v (if v has other way) Today morning I fix it and get AC. I think that my solution also greedy.

As I said, BFS from each vertex is essentially the same as Floyd Warshall. The time complexity of your code is O(n*m + n*n*n). My point is that you cannot lower that O(n^3) factor to O(n^2).

finally expert :)

When you want to troll ~12k contestant with 998244853 but almost no one got tricked

Spoileradmittion

I believe that nobody reads the number!! just copy-paste it!!

I spent 30 minutes on this module number. I have wrote the code of E for 3 times. At first I use the math formula. It's right and the complexity is $$$O(n)$$$. At last I use $$$O(n^2)$$$ dp. I won't believe the module number any more. 555...

Me too.And even worse,I spent 2h on this number without finding out it.

Perhaps I am a fool:(

I learnt a lesson,and it taught me a lot

Does anynoe use modulo 998244353 on problem E like me...

I think you can't pass the fifth sample if you use 998244353... XD

Yes.And I thought my solution is wrong until I found it.

Your previous submissions don't even have modulo hardcoded. Did you type ...353 by memory? It is one of the things you should never do on contests. If something is written in the statement, copy it from there.

Yes.Now I learned a lesson.

I am solving the problem c in o(n3 + m) time complexity still getting a tle Here is a link to my solution , can somebody please help me https://codeforces.com/contest/1204/submission/59177220

Please help me with this test case. ( Problem C)

The answer according to accepted code is

2-> 1 4

But if you draw the graph you can see that there is another road to go from '1' to '4' which is 1--3--4.

So why the answer is

2-> 1 4.

I think it may be 3-> 1 2 4.

Help me with proper logic of this test case.

Thanks in advance

Important only that way from 1 to 4 shortest (not important how many ways exist).

I think it means that you can't restore original way from correct solution and it not necessary.

To prove, that this is correct answer for this problem, suppose, that there exists a correct answer of strictly lesser length than 2 (in this case we are only considering a subsequence of length 1).

Of course a path of length 1 is incorrect for this test case, because (from the statement) the first and the last elements of path $$$p$$$ and its subsequence $$$v$$$ must be equal, so if path of length 1 would be correct, we would have $$$1 = p_1 = v_1 = v_k = p_m = 4$$$, so there is no correct answer of length strictly lesser than 2.

We proved that the answer's length is at least 2, so now we can prove, that $$$v = (1, 4)$$$ is a correct answer:

As you can see $$$v = (1,4)$$$ satisfies all needed conditions, so it is a correct answer.

Thank You

Why problem d was two parts? I think E Should be two parts

The system test of Problem C is too weak

I hacked about 15 solutions after the contest!

sorry for this. i am rather unexperienced problemsetter and i can't foresee all the wrong solutions to invent the countertests

Hello! I think that the output for the fourth input of this problem D1 has mistake.

Because the longest non decreasing subsequence of output is 13, but the input is 12. If i made a mistake help me understand.

Sorry for my poor english.

both have the same length of the longest nondecreasing subsequence. please don't say "the output is wrong" 2 weeks after a contest. it was carefully prepared, tested and then a lot of participants solved that problem.

Your round is one of the best rounds I have participated in. Thanks a lot