#P16774. [GKS 2020 #G] Merge Cards

    ID: 19118 远端评测题 5000ms 1024MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2020Special Judge前缀和期望Google Kick Start

[GKS 2020 #G] Merge Cards

Problem Description

Panko is playing a game with NN cards laid out in a row. The ii-th card has the integer AiA_i written on it.

The game is played in N1N - 1 rounds. During each round Panko will pick an adjacent pair of cards and merge them. Suppose that the cards have the integers XX and YY written on them. To merge two cards, Panko creates a new card with X+YX + Y written on it. He then removes the two original cards from the row and places the new card in their old position. Finally Panko receives X+YX + Y points for the merge. During each round Panko will pick a pair of adjacent cards uniformly at random amongst the set of all available adjacent pairs.

After all N1N - 1 rounds, Panko's total score is the sum of points he received for each merge. What is the expected value of Panko's total score at the end of the game?

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case begins with a line containing the integer NN. A second line follows containing NN integers, describing the initial row of cards. The ii-th integer is AiA_i.

Output Format

For each test case, output one line containing Case #xx: yy, where xx is the test case number (starting from 11) and yy is the expected total score at the end of the game.

yy will be considered correct if it is within an absolute or relative error of 10610^{-6} of the correct answer. See the FAQ for an explanation of what that means, and what formats of real numbers we accept.

2
3
2 1 10
5
19 3 78 2 31
Case #1: 20.000000
Case #2: 352.33333333

Hint

In sample case #1, N=3N = 3. The initial row of cards is [2,1,10][2, 1, 10]. In the first round, Panko has 22 choices, of which he will choose one at random.

  • If Panko merges the first pair (2,1)(2, 1), then the row of cards becomes [3,10][3, 10], adding 2+1=32 + 1 = 3 points to his total score. In the second round, there is only one pair remaining (3,10)(3, 10). If he merges them, the row of cards becomes [13][13], adding 3+10=133 + 10 = 13 points to his total score. Panko ends the game with 3+13=163 + 13 = 16 points.
  • If Panko merges the second pair (1,10)(1, 10), then the row of cards becomes [2,11][2, 11], adding 1+10=111 + 10 = 11 points to his total score. In the second round, there is only one pair remaining (2,11)(2, 11). If he merges them, the row of cards becomes [13][13], adding 2+11=132 + 11 = 13 points to his total score. Panko ends the game with 11+13=2411 + 13 = 24 points.

Thus, the expected number of points Panko ends the game with is (16+24)/2=20(16 + 24)/2 = 20.

In sample case #2, N=5N = 5. The initial row of cards is [19,3,78,2,31][19, 3, 78, 2, 31]. There are too many possibilities to list here, so we will only go through one possible game:

  • In the first round, if Panko merges the pair (78,2)(78, 2), then the row of cards becomes [19,3,80,31][19, 3, 80, 31], adding 78+2=8078 + 2 = 80 to his score.
  • In the second round, if Panko merges the pair (80,31)(80, 31), then the row of cards becomes [19,3,111][19, 3, 111], adding 80+31=11180 + 31 = 111 to his score.
  • In the third round, if Panko merges the pair (19,3)(19, 3), then the row of cards becomes [22,111][22, 111], adding 19+3=2219 + 3 = 22 to his score.
  • In the fourth round, if Panko merges the pair (22,111)(22, 111), then the row of cards becomes [133][133], adding 22+111=13322 + 111 = 133 to his score.

At the end of the game explained above, Panko's total score is 80+111+22+133=34680 + 111 + 22 + 133 = 346.

Limits

1T1001 \le T \le 100.

1Ai1091 \le A_i \le 10^9 for all ii.

Test Set 11

2N92 \le N \le 9.

Test Set 22

2N1002 \le N \le 100.

Test Set 33

2N50002 \le N \le 5000.