Google Kick Start Archive — Round F 2017 problems

Overview

Thanks to everyone who participated! Kickstart Round G will take place next month; check the Kickstart schedule for more details.


Cast

Problem A (Kicksort): Written and prepared by Ian Tullis.

Problem B (Dance Battle): Written by Ian Tullis and prepared by Jonathan Irvin Gunawan.

Problem C (Catch Them All): Written and prepared by Celestine Lau.

Problem D (Eat Cake): Written and prepared by Xianghong Luo.

Solutions and other problem preparation and review by Ian Tullis, Yiming Li, Tony Wong, Xuanang Zhao, Jonathan Irvin Gunawan, Trung Thanh Nguyen and Xiaomeng Yang. Thanks for their great help!

Analysis authors:

  • Kicksort: Ian Tullis
  • Dance Battle: Ian Tullis
  • Catch Them All: Xiaomeng Yang, Tony Wong
  • Eat Cake: Jonathan Irvin Gunawan

Problem

Wheatley is at the best party in the world: it has infinitely many cakes! Each cake is a square with an integer side length (in cm). The party has infinitely many cakes of every possible integer side length. The cakes all have the same depth, so we will only consider their areas.

Wheatley is determined to eat one or more cakes that have a total combined area of exactly N cm2. But, since he is health-conscious, he wants to eat as few cakes as possible. Can you help him calculate the minimum number of cakes he can eat?

Input

The input starts with one line containing one integer T, which is the number of test cases. T test cases follow. Each case consists of one line with one integer N, which is the exact total cake area that Wheatley wants to eat.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the minimum number of cakes that Wheatley can eat while eating the exact total area N.

Limits

Time limit: 30 seconds per test set.
Memory limit: 1GB.

Small dataset (Test set 1 - Visible)

1 ≤ T ≤ 50.
1 ≤ N ≤ 50.

Large dataset (Test set 2 - Hidden)

1 ≤ T ≤ 100.
1 ≤ N ≤ 10000.

Sample

Sample Input
content_copy Copied!
3
3
4
5
Sample Output
content_copy Copied!
Case #1: 3
Case #2: 1
Case #3: 2

In Sample Case #1, the only possible strategy is for Wheatley to eat three cakes of side length 1.

In Sample Case #2, Wheatley can eat one cake of side length 2, which requires fewer cakes than eating four cakes of side length 1.

In Sample Case #3, the best strategy is for Wheatley to eat one cake of side length 2 and one cake of side length 1.

B. Kicksort

Problem

Here at Kickstart, we are fans of the well-known Quicksort algorithm, which chooses a pivot value from a list, moves each other value into one of two new lists depending on how it compares with the pivot value, and then recursively sorts each of those new lists. However, the algorithm might choose a pivot that causes all of the other values to end up in only one of the two new lists, which defeats the purpose of the divide-and-conquer strategy. We call such a pivot a worst-case pivot.

To try to avoid this problem, we have created our own variant, Kicksort. Someone told us that it is good to use a value in the middle as a pivot, so our algorithm works as follows:

  Kicksort(A): // A is a 0-indexed list with E elements
    If E ≤ 1, return A.
    Otherwise:
      Create empty new lists B and C.
      Choose A[floor((E-1)/2)] as the pivot P.
      For i = 0 to E-1, except for i = floor((E-1)/2):
        If A[i] ≤ P, append it to B.
        Otherwise, append it to C.
    Return the list Kicksort(B) + [P] + Kicksort(C).
    // [P] is a new list with just P; + means concatenate

For practice, we are trying Kicksort out on lists that are permutations of the numbers 1 through N. Unfortunately, it looks like Kicksort still has the same problem as Quicksort: it is possible for every pivot to be a worst-case pivot!

