Hi Codeforces!

stefdasca, koala_bear00 and I are very excited to announce our first contest Codeforces Round #676, which will take place Oct/18/2020 12:05 (Moscow time). The round will be rated for participants with rating up to 2099.

The tasks were written by me with help from stefdasca and koala_bear00 and you have to help some of the authors' favorite musical artists to solve the problems they're faced with.

We hope we compiled a very interesting contest with memorable tasks :)

Special thanks to:

antontrygubO_o for coordinating our round and pushing us to come up with more and more interesting tasks.

dorijanlendvaj, kclee2172, Devil, thenymphsofdelphi, jainbot27, Stelutzu, khiro, SleepyShashwat, stack_overflows, AmShZ, Osama_Alkhodairy, raresdanut, katsurap_, A_N_D_Y, Usu and kpw29 for testing the round and providing useful feedback.

MikeMirzayanov for awesome Codeforces and Polygon platforms. Thanks!

You will be given 2 hours to solve 5 problems, good luck everyone and have fun!

**UPD 1:** After the round you can watch videos explaining the solutions to the tasks on stefdasca's Youtube channel.

**UPD 2:** The round was rescheduled, because of intersection with other scheduled contests.

**UPD 3:** The scoring distribution is standard **500** — **1000** — **1500** — **2000** — **2500**.

**UPD 4:** The editorial was posted and you can check stefdasca's video solutions aswell.

**UPD 5:** The round is finished, we are glad everything went smooth and hope you enjoyed our tasks!

Div1 winners (unofficial):

Div2 winners:

Congratulations to those above and to everyone for participating!

Auto comment: topic has been updated by flaviu2001 (previous revision, new revision, compare).i lost the rated contest again :weary:

look on the bright side, you participated in another unrated contest ^o^

As a tester, you know ... what I want :)

PSInteresting Problems. Good Luck!

Meet your needs.Thank you for your efforts！

As a tester, I would like to say "Good Luck everyone. Have Fun!!"

As a tester, I would like to say that I haven't seen the problem yet.

How did you test the round then?

flaviu2001 just wrote my name before I actually tested the round. But I did that yesterday :)

Oh, I see. Thanks for the clarification! :)

`Notice the unusual time !`

Good luck for your maiden contest !!

The blog is written $$$4$$$ days before the contest -> bad contest

why?

lifetime experience

As a tester, I would like to say, good luck and have fun!

What time is it? 18:35 or 21:05?

Now author fixed it.. But people why downoting ??

Ok... thanks

Auto comment: topic has been updated by flaviu2001 (previous revision, new revision, compare).Auto comment: topic has been updated by flaviu2001 (previous revision, new revision, compare).It seems the blog text does not use the "Contest time" feature of the blog editor, like

`[contest_time:1421]`

Oops, let's say I'm worse at writing blogs than problems.

I expect that this contest can be held smoothly.

As a Romanian, I am excited to participate in a round made by Romanians!

"With" ?

Yeah, good point. I've edited the original comment.

Good luck to everyone

if someone knows why there is

no announcement of the Codeforces Raif Round 1??Now there's an announcement of the Codeforces Raif Round 1.

bro can you change the time to 8:00 pm as per IST

What is "Raif" in

`Codeforces Raif Round 1 (Div. 1 + Div. 2)`

?I would hope it is in honor of Raif Badawi who unfortunatly is still in prison for being a blogger and activist.

Deleted

The problemsetters seem to be all from Singapore

Clash with codechef cook-off, but do we even care?

I do, cook-offs are decent and it's always better to have more contests to participate in.

What a great way to promote a youtube channel :D, JK,

stefdasca orz

I am a newbie so should i participate in div2 or div3 contests Please help. i want to increase my rating seriously

Participate in both.

I only get notifications of div1 and div2 contests. till now i have not got even one div3 contest notification so that i can participate

Frequency of Div3 contests is lesser than Div2 so you should try to participate in every Div2 and Educational Rounds. Educational Rounds are good too.

