Hello, Codeforces!

On Sunday, April 26th at 19:00 MSK the 300'th regular Codeforces Round will take place.

I would like to congratulate all Codeforces members and administration on this remarkable milestone. The platform has grown hugely in size and quality since its foundation, has hosted lots of exciting competitions, and has been providing the opportunity to everyone to hone their problem solving and algorithmic mastery. For this we thank the Codeforces platform creator MikeMirzayanov and all the Codeforces crew. Keep up the incredible job, guys!

That being said, I'm excited to announce that the problems on the jubilee three-hundredth Codeforces Round will be set by me, Mikhail Tikhomirov (Endagorion). You may remember the past rounds with my problems: #99, #109, #265, #283, and (in part) #295. I thank the Codeforces problems coordinator Max Akhmedov (Zlobober) for helping me in preparing this round, and Maria Belova Delinur for translating the statements in English. Also, special gratitude to Vladislav Isenbaev (winger), Alex Fetisov (AlexFetisov) and Pavel Kunyavskiy (PavelKunyavskiy) for testing the problemset and help with preparation.

This round will be shared for both divisions and will last two hours and a half (as you can see at the Contests page). The round will feature several ( ≥ 6) problems, varying in difficulty and topics involved. I hope that everyone will find an interesting and satisfying problem just for themselves! The scoring will be announced later.

To add up to the excitement, there are 30 exclusive Codeforces T-shirts to compete for in this round! The top 15 participants will get their T-shirts right away; another 15 will be randomly distributed among those who place in the top 300. Even if you think your chances on being the very best are weak, there are still good odds you will get a treat! =)

That's it. Mark your calendars and come back to compete for the prizes and for the fun!

**UPD**: there will be **8** problems. The scoring is standard (i.e. not dynamic): **500-1000-1500-1500-2000-2500-3000-3000**.

**UPD2**. In order to choose people getting T-Shirts we will use the following python3.4 code that you can manually run in "custom invocation" tab on Codeforces. It uses an integer as a seed for random generator, this integer should be equal to the last submission number that happened during the contest.

import random
seed = int(input())
rnd = random.Random(seed)
# all contestants except top-15
contestants = list(range(16, 301))
rnd.shuffle(contestants)
tshirts = list(range(1, 16)) + contestants[:15]
for x in sorted(tshirts):
print(x)

**UPD3**: Thanks for participating!

The winners are:

The list of places getting the T-shirts are `1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 40 46 67 75 80 102 103 144 152 161 169 198 233 243 269`

. The lucky participants are as follows: bmerry, Egor, Petr, rng_58, scott_wu, TooSimple, eatmore, atetubou, qwerty787788, niyaznigmatul, gs12117, W4yneb0t, izrak, zxqfl, yeputons, kcm1700 & cubelover (sharing the 40-th place, so both get a T-shirt), piob, Leo_Yu, matrix & Nerevar (sharing the 75-th place), Haghani, ACube, DemiGuo, FerranAlet, Emarci15, hlwt, Salvare001, MiraculousCoder, FatalEagle, gchebanov & nwin (sharing the 243-rd place), and Solaris (please let me know about any mistakes). Congratulations!

**UPD4**: At long last, the editorial is up! Enjoy, and sorry for the waiting.

I really hope that such jubilee round will be awesome :)

can't wait more!

Sounds cool!

May I suggest that in future rounds, the i-th ranked contestant would have 1/i chance of receiving a t-shirt. Then everyone will have a chance to win t-shirts, while the expected number of t-shirts needed/given is small!

The number will be about ln(n) in total

~~which is too small~~.so the winners (1/1==1) would always recieve T-shirt ?)

Yes, of course rank 1 should get a T-Shirt.

I think that even though it's a small number for a contest, there are approximately 2 contests every week; therefore, it would be a lot in total. However, this idea could be applied every 10th (or something) contest.

Also, some users are consistently in top-15; that's why some people would get unnecessarily many t-shirts, which would be boring even for them. A time restriction (for example, you can't win a t-shirt for 3 months after you've won one) would be great. Still, some people would get the exact t-shirt more than once, and this problem could be solved by changing t-shirt design let's say every 6 months.

