Hi, Codeforces!

I'm quite excited to invite you to Codeforces Round #439 (Div. 2) which takes place at 16:35 MSK on 6 October and lasts for two hours.

One of the five problems is created by quailty, and the others are created by me. This is our first round here.

The round couldn't have been realized without efforts of KAN and vintage_Vlad_Makeev. Besides, I also want to say thanks to our testers: cyand1317, visitWorld, Nisiyama_Suzune, cdkrot and 300iq. Thanks for your help to the contest. Also, thanks to MikeMirzayanov with the fantastic Codeforces and Polygon platforms.

The contest will consist of five problems and it is **rated** for Div. 2 contestants. The same as before, Div. 1 contestants can take part out of competition.

The problems will feature... *Well, let's wait and see* for another time ;) As per Codeforces tradition, moderate length text and no spoilers about the actual plot.

I hope you can have fun during the contest. Good luck and have fun, wish you high rating!

The scoring distribution will be announced later.

See you tomorrow!

**UPD1** : The scoring distribution is 500-1000-1500-2250-2500.

**UPD2** : The contest is over. The editorial is available.

**UPD3** : Congratulations to top five coders!

### Division 2 :

- 1heart2plans (solved all problems!)
- fateice_ak_ioi
- esfeng738
- XZA
- T404

### Division 1 + Division 2 :

- 1heart2plans (solved all problems!)
- unused
- fateice_ak_ioi
- nuip
- chemthan

Have fun! See you next time! (Let's wait and see...)

Auto comment: topic has been updated by Tommyr7 (previous revision, new revision, compare).The E's data has some weak,because many people violence passed,but if Op 1 is 50000 and op 3 is 50000 many people will be TLE.

Well... It's my issue... In fact, the generator for div2. E isn't easy to code... I added 20 tests after randomly getting the ractangles... and I didn't consider this situation... Maybe I will do better next time... Thanks for pointing out!

Tommyr7's round, must be very nice to perticipate.

Another chinese round. Hope for short problem statements.

Not only Chinese round,but Shanghai Round as well.It must be a fantastic one as the ones written by cyand1317!

WOW Shanghai Round OvO

zici!

%%%%Q神

?? What's mean?

Wow, another round with special starting time. I can't stop craving those thought-provoking problems.

Very good! Hope the statement will be short.

HAHAHA

Nope

Can you read?

umm, NO

Auto comment: topic has been updated by Tommyr7 (previous revision, new revision, compare).I did terribly today and cannot wait until tomorrow to try to restore my confidence.

Good luck!

gl hf every one

good luck!Poor English.

I have confused blogs.Hope this contest problems also have a short statements like previous. Wish everybody luck! YA!

Its not.

If there written "Codeforces Round #***", it means that it is rated. But not always, for example.

Right. In addition, "Codeforces Round #***" can be unrated if there occurs any unexpected technical problem (though those contests are declared as rated, later they're declared as unrated in those cases).

Wish that this contest announcement will be, as short as, statement)

Yeah, This contest will held on my Dad's birthday.The bad thing that I will be doing this contest during my family сelebrating dad's birthday(((

I really think you should celebrate your dad's birthday in case contest clashes with the party time :)

Finally came to a Chinese Round！Fantastic！ Zici Tommyr7's first round. And ORZ Giant Red Q ! I think at Tommyr7's and quailty's level , it's not difficult at all to prepare for Div1！

Yes, and they (classified information) with (classified information).

Well, it's our first round. So I think it more proper to prepare for just one Div. 2 round.

proper uh... how to understand this words ? my English is not so good sorry ... proper means suit? maybe... QAQ

Yep...

Chinese Round,fighting!

Congratulations....hope your 1st contest will be very interesting and expecting high rating :)

Thanks.

You are most welcomed

So before every contest, I get an email announcement for the upcoming contest, and every time it states that the contest has an unusual start time.

Out of curiosity when is the usual start time?

19:35 moscow time

For(escapequailtyhit callIf you ask me zici not zici, I'm zici

EMPTY

As someone who is about to retire, I hope to have a happy ending.Come on！

quailty, I hope the problem's quality is very good.

Auto comment: topic has been updated by Tommyr7 (previous revision, new revision, compare).I wonder how Tommyr7 became rating ~2300 so fast (3-4 months) from rating 1800 (with many participation)...

In fact... I can't performance well in Div. 2 contests and I even don't know why...

Always FST on div2AB may be one of the reasons...

You can see that I fst problem B yesterday again... which makes me very upset...

Unrelated commentwhat's FST short for :DFail on System Test

Thanks, good to know :D

Let's Hack it!!!!

Someone tell me how to solve C??

excuse me?

Let me bring my crystal ball... Oohhhh, I can't find it. Must have missed it on the field some day.

Well, now you're gonna need to read the problem first.

You can do it greedily. Just sort the array and try all the numbers from smallest to largest.

Greedy won't work. check this test:

4

3 -4 10 -4

You can use Brute force. Just O(n^2). n is less than 10000. So brute force can solve it.

But time limit is only 439 miliseconds. And implementation is pretty heavy so the program can be slow. How can you deal with it?

You can use fastIO, such as read() or fread(). :)

The brute force solution seems to be O(n^2), but is actually O(n*log(n)), so the solution can easily pass all tests.

I solved it with Convex Hull on linked list! :)

Alert !! Sarcasm Overflowed.

Yay! I don't have to feed the cows this morning. Let's have fun in this contest :) !!!

Good luck~

Today's round ID is 869, which is one of the substring of E869120. I'm lucky that I can participate.

The next contest which's ID will be in your username as substring will be 912. After some more contests :)

the start round doesn't suit me, i've to run from college to home quickly and the contest has started 10 minutes :)