oh thanks for the info, btw i saw some participants graph starting from 1200 and for some it was below 1200 , what is this method of starting and rating further

Starting rating changed some time ago. You can learn more about it here -> https://codeforces.com/blog/entry/77890

hey today i gave a div 2 contest , i was only able to do the first problem(this was my first contest ever). the soln got accepted but in my account ,contest section its showing nothing

Rating changes happen some time after the contest (usually few hours). That's when your profile page section will be updated. If you want to know rating changes as soon as possible, there are some pretty accurate CF Predictors, that you can find just by googling them.

oh ok thank you

hey when will the result of 677 div3 be announced?

Hey i wanted to ask how is it possible that a solution works on my editor giving perfectly correct answer to a question in today's contest, but their editor declared it wrong for the same input. i even checked on one of the online compilers ,there also it was giving correct answer. ultimately due to this my solution was not accepted.

This contest clash with the cook-off.....They should prepone or postpone the contest whichever is possible

A lots of thanks to MikeMirzayanov and other involved organisers for rescheduling the round:)

Why can't CodeChef reschedule for CodeForces?

Cause codechef scheduled the contest before codeforces

In that case, CodeChef two monthly short contest are always prescheduled... So does that mean CodeForces will always have to keep that in mind.. Only a bit unfair, But this also shows how considerate Codeforces is for all of us..

It depends on author/s. If author/s want to reschedule it, it will be rescheduled. If s/he doesn't, it will not.

Great another 2AM contest

As a tester, I can say that the problems are really interesting!

Really interesting and a hard jump from C->D? Usually I have noticed Div-2 contests with five problems have a large jump in difficulties from C to D, hopefully this round is more balanced.

You see the thing is not that simple, say the author wants to have a difficult problem D (say rating of atleast 2000), for an average participant it requires 40-60 min atleast, so if C is also medium difficult most of them won't have to time solve D or even read E.So they generally make C a bit easy and make D and E kinda difficult, so though there is a huge gap in difficulty but still you will be able to attempt it properly. Though no idea how this contest is gonna be

I hope it will be nice contest, good luck for all participants and have fun!

stefdasca Can you leave a dot in comment section? So that we can upvote :p

That's a good time for Chinese competitor!Thanks flaviu2001!

what will be the scoring distribution? or is there a fixed scoring distribution for div2 rounds and I'm just not aware of it?

score distribution will be published soon

The scoring distribution is standard 500 — 1000 — 1500 — 2000 — 2500.

Whoah that's really early round, gives me no time to have dinner. Anyways glhf

LOL its 6AM in Brazil glhf

15 min to be ready (:

Thanks for the video Editorial/Solutions.

Good time for a participant from India. Just after lunch full of Energy.

Was that sarcastic? I feel Sleepy tbh XD

In question B what does "adjacent by side" means does that exclude diagonal cells?

ask via round questions, not here

all problems have nice music :) thanks <3

Well, cool round! Problem C was sickkkk

problem B is very big brain.

https://www.youtube.com/watch?v=ebgjlXyH9s8

Do watch the video editorial here

Sorry, but it was a very stupid problem B.

don't comment during ongoing contest.

Sorry. I edited the comment and left only the assessment.

The Kind of day i want to wake up to almost everyday during lockdown!!!

Afternoon — CF round

Evening — Kickstart

Night — CC round Cook Off

HAPPPYYYYYYYYYYYY!!!!!!!!!!!!!!!!!!!!!!!!!

Usually comments start after the contest ends

Gap between D and E is very Huggggggggge.

stefdasca, flaviu2001, koala_bear00 You guys surely have a great taste in music:) I had never imagined that I would be introduced to new music via a contest!

Am i the only person who find C much easier than B and regret for wasting too much time on B instead of solve C. Or i overcomplicated B?

Solution for C is simply,

R n-1

L n

L 2

Which takes about 15 minutes to come with idea and solve.

For B my idea is

Let b1 and b2 two adjacent cell of F and c1,c2,c3 be adjacent cell of b1 and b2.