For example, consider the list 1 4 3 2. Kicksort will choose 4 as a pivot, and all of the other values 1 3 2 will end up in one of the two new lists. Then, when Kicksort is called on that list 1 3 2, it will choose 3, and once again, all of the other values will end up in one of the two new lists. Finally, it will choose 1 from the list 1 2, and the other value 2 will of course end up in only one of the two new lists. In every case, the algorithm will choose a worst-case pivot. (Notice that when Kicksort is called on a list with 0 or 1 elements, it does not choose a pivot at all.)

Please help us investigate this further! Given a permutation of the numbers 1 through N, determine whether Kicksort will choose only worst-case pivots.

Input

The first line of the input gives the number of test cases, T. T test cases follow; each consists of two lines. The first line has one integer N: the number of elements in the permutation. The second line contains N integers Ai, which are a permutation of the values from 1 through N.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is YES if Kicksort will choose only worst-case pivots when sorting this list, or NO otherwise.

Limits

Memory limit: 1GB.
The values Ai are a permutation of the values from 1 to N.

Small dataset (Test set 1 - Visible)

1 ≤ T ≤ 32.
Time limit: 20 seconds.
2 ≤ N ≤ 4.

Large dataset (Test set 2 - Hidden)

1 ≤ T ≤ 100.
Time limit: 200 seconds.
2 ≤ N ≤ 10000.

Sample

Sample Input
content_copy Copied!
4
4
1 4 3 2
4
2 1 3 4
2
2 1
3
1 2 3
Sample Output
content_copy Copied!
Case #1: YES
Case #2: NO
Case #3: YES
Case #4: NO

Sample Case #1 is the one described in the problem statement.

In Sample Case #2, our first pivot will be 1, which is a worst-case pivot, because it causes all of the other values 2 3 4 to end up in one of the two new lists. However, the Kicksort call on the list 2 3 4 will choose 3 as a pivot. This is not a worst-case pivot, because it puts 2 in one of the new lists, and 4 in the other.

In Sample Case #3, Kicksort will start by choosing the worst-case pivot 2, and then it has no other pivot choices to make.

In Sample Case #4, Kicksort will start by choosing 2, which is not a worst-case pivot.

C. Dance Battle

Problem

Your team is about to prove itself in a dance battle! Initially, your team has E points of energy, and zero points of honor. There are N rival teams who you must face; the i-th of these teams is the i-th in a lineup, and has a dancing skill of Si.

In each round of battle, you will face the next rival team in the lineup, and you can take one of the following actions:

  1. Dance: Your team loses energy equal to the dancing skill of the rival team, and that team does not return to the lineup. You gain one point of honor. You cannot take this action if it would make your energy drop to 0 or less.
  2. Delay: You make excuses ("our shoes aren't tied!") and the rival team returns to the back of the lineup. Your energy and honor do not change.
  3. Truce: You declare a truce with the rival team, and that team does not return to the lineup. Your energy and honor do not change.
  4. Recruit: You recruit the rival team onto your team, and that team does not return to the lineup. Your team gains energy equal to the dancing skill of the rival team, but you lose one point of honor. You cannot take this action if it would make your honor drop below 0.

The battle is over when there are no more rival teams in the lineup. If you make optimal decisions, what is the maximum amount of honor you can have when the battle is over?

Input

The first line of the input gives the number of test cases, T. T test cases follow; each consists of two lines. The first line consists of two integers E and N: your team's energy, and the number of rival teams. The second line consists of N integers Si; the i-th of these represents the dancing skill of the rival team that is i-th in line at the start of the battle.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum amount of honor you can have when the battle is over.

Limits

1 ≤ T ≤ 100.
Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ E ≤ 106.
1 ≤ Si ≤ 106, for all i.

Small dataset (Test set 1 - Visible)

1 ≤ N ≤ 5.

Large dataset (Test set 2 - Hidden)

1 ≤ N ≤ 1000.

Sample

Sample Input
content_copy Copied!
2
100 1
100
10 3
20 3 15
Sample Output
content_copy Copied!
Case #1: 0
Case #2: 1

