TopCoder problem "SchoolTrip" used in SRM 384 (Division I Level Two)

Problem Statement

    There are n students standing in a circle and playing a game with a ball. The students are numbered from 0 to n-1 in clockwise order. On each turn, a student must take the ball and throw it at another student in the circle. Student i has a probability[i] percent chance of hitting the target student, and if he is successful, the student who is hit must leave the circle. Turns go in clockwise order, and student 0 gets the first turn. They are not allowed to skip turns. The game ends when there is only one student left in the circle.

The students are playing this game against their will, so their common goal is to finish the game in the least number of turns. Return the expected number of turns the game will last.


Method signature:double duration(int[] probability)
(be sure your method is public)


-The returned value must be accurate to within a relative or absolute value of 1E-9.
-Each student does not worry about how long he will stay in the circle, only the total game time matters.


-probability will contain between 2 and 6 elements, inclusive.
-Each element of probability will be between 10 and 100, inclusive.


Returns: 1.0
The first student will certainly hit the second one, so the game will be finished after 1 turn.
Returns: 2.0
With probability 1/2 the game will be finished after 1 turn, with probability 1/4 after 2 turns, .. with probability 1/2^k after k turns. The infinite sum 1*1/2 + 2*1/4 + 3*1/8 +.. n*1/2^n = 2.
Returns: 3.892383478590375
Returns: 4.0
The first two students will start out by hitting the last two students. After that, there will be three students remaining, and all of them always hit their targets, so only two more turns will be required for the game to end.

Problem url:

Problem stats url:




PabloGilberto , Olexiy , lovro , ivan_metelsky

Problem categories:

Dynamic Programming, Math, Recursion