Then i checked condition for possible cases which takes 1 hour to implement.

Actually it was reverse for me,It took 20 min for B and an hour for C.

For me B was much much easier than C, I think it depends

C wasn't obvious at all, at least not for me. I wasted 1 hour 45 minutes in it and no AC xD

For B it will be easier if you check two adjacent cells of S and two adjacent cells of F.

My badness. I really over complicated

Just make both the two cells at one end 0 or both the two cells at another end 1 or vice versa. check this condition I was first tried to implements bfs and got confused with conditions then I got this one. Though ~50 minutes are wasted. Here is my submission.

It took me more time to understand C than solve B lol, So many misunderstandings in C for me

u can check only 2 adjacent cell of S, and only 2 adjacent cell of S as barriers to reach inside, no need for c1,c2,c3

ConstructiveForces...xD....nice problems. Here are my submissions..hope to pass ST. Still 20 short of Specialist tho :(

ABCAny ideas on D?

Think about an alternative way of going to a neighboring cell.

Path C6 == Path C1 + Path C5

So, the cost of C6 = minimum of the cost of C6 and (cost of C1 + cost of C5)

A similar approach for all other paths.

How did you got all possible ways of going from one cell to another?

First, compute the optimal cost to all the directions, Then you need to do basic math to get the optimal answer.

And how did you get that maximum only addition of two paths will give minimum cost? Why not three or more?

I don't think there exists any other optimal way except those two. You should try to observe the figure given in the problem.

For simplicity consider $$$C2$$$(pure right), only $$$C1,C2,C3$$$ can give some right component. If you include any of $$$C4,C5,C6$$$ then you must need extra $$$C1,C2,C3$$$. Say if you want to use $$$C6$$$ then your path will be sth like $$$C6,C2,C3 > C2$$$ or $$$C6,C1,C3,C3>C1+C3$$$.

Therefore minimum cost of $$$C2$$$ will be $$$C2$$$ itself, or optimal $$$C1$$$ + optimal $$$C3$$$. In fact, the minimum cost of $$$C2$$$ will be $$$C2$$$ itself, or the original $$$C1$$$ + original $$$C3$$$.

The proof based on no two consecutive directions are not optimal. Assume $$$C1,C2$$$ are not optimal, then $$$C1>C2+C6$$$ and $$$C2>C1+C3$$$. Adding two inequalities gives $$$C1+C2>C1+C2+C3+C6 \implies 0>C3+C6$$$. Contradiction. If minimum cost of $$$C2$$$ is optimal $$$C1$$$ + optimal $$$C3$$$, then optimal $$$C1$$$ must be itself, the original cost of $$$C1$$$.

After applying

Floyd Warshall's Algorithm, you may use Greedy approach to find the answer.Target cell can have huge coordinates, so how is a graph algorithm feasible here? Is there some trick to compress the graph or just some O(1) thing?

I mean considering a graph with only seven vertices. Essentially to find the minimum cost to reach from $$$ (x,y) $$$ to $$$ (x+dx,y+dy) $$$.

Ok will try to upsolve...thanks!

Optimal right move = min(c[2],c[3]+c[1]) and Optimal left move = min(c[5], c[4]+c[6])

For positive x: You just gotta find where y lies w.r.t x. There will be 2 choices of movement if (y>x or y<0), and if 0<=y<=x there are 3 possible choices of movement, try to figure them out.for negative x: just put x=-x, y=-y and swap opposite Cs and do the same as positive xI see will try to upsolve...thanks!

I did exactly same but got WA. Might have missed something

ppl solved D with O(1)?? I keep getting wrong answer on testcase 3 too. Is O(1) method correct way? find enclosing axis then max three ways to reach point??

I guess it is possible, but it seemed so troublesome to correctly consider all cases that I abandoned this idea

I guess you need to consider also max cases

say you need to get to (7, 5)

You can go 7 up-right and 2 down and this can also be optimal

Yup thats how I did it, but among those 3 ways to reach a point, left and right movement have 2 ways each, I missed that part initially

I just dont get why authors dont get it .. Giving such stories just confuses us ... It does not make any thing "cool about your set". It makes it worse.

Man on a serious note, I know coming up with a interesting plot is definetly tough but I dont want to know that what the hell your character wants to achieve then first grab the info of what is doing what .. and not to mention the names u come up on

I tried to hack the following solution for A by generating 1e4 test cases where 1e8<=a, b<=1e9. This solution should time out but I wasn't able to hack it. Can someone please help me why it didn't TLE ?

Actually the valuable $$$min$$$ and the loop doesn't do anything. After the loop, $$$temp$$$ is always set to $$$a\oplus b + b\oplus b = a\oplus b$$$, so the compiler ignored the loop.

Unluckily, $$$a\oplus b$$$ is just the correct answer.

So the compiler after few test cases realized that temp is being set to a^b and hence ignored thousands of other test cases ? Can you please elaborate a bit more ?

It's called as

`O2-optimization`

, you can google for more information.I got -150 for this ;-; . I will definitely check for -O2 optimization.

Hello I did exactly same code but get TLE why?

Submission

b can be upto 1e9. you can not traverse to 1e9. You need to optimise your code.

You can read about O2 optimisation here

how this code is optimized?

This is not the same code. The code that gets optimized above is roughly equivalent to

So you can see that in the code above, mi is always overwritten in every iteration. This makes it easy for the compiler to realize that only the final iteration of the loop needs to be performed. (The submitter got lucky that this apparently incorrect code always gives the correct answer.)

B was so stupid because it was all about typing, annoying question imo.

Unless there's a better way of solving it..

Only cells adjacent to start and finish matter.

Yes I know that, that's a very obvious observation and that's why its a stupid problem, its very easy to figure out the solution but implementing took a lot of time it was so frustrating.

Consider the set of the cells adjacent to S and F and flip the values of the cells adjacent to one of them. Then take the smallest of the two subsets of cells having the same value.

C was just solving this test case — "abc"

How to convert this to a palindrome ?

3 operations

L 2 ==> babc

R 2 ==> babcba

R 5 ==> babcbab

'babcbab' is a palindrome.

exactly

abc

Perform R 2

abcb

Perform L 3

cbabcb

Perform L 2

bcbabcb

Now just generalize this to a being the first character of the string, c being the last and b being all the ones in-between. This is actually how I solved it during contest as well.

Hope not seeing comment section filled with memes this time

Is it only me, who did B in 15 mins but struggled in C for an hour? C definitely was a good problem, but required some thinking, and took me an hour to come up with a 4 line solution :(

You're not alone. And the sad thing is I didn't get it in contest but after it.

I first did D, then looked again into C.

I missed C by one second. As soon as I submitted, the contest ended:(

we got speedforces again lol

I coded D earlier than C

So my rank is low (ノ￣ー￣)ノノ(º_ºノ)

The 55-minute-long D makes me down. ╮(╯﹏╰）╭

Ohhhhhhh as a rock fan I looooove these discription! C and D were so cute！hard E,really. Like this contest!

C is one of the worst Adhoc ever, you have to keep trying combinations until you get it, no clever obervation, no analysis, just have pattern aSb and keep trying.

B is equally annoying problem, so many if conditions, pure implementation

Really dissappointed

There is a clever observation to make first and last characters same and then apply both the operations one at a time . I dont know if this is clever or not but I was not clever!

Can you arrive at it without trying a zillion samples ?

Yes? Clearly distinct characters will be the most difficult case and 3 is the smallest n, so try 3 random distinct characters. This gets an easily generalizable answer. As for solving this case, I just checked for a sample with a distinct character at the end (sample 2) and used it as the basis.

I can propose solns that will work for abcde but not for abcdef

Exactly what I did just by seeing the problem but I was trying to do it int three operations. I thought there is a way since "abc" can be done in just 3 operations. but bad luck went in wrong direction earlier!

L2 R2 R 2*n-1 would be clever lol .Yes just by thinking

It would have been, if you arrive at it with a proper algorithm,instead of noticing pattern in a few examples

And what is a "proper algorithm" for solving a problem?

In my experience, nearly all problems break down to analyzing it (possibly with some small cases or simplifications or maybe just writing a brute force), noticing interesting properties and then getting some intuition about how to proceed.

Even most problems requiring formal algorithms or some data structures usually require you to realize that some properties hold which makes you think of that approach.

Test case 1 makes you realize that you can always do it in two moves if the first and third characters are the same. The rest is thinking if you can obtain such a string in a few moves.

Nah, C had a very nice observation. You don't need to try many samples, just take a string where all characters are distict. If that works, everything else will work. After that its just a bit of thinking and 3 lines of code

Firstly, $$$type 1$$$ operation will make a prefix of the string a palindrome and $$$type 2$$$ operation will make a suffix of the string a palindrome. A palindrome can have at most $$$1$$$ element having frequency $$$1$$$. Here in the given string, you cannot change the frequency of the first and the last element at the first operation. But if the first and the last element are different, then you must have one of them in some operation. So, first, you do $$$R \ n-1$$$, and then to double the frequency of the last element of the given input string, you do $$$L \ n$$$. Now, it is easy to see that, doing another operation will result in a palindrome.

I will give you best answer

Consider a non palindrome of two parts

X|Ywhere

|is middle point ok?lets take for example initital string =>

X1.X2.X3|Y1.Y2.Y3This must be worst case right?

Now do L,'2'->

X2.X1.X2.X3|Y1.Y2.Y3Now Do, R,'2'-> X2.X1.X2.X3.Y1.Y2.Y3.

Y2.Y1.X3.X2.X1(length besz)Now Do R, (sz-1)where** sz** is length of string reached above-> X2.X1.X2.X3.Y1.Y2.Y3. Y2.Y1.X3.X2.X1.

X2This is palindrome. Just use

inductionfor all lengths of X and Yez clapwqwrd ewerd qwerd

I did two bfs on B. smh

Thanks for Tzuyu's reference in Problem A

<3

Is it just me or was this contest too much ad-hoc?? A,B,C,D all can be implemented in O(1), it was only about solving various test cases until you can see the pattern. I may be very wrong here but I think not much real programming or barely any Data structures and algos were used in this contest :/

Damn man, why all Constructive. Imma lose a 100 rating points, where gaining 14 would've put me over to CM. It was so hit or miss.

Speaking of patterns did you notice 676 is also a palindrome?

did you notice

`1421 % 676 = 69`

?bruh

In problem B, is diagonal movement allowed?

no, adjacent by a side, not a corner

No, as neighbors in the grid are those who share an edge.

Well... Instead of CM i I failed system test for problem B.

Granted, I did implement it in a disgusting way, bit it passes pretests :/

so many if-else, it was very confusing trying to write the correct code and not miss any case

I got wa on 1st attempt bcause i just wrote 1 on the place of 2 and 2 at the place of 1!!

Solution for C was so easy :(

cryingAd-hoc Missiles. I'm not complaining though :))

q.B was interesting and fun. The implementation was a bit big. I got wa on first attempt and at 2nd attempt, i had to just swipe 1 and 2 which i wrote wrong

Problem B: Consider the grid as:

A = Start Case 1. if B=D=0 and C=E=G=1 then STUCK. Case 2. if B=D=1 and C=E=G=0 then STUCK.

We have to make anyone of the two cases in atmost two steps.

Is this logic correct?

Logic only depends on, BD and HF.

Yes, this is also mentioned in editorial and I understood it. But why the above logic did not work? Am I missing something? Any idea.

Well, so my idea was like- He will start walking from (1,2) or (2,1) and enter F from (n,n-1) or (n-1,n). So, if (n,n-1)=(n-1,n), you just which one or whether both of (1,2) and (2,1) equals to (n,n-1)and print it else if (1,2)=(2,1), you just which one or whether both of (n,n-1) and (n-1,n) equals to (1,2) and print it else just see (1,2)/(2,1) is different from whether (n,n-1) or (n-1,n) and print both of them // (1,2)/(2,1) and the different one

I don't see any counter example for this strategy, i guess this should work well !

Yeah, this works, I have implemented something similar.

Your logic is correct. If $$$C=E=G$$$ then at most two steps. Otherwise there will be one of 0/1 appear two times, let's denote if by $$$x$$$. If at least one of $$$B,D$$$ is $$$y$$$ then set $$$C=E=G=x$$$ and $$$B=D=y$$$ which requires at most two steps. Else both $$$B,D$$$ are $$$x$$$, set the two $$$C,E,G$$$ with x to y.

I think the Problem BCD are boring

Agreed. Just if-else needed, LoL.

Great contest, great songs. However, I should not have listened to them during the contest.

This was a total disappointment since every question was a case work, I came here to solve the problems hoping to use my implementation and coding skills and not using my case work skills. I don't know about problem E but other 4 questions were total case work. Were you guys expecting us to write only if-else statements rather than using some great concepts out there?

I wasted so much time on B. I had the solution but got mixed up on silly errors with the cases. Then for some reason I failed pretest 2 for a reason I can't see

C and D looked interesting but brutal

Was this contest unrated??

LoL, you shouldn't have asked it, Downvotes coming.

Why?? I don't understand XD

Problem E is so hard, so interesting, so mysterious. Thanks for the author's ideas and efforts!

As soon as I read the first line of problem A, I was like — Aaaah... Imma kill this round... sad not all the problems had "twice" background.

Note for C/C++ users: I lost problem D because I was doing negative % positive in the indexes of the c[] array. We know that (-1) % (6) should give 5 but in C/C++ when you do that you will get -1. So I wrote my own modulo to overcome this and I said it would be good to share it with you people:

`#define mmod(a,b) ( (a >= 0) ? ((a%b)%b) : ( ( ( a + ( ((abs(a)+b)/b) * b ) ) % b ) % b ) )`

Hope it help you and myself get out of Gold Nova! I mean Expert!

Problems B, C, and D were just if-else...Not so great round, Should rather call it speedforces, implementationforces, etc.

I have a Rating of 369 and I had solved 1 problem in this contest. Will my rating change as this is a Div 2 contest?

Yes it will increase

Okk, Thank You, it increased to 635!

I graduate to pupil for this round :) I'm so happy!!

This round promoted me to Specialist.

Thanks for the

wonderfulround XDhello there, in the first question of Codeforces Round #676 (Div. 2) some test cases are wrong for the sake of understanding ..in one of the examples given in that question ..if we take a=28 and b=14 the output according to your code is 18 but

minimum possible answer is 10 if we take x as 4 28^4+14^4=10

28^4+14^4==34 not 10 don't write like this in code because priority of '+' is greater than '^' so 28^4+14^4 will become 28^18^4 which is equal to 10 so write in code like this (28^4)+(14^4)

I was the first contestant to solve 1421C - Палиндромайзер, why didn't you put in the announcement people who were the first to solve each problem?

I found this because of your contest. Thank you :)

I gave the contest and used ideone.com to write my code. Someone stole my code, because of which I was marked a violater. But I haven't done any type of cheating. How can I get my rating back??

Sorry we can't do anything about it, but in the future please don't use ideone or at least try to find a way to mark your source codes private.

Thanks for the reply. Will take care in future.

Of course, A little easy.

Is no one going to speak about how incredibly cool the band references and music links were

Yeah but AC/DC were missing. :(

https://www.youtube.com/watch?v=krxU5Y9lCS8

Though on a serious note, a sorta hidden gem of the band in case you haven't listened to it yet https://www.youtube.com/watch?v=sFUGvdxuQGQ

Doesnt matter if all you do from A-D is if(..) else(...) .. if(..) and I dont know why people downvote this type of comments. On a serious note did you seriously like this contest ?

And yes! We are celebrating

TWICE's 5th anniversarytoday.