In Sample Case #1, there is only one rival team. You cannot dance against them because it would make your energy fall to 0, and you cannot recruit them because it would make your honor fall below 0. Delaying does not help, so the only option is to declare a truce. You finish with 0 honor.

In Sample Case #2, one optimal strategy is:

  1. Delay against the first rival team. They go to the back of the lineup.
  2. Dance against the second rival team. Your energy drops to 7, and your honor increases to 1.
  3. Recruit the third rival team. Your energy increases to 22, and your honor decreases to 0.
  4. Dance against the first rival team (which is now at the head of the lineup again). Your energy drops to 2, and your honor increases to 1.

You finish with 1 point of honor.

D. Catch Them All

Problem

After the release of Codejamon Go, you, like many of your friends, took to the streets of your city to catch as many of the furry little creatures as you could. The objective of the game is to catch Codejamon that appear around your city by going to their locations. You are wondering how long it would take for you to catch them all!

Your city consists of N locations numbered from 1 to N. You start at location 1. There are M bidirectional roads (numbered from 1 to M). The i-th road connects a pair of distinct locations (Ui, Vi), and it takes Di minutes to travel on it in either direction. It is guaranteed that it is possible to reach any other location from location 1 by travelling on one or more roads.

At time 0, a Codejamon will appear at a uniformly random location other than your current location (which is location 1 at time 0). Uniformly random means that the probability that it will appear at each of the N - 1 locations other than your current location is exactly 1 / (N - 1). The instant that a Codejamon appears, you can immediately start moving towards it. When you arrive at a location containing a Codejamon, you instantly catch it, and then a new Codejamon will instantly appear at a uniformly random location other than your current location, and so on. Notice that only one Codejamon is present at any given time, and you must catch the existing one before the next will appear.

Given the layout of your city, calculate the expected time to catch P Codejamon, assuming that you always take the fastest possible route between any two locations.

Input

The input starts with one line containing one integer T: the number of test cases. T test cases follow.

Each test case begins with one line containing 3 integers N, M and P, indicating the number of locations, roads, and Codejamon to catch, respectively.

Then, each test case continues with M lines; the i-th of these lines contains three integers Ui, Vi and Di, indicating that the i-th road is between locations Ui and Vi, and it takes Di minutes to travel on it in either direction.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the expected time in minutes to catch P Codejamon. Your answer will be considered correct if it is within an absolute or relative error of 10-4 of the correct answer. See the FAQ for an explanation of what that means, and what formats of real numbers we accept.

Limits

1 ≤ T ≤ 100.
Time limit: 40 seconds per test set.
Memory limit: 1GB.
N - 1 ≤ M ≤ (N * (N - 1)) / 2.
1 ≤ Di ≤ 10, for all i.
1 ≤ Ui < ViN, for all i.
For all i and j with i ≠ j, UiUj and/or ViVj. (There is at most one road between any two locations.)
It is guaranteed that it is possible to reach any other location from location 1 by travelling on one or more roads.

Small dataset (Test set 1 - Visible)

2 ≤ N ≤ 50.
1 ≤ P ≤ 200.

Large dataset (Test set 2 - Hidden)

2 ≤ N ≤ 100.
1 ≤ P ≤ 109.

Sample

Sample Input
content_copy Copied!
4
5 4 1
1 2 1
2 3 2
1 4 2
4 5 1
2 1 200
1 2 5
5 4 2
1 2 1
2 3 2
1 4 2
4 5 1
3 3 1
1 2 3
1 3 1
2 3 1
Sample Output
content_copy Copied!
Case #1: 2.250000
Case #2: 1000.000000
Case #3: 5.437500
Case #4: 1.500000

In Sample Case #1, there is only one Codejamon for us to catch. With equal probability, it will appear at locations 2, 3, 4, and 5, which are at distances of 1, 3, 2, and 3, respectively, from our starting location 1. So the expected time it will take is (1 + 3 + 2 + 3) / 4 = 2.25 minutes.

