Hi, all!

This is not Tommyr7, but the **impostor** behind the round (guess who I am? :P). The statements are written by me. The characters in the round feature, again, the *Monogatari* anime series, and to be more specific, *Nisemonogatari: Fake Tale*. The statements involve stories with fake things and oddities, but as per tradition, contain no spoilers. Thank you, everyone, and hope you've all enjoyed the round!

On a side note, special kudos to the best impostors of the round (except me, lol)!

Any feedback on problems and tutorials are welcome -- we look forward to doing even better in the future!

Here are hints for all problems and detailed tutorials!

### Hints

**Problem A**

**Problem B**

**Problem C**

**Problem D**

**Problem E**

### Tutorials

### 869A - The Artful Expedient

**Author** Tommyr7, cyand1317 / **Preparation** Tommyr7, cyand1317 / **Tutorial** cyand1317

**Tutorial**

**Solution 1 (Tommyr7)**

**Solution 2 (cyand1317)**

### 869B - The Eternal Immortality

**Author** Tommyr7 / **Preparation** Tommyr7 / **Tutorial** cyand1317

**Tutorial**

**Model solution (Tommyr7)**

### 869C - The Intriguing Obsession

**Author** Tommyr7 / **Preparation** Tommyr7 / **Tutorial** Tommyr7

**Tutorial**

**Model solution (Tommyr7)**

### 869D - The Overdosing Ubiquity

**Author** quailty / **Preparation** quailty / **Tutorial** quailty

**Tutorial**

**Model solution (quailty)**

### 869E - The Untended Antiquity

**Author** Tommyr7 / **Preparation** Tommyr7, cyand1317, visitWorld / **Tutorial** cyand1317

**Tutorial**

**Solution 1 (visitWorld)**

**Solution 2 (Tommyr7)**

**Tommyr7: I do hope you all enjoyed yourselves during the contest. See you next time!**

Maybe swap D and E will be better

Maybe... It's my issue...

(I firstly thought problem D is just difficult to code, but easy to get the right solution, but after the contest I found that I was wrong.)

Yes, it's easy to figure out the solution for D, but i can not implement it in 1 hour. And i don't know why i forgot to put random in E :(

I agree with you.But I can't solve the C.The contest is always showing wrong answer

I know the answer is always Karen but could someone tell me how this gives a wrong answer? It seems super simple so this is driving me a tad bit insane

I saw a lot of that in the room after systest — I don't know how I missed that.

You are doing x[i]^y[i], which is supposed to be x[i]^y[j]. Hope this helps.

Goodness, I feel dumb. Thanks rkm!

I made the exact same error. So don't feel bad. This can happen to everybody.

rkm ? what does it mean?

That's nickname of purple guy who helped him.

oh , i see , but i want to know the full name ,Lol!

Is y[j] not y[i], I made the same mistake too.

Probaly because of this typo: x[i] ^ y[

i]Same problem. Mine gave WA for a long time too until I changed it to the O(1) solution. :/

I found that many people have made such a mistake, many of my friends are the same.

What computer language is this？ python？ i guess...

true

why A div2 get TLE with map

n*n = 4,000,000, plus the log(n) lookups in a map it should not pass, however with an unordered_map/set it passes. My submission

but i dont use map.find() i write map[]>=1 which is in o(1)

`map[]`

is still and actually worse, because if the index doesn't exist, it will add one after the operation increasing the value ofnafter each query.n*n*logn should fit

Use Can Use This Condition:

`if(mp.find(a[i]^b[j])!=mp.end())c++;`

instead of this condition:

`if(mp[a[i]^b[j]])c++;`

TLE: 31073258 AC: 31088664``

yeah i got it :)

that's why i used set, which has O(logN) finding complexity!

use unordered_map

.....

For me, my solution in problem A is the shortest that I have ever done in Codeforces

Can someone please explain the following solution: http://codeforces.com/contest/869/submission/31075785

Is it some kind of range update on 2D BIT?

It uses the idea of 2D comulative sum array

There is a tiny mistake in the editorial. For problem A, it is 2·10

^{6}instead of 2·10^{5}, so the size of array should be at least 2^{21}= 2097152.Oops, my bad ._. Thanks for pointing out.

