Single Round Match 760 Editorials

Share

We fill all the missing places greedily. Whenever a character in team is missing its always beneficial to fill it with W because giving some points to Warriors is always better than giving them to Raptors. Now, that we have no missing characters in team, whenever score is missing, if Raptors scored it, we want to give them the minimum possible (i.e 1) and for Warriors maximum possible (i.e 4). This is the best possible way of filling a Warriors fan could have done. So after filling team and score accordingly we can check if Warriors have more points in this optimistic way of filling. If they have more then answer is 1, otherwise any other filling cannot give Warriors the lead. Hence answer is 0.

Firstly we need to observe that we do not need the diagonal moves. We can simulate any diagonal move using two of the moves from (left, right, top, down). Now, we can see that for a frog at (x1,y1) which can jump such that its either coordinates change by k1, all the points it can reach will be of the form (x1 + a*k1, y1 + b*k1) , where a,b are integers.

Similarly frog at (x2,y2) which can jump such that its either coordinates change by k2, all the points it can reach will be of the form (x2 + c*k2, y2 + d*k2) , where c,d are integers.

So now for them to meet, x1+a*k1 = x2+c*k2, and y1+b*k1 = y2+d*k2,

Now x1-x2 = -a*k1 + c*k2 (This is true only when abs(x1-x2) is divisible by gcd(k1,k2))

Similarly abs(y1-y2) must also be divisible by gcd(k1,k2).

So now we can check these two conditions and return appropriate answer.

Firstly let us select the n/2 teams which host the home game in first round. This can be done in C(n,n/2) ways.

Next, we need to assign away teams for the home games in the first round. For this we can assume home teams are lined in some order (for example lexicographic). And we can take n! possible orderings of away teams and allot ith home team with ith away team and so on. So now, each of the n! orderings give different schedule for first round.

Hence , first round can be organised in n! * C(n,n/2) ways.

Now given a first round, home teams for second round are already decided which will be away teams of first round. We can assume there is some ordering among home teams again. Now , we want to take orderings of away teams such no team plays the same team they played in previous round. This is same as counting number of derangements of size n/2 because each one does not go to exactly one place(let us represent by D(n/2) ).

Derangements can be calculated using recursion D(n) = (n-1)*(D(n-1)+D(n-2)).

Firstly let us handle case C = N, then the only rays are either diagonals or vertical or horizontal. We will prove soon that for other rays, number of lattice points is less than N.

So from here on lets assume C!=N.

We can first see that we can calculate the number of rays in first quadrant and see that due to symmetry it will be same in the rest of the quadrants.And since C!=N,each ray uniquely belongs to a single quadrant. So we can find number of rays in first quadrant and multiply answer with 4.

Now, we have reduced to counting rays in first quadrant. We can further use symmetry along line x=y and reduce question to finding number of rays in the first 45 degrees. And multiply it by 2.

Now any ray equation in the first 45 degrees looks like y = a*x/b where (0<a<b and a and b are co prime). Different pairs of (a,b) give different rays. (we are not considering diagonal and horizontal directions because for them C=N).

Now, we can notice from 0<a<b constraint that b>=2. (remember this we will use later).

Next number of lattice points on the ray y = a*x/b. x can take values b,2*b and so till floor(n/b). Since a<b, y will always be less than x and hence if x is in the range then y is also in the range. Hence number of lattice points on the ray will be floor(n/b).

Now , since b>=2, maximum number of lattice points on such a ray will be floor(N/2) (< N) (Proof that C=N cases are completely described above).

Since number of lattice points on a ray does not depend on a, we can iterate on b. And see what for all values of b we have floor(N/b) = C.

Now we want to count for a particular b , number of values possible for a such that a>=1 and a<b and a is co prime to b. This is exactly phi(b) (euler totient function).

So now answer will be sum of phi(b) over all b that satisfy floor(N/b) = C.

O(nlogn) to calculate euler totient function for the first n natural numbers.

ComponentsForever

Solution:

First thing is (x+y)modulo 2 value is constant across hops. So a frog at (x1,y1) cannot reach a frog (x2,y2) in any number of hops if (x1+y1) modulo 2 != (x2+y2) modulo 2.

So let us separate the frogs based on (x+y)modulo 2 and add the answers of the both cases. Because there will not be any common promise among both groups because they cannot talk to each other ever.

Case 1:

Now, we will work with group where all the frogs have (x+y)modulo 2 =0. From now on, in this case whenever we refer to a frog,we assume its coordinates are such that sum of them is even.

For, two frogs at (x1,y1) and (x2,y2)

if (x2-x1) modulo 2 =1, then frog 1 can reach second frog only after odd number of hops. Similarly, for (x2-x1)modulo 2 =0, we can reach only in even number of hops.

Minimum number of hops needed for frog 1 to reach frog 2 is max(abs(x2-x1),abs(y2-y1)).

Proof: this minimum cannot be less than this because each hop x and y coordinate will change atmost by 1. Now, we will show a construction. Lets assume abs(x2-x1) > = abs(y2-y1). Now, first abs(y2-y1) moves we move y1 towards y2 and x1 towards x2. For rest of the moves we oscillate y1 near y2 (like move from y2 to y2+1 and back from y2+1 to y2) and move x1 to x2.

Also now we can see that if minimum number of hops is d, there always exist a path of length d+2k for any whole number k because after minimum hops we can move to adjacent vertex and back. And we have proved above that parity of path lengths is always same. Hence, these are the only path lengths possible.

So now, Let us take a walkie talkie with range (L<R) . Now this can connect frog 1 and 2 only if

R>=max(abs(x2-x1),abs(y2-y1)) because [L,R] will consist of atleast two consecutive integers and in every two consecutive integers above minimum hops one of them can be path lengths. So now, connectivity does not depend on L.

Since L is not important for us. We will just focus on R and for each R , for every valid L answer will be same.

Now, for each R, we will construct a connectivity graph and compute number of components in it. Since in each component we can make different promise,we need expected number of components in the graph.

We can start from R = a, and keep increasing R gradually and add edges as they become eligible (an edge becomes eligible only when min hops between the two frogs <= R). If you can see this procedure is very close to kruskal algorithm to finding MST. Also another parallel is only when an MST edge is added, count of components changes. So, the only important R values where number of components change will be the edge weights of MST edges.

So now, we will have n-1 important R values and between each pair of adjacent R values components do not change. So we can count number of [L,R] pairs with that number of components and use it for finding expectation.

Finally we are left with finding MST in this graph. Now, there will be O(n*n) edges. Hence, it is not feasible to construct the graph and use kruskal algorithm.

For two points (x1,y1) and (x2,y2), max(abs(x2-x1),abs(y2-y1)) is called Chebyshev distance .

Let us consider transformation of point (x,y) to ((x+y)/2,(x-y)/2)

Now, if we take ((x1+y1)/2,(x1-y1)/2) and ((x2+y2)/2,(x2-y2)/2), manhattan distance between them will turn out to be max(abs(x2-x1),abs(y2-y1)). Hence, graph constructed with edge length as manhattan distance between the transformed points will be same as previous graph. Now since the graph is same, MST will also be same. So we need Manhattan MST between these transformed points which is a standard problem (Link here).

Hence, we can now construct the MST fastly and solve the problem.

A small trick to avoid doubles, the case where points have (x+y)modulo 2 as odd, we can move all the points 1 unit right. This will not change anything in the problem since all distances will remain same.

counting number of pairs of [L,R] such that R is between R1 and R2 . Assume a<=R1<=R2<=b.

Number of pairs will be (R1-a)+(R1+1-a)+…(R2-a) = R2*(R2+1)/2 – (R1-1)*R1/2 – a*(R2-R1+1).

counting number of pairs of [L,R] such that R is between two consecutive change points (that is for all these R number of components is constant) is O(1). Since its just some formulas. And there are O(n) such intervals . So O(n) complexity