In Sample Case #2, there are only two locations connected by one road. Every time a Codejamon appears, it will be in the location other than our current one, and we will have to take the road to get there. So we take the road 200 times, taking 5 minutes each time, for a total of 1000 minutes.

Sample Case #3 uses the same map as Sample Case #1. There are 16 ordered-pair possibilities for where the two Codejamon will appear, and doing the math yields an expected 87/16 = 5.4375 minutes.

In Sample Case #4, the one Codejamon we need to catch will appear at location 2 or location 3. If it appears at location 2, it is better for us to get there in two minutes via the 1-to-3 and 3-to-2 roads, instead of taking the more time-consuming 1-to-2 road. So the expected time taken is (2 + 1) / 2 = 1.5 minutes.

Analysis — A. Cake

Eat Cake: Analysis

Small dataset

Let f(N) be the minimum number of cakes that have to be eaten such that the total combined area of the eaten cakes is N. To compute f(N), we can start by checking all possible sizes that we could use for the first cake that we eat. If the first eaten cake has an area of A × A, then we need to eat remaining cake(s) with a total combined area of N - A × A, which requires at least f(N - A × A) cakes to be eaten. Therefore, f(N) can be recursively computed as follows:

f(N)
  if (N = 0)
    return 0
  ans = infinity
  for i in [1, sqrt(N)]
    ans = min(ans, f(N - i * i) + 1)
  return ans

The algorithm above is fast enough to solve the Small dataset.

Large dataset

We need to use dynamic programming (DP) / memoization to solve the large dataset. If the value of f(N) has been recursively computed before, then the next time we need to know that value, we can simply use the previously computed value. Since the DP table of values has size O(N), and computing each value of f(N) takes O(N0.5) time, the total time for this algorithm is O(N1.5).

f(N)
  if (N = 0)
    return 0
  if (dp[N])
    return dp[N]
  dp[N] = infinity
  for i in [1, sqrt(N)]
    dp[N] = min(dp[N], f(N - i * i) + 1)
  return dp[N]

Alternative solution

There is another solution that does not even require a recursive strategy. From Lagrange's four-square theorem, we know that f(N) cannot be larger than 4. Therefore, for each k (1 ≤ k ≤ 3), we can simply have k nested loops of possible cake areas to determine whether k cakes are enough. If 3 cakes are still not enough, then the answer must be 4 (and we do not actually need to determine which cakes are used).

f(N)
  for i in [1, sqrt(N)]
    if (i * i = N)
      return 1
  for i in [1, sqrt(N)]
    for j in [i, sqrt(N)]
      if (i * i + j * j = N)
        return 2
  for i in [1, sqrt(N)]
    for j in [i, sqrt(N)]
      for k in [j, sqrt(N)]
        if (i * i + j * j + k * k = N)
          return 3
  return 4

Since each layer of the triply nested loop adds O(N0.5) time, the total time for this algorithm is also O(N1.5).

Test Data
info We recommend that you practice debugging solutions without looking at the test data.

Analysis — B. Kicksort

Kicksort: Analysis

In Quicksort, the best pivots at any stage are the ones that are closest to the median of the current list (although we would have no way of knowing which ones those are without doing some additional work before choosing!) This problem hinges on a misinterpretation of that idea, in which we take pivots from near the middle index of the list instead. Unless our sequence happens to be already sorted or close to sorted, this is no better than choosing an arbitrary index!

A Small-only approach

The sequences in the Small dataset can have between 2 and 4 elements. There are only 2! + 3! + 4! = 32 different permutations that meet these criteria. This enables some strategies that do not usually work in Kickstart. For example, we can pre-solve each case by hand, or use nested conditional statements that take advantage of the small size of the sequence. But we recommend using one of the two approaches below instead.

A quadratic simulation approach