Regarding problem A: during contest several participants raised the doubt that the number of pairs in sample 2 should be 20 instead of 16. Would anyone mind explaining the reason? :)

Upd.Figured it out. When you write`i ^ j`

instead of`x[i] ^ y[j]`

you get the answer 20.I want to know why A di2 TLE with map :(

Does your code look like

`mp[x[i] ^ y[j]]`

? You should be careful, because only by calling`operator[]`

with key on map, the corresponding value is constructed. This makes a really large map (up to 4000000) in this problem, which may result in TLE. Use`mp.find(x[i] ^ y[j])`

instead.I think you are right！

yes i use the operator[] but this will result in n*n map size i know but n*n map size is okay as using mp[x[i] ^ y[j]] is in o(1) and n*n size wont cause MLE

Nope, the time complexity of

`mp[x[i] ^ y[j]]`

is , whereNis the size of the map. Also there are several issues that make your code slow. (For instance,`long long`

.)yeah thanks :)

Anyone can tell me how you guys managed to use the idea of randomization for problem E? I can see a lot of submissions using this idea.

Try to view the problem as building blocks with random heights on a platform.

Then "Two points are connected" is equivalent to "Two points are on the same height".

After that you can use Fenwick Tree to maintain the heights of all points.

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. You can see my submissions for problem 'A' Previous submission with given constraints New submission with change in only size of array 'ha'

Sorry it was my fault. Please ignore the comment

Please help me! For problem A, why did my solution give WA on test case 4? I though of the fact that if (x,y) satisfies the property, then so does (y,x). Hence for x!=y, the count will always be even. So I just calculated the count for all x=y cases. Here is my code: https://pastebin.com/XqRjHbBL

May somebody help me? For problem 'A': I've got WA in test 48, but couldn't find where I was wrong. Then i worked in CF Sandbox, added to my solution ' cout << ""; ' and got full score. Why?)

Code(that cout has commented):

why my solution of c fails http://codeforces.com/contest/869/submission/31083066.

Well because all the answers in the first question is "Karen". Completely wrong code also got A/C :p :p .31069801

Can anyone explain solution of the problem C?

I will explain mine, but I am not sure that it is the expected solution.

You can check my code here: 31079668

For me, the most important piece of information from the statement (apart from constraints, heh) is the following:

For any two islands of the same colour, either they shouldn't be reached from each other through bridges, or the shortest distance between them is at least 3Let's think about what this constraint is imposing and lets enumerate the impositions:

1) There can be no edge between two islands of the same colour. Why? Because if there was an edge between two islands of the same colour, their shortest distance would be 1, which is less than 3. Good! this means that we only need to consider edges between islands of different colours. We even have an upper bound right now: 2

^{ab}× 2^{bc}× 2^{ac}(the product of all possible subsets of edges between islands of different colours). In fact, this is the very first formula I tried.2) There can be no pair of islands of the same colour

a_{i},a_{j}such that they both have an edge to the same city of different colorb_{k}. Why? because if they had, then the shortest distance betweena_{i}anda_{j}would be 2, which is less than 3. This second fact is the cause that makes the previous formula to give an upper bound, but not the correct amount.So, with these observations we can conclude that there is no need to consider the three colours at the same time, it will be enough to compute the product of the number of possible edge configurations between all the possible pairs of colours. The problem is that the formula is not as direct as a power of two or something similar, so let's think about it.

Let's try to build a function

f(r,b) that tells us in how many ways we can connectrred islands withbblack islands without violating constraints. The base case for me isf(0,b) =f(r, 0) = 1. The non-base case isf(r,b) =f(r- 1,b) +b×f(r- 1,b- 1). Why? f(r-1, b) is the number of ways that you get if you ignore the rth red island. If we want to connect the rth red island with some black island we havebchoices, so we must add the amountb×f(r- 1,b- 1). Note that we only need to consider where we put a single edge, because more than one edge from the rth red island to the black cluster would imply a violation of constraint 2. Given thata,b,c≤ 5000 we can compute this formula with dynamic programming. Also, we are ignoring intra-cluster edges, as they would violate constraint 1. I hope that this convinces you enough about the correctness of the formula.Once we have these values computed, we must output

f(a,b) ×f(a,c) ×f(b,c).I am not very sure if this solution was the expected one given the time it takes to complete a single test case and the time limit. Even if it is in java, I am not 100% sure.