It seems that chinese round is difficult as usual.

That's why people shouldn't vote up the announcement before they read problems.

I am not being able to open any solution for hacks. [Locked solutions]

Awesome Chinese names got me!! :-P Instead of "Koyomi", I wrote "Kayomi" and got pretests passed. So, I locked for hacking and now I realized that I made a mistake!! Worst feeling when you know your submission is going to fail in system testing and you cant do anything :-P

dont worry...have hope...it wont fail :p

I see what you did there XD

The answer is always Karen so don't worry and you'll be accepted

Damn...that is great! Totally missed it.

I realized that when I tried to hack this code:

int main() { printf("Karen\n"); }

It's not always :)

Consider the case: 2 4 1 5 4

No of pairs: 3 (1,1) (2,1) (2,2)

Luck wasn't in my side, and contest got over before i could hack a submission.

all numbers are distinct

Oops, I missed those constrains.

Why is it always Karen? Sorry I'm kind of slow on these things.

It's easy to see, xor(X,Y) = xor(Y,X) right ? So if (X,Y) is a valid pair, (Y,X) would be as well.

As you can see the answer comes in pairs, that's why it's always even.

Don't forget that xor(X,X) = 0 and 0 is not allowed in the problem so you will never count this one.

I guess the names are Japanese as the characters from the problems are a part of an anime known as the Monogatari Series.

Yes, they're Japanese names. Though Kanbaru didn't appear in problems XD

XD ? what does it mean ? it's a abbr?

Rotate it by 90° clockwise.

502 Bad Gateway

C is savage :/

Why ? As I am new to the world of CP. Why Chinese contests are not good ? :P

There is a difference between Hard and good :/

since when codeforces started allowing Div2 participants in Div1 contests.....???

How to solve A ? [ What is the hack case for A as I solved it using O(n * n)]

ans for A is always "Karen".

The answer will always be karen because the number of pairs will be even always. Suppose, for pair (1,2) the answer(xor of both numbers) exists in array, then obviously for (2,1) answer will also exist. Hence for x (x1,x2) number of pairs there are other x(x2,x1) pairs. Hence 2x pairs in total. so the answer will be karen.

I am not able to see other solution for hacking after locking my solution. Also my score shows 0 in my room. Why is this happening? :/

Clear Cookies of your browser.

Didn't help

Try C then :-p

Problem descriptions were not that long. Thank you!

It was a high quality round.

Thanks for your supporting!

what should be the output for

1 1048576 1048575

in A ? update- it was my mistake .

I tried to hack one solution which was giving Koyomi but ended up with unsuccessful hacking attempt.

bool vis[2000200]; will get Runtime Error, but int vis[2000200]; wiil not. I tried to hack the second twice but both failed！ It is really strange…

Karen?

Are there any test cases that don't give Karen?

what is the ans for 1 5 0?

1 <= xi, yi <= 2*10^6

It is always "Karen"

Math, Math and more Math.

.

I did hashing on fenwick tree for problem E. Even though I did double hashing, I'm still crossing my fingers.

isnt there 2-dimensional segment tree?

When you're < 30 seconds late from submitting a solution.......

Is there any alternate approach to E, mine is ->

