Hello Codeforces!

I would like to invite you to CodeCraft-18, which will take place on Saturday, 20th January, 9:05 PM IST. This is a combined Div. 1 + Div. 2 round and is **rated** for participants of both divisions.

CodeCraft-18 is part of Felicity, IIIT Hyderabad's most awaited set of events, Threads. With a plethora of intellectually engaging online contests in various fields of programming, mathematics and general knowledge, Threads is a celebration of the spirit of computing and engineering. Other than CodeCraft, we have the Project Euler-style contest Gordian Knot and a jeopardy-style CTF event called Break In, amongst other events.

I would like to thank the CodeCraft team -- FundamentalEq, RohanRTiwari, additya1998, aman_shahi2, born2rule, codelegend, crvineeth97, deepanshugarg, devanshg27, mprocks, nir123 and virus_1010 -- for their amazing work in problemsetting. I would also like to thank gritukan for helping us prepare the problems, and MikeMirzayanov for the great Codeforces and Polygon platforms.

## Prizes

- Top 20 participants win Codeforces T-shirts
- Top 5 participants from India win Codeforces T-shirts

You will be given 8 problems and 3 hours to solve them. The scoring distribution will be announced later. Good luck!

**Update:** Scoring is 500 — 1000 — 1500 — 2000 — 2500 — 3000 — 3500 — 3750

**Update 2:** We would like to apologize about problem F and G, which turned out to be easier than we expected, and about the mistake in the statement of H.

Congrats to the winners!

Top 20:

1. SkyDec

2. Syloviaely

3. matthew99

4. dotorya

5. FizzyDavid

6. laofudasuanbaofushehui

7. ikatanic

8. j_______________________

9. Um_nik

10. cki86201

11. rajat1603

12. geniucos

13. MrDindows

14. Radewoosh

15. pavel.savchenkov

16. shanquan2

17. 613

18. JOHNKRAM

19. mxh1999

20. satyaki3794

Top 5 in India:

1. rajat1603

2. satyaki3794

3. jtnydv25

4. akulsareen

5. akashdeep

The editorial is ready!

20th is Saturday, typo I believe.

You are right, it has been fixed. Thanks.

typo nothing hepens

Only 20 T-Shirts. Please added High School Programmer Quote :'(. For Prizes

Please update the duration in contest tab! I am seeing 2 hours there!

Yes, it will be updated soon. Thanks for pointing out!

Auto comment: topic has been updated by Superty (previous revision, new revision, compare).Why is it not on the home tab yet?

It will attached to the home page soon after today's contest is over.

Hope for even amount of geometry and math problems.

(additional info) Last year there was only 1 math problem and no geometry problem.

why not odd amount?

r_ _ _ _???

Round 458, sir!

Auto comment: topic has been updated by Superty (previous revision, new revision, compare).I know it is very hard to prepare a contest and we are so thankful about that, but dear authors please double check your solutions. We all prefer not to have 2 consecutive unrated rounds!

Are there any prizes for school students in India

Dont think so.

Will the Project-Euler style contest be conducted on CodeForces ?

No. Gordian Knot will be hosted on Felicity's site on 25th for 24 hours.

You can see other technical events here.

Expecting 10,000 registrations.

Expecting al of them will be worse then me

HAHAHAHAHA

Good luck! Have fun! xD

Clashes with COCI

Here are some past codecraft contests:2017:Contest, and Editorial2016:GYM, and Editorial2015:Contest: GYM, Replay on codechef and Editorial2014:Replay on codechefI wonder, is there any reason why you are not giving T-Shirts to random participants like last year's contest?

It shouldn't be. Winner should get prizes not mine. If they give 10% or 20% randomly(for inspiration/more participants) that can be a better; I think.

Its time tu up my reting. Good luk to me

It works.. why? probably to be hacked.

So many indians, its their chance to prove that they can be good problem setters.

(nope)

I hope one day i will get a T-shirt from Codeforces. that will be the best recognition of my hard work.

Yeah, they are rare af! :O

Yeah , but i will get it one day

Will the drain be adjusted, please?

Yes!

"The scoring distribution will be announced later" ... ?

In Problem B, Is there any problem in range of a[i]? check my submission please.