how it is 2^(ab) × 2^(bc) × 2^(ac) in the first case

Oop

For two clusters having islands a and b, the number of bridges will be a*b. For all the pairs, the total number of bridges are a*b + b*c + a*c and every bridge has two possibility i.e. either to exist or not. So the upperbound is 2^(a*b + b*c + c*a)

Correction.`f(r-1, b) is the number of ways that you get if you ignore the rth red island`

.You dont need to use the recursion to calculate

f(a,b). Letf(a,b) denotes total number of ways to connect two clusters withaandbislands respectively.f(a,b) will be .Final answer will

f(a,b) *f(b,c) *f(a,c)Oh, I see! Now I understand why I saw many 15ms submissions. Thanks!

I think you meant the starting i for the summation to be 0 not 1

Yes! Edited it.

but why is the formula like that? can u explain? thanks :)

Why this i! in multiplication?

Got it!

how to get to this formula?please help somebody.

nice explanation.

Here's an O(n) solution for calculating f(r, b)AC CODE`f(r, b) = 0; r >= b`

`for(i=0; i<=b; i++)`

`f(r,b) += (P(r,i) * C(b,i)) % MOD`

where P(r, i) = permutations of i things out of r things = r!/(r-i)! ; C(b, i) = b choose i = b!/(i! * (b-i)!)

Just precalculate factorials, inverse factorials and that would solve it O(n)Awesome

"So, with these observations we can conclude that there is no need to consider the three colours at the same time" I didn't understand it ! why we are ignoring the three colours at a time? can't there be any bridges like 1->2->3->1 ?

please correct me!

You can indeed have 1>2>3>1 bridges, however, saying:

"...there is no need to consider the three colours at the same time"

doesn't imply that you won't consider the 1>2>3>1 cases for the solution, it just means that for the solution build process, you don't need to consider the three colour islands at the same time.

thanks!

I understood the first part of your formula on the right hand side should be f(r-1,b). But why is the second part b x f(r-1,b-1)? Specifically why b-1 ? Can you please explain? Thanks.

Did anyone actually used the solution for A that prints "Karen"?

Yes, myself: 31068666

I got an RTE previously with a naive solution because I did not allocate enough space for the first 2

Nnumbers. I returned to the statement, read the words "ordered pair" and realized the trick.yup and it's totally correct soln, but what i did'nt get was the explanation of first testcase, Since it is mentioned in ques that he has to make pair of (x,y) but they are forming pairs within set of x and in that too they have considered (1,1) (3,3) but not (2,2). Can anyone explain why??

They have considered (3,3). But (3,3) doesn't satisfy the constraint because 2 xor 5 is 7 which is not included in 2N numbers.

Is there any deterministic solution for E?

Auto comment: topic has been updated by Tommyr7 (previous revision, new revision, compare).Where can I find more counting/combinatorial problems like C? I can never get the idea in-contest, and its hard to find more of them to practice

Auto comment: topic has been updated by Tommyr7 (previous revision, new revision, compare).Auto comment: topic has been updated by Tommyr7 (previous revision, new revision, compare).Auto comment: topic has been updated by Tommyr7 (previous revision, new revision, compare).Auto comment: topic has been updated by Tommyr7 (previous revision, new revision, compare).This is my first Codeforces contest and I have doubts regarding Tommyr7's solution to Problem C.

Why is the value of

`Maxn`

5007 (The constraint is`1 <= a,b,c <= 5000`

)? Souldn't it be 5000?Also, why taking

`%modp`

at every step is necessary?When dealing with factorials and combinations, is it a norm to store the values in advance for fast calculations? I am quite intrigued by the beauty of the code.

Thanks for supporting my contest. Here are my answers: 1.Well, 1<=a,b,c<=5000 is true, but you know sometimes your program gets an RE if you just use array size of 5000 (if you have some operations like a[i+1]), it's a good habit to make the size of array a little bigger. (Just a little is needed) 2.It's unnecessary. But sometimes if the answer is too large, a multiplies b may get WA (two very large integers may get an answer less than 0). For example, (1e12)*(1e12) will get WA. So, talking %modp at every step is also one of my habits. (Of course, it's not necessary) 3.Actually, it's not necessary again. But, store the values in advance can make me clear what I should do next. I don't need to be careful about the combination values if I have stored them in advance. This can make me have more possibility to get Accepted or I can code it faster.

Thank you :)