Apart from these, I think this is a really cool idea.

Well, I think it's not fair that e.g. 4th person gets a T-shirt while 2nd person gets nothing!

But now it could be that the 300th gets a t-shirt while the 16th gets nothing too!

I didn't say that the current system is fair!

Thanks MikeMirzayanov! Congratulate codeforces on last 300 raund!

Congratulations！;-)

round or raund...

Looking forward to the contest!!!!!!!!

After more than 1 week we have a contest :D

Love CF rounds by Endagorion, the problems are interesting and challenges in the editorial are also awesome! Looking forward for that one!

I hope the problems will be interesting and attractive.

And, I also hope will have more participants in this contest. Because this is the contest for both divisions, so it is easy to get high ratings.

when I felt that I lost the sense of life, I found codeforces and in general ACM contest, this really change my life, Get to work facebook, google, etc. not is a dream if you study algorithms and practice on codeforces in special. so far are 300 th contests, it is incredible thanks MikeMirzayanov and Endagorion!!!! good luck and have fun for everyone thank you codeforces!!!

We enjoy the 300th Round in 2015, and imagine the moment that, when we have been old, we enjoy the 2015th Round together with our children — around the year 2045.

It seems it better to imagine 1024 round in the 2025:)

Thanks MikeMirzayanov! :)

It will be really very interesting and funny :D

congratulations codeforces :).Looking forward eagerly to it.Hope I win one o em tshirts !!:)

Was congratulating codeforces and expressing eagerness in order to try to win the contest and become a better programmer so bad that i got 16 Downvotes? :(

I have just ended my exams and that's what I'm waiting for :D

hope not to go downrated :3

my exams begins to start.such a bad luck :(

You are beating PrinceOfPersia in Top Contributors list! What a week! A lot of changes and too bad for Petr :D

How hard is the first problem expected to be? Div 1 A, Div 2 A or Div 2 B?

Div2A, I guess.

what is the probability for someone to win 2 T-shirts? (one being within top 15, another one randomly)

Using common sense theorem, 0, I guess.

Why is everyone worried about T-Shirt? Just be glad we have new problems to solve.

... as if all the existing problems have been already solved

Is there any question that has 0 accepted solutions ;)

yes, NP-hard problems with N<=100,000

you mean N >= 100,000 right?

Can you solve NP-hard problems when N=99,999 ?

"The round writer is Codeforces problem coordinator Endagorion. Do not miss the round!"

Looks like Zlobober has lost his job... :P

Holy Dijkstra! My bad again :-( Sorry! Seems to me it's time to retire...

Holy Dijkstra!

Seems it's time to write automatized script with one

`if`

statement, which will generate the email.Holly Dijktra! so funny =))

I want the 300th rank!

bavar konin yeki az dooostam kerm rikhteeee

chera amsale toye kosmaqz hameja bayad aberooye irano bebaran ?

alowwwf goosh midiii?

Currently, there are only about 3500 participants registered in this contest.

It is too little in my thinking. I hope will have more, (about 5000 participants).

Because this is the contest for both divisions, so it is easy to get high ratings (if have more participants).

it will be 5000 in 19.00

18:51:There are 5014 participants abroad!Wow It's 5023 till now!!! The registration rate is so high during last minutes.

Cause it's so exciting to be almost late!

My dream became the trust !

Mark your calendars and come back to compete for the prizes and for the fun!!! I hope the Round to be FUN like your last round #295 :D

hope it is ratingful!

no reason for unrated

make down

why minuses??

Because no reasons for unrated?

Don't want Dynamic Scoring.

Обожаю когда Div1 и Div2 пишут один контест) хоть в этом контесте можно посоревноваться и с Div2 и с Div1))

I hope i become master and afsh5 deader w omarhashim

Brad_bit el fahsee5 :D :D

Dah 3abet :D :P

Congrats! :D

You're now first in top contributors list.

NO Iphone 6 so tourist wont participate

it seems that the contest will be great,good luck everyone !!

When registering for the round, do you need to do something more than click the register button? I clicked on it yesterday, but I can't submit anything now, because apparently I'm not registered. I suppose there is no way of registering during the competition... this is so annoying.