The most straightforward general approach is to actually run the Kicksort algorithm on the given sequence, and see whether it ever picks a pivot that is not worst-case. One possible complication is figuring out how to have this recursive algorithm pass information up the call chain if it does find a non-worst-case pivot; one option is to have it throw an exception that can then be caught. We must also be careful not to overflow the stack with recursive calls, since there can be N of them; one way to avoid this is to use our language's provided way to change the system's stack size. (Warning: This sort of system tweaking can be dangerous in real-world applications, which tend to avoid deep recursive strategies like this one.)

Quicksort is famously O(N2) when it consistently picks worst-case pivots, and that is what many of our simulations will do, so this strategy takes O(N2) time. This is still fast enough to solve the Large dataset, but can we do better?

A linear approach

First of all, let us consider different sequences of the same length that have "YES" answers — that is, the ones for which Kicksort will always pick worst-case pivots. Even though the pivots themselves may have different values, Kicksort will always use the same indexes, in the same order, as pivots. For a sequence of length 6, for example, it will pick index 2 as a pivot, and then divide the other values into one empty list (which is unimportant since no pivot is chosen from it) and one list of length 5 containing the remaining values. (It turns out not to matter for our purposes whether that list is the "low" or "high" one.) Then it will pick index 2 from that list of length 5, which corresponds to index 3 in the original list. If we continue to trace such a case, we find that the indexes chosen from the original list will be 2, 3, 1, 4, 0, 5. That is, we are starting at index floor(N - 1) / 2, then jumping 1 to the right, then 2 to the left, then 3 to the right, and so on. This does not depend at all on the values in the list!

A similar pattern holds for lists of odd length, although in that case, the first jump is 1 to the left. Knowing this, we can visit the indexes of our pivots in order, without doing any simulation. It is not too hard to implement the pattern of changing direction and adding a distance of 1 with each new jump.

Each time we visit a new index, we check whether it holds either the lowest or highest value that has not already been used. If this continues all the way through the last index, we have a "YES" case. However, if we ever encounter a value that does not satisfy those conditions, then we have a "NO" case and we can stop.

At this point, we can take advantage of the fact that our sequences are permutations of numbers from 1 to N. (Of course, in real life, we could "sort" such sequences in constant time, without ever reading them, if we knew the length in advance!) We know at the outset that our lowest unused value is 1 and our highest unused value is N. When our current value matches this lower bound, we increment it by 1, because we already know that the next lowest value is exactly 1 more than the previous lowest; similarly, when our pivot matches the upper bound, we increment our upper bound by 1. We only need to keep track of these two values as we go.

Since we potentially have to bounce around the entire sequence, this strategy takes O(N) time. It is unusual for a problem about sorting to have a solution that takes less than O(N log N) time, but in this case, it is a consequence of having restricted our sequences to permutations.

Test Data
info We recommend that you practice debugging solutions without looking at the test data.

Analysis — C. Dance Battle

Dance Battle: Analysis

Even before solving the Small dataset, we need to reduce the number of options available to us, because the Delay action could allow a dance battle to go on forever! A critical initial insight is that we can use Delay to sort the opponents and face them in whatever order we want: we can delay against everyone else until we face our first desired opponent, then take some other non-delay action on that opponent, then keep delaying until we face our second desired opponent, and so on.

Small dataset

Once we know that we can use Delay to sort, the Small dataset's problem space allows us to use brute force. We will consider the worst case, N = 5. We can choose one of 5! possible orders in which to face the opponents; for each opponent, we choose one of the three other actions (Dance, Truce, or Recruit). That is a total of 5! × 35 = only 29160 possible scenarios. We can simulate each of them to make sure that our energy does not drop below 1, and our honor does not drop below 0. Then, we take the maximum honor value among all valid scenarios. Each simulation takes linear time, so the overall time complexity of this strategy is O(N! × 3N × N).

Large dataset

The brute force method above will not scale to 1000 opponents. Let us devise an alternate strategy. For starters, we can observe that we cannot use the Recruit action until we have defeated at least one opponent (and gained a point of honor) by using Dance. So, if our starting energy level does not let us defeat the opponent with the lowest dancing skill, then our best option is to use Truce on everyone and finish with 0 honor.