I maintain a 2D BIT, where in every cell I store hash of set of rectangles that affected it. For query, I just check if hash of given two cells is same. Query as well as updating all elements inside rectangle while adding or deleting it takes

O(log(n)^{2}) time.My hashing technique though is very naive, first I assign unique numbers to rectangles, now I maintain few functions like sum of numbers, sum of squares, xor of numbers, xor of squares in every cell. Comparing two cell then will require to compare these 4 numbers. Using lesser and better functions should also suffice.

O Just sum some random number in the rectangles using bit 2d

I did in q*n.

code

So I did but got tle on test31.

mine is atmost 2500 basic ops per query.

A. Nice Hello World

B. Nice problem for Python

C. Nice problem for Python

D. Nice difficulty

E. Nice data structure

The difficulty distribution is 500-500-1500-3000-2000.

Please dynamic problem scoring.

Mathematic round

do the pretests of E contain big tst cases

In problem B one should check for 0!/0! case answer should be 1 .

if(a==b) {

printf("1\n");

return 0;

}

thug lifeHow to solve C?

Fix the number of edges between a-vertices and b-vertices. Let there be x of them. Choose x out of a possible a-nodes and x out of b possible b-nodes and connect them in x! ways. Do same for a-c and b-c pairs. Multiply all 3 values to get final answer.

Code

I don't get it :/. What do you mean by "Fix the number of edges between a-vertices and b-vertices" ? Can you explain with the example a={1,2} and b={1,2,3} ?

x can range between 0 and min(a, b). In this case, x can be 0, 1 or 2.

If x = 0, number of ways = C(a, 0) * C(b, 0) * 0!

If x = 0, number of ways = C(a, 1) * C(b, 1) * 1!

If x = 0, number of ways = C(a, 2) * C(b, 2) * 2!

C(n, r) is number of ways of choosing r objects from n objects.

C(a, 0) * C(b, 0) * 0!,

C(a, 1) * C(b, 1) * 1!,

C(a, 2) * C(b, 2) * 2!

Sir, can you explain why you multiplied the expression with the factorial? 0!, 1!, 2!??? satyaki3794

After you've chosen x values each from the two sets of nodes, you need to make x pairs from them. For the first a-node, you have x possible b-nodes to choose from. Make a pair. Now, for the 2nd a-node, you have x-1 possible b-nodes to choose from, and so on. Total ways is x!.

Calculate

F(a,b) independently for 3 pairs and multiply.if

a< =b,F(a,b) =F(a- 1,b) +F(a- 1,b- 1) *bBecause, for 1

stelement of first set, if nothing is assigned in 2ndset, there areF(a- 1,b) ways left, and if 1stis assigned inbways,F(a- 1,b- 1) ways are left.why not so:F(a-1,b-1)*a+F(a,b-1)

The relation is symmetric, hence I mentioned

a< =bbefore it.But How are they independent of each other ???

When you have joined a point in set 1, with a point in set 2, joining this point to any point in set 3 doesn't contradict any given conditions, hence you can treat them as independent.

Thanks a lot . I got it :)

swap(D,E)

I did not enjoy the difficulty this round. The difficulty was A, A, D, E, E. One small mistake on the first two problems can drop your rank by 500.

I've been tricked After I hacked someone's code,I saw this:

#define int long long intThat's cruel.

Exactly this was something I faced in a previous round. From then, I always check ;)

Yeah,I will check it from now on.I were so innocent...

I use this, It save me from overflows :D

but long long int is time consuming.

Codeforces is not so strict with time limits :D until you are writing some sub-optimal solution in which case you need to edit this #define int long long

wanna cry, come to the solution of C in the last ten minutes but fail to submit it.

Same for me!

Hacks on fire :-p

if wouldn't have done it, could have solved c :p

But U solved C right? :-p (Mentioned above)

i could have submitted* :p xD

Answer of A is Always Karen ????

Yah

How ?

a ^ b = c -> b ^ c = a

Yes. Because, if A^B = C then B^C = A and A^C = B both are true. When you find a pair A and B such that A^B = C and C is one of the numbers of your list, then it's guaranteed that you will find another pair looks like A^C (Which will give B) or B^C (Which will give A). So for one successful XOR another successful XOR is guaranteed!

ordered pairswas the key word.Auto comment: topic has been updated by Tommyr7 (previous revision, new revision, compare).Oh my god, I made an obvious mistake and I passed the pretest of problem A..... Farewell my rating.