Sorry, my poor mistake.

problem A: Sample test 2; How 576 is a perfect square????

Please do not discuss

24 * 24 = 576

"24 * 24 = 576" isn't simpler than "576 is square")

Can't hack solutions , it keeps on loading

shit contest kys.

what a hard contest!!

Getting runtime errors on pretest 1 with no error specified for problem C. Can the admin please help? It runs fine on my machine.

same thing happened to me. you are probably using a different compiler. try using custom invocation option. In my case i was using negative indices

May be another unrated contest is running..............................

i think they have to unrated the contest because of problem H :D

Even though I performed miserably in this round, I must admit this round was pretty neat as the problems were good. Good job!

this round should be unrated because of problem H like yesterday contest

I think that, because almost nobody even attempts H, the round should still be rated.

You are talking about 'H'; not 'A' or 'B'. As if you have stood 2nd because of this mistake.

I'm mentioned in problem H!

waiting for notification, when they say , round is "UNRATED " :P

C problem,,, looks interesting :P ...

Is fast enough for D ?

You need .

.

it might just pass if your constant is very small but unlikely.

How to solve C and D?

C digit dp.

D answer is yes if there is <= 1 numbers that isn't divisible by X.

How to find that for D?

Make a segment tree where you keep the gcd in each node. A query will point to multiple nodes and you'll pick the only one not divisible by X(if there are more, there is no solution). This node has either 1 or 2 sons with a value not divisible by X. If both values are not divisible by X you have no solution, otherwise just go to the node that isn't divisible by X.

Can you share your code for D?

Your solution to D did not timeout?

C : once you apply operation to N, it must less than or equal 1000. So you just have to pre-calculate k-value for 1 to 1000

C is a DP. cases k = 0, 1 are easy. k = 2, 3, 4. needs a DP., rest k's are 0.

I think C pretests are weak. it doesn't have any test case with k = 1?

just now i noticed a problem with my k = 1. I output s.size() instead of

`s.size() - 1`

Was a N * log(N) + Q * log(N) * log(N) solution supposed to pass D?

yes

mine didn't :(

I think so, at least pretests, i was able to pass pretests with this complexity.

Passed systest =)

No. That is what I originally did. You need .

I passed the pretests with a solution that i think is slower that Q log^2 N. Maybe i overestimated my complexity, or you implemented something slower than that.

That moment when you realize your C will fail 4 mins before contest end after locking it.

me too, for k = 1? or something else for you?

k = 1. Used it to get 3 blind successful hacks in the end but still RIP rank.