Yes; there are two separate registrations, one is to register for Codeforces (getting an account) which is done at the very first time before you have an account, and another is to register for each contest which is done every time before a contest starts.

The room link is busy most of the time during hacking(codeforces is temporarily unavailable), rest all links are working.

538B - Quasi Binary:

http://www.eclecticenglish.com/grammar/HaveTo1A.html:

So why the f#ck a solution that prints leading zeroes gets WA?

Agree. Codeforces need a proper translator.

Luckily during the round I guessed this was a translation error, but errors like these

mustn'thappen.The clarification of problem A was simply enough to ruin the whole contest. :)

True, with this clarification the contest went directly to hacking phase. After I got the notification I resubmitted mine solution cause I read it differently first time and then made around 13 successful hacks on all solutions in the room whose authors didn't update the code yet.

Why did they feel the need to resend a part of the statement as a clarification? The word substring was present in the original statement, so whoever misread it shouldn't get any extra help...

Edit: The clarification was the

exactly oncepart, not the substring part. The comment still stands, though: if they thought the statement was OK, this shouldn't have been done (and if they thought the statement was not OK, they should have changed it).What was the pretest 4 for B?

#comment-224262

You're absolutely right, but I didn't print leading zeros.

About me, there is not image to describe the problems.

Too many words to be able to understand the problems.

Does anyone misunderstood the problem A statement like me?

Most of them did, I guess. There were so many hacks on A coz of this :P

Happiness is — when Pretests pass after 8 WAs :D (I just hope I get AC)

`Problem D`

, I can't understand.Explain for me.

Fill our answer

xxxxx xxxxx xxoxx xxxxx xxxxx

For every cell we iterate for all figures (can all figures beat current cell). And check all crosses on table are beated.

What the fuck hack on problem A was ?

Everyone thought it's "can cut out as many substrings as you want"

But actually it's only allowed to cut once. (Clarification)

So contest went directly to hacking, to hack people who didn't update their code yet

CODQEFORQCES

Pretests are extremely weak; most of solutions which I hacked were checking that input string contains "CODEFORCES" as a subsequence. Checking that input string contains "CODEFORCES" as a substring also passes pretests.

Strong pretests take all the fun of it :)

How does it pass the first sample test? (CODEWAITFORITFORCES)

Agree, my bad. Probably I understood one of hacked solutions in a wrong way.

However, here is another example of

what can pass pretests— 10893468. This solution doesn't even check relative order; there is only counting of occurrences for every letter. It will sayYESfor testSECROFEDOC.Just cur CODE

~~**WAITFORIT**~~FORCESI hacked some solutions in the end of round with tests "CODEFORCESB" and "BCODEFORCES"

Can someone check what is wrong with this submission for problem D : http://codeforces.com/contest/538/submission/10895720

It gives WA for pretest 1, and I can't find any move that violates the given input.

Do you print

`o`

in the middle of the board? :)Yes.. on Line 15

I printed *'s in place of x's.. :/

You were supposed to print

`x`

for the moves, not`*`

.Yup..

I will become

`Candidate Master`