using map in A gets TLE :(

Solution was O(1) answer is always Karen :D

Too lazy to think for problems which can be solved with brute force approach :p

Same happened with me in B i just looped from a+1 to b without noticing constraints lost 50 points :(

I did the same mistake too.

no it doesn't my submission

but why it's with me:31069010

It's giving TLE with [] operator but passing with find() but both are logarithmic in complexity .

Using [] operator adds keys to the map(if it doesnt exist), thus making it too large for further operations. So find() runs fine but [] gives TLE. Made the exact same mistake !

I had a small doubt, if [] is making keys then when it is called for the second time why doesn't it return true.

Map stores key value pairs. Sure it adds new keys, but maps them to false value.

Lol, never use a map just to map something to bool, you can always just use set in such case instead.

O(N^2*6) DP getting TLE in C but O(N^2) Pascal's trangle passing :( too strict time limit

Auto comment: topic has been updated by Tommyr7 (previous revision, new revision, compare).CodeForces runs 10^8 operations in 1 second? correct me

But it hates combination of map and long long int

I'm talking about C

You should have mentioned :)

In problem C, it is Impossible to solve the problem in Java using

O(N^{2}) Dp. However the same solution, in c++ passes like a wonder.Remember that I used my mind to come up with an idea for a good problem, I type the Code in Java, TLE on pretest #7. The same code I type in C++, AC 740 MS. So, U should also prefer a better language, not only use ur brain to solve problems.

I have been facing disappointments like these from time to time.

I'm just going to stop using Java anymore, because I feel a lack of respect towards it in the programming Community.

Some Proof :

C++

Java

It's not impossible http://codeforces.com/contest/869/submission/31077410 , there are others, you can see it on http://codeforces.com/contest/869/status/C , just set the language to Java ... I don't think there is suck lack, look to the top ones in the rating and you gonna see that some of them use Java

It's not that it's impossible in Java, it's just that you need much lesser effort (less efficient code) using c++ and much more effort (highly optimized code ) using Java

Some problems are impossible to solve in java, especially the ones using TreeMap/TreeSet. The fact is C++ has the advantage and java is at a disadvantage.

how 2 solve E in 8 minutes like

`fatice`

did ?begin with E first read problem in 1 minute think for 2 minutes code BIT in 1 minute code others in 3 minutes submit code in 1 minute

In fact, fatice ranked 1st in CNOI2017.

He's really cool.

/And I was shocked when I knew he took part in the round...lol

That 's amazing !!! When I was in the game, I was shocked when I looked at the rankings. Now read his code, I think in this life I can hardly write such a simple and concise code. lol

Perfect contest time after a long week.

This contest is not showing up in my profile. Is there some time to wait before it is updated?

Relax, it's a Codeforces.

checking an element a1 in map like ma.find(a1)!=ma.end() passes all the testcases for A but checking like ma[a1]!=0 gets TLE. why??

When you use the second option, an element with key a1 is inserted on the map, so the runtime will be slower since you gonna have more values in your map

but time complexity is same for both the cases and also atmost 4*10^6 elements will be inserted.

Running on tc22 for 15 mins...Can someone explain why

why in problem A mp[] gives TL ,, okay it will insert more elements in the map but still the max size of the map should be 2000+2000+(2000*2000)=4004000 so the overall complexity should be n*n*log(4004000) and this should pass in one second!

The constraints given in problem A are wrong thats why my solution did not pass The constraints for xi and yi given are 1 — 2*1000000 whereas actually the constraints are 1-4*1000000. Please correct me if I am wrong.

constrains is right but A^B may be more than 2*10^6

when the ratings will be updated? can't wait to become purple

Am I the only one who solved problem B by finding the repeating pattern of the last digits of b!/a! ?? This is my solution.

Yes. You are nezt gennady.

Haha lol! I wish I could be like him... :)

ratings?

What kind of joke it is to provide editorial of only 2 problems? :/

what is difference between finding in map and direct checking? ex. map<int,int>mp; mp.find(x)!=mp.end() and mp[x]!=0

If you access a key using the indexing operator

`[]`

that is not currently a part of a map, then it automatically adds a key for you. This makes a really large map (up to 4000000) in this problem. You should use`.find()`

or`.at()`

for lookup.Is there a good deterministic solution to E?

How to solve D?

My solution is hacked again. It's a sad story.T_T

Auto comment: topic has been updated by Tommyr7 (previous revision, new revision, compare).If 1e18*9 is not allowed by long long,then I could have hacked many solutions on B:D

Auto comment: topic has been updated by Tommyr7 (previous revision, new revision, compare).