Suppose that we can defeat at least one opponent by using Dance. Then we have no reason not to choose the opponent with the lowest dancing skill, since all opponents give the same amount of honor when defeated via Dance. Moreover, we may as well continue dancing against the weakest remaining opponent as long as we have the energy to do so.

What happens when we do not have enough energy to Dance against any remaining opponent? We can either use Truce to send everyone else away, or Recruit some opponent. If we are going to recruit someone, we should pick the opponent with the highest energy, since the cost of recruiting any opponent is the same. But how do we decide whether to use Recruit or Truce?

As long as there are at least two opponents remaining, it cannot hurt us to recruit the one with the most energy, because after that, we can surely defeat at least the one with the least energy. Even if we can defeat only one, and that one has the same skill as the opponent we recruited, we have lost one honor and gained one honor, and we have had no net change in energy, so we are no worse off than if we had used Truce to remove the same two opponents.

So, we can sort the opponent list from lowest to highest energy, and start with one pointer at each end. Setting aside the case in which we cannot defeat anyone: first, we move our left pointer forward, defeating opponents by dancing until we no longer can. Then, as long as there are at least two step, recruiting the strongest opponent and gaining energy. We repeat this until the pointers meet, or we have one opponent left that we cannot defeat (in which case we should use Truce instead of wasting a point of honor by using Recruit). Whatever amount of honor we have at that point is our answer.

This algorithm has an O(N log N) sorting step followed by an O(N) execution step. Other O(N) execution steps are possible; for example, we can notice that the algorithm above will terminate with the pointers either zero or one opponent apart, so we can use partial sums and binary search to directly find that point.

Test Data
info We recommend that you practice debugging solutions without looking at the test data.

Analysis — D. Catch Them All

Catch Them All: Analysis

Small dataset

We can start by computing the shortest distance between each pair of locations using the Floyd-Warshall algorithm. We will use dis[i, j] to represent the shortest distance between locations i and j.

Let dp[K, L] be the expected time needed to catch K Codejamons when starting from location L. Then we can use a dynamic programming algorithm with the following state transition equation:

  if (K == 0):
    dp[K, L] = 0;
  else:
    dp[K, L] = Σi!=L(dp[K-1, i] + dis[L, i]) / (N-1).

The algorithm above takes O(N2P) time, which is fast enough to solve the Small dataset.

Large dataset

We can find that for each dp[K, L], the answer is a linear expression of dp[K-1, i] when K != 0. So, we can rewrite the state transition equation as the product of a matrix and a column vector, as shown below.

Let S[i] = Σj!=i(dis[i, j]).

  +----------+   +---------------------------------------------+   +------------+
  | dp[K, 1] |   |    0   , 1/(N-1), ... , 1/(N-1), S[1]/(N-1) |   | dp[K-1, 1] |
  | dp[K, 2] |   | 1/(N-1),    0   , ... , 1/(N-1), S[2]/(N-1) |   | dp[K-1, 2] |
  |   ...    | = |   ...  ,   ...  , ... ,   ...  ,     ...    | * |    ...     |
  | dp[K, N] |   | 1/(N-1), 1/(N-1), ... ,    0   , S[N]/(N-1) |   | dp[K-1, N] |
  |    1     |   |    0   ,    0   , ... ,    0   ,     1      |   |     1      |
  +----------+   +---------------------------------------------+   +------------+

Let FK denote the column vector of dp[K, i], and let A denote the transition matrix. Then we have FK = A * FK-1 = AK * F0.

With the approach above, we can use exponentiation by squaring to accelerate the computation of AK. This gives us an O(N3logP) algorithm which can solve the Large dataset.

Other solutions