I think the problem D is actually the hardest one.

Auto comment: topic has been updated by Tommyr7 (previous revision, new revision, compare).I want to learn about quad trees and also want to solve some good problems of quad trees. Please share the links of some good tutorial on quad tree, and the links to some good problems ! Thank you !

quad tree is not good, it will be worst case O(N), where N is the length of the box, if your query is the upper line. That's why you can't solve any problems with it.

869B — The Eternal Immortality ，// the Tutorial of this question said "and it's not hard to prove a tighter upper bound of 5." hence my understanding is the maximum Output is 5？ but i want to give a example ,such as ,if the Input is 0 3 ,then Output should be 6 ,but the tutorial said "a tighter upper bound of 5" so it exceed the bound 5..... my point of view...

No, it means if you calculate (

a+ 1) × (a+ 2) × ... and stop when the last digit becomes zero, you will stop before or ata+ 5.seems to understand it! thanks! buddy!

i read solution of problems D, try to understand code.but i got no idea. can anyone explain it for me. thanks

a

it means all the numbers are different! Note：these 2*n numbers all different ... cuz the statement says there doesn't exit “ xi=yj ” “xi=xj” “yi=yj” ，， In short, all the numbers are different! hope you can got it!

2 wrong hacks Ah!! anyone please provide me a cup of water to die :(

You have typo in word "Suppuse".

Thanks for pointing out!

I probably misunderstood E, but why can't we use 2D segment tree in the next way: 1: set square [r1,r2]*[c1,c2] to unique value 2: set square [r1,r2]*[c1,c2] to same value as (r1-1, c1-1) 3: check if (r1, c1), (r2, c2) have the same value?

Thank you!

I was confused that problem E why has to be randomize? I try to replace

with

I submit this a little bit different solution and this pass 19 test,and fail in test 20. I think it may be different set of number has the same sum,but in this situation,the collission probility may be not 2-64.

In problem E for the BIT solution,why is probability of collision 2^-64 ? Suppose a certain cell belongs in region 1 and 2, and there is another cell that lies in region 3. If we were to query whether a path is possible between them, the output would be Yes even though it was supposed to be no, and there was no repetition in random number generation

Intuitively I feel that the probability of collision is still extremely low, but I am unable to prove it. Can anyone pls help me?

Let's assume you assign 64-bit random value for each rectangle. If you want to check reachability for two points you can calculate xor of covering rectangles. If this values aren't equal — points aren't reachable for sure. Otherwise there can be a collision. Let's estimate its probability. One can observe that bits of values are in fact independent. For one bit: this bit will be equal in two calculated numbers with probability (xor of any non-empty set of bits is either 0 or 1 with equal probability, because and and we generate bits with equal probability too). So we obtain probability of collision.

Thanks!

The following is a GNU C++11 solution for problem 869E - Неприглядная античность:

31224413

The solution uses a rectangle_tree class whose root node is the entire 2500 x 2500 area. The children of each node (u) in the rectangle_tree are the largest non-intersecting rectangles contained in (u). Insertions and deletions update the tree according to inserted/deleted rectangles. A walk query returns "Yes" if and only if the smallest rectangles containing the source and the target cells are the same.

A way can be there in total ways or not when there is no bridge among any islands?

yes

what is meaning of -del in this statement udel=make_pair(-del.first,-del.second)

Can anyone explain solution of the problem B?

in problem E : " the creation/deletion of a barrier is equivalent to adding/subtracting/xoring a value to all cells in a rectangular region".

can anyone please explain (or give some links) how range updates works in 2D segment trees or 2D BIT !

hello ,

i know i didn't totally listen to the editorial but i don't understand why my implementation could be wrong and why i get WA at test 9 on problem E.

My implementation is 33746043.

i thought about keeping the square in which my element 1 and element 2 are and i am keeping it using a global variable k which i use to mark the square in which my element is. i am introducing new squares by increasing k and marking with it, removing a square with a matrix whatwasthere and marking with the value of whatwasthere[line][column] of the first element of the query , and display yes when the square in which the first element is meets the square in which the second element of the query is