. So sadme too((

How to solve Problem E ?

DP over subtrees, with four cases depending on which player's turn it is and which player is allowed to rearrange the numbers in the leaves beforehand.

The number that then needs to be stored for each tree is the final result if the leaves of that subtree are labelled with the numbers from 1 to msub, where msub is the count of leaves in that subtree.

Would you mind to elaborate a bit more? It's not clear to me.

Let's first say we are the minimizing player and also allowed to modify the labels.

For the subtree rooted at

i, definef(i) =kif the game on that subtree ends on thek-th smallest label among the leaves in that subtree.There are two kinds of nodes, depending on which player's turn it is. Let's call the current node

iand its childrenj_{1}, ...,j_{t}.Case 1: it is our turn. Let

jbe the child ofiwith minimal value off(j). The best solution then is to rearrange the labels in the current subtree so that all the smallest labels end up in the subtree rooted atjand make our move toj. So in this casef(i) =min(f(j_{1}), ...,f(j_{n})).Case 2: it is our opponent's turn. As he wants to maximize the result, he will move to the child

jwhere thef(j)-th smallest label is as large as possible. We need to find a rearrangement of the labels to keep that value as small as possible, and we can see that the best we can do for that label isf(j_{1}) + ... +f(j_{t}). For instance, we can place thef(j_{1}) smallest labels belowj_{1}, the nextf(j_{2}) belowj_{2}, and so on. So in this casef(i) =f(j_{1}) + ... +f(j_{t}).Let's now say we are the maximizing player. This works in fact dually, we now only have to define

f(i) =kif the game on that subtree ends on thek-th largest label instead. We can even reuse the code from the minimizing case (something I didn't notice during the contest).Code: 10900486

I figured out simplier solution: let's consider 1-st player as person we wish to win. Firsly, you can recognize, that if our palyer can get x as an answer, then he can do it for x-1.

So you can use bin-search on answer. Let suppose your answer x, then all numbers >= x denote as 1, others 0. So you need to have 1 at the end in order to win. Let's calculate next value(

dp): minimal number of 1's needed to win in current subtree. If this is vertex where you are to make move, yourdpis just max of values of your childs. Else you value is simply sum of those values.Now the magic: we can remove bin search (which was displyed for better understanding), and replace our calculting value with something as: minimum number of large(small) numbers to win. So the answer for first would be (

leafNumber+ 1 -dp[root]), anddp[root] for the second.http://codeforces.ru/contest/538/submission/10897335

DFS, for each node you compute how many leaves are there in this subtree and index of a leaf to be returned (for example there are 10 leaves and this node will return 7th of them (ordered by value)), i.e. the exact values don't matter. E.g. for MAX node with two children-leaves the result is simply 2|2; for maximizing game for MAX node with children 1|3 and 1|2 the result is 4|5.

Then figure out how to combine such values for MIN/MAX nodes, it's quite easy.

I looked at your dfs code. But I can't figure out why is summing the children nodes correct for some cases? Other cases are clear though.

For example in maximizing game look at MIN node. Assume children have values

a_{i}|c_{i}, that means that in a child nodeithea_{i}th element will be selected. It means we can puta_{i}- 1 smallest elements there and they will not be selected. Sum of alla_{i}- 1 gives us number of smallest elements we can drop away. And then it does not matter where we put the next smallest number — the MIN node will choose it, that is the index of chosen node will besum(a_{i}- 1) + 1.Thanks its clear now

How do you calculate which and how many numbers to use for B ?

Maximum digit.

Oh, maybe I will get

`Wrong Answer`

.The maximal digit is the answer to "how many". Simply generate a number which has '1' where N has nonzero digit and '0' everywhere else, then subtract it from N. Repeat this until N becomes 0. To me, this is done easier using strings, not numbers.

Looks like I finally solved B 5 minutes past 12. :( But maybe Iam wrong. System tests are pending, so they won't accept my submission yet. Here is link . http://ideone.com/1Izsvr

when will system testing start ?

Now

2.5 hourits too much for 8 problem.I think10 minit's enough to write all of them ;) Why author of problem D thinking that his problem gives ussomething...,becose I know how to solve problem E(and idea for F) fuck him as my brain is fucked now by '#','o','.' symbols -_- Happy 300 Round -_-easy man !!! calm down :D :D

I think similar to you, you are my best friend, LashaBukhnikashvili

People can't understand us,they are w8ing their t-shirts and positive ratings so dont afraid about their minuses ;)

aeiou

Yes, problem D gave me something. I realized, that in some problems I need to output redundant "YES" before the solution itself and in some (other) problems you do not need to :) It cost me level up :) Nevertheless, I liked the contest and problems and do not feel that 2.5 was too short, first 4 problems were relatively easy, although not always well formulated :) Don't worry man, just have fun...

Am i the only one who overkilled B, with a knapsack? XD

DP was used by me too.

i used bfs

Note for myself: don't solve problems starting from the hardest if the contest is for both divs.

F worth 2500 pts — 300 AC

G,H worth 3000 pts — <10 AC

