Is there a reason for bounds on F to be 10^6 instead of something a bit smaller like 2*10^5? The problem ended up being pretty hard to solve in Java even with the intended solution. I understand not wanting more naive solutions to pass though.

Java is about 5x slower than C++, so many times, perfectly fine solutions in C++ fail in java. I've had java solutions fail many times because I was using long instead of int, or an arraylist/customdatastructure instead of an array, or recursion instead of iteration. I've also gotten accepted verdicts by rewriting slow java code in c++.

Though you can lose time reading input with scanner, there are many other ways to lose time, ways that C++ or even python uses don't need to care about. I don't think any accepted solutions in java for this problem used recursion, but many did in C++.

As mentioned by redux on Announcement page, problem D can be solved by using recurrence relation such that if (a,b) is a co-prime pair then (2a - b, a), (2a + b, a) and (a + 2b, b) are also co-prime where a > b.

Base cases are (2, 1) for even-odd pairs and (3, 1) for odd-odd pairs. The list is also exhaustive as mentioned in this link so there is no overlapping also. The only breaking condition will be a > n or when m co-prime pairs are stored.

In this way, we will always jump to only co-prime pairs rather than checking every other pair.

So time complexity will also be much less than one given in editorial (Correct me if I am wrong).

Lets say you check that m>=n-1 and phi(2)+...+phi(n)>=m. The probability of gcd being 1 is O(1) so we can randomly generate edges and check if gcd is 1. If we use map complexity will be: O(n+mlogm). But one can also prove that without randomizatiom complexity is O(n+m).

a[i] = 1, 3, 8, 20, 48 ... a[0] = 1, because a n-segment can be paved with a n-segment in one way. a[i] is equal the number of ways to pave the free parts of the n-segment for all possible positions of the segment of length >= n-i. (statement 1)

Clarification: a[i-1] — because we count for all segments of length >=n-i.

2*2^(i-1) is equal the number of ways to pave the free parts in case 1 in pic.

Σ 2^(k-1)*2^(i-k-1) (from k=1 to i-1) is equal the number of ways to pave the free parts in case of movement (n-i)segment, where 2^(k-1) — left part, 2^(i-k-1) — right part. (case 2 in pic)

Proof of "The number of ways to pave the n-segment by k segments is ((n-1)!/(k-1)!)/(n-k)!" you can find here http://kvant.mccme.ru/pdf/1999/05/kv0599levin.pdf after 11-task (it is on russian lang, but i cant find this in english). The number of ways to pave the n-segment by different segment is sum and equals 2^(n-1).

In the add method we are comparing pairs (val_at_i, i) and (val_at_cur_max, cur_max), if both the values are same then we are updating cur_max if i > cur_max. If the values are same we want to change the value of cur_max only if i < cur_max according to given conditions. Did I miss something?

code In this solution of problem F,he is just forming depth array of all the nodes in the form of array of maps ,and using the factor of logn ,pulling up the maps and merging smaller into bigger at all stages ,but why is he not getting MLE ....as if tree is linear,then space would be O(n^2) UPD:GOT IT.....EVERY TIME WE MERGE PARENT AND ITSCHILD ,WE ARE SWAPPING THEIR MAPS(IF SMALLER) AND THEN MERGE,SO SIZE OF PARENT INCREASES AND ITS CHILD DECREASES....

I have a question about the memory complexity of F: since every node's memory is its max "height", when the tree becomes a chain,it will rise to n(n+1)/2, And I can't run the data on my computer,so I wonder if it will get MLE

If the tree is a chain, I believe you will actually only have 1 (mutable) array. All the nodes will point to that same array. Each time you go up the tree you just copy that array and add 1 to it. This works since you never need to use arrays lower down in the tree again

I am a little confused by the D solution given in the tutorial.In my opinion the O(N^2*log⁡(N)) is not going to fit in time because, as i know the time formula = TimeComplexity(MAXN)/10^9 which is O(N^2*log(N))/10^9 and converting N to MAXN(10^5) is (10^5)^2*log(10^5)/10^9 seconds, 10^10*16/10^9 = 160 seconds. Correct me if i am wrong(i am 100% wrong but i would like an explanation why). As i know from my little experience a O(N^2) solution for a TimeLimit of 2 second a MAXN of 10^5 is way to big.