such a bad way to fail a problem :(

Exactly same thing happened to me. So, in the last 2-3 mins, I just put that hacks in other's code. Didn't read them so got 2 unsuccessful hacks as well with 4 successful ones xD

In my opinion, they should have had k=1 in test case. its not something at the core of this problem. .. but maybe that's my defeated soul consoling me..

Haha, I used K = 1 to hack someone else before I realized my code would fail the case too.

why if k==0 the answer should be 1 ?

because only 1 needs 0 steps to make it equal to 1

Is E divide and conquer? If yes, how to merge the answer of 2 sub-trees efficiently?

Maintaining mask of letters will work I think.

Thank you. Oh! I see, you just iterate through

`0^mask`

,`0b1^mask`

,`0b10^mask`

,`0b100^mask`

... right? Now the complexity is some O(26n). Ahhhh...Can u explain a bit more?

I haven't solved it yet. Here's how I want to merge sub-trees. So use a 26 bit mask to represent the odd/even of letters on the paths that ends at the current node under analysis

`u`

.If 2 paths

`p1`

,`p2`

ending at`u`

can be connected and form a palindrome path,`p1 - u - p2`

, then, if you xor the mask of`p1`

and`p2`

, either the resulting mask has only 1 bit set or all bits are 0.There's only 20 letters, [a, t]. Very important.

Oh thanks, I didn't notice that...

What if there is a path p1 ending at u which has mask something like ....000011 and p2 has mask .....000010 so in that case we won't be counting p1?

Since

`000011 ^ 000010 = 000001`

, there's only 1 bit... So`p1-u-p2`

should also be counted in this case...But u r not storing the bits except for 00001,00010,00100,01000,10000 for each node right?

so...in my opinion, u will store all possible masks. since the number of these masks is at most

`O(n)`

(# of paths to u from sub-trees), it is acceptable.I have a doubt. In a subtree there can be atmost N possible masks and when we are merging we have iterate on them. So, isn't the complexity is O(N^2)?

The solution should be a little bit different from dfs, by exploiting the information about the size of sub-trees (centroid decomposition, you may refer to the editorial).

thanks! i didn't know that it is out.

Must D be done faster than Qlog^2 or is my segtree implementation just bad?

I do that O(Q log^2 * gcd) with some optimization, and it runs about 2s. (Hope I can pass the system test)

However, I think the ideal solution may runs in O(Q log * gcd).

My Qlog^2 solution ran pretests in 1092ms

It's possible to implement query() in O(log n) and update() in O(log n * gcd). I think that's the fastest.

How does that work? I use a segment tree and binary search.

Also, I use the pragma to optimize my code, but it still runs in 2s.

This is my solution:

Keep a segment tree of gcd()s.

The query is basically "Is there more than 1 value in this interval, which isn't divisible by X?". You can check it by finding the leftmost and rightmost "bad" index; if they're the same, then there's only 1 bad value.

Yeah, you can binary search this, but that's not very fast. The segment tree query will produce O(log n) nodes that cover your query interval. Then, you can iterate over them from the left, and when you find one whose (already calculated) gcd is bad, you can locate the leftmost "bad" index in O(log n), by deciding to either go left or right in each layer of the tree. The same thing can be done from the right to the left side, again performing at most one O(log n) traversal.

Update: O(log n * gcd) Query: O(log n)

(During the contest, I was too lazy to code this, so my query was O(log n * gcd))

Simple implementation of an update actually works in O(log n + gcd).

Idea: After each two steps of the Euclid's algorithm, both numbers will be less than half of the original ones. Therefore, the number of steps is bounded by 2*max{a_i}.

(This should explain faster than expected running times.)

That's pretty much what I did.

For the queried range, I divided it into 2 parts and recursively went down the segment tree, until reaching a segment of size 1. If during the search, both parts had a gcd not divisible by the guess, that meant, it's wrong. Otherwise I chose the wrong part (if any) for continuation of the search,

You can check my submission if you want to see it more clearly.

How could you solve C?

baktrak binomial newtom

Hacking says that my version of Flash is too low to run. I am running the Flash that came with the latest version of Chrome.

Same here

For B : Hack Case is

5 1 1 1 2 2

What was the hack Case for A ?

I used this one..

5 0 0 0 0 -5

by a mistake I resubmitted for problem A near the end of the contest and so I got two hundred instead of 490 which was my initial score is there any way for me to get my initial score?

Nope

what are the hack cases for C ?

111 1

If you don't write an if() you'll print 3 instead of 2.

I bruteforced for values less than 1000 Maybe mine fill fail on larger values when k == 1

Why the third sample input of H is 36?

I think

`1 2 3`

and`3 2 1`

just have a single way to modify. QwQFor

`1 2 3`

, you can flip`3`

or negate`2 3`

. For`3 2 1`

, you can flip`2 1`

or negate`1`

.Oh, that's my mistake.. thanks a lot

Problem G submission by rajat1603 LOL

How did you guys hack problem C?

k = 1, you're supposed to print len(n) — 1

I Hope F is better than

Bitset is better than everything!

I see

O(N^{2}) solutions with bit masks at the top.I think it can be done in O((N + Q) * rootN) by keeping a suffix tree over each block of sqrtN. Not sure though, got confused with the implementation details.

Me : How to solve F?

Some red guy : Do kmp 7000 times

Me : That's obvious, but it won't run in time

Some red guy : Why not, CF is super fast

Me : (facepalm)

I know it is O(n^3) but why does it get wrong answer on C: code

K= 0 in test 4, if that is of any help.1111 0 ans is 1 your ouput is 4

understand now, thanks

I really enjoyed the problemset especially the tricky ones. Thank you guys!

2 — 1 in last min!!

During contest I tried to hack 2 solutions with the test "1 -64". However they both gave unsuccessful verdict. So I copied their code by hand, put it into codeforces compiler and it gave WA! Images: https://imgur.com/a/NrhKb. However, now I put their original codes and it gives correct answer! Could anyone please tell me the difference between their original code and my copies that could cause this? Code1 Code2

Your manual copies look same to the original codes. So, only difference could be the choice of compiler but then I have no idea how would that produce different results.

I tried both C++11 and C++14

Could you please tell ids of hacks?

403017 and 403242

Seems like it's some kind of undefined behavior.

Reference

C++ guarantees only that domain error will occur in case of negative x and nothing about return value.

Damn :( I thought since it's the same compiler, the undefined behavior would be the similar. Anyways, thx for clarifying it to me.

I checked C++14 and 11 gives INT_MIN while c++ 5.6.1 gives -64. (at custom test)

"A path in the tree is said to be palindromic if at least one permutation of the labels in the path is a palindrome."

Is there any reason to call the special paths in problem E palindromic when the obvious notion of palindromic path is different ? I only noticed the different definition 30 minutes before the end of the contest...

And it didn't help that the first sample didn't distinguish that. :(

Anybody knows pretest 5 in problem C? Maybe k=5?

lol

Lul

For C problem, I have doubt, pls someone help me...

question is saying that ,,

He calls a number special if the minimum number of operations to reduce it to 1 is k.****so , for the second test case,,,input is like

1111110112when I convert binary to integer,,, value is 507,,,

from 1, 507, there are 498 special numbers according to definition of the 'special number'...

then how come answer is 169 ... ( I know answer is 169 for

EXACTLY K steps) ...I spent whole lot of time on C ,,, could have done D though :| ...

This is my brute force, code,,,

code is here

( this is the code to generate and check special numbers , it takes string(s) and integer(k) as input, and converts binary string to integer value(n) , and then iterates from

1ton, and for eachicheck whether it's special number or not )this is the list of the special numbers

Yes, the statement did confuse me a lot, but here "exactly" is same as minimum number of operations as he any further operations is a wasteful activity on 1.

The problem statement actually means that the minimum number of operations to reduce it to 1 is "exactly" k. Minimum number was mentioned in the statement with respect to the operations needed. Although this is bad on the part of the statement writer since this creates doubt as mentioned by you because the operations needed for a number are fixed and there is no need to minimise the operations. I was also stuck at this for quite a lot of time during the contest.

Maybe they didn't want to tell us that the number of operations was fixed, on purpose, so that we have to figure it out ourselves.

well, next time just give input and output, and we will figure out everything ourself...

This is ridiculous, I had finished my C quesiton code in just 30-40 minutes,,, and then I was debugging that code for 1 hour,,, then decided to write bruteforce,,, and finally understood that it's "**exactly K**"

Moreover, we can't make any assumptions ,,,

in case you want to see my code for C question(considering

minimum k)code for minimum k steps

He calls a number special if the

minimumnumber of operations to reduce it to 1 is k.The word

minimumin the above statement changes entire meaning ...Now, don't try to fool yourself, and tell me that,,, I should look at the test case and then understand by myself,,,

I know life is not fair,,, but I had my faith in codeforces, why you do this :( ???

(JK)

Acctually the number of operations is not fixed because you can keep aplying the operation to number 1 any number of times. So by saying minimum they mean you cannot do that I believe

Another Hacking round. It seems to be a hacking contest, not a programming contest.

Cant be worse than me tbh. Solved B, it got hacked. I was like "Yeah, easy fix. Done. No more bugs now." Re-submitted, got accepted, immediately locked and...hacked again XD.

Lesson: Dont hurry for few points, take time and think over the problem. In the end, thats true- we get hacked because we did mistakes.

(Anyway, getting hacked> failing systest so...:p )

Why don't you like hacks? maybe it is just because you can't hack?

I can but I think to solve the problem is the matter.

I thiNk that hacking (finding mistakes) is helpful because you learn how not to make your own mistakes.

Also I solved 3 problems, but the duration of round is 3 hours, so hacking is a good way to waste your own time.

I can, still my opinion is that making three easily hackable problems is not a fine choice for a combined round.

i think it is fine.

please stop systests at this point! haha joke

Hack testcases for A?

For your code: 2 -1 0 ,Your output is 0, You just need to change "<=" to "<" in your function.

i'm so curious for the hack case do you know it ?

5 0 0 1 1 1

or

5 0 0 0 1 1

might work.

You can check out others' solutions to see where you got wrong. The person who hacked you maybe?

I think that's better than asking for the specific test case, because your algorithm is likely to be already incorrect anyway :p

So much failed system tests on C

pretty bad pretests if you ask me..

Actually, I guess that is intended. It makes contest worse not funner.

Exactly. One can get AC after a few minutes with 1/2 additional penalty. But after system test, BOOM, you dropped down by a huge margin for some silly coding error or corner case misses which could have been easily found.

If pretests will include every case, why have them?

A good good good contest :) but too bad for me, solving E and not get acc from it just because of my poor coding skills ... :( feel so upset ...

if(k == 1){ res--; } and only becaus of this i lost 850 point :(

That moment when you got TLE on test 105 problem C... I should change my profile photo...

This k=1 case where most solutions count 1 and still get pretest pass is a full killer. I doubt I will be able to find such a corner case ever in my life by myself.

Is there some easy solution for problemE rather than DP on tree with bitmask?

I type code slowly, so there seems not enough time for me. So I wish to know some simple implemented solution, thanks!

If you only consider the Centroid Decomposition solution easier, then yes.

How to solve F ?

i see that most of the people who solved it used bitset so is it a known algorithm ?

It's brute-force. O(N^2 / 64)

how ? if you did it with complete brute force it will be larger than N^2

Am i the only one solved E with DSU on tree? I was afraid that my solution is wrong cause it run so f*cking fast.

UPD : I tried to choose a random root. And it's even faster now.

no some others use this approach like me :)

also Arpa use this too ...

But still, my solution is much faster than Arpa's. I don't even know why.

lol ... you're right ... :)

how did u solve it with Dsu on tree?

can u explain your approach ? :)

Let's choose 1 as the root of the tree.

We will use Dsu on tree to get these two things :

calc[u]as the number palindromic paths such that Lowest Common Ancestor of start and end vertices is u.start[u]as the number palindromic paths start or end at u.So now, if we have a palindromic path (x, y), this path will pass through u only if lca(x, y) = u or lca(x, y) is not inside SubTree(u).

So the number of palindromic paths pass through u is

sum(SIGMA(start(v : subtree(u))) — 2 * SIGMA(calc(v : subtree(u)) + calc(u)....

thank's :D

So do i get 2 tshirts?

ideally you should

Yes, I think we will. AFAIK, it has happened before too.

Yes, you will. Congrats! :)

The way organizers had mentioned, you should be getting 2 T-shirts but wouldn't it make more sense to pass on one T-shirt to the next winner either in Top-20 World category or Top-5 Indian category? Anyways what value would the 2nd T-shirt of same contest add for you?

However, you guys are the winners, so it's up to you. Congrats btw!

I submitted my solution to problem C and it results in RE at test 5. I tried running the testcase locally, it outputs correct. I am unable to reproduce the error. Can anyone help?

My submission id: 34391810

Try custom invocation and print some debug variables (maybe you forgot to declare some variable — locally it stars with 0 but on codeforces server it starts with something else)

use diagnostics compiler

Adding cout to begin of choose_again() removes RE. So I thing you've somewhere got UB.

Yeah, you've got UB. if last cycle for i = 1 and j = 2 and number s with one 1-bit you access element at index 1, while size of bits is 1. So you've got there UB.

Yeah, I found that out. Was just waiting for the problem to get unlocked for practice again, just to be sure.

Thanks, anyway! :-)

this guy skipped Candidate Master!!

Is anybody check the brute-force solution of H?

For (n,d) = (4,2) and (4,3), my submitted solution and brute-force solution both give 268 and 364 respectively. But test outputs give 168 and 264.

My two solutions agree about some small input, so I couldn't recognize what is wrong. If anybody recognizes something wrong in my solution, please gives help. Thanks.

Both players should play optimally. If for some tree Storm will win then Ember won't choose this tree.

Oh, I misread whole problem statement...

I need to sleep... Thank you!

Why did Um_nik disappeared from the scoreboard? I definitely saw him on the scoreboard..

Admins decided that I was heavily affected by issue with problem H (probably because I was the first one to submit a clar about wrong sample), but I certainly wasn't, so they will restore me.

Oh, I got it. Thank you for your response. It was great that you solved problem H finally!

Probably he spent some time on H and found an error in the statement. Since he spent some time there he probably asked to be out of competition

I am not able to figure out wrong logic in my approach for C. I precalculated the number of 1s in binary representation from 1 to 1000. Now I start from 1st digit. If it is 1 I have 2 options. Put 1 and move to the next digit or put 0 and make number of 1s equal to all precalculated values with k-1 1s. If it is 0 I move on to the next digit directly. For example k = 2 then I try to make number of 1s equal to 2, 4, 8, 16, 32... so that in next step I get 1.

The last year magic was canceled in the scoreboard

UPD: It's fixed

WTF is happening with the rating changes?

What happened. The rating just disappeared

I guess history just repeated itself :(

They are updating some scores. Usually it is someone who was detected of cheating that acctually wasnt — so they reinsert him in the scoreboard (which requires everyone's rating to be uptaded).

So no, it does not mean that the contest is unrated

I hope

I am not sure but I guess they are checking Um_nik's submissions because first the contest was made unrated for him (see his comment above). So they have to change the rankings.

Um_nik obligingly pointed on the issue in the H and spent a lot of time to argue his point of view. I've made him "out of competition", but he asked to return him despite he has negative rating change. He is my hero for today!

It is Um_nik but not for cheating but for not bothering a legendsry grandmaster

What happening

They've decided to retest Um_nik's solutions?

And...He has already changed his username from KarimHussein to THE.OWL? Or it's just a bug?

Did you just scr...

Auto comment: topic has been updated by Superty (previous revision, new revision, compare).Why there is no changes at the

top ratedboard?MikeMirzayanov Hello Mike! I'd like to ask a question. Is there any way to test someone's code with a large file data or a data generator after a contest is completed? After this contest, I've do some analyses for the problems, and found some coders' solution make me confused after calculated the time complexity myself, so I want to do a test to those solutions. Many thanks!!

I got Top 20 this time.How can I receive the T-shirt, thanks. I haven't receive any gifts from Codeforces before so that I do not know what to do. @Superty

We will inform you soon.

Thanks.

I have question about ProC（testcase 27）:

11111100010011110101100110100010001011100111001011001111101111110111001111011011110101100101001000111001000100000011011110110010001000111101001101001010100011 1

It have 158 bytes and we can set every bits to '1' and they all less than N.So why the correct answer is 157?

sorry,i know the mistake

where is the tutorial ?

so sad...

Please help me why mycode 34405334 for problem C is getting MLE, my sloution is somewhat similiar to the editorial.

You have initialised an array with size 4*10^8 bytes. C++ has a limit on its array size to around 2*10^7 bytes.

Oh I added 1 0 extra and every time ignored it while debugging. Thanks

Can anyone tell me what's going on? Yesterday on system tests I got TL for problem D. But after some hours I send absolutely the same code and got accepted. How does it works?

yeah with 4 ms difference from tl

It isn't important, I'm don't understand how two same solutions can work for different times.

System load is probably the factor :)

I think CF should seriously think about this problem. For example;e someone can got accepted on pretests in round time and after that get TL on the same tests on system testing only because system load...

Thanks for very interesting problems

Can anyone explain the sample tc #2 for Div. 2 C.

If I'm not wrong, we will take all the numbers with 2 bits set, 4 bits set and 8 bits set.

Because 11000000 -> 010 -> 001

Similarly, 1111000 -> 100 -> 001

And, 11111111 -> 1000 -> 001

But C(n, 2) + C(n, 4) + C(n, 8) exceeds 169.

Please, point out my mistake. Thanks in advance.

Edit :I was computing C(n, 4) wrong. So stupid of me.You can not choose

`111111110`

nor`111111101`

(however, they have 8 bits on), because they actually exceed n which is`111111011`

.So, the answer is

Thank you :)

The contest problems were amazing , I solved only 2 of them , but got some rating + , so I am happy with it :D