Are you serious? One thing is that there were no "middle" problems, whole problemset was completely not balanced. Second thing is that point distribution was very badly adjusted. Problem with 300 AC can be worth at most 1500 pts, no matter what. If someone creates a contest with some very easy problems designed to easy Div2 problems than what he should do with Div1-type problems is to save the

ratiosof values of other problems, not theirdifferences!Last submission ID was 10896731. So the T-shirt list will be:

69 is so pupular number ^_^

I don't think so

Because you know nothing :D (john snow)

More like because he ranked 69th.

I am on my way to become a newbie, and you care about a T-Shirt. Priorities, I guess, :(

151st

Same here. Please disqual one person! :D

Same T_T

And

off by onefrom me:What we say to CF t-shirt?

Not today.Yes, we confirm this list.

There are two 40th ranked contestants, cubelover and kcm1700. Will they both get a T-shirt? ;)

Both of them will get a TShirt.

Good bye

`Master`

. Good bye`Orange`

. Good bye Stupid-Doghello more stupid-Dog!

Don't give up! You're not becoming gray, at least.

Hey, don't do that!

.

why 2.5 hour for 8 problems!!??..main question,same herelashabuknikashvili ylea?

The problem statement mentioned "cut out

somesubstring from the banner" in the second paragraph, also from the last paragraph, "cut out of the bannersomesubstring”. But I can't see where did the problem told us that just cutexactly onesubstring, at least in the English version.So could someone explain to me?

"some substring", not "some substrings" ?

maybe "a substring" is more suitable and unambiguous than "some subtring"

VERY similar problem to C was presented in TC 3 months ago: http://community.topcoder.com/stat?c=problem_statement&pm=13634&rd=16279&rm=324891&cr=23113968

The Div2 version of that problem was even more similar: http://community.topcoder.com/stat?c=problem_statement&pm=13606&rd=16279&rm=&cr=40118276.

Despite that, I really enjoyed this contest. Even the easy problems were interesting and well-written.

aeiou

Can somebody please help, I don't know why am I getting runtime error on problem B code here while it works perfectly on my machine.

I have same problem by D

in problem b 538B - Quasi Binary : in this submission 10894501 when i run the system test, the wrong test case on my machine it gives me the following:

415

5

111 101 101 101 1

here the systems says it gives

9

110 100 100 100 1 1 1 1 1

actually i have been all the contest trying to figure it out and i feel so angry can anybody tell me what is wrong with my code ???

i too got the same thing..:(

log10(x) is a floating point function and you should never use it for integer operations.

Also the function has different implementations in different systems and compilers, and will give different results and/or precision errors.

but i cast it as (int) ?

casting, in your case, does nothing at all because your already floored the answer.

What I am saying is that, in some systems, log10(100) may be < 2 so when you take floor, it gives 1 instead of 2.

My solution for B gives correct answer on ideone while it fails on codeforces server for 4th test case.linktocode

strange!!!

How to solve D? I was trying to check all the possible moves for various pieces, but couldn't come up with a better logic. Any pointers for it?

Yes, you essentially do a brute-force check.

For each piece (o), and for each clear space (.), you can compute the difference dx, dy. And you know that dx, dy cannot be in the set of vectors. You disallow all such dx, dy (this takes O(n^4) time).

Now, considering the dx, dy which are not banned, you check that each "x" is indeed attacked by some piece, i.e. for each "x", there is some "o" such that the difference is not banned.

If this is true for every "x", it is possible and you output all non-banned dx, dy. If this is false, it is impossible.

Are the editorials out? When and where to find them?

The main blog is going to be updated when the editorial comes out.

I'm terribly sorry, but the editorial will most probably not come out today. Stay tuned tomorrow, I'll update the main blog post when editorial is up.

Thanks for clarifying

In problem D in testcase 2(knight) (o..x) is invalid (I suppose we cant make this move since it forces the piece out of the board) but why in testcase 1(rook) (ox) is valid? Can anyone explain why?

"If there are several possible answers, print any of them."

o..x is a perfectly valid move in the second case, but the possibility given in the samples also correctly describes the board.

Thanks! completely missed the statement that " there can be multiple answers"

my dreams came true, finally I hacked a red one and became international master. Thanks Endagorion..;)