For problem 1009 D, how is it sufficient to prove that graph has m valid edges in order to make sure that graph is connected? (Like what I can't get is when is the program making sure that graph that it prints is "connected"?)

In the line, forn(i, n) for (int j = i + 1; j < n; ++j), we are making sure that the graph we built is connected because in the very first loop when i = 0, we are connecting the vertex 1 with all other vertices for j = 1 to n-1, thus making it a connected graph.

In this hacked solution, I used long double for the answer, and added up the intermediate means. The editorial says 10 byte float will pass. So, how many bytes does long double uses? 8? If so, how can we use a 10 byte or larger float in C++? I know I could've done this by first calculating sum using long long int, but just want to know out of curosity, it may help in some future contests. Thanks :)

There's a subtraction in line 26, and if it could be negative, you have to consider it becomes negative. If your procedure is correct, you may fix it by adding mod in line 26.In C++11 or above, modulo (by using '%') for negative integer, it will be negative integer from (1-mod) to 0.

(But sadly, I checked your solution with simple fix got TLE. And finally, I cannot remove TLE... My solution is calculating 2^n every time (and this method is slower than yours) but got AC, so I think the cause of TLE is twice multiplication in loop... Sorry for failing to get AC. Someone, please help!)

ai is always positive. Since diff[i] is sum of ai divided by positive numbers(2^n), it is also always positive. So I don't understand what could be negative and why we should add mod. Also where are taking mod for a negative integer, the minus sign is outside the multiplication?

You can multiply negative numbers, and also subtract, divide(if mod is prime).When the number is negative, add mod and you can get right value.And more, considering the value is big or already positive, you can write like this.

String a is lexicographically less than string b (if strings a and b have the same length) if there exists some position i ( 1 ≤ i ≤ | a | , where | s | is the length of the string s ) such that for every j < i holds a [j] = b [j] , and a [i] < b [i] .

What is string a ans string b, and indices i and j. What I understood was that string a is the output, and string b is the input string..!!!`` I cant get the relation between editorial explanation with the above statement given in the question. Help me pls regards ;)

I would like to share another approach to solve D, though it runs slower and harder to code. (Sorry for the poor English beforehand.)

Let rp[i] be a set storing all j <= n, such that gcd(i, j) = 1.When constructing rp[i], we can count total number of pair (i, j) such that j > i and j is in rp[i], and terminate when this number >= m.It is clear that the total number of found numbers won't exceed 2m + n when we stop.

The basic case, rp[1], is all integers in [1, n].To construct rp[i], first we pick maximum prime factor p (can be found by sieve) of i, "remove" it from i. Formally, let i = p1k1 * p2k2 * ... * pzkz * pk, pi are primes, then we calculate the value temp = p1k1 * p2k2 * ... * pzkz, it can be found in O(logpn) by dividing i by p while i % p == 0.

Since gcd(i, j) = 1 if and only if (i, j) share no common prime factor, we haverp[i] = rp[temp] - set of all numbers in rp[temp] which has prime factor p.

We can simply iterate through rp[temp] from small to large, when we found a number x, check whether it can be divided by p, and add it to rp[i] if it cannot.

By doing so, we may fail (found a number x but doesn't add it to rp[i]) many times, but for at most O(logpn) failures there must be one success (add x to rp[i]) before all those failures, because when we fail for some x = p1k1 * p2k2 * ... * pzkz * pk, we must have add y = p1k1 * p2k2 * ... * pzkz to rp[i] before. Since x <= n, k can only be numbers picked from [1, floor(logpn)], thus each success corresponds to at most O(logpn) failures.

Because p must be greater than 2, and success always comes before failures, this algorithm runs in O((n + m) * log2n).

Question 1009C: Annoying PresentI am getting a relative error = '0.0000206' in test case 9. I am using a double type variable to store the mean values for each new transformation. I have no clue how to reduce the error for the result. My code is here : https://codeforces.com/contest/1009/submission/40511515.Any help is appreciated.

Every single floating point operation causes precision error. Try to replace them by integer operations as many as possible.

For example, you can sum up all x by integer operations, and add it to mean at last. In this way we use just a single floating point operation rather than O(m) floating point operations, therefore the precision error can be reduced.

For problem F (editorial solution), aren't there a lot of memory leaks? Ex: in the pull method, when we return (a new allocated vector in the heap) and save it in s[x], where s[x] already has allocated a new vector in the beginning of the dfs method. Why not use unique_pointers to prevent memory leaks? Other than that code looks rad. :)