Let Prt be the probability of being at location 1 after catching t Codejamons. Initially, Pr0 = 1. Since at any time, the probabilities of being at locations 2, 3, ..., N (let's call them the "other locations") are the same, we can calculate the probability of the next Codejamon appearing at location 1 by multiplying the probability of being at any of the other locations by the probability of location 1 being chosen. Therefore, we have Prt = (1 - Prt-1) / (N-1).

After computing the values of Pri for i = 1, 2,..., P-1, the answer to the problem is:

Σ(Pri * (expected distance from location 1) + Σ(1 - Pri) / (N-1) * (expected distance from location j) for j = 2, 3, ..., N) for i = 0, 1, ..., P-1.

Note: the expected distance from location i equals Σdis[i, j] / (N-1) for j = 1, 2, ..., N.

We might not have enough time to compute all P values of the sequence Pri, but one may notice that this sequence converges quickly (except for N=2, which we can handle separately). Intuitively, as the game progresses, the probabilities of you being at each of the N locations become more equal. For example, if N=4, the first few values for Prt are 1, 0, 0.333, 0.222, 0.259, 0.247, 0.251, 0.250, ... Once this value gets very close to 1 / N, after some threshold like i = 100 (depending on the numerical error allowed), we can simply approximate Pri = 1/N for all i larger than the threshold. Then i = 100, 101, ..., P-1 in the previous summation can be replaced by a multiplication: (P-100) * Σ(expected distance from location j) / N for j = 1, 2, ..., N.

The time complexity for the above algorithm is O(N3 + C), where C depends on the numerical error allowed.

Using the sequence of Prt values, it is also possible to calculate the exact answer. Let's make another sequence Ai = Pri - 1/N. This sequence converges to 0 and is a geometric progression because:

  • Pri = (1 - Pri-1) / (N-1)
  • Ai + 1/N = (1 - Ai-1 - 1/N) / (N-1)
  • Ai + 1/N = -Ai-1 / (N-1) + 1/N
  • Ai = -Ai-1 / (N-1).

ΣAi for i = 0, 1, ..., P-1 can be calculated using the formula for the sum of a geometric series. With ΣAi, we can obtain ΣPri = 1/N * P + ΣAi, and ultimately the answer. The time complexity for the above algorithm is O(N3).

Test Data
info We recommend that you practice debugging solutions without looking at the test data.

Statistics — A. Cake

Test set 1: 1253 correct solutions (72.8% solve rate)

First
kazel 39:33
Helpful crimson dolphin 41:42
dhananjoy 42:27
polingy 42:29
biltharesatyendra 43:08

Test set 2: 1122 correct solutions (65.2% solve rate)

First
kazel 39:33
Helpful crimson dolphin 41:42
dhananjoy 42:27
polingy 42:29
biltharesatyendra 43:08

Statistics — B. Kicksort

Test set 1: 1445 correct solutions (83.9% solve rate)

First
tanmay17061 31:46
ezamyatin 35:00
vhnvn 35:05
windkit 35:30
PMirdha 35:38

Test set 2: 975 correct solutions (56.6% solve rate)

First
tanmay17061 31:46
dredwerkz 36:03
IguanaBen 36:12
vinamilk 36:21
andy1li 36:26

Statistics — C. Dance Battle

Test set 1: 966 correct solutions (56.1% solve rate)

First
voxel 35:55
sokian 36:23
naveen_iitr 36:33
zzxzxzzxz 38:07
uhateme 38:18

Test set 2: 831 correct solutions (48.3% solve rate)

First
voxel 35:55
sokian 36:23
naveen_iitr 36:33
zzxzxzzxz 38:07
uhateme 38:18

Statistics — D. Catch Them All

Test set 1: 432 correct solutions (25.1% solve rate)

First
1717374 42:01
HowDoISetMyNickname 43:29
ec24 58:52
percywtc 58:59
pwypeanut 63:17

Test set 2: 218 correct solutions (12.7% solve rate)

First
1717374 42:01
ec24 58:52
percywtc 58:59
jtnydv25 69:57
voxel 75:13