My code for Probelm A fails on this testcase: MDBUW**C**ZFFZKFMJTTJFXRHTGRPRE**O**RK**D**VUXO**E**M**F**YS**O**MSQGHUKGYC**RC**VJTNDLFD**E**WF**S**

My output is YES but the answer is no. Why is it no? I can cut out the substrings in between the words needed and produce CODEFORCES as i have shown above. Where does my logic fail? Is there a catch?

http://codeforces.com/contest/538/submission/10898807

2015-04-26 19:08:16 Announcement Problem A. Cutting Banner

Note that you have to cut out the

substringexactly once in this task.So I keep bruting till I find a second. If I fo find one more then no. right?

I grossly happened to misunderstand the question. When it said 'ONE substring', i thought 1 CORRECT substring. Not 1 wrong substring. I don't know if this was misinterpreted by someone else too but the language should have been more specific. So much for the downvotes on my comment!!

I misunderstood it as well, albeit I should have read it more carefully; but yeah it got me hacked.

It was later given in announcement that you have to cut out the substring exactly once in this task.

Can anybody please explain why I am getting wrong answer on case 13 in C? http://codeforces.com/contest/538/submission/10891638

In the

`hei[i] > hei[i-1]`

case, you want`int ans=hei[i]+(k-index[i-1])/2;`

instead of`int ans=hei[i-1]+(k-index[i-1])/2;`

, i.e. you want to increment the largest of the heights instead of the smallest.Thank you. Got it accepted after correcting this small error.

Waiting for the editorial :)

Can someone please tell me what is wrong with following submission of Problem — B:

http://codeforces.com/contest/538/submission/10876749

It says wrong answer of test case 4 which is: "415" and the output as:

5 110 100 100 100 1

where as on my laptop the answer is:

5 111 101 101 101 1

Can someone please tell? Thanks.

Don't use pow for integers. Look it: http://codeforces.ru/contest/538/submission/10899576

If you change

`ans[i] += pow(10, cur)`

to`ans[i] += pow(10, cur)+0.01`

the solution passes aspowreturns a float. Be careful with floating points — the compiler flags from Catching silly mistakes with GCC would have catched that.you could write this, but it's not standard:

also you can implement your multiplication function too.

read this for more info.

Thank you guys!

Problem A sample test: BOTTOMCODER :)) Is it joke ?!

LOL, minion voice ))

For task A, one of the sample cases was DOGEFORCES. Sadly, the original image post is now down: http://codeforces.com/blog/entry/9729

Does anyone have a working link?

if (abs(notes[i].second — notes[i — 1].second > notes[i].first — notes[i — 1].first)) {

cout << "IMPOSSIBLE" << endl; return 0;

}

I can't believe my typo in problem C passed pretests...

I felt violated getting hacked on question A due to misinterpreting the question. Violated in a good way though, by all means.

Can anyone explain why this solution fails for E? http://codeforces.com/contest/538/submission/10901544 There are four DP cases based on whos turn it is to move and who is manipulating the game. I guess I messed up one of the cases somehow...

Wow, I'm surprised that caused WA instead of an error... Thanks!

I am waiting for the tutorial in this contest (especially problem D)

Maybe first time that AngryBacon wants to be at place 269 instead of 16 :)

wasn't able to participate live in the contest because of a worthless college exam. looks like everyone really had fun..

Still waiting for

editorial...And waiting.

Codeforces Round #300 (with prizes(without editorial!!)!).

Finally done. Sorry for the delay.

The first CF round that I couldn't solve problem A, although I spent more than 1 hour solving it :(

Can someone explain what problem D is about? i just dont understand what the problem is.

I had an interesting experience with problem A. i locked and then found that my submission was wrong! So what I did was that I first hacked all those solutions which were locked so that they cant hack mine! So in the end I managed 10 successful hacks ( = + 1000 ) before my solution was judged wrong answer. I hacked 5 solutions with the same hack ( = 500 points) that would give WA on my solution as well!! It was like Counter-Strike!!

BTW, It's allowed (or at least was allowed) to hack if you locked before your solution was hacked

basically that is why i tried to hack all the locked solutions before they hacked mine. And I won! :)