Get Time

   Problem Statement  

 Problem Statement for VendingMachines

Problem Statement

    This problem has a non-standard time limit: 6 seconds. This problem also has a non-standard precision limit: 1e-4.

Yesterday, Cat Noku has spent a long day at the arcade. You are given the int tickets: the number of tickets he won by playing the games. This morning he is the first customer to enter the arcade. He now wants to exchange his tickets for prizes.

There are multiple vending machines at the arcade. All vending machines have just been reset into their initial state. The vending machines are probabilistic. Each vending machine has two parameters: its luck factor L and its value V. A vending machine with luck factor L and value V operates as follows:

  1. A person inserts exactly one ticket into the machine.
  2. The machine computes K: the number of tickets inserted into the machine since it last gave out a prize. (This includes the ticket inserted in the previous step. If the machine didn't give out any prizes yet, K is simply the total number of tickets inserted into the machine today.)
  3. With probability min(K^2,L^2)/L^2, the machine gives out a prize with value V. Otherwise, the machine does nothing.

You are given the description of the vending machines: int[]s luck and value. For each valid i, there is a vending machine with luck factor luck[i] and prize value value[i]. Each machine can be used arbitrarily many times and contains a sufficient supply of prizes (each of the same value).

Cat Noku is very stubborn. Whenever he chooses a vending machine, he keeps on inserting tickets into the machine until he either wins a prize or runs out of tickets. Additionally, Noku likes variety: whenever he gets a prize from a vending machine, he goes to a different machine. In other words, Noku will never claim two consecutive prizes from the same machine.

Given the above constraint, Cat Noku wants to maximize the sum of values of prizes he gets. More precisely, he is interested in the strategy that maximizes the expected value of that sum. Find that strategy and return that expected value.



Parameters:int, int[], int[]
Method signature:double expectedValue(int tickets, int[] luck, int[] value)
(be sure your method is public)


-Your return value must have absolute or relative error less than 1e-4.


-tickets will be between 1 and 40,000, inclusive.
-n will be between 2 and 15, inclusive.
-luck,value will have exactly n elements.
-Each element of luck will be between 1 and 1,000,000,000, inclusive.
-Each element of value will be between 1 and 1,000,000,000, inclusive.


Returns: 5.0
Cat Noku has 2 tickets. There are two vending machines. Each of them has luck factor 1, which means that the machine always gives out a prize. Since Noku doesn't want to claim two consecutive prizes from the same machine, he will use each machine exactly once. Thus, he is guaranteed to get a prize worth 2 and a prize worth 3, which means that the answer is 2+3 = 5.
Returns: 11.0
Cat Noku can use a particular machine multiple times, he just has to switch machines each time he gets a prize. Here, the optimal strategy is to use machine 2 (0-based index), then machine 1, and then machine 2 again. This leaves Noku with prizes worth 4+3+4 = 11.
{1, 1, 1}
Returns: 9999.99999999991
Returns: 7.693072702331962
In this case, Noku has 4 tickets and there are two machines. Machine 0 has luck factor 2 and gives out prizes with value 4. Machine 1 has luck factor 3 and gives out prizes with value 5. One example of how Cat Noku can spend his tickets:
  • Cat Noku chooses to put his first ticket into machine 0.
  • This machine computes that K=1. This means that Noku will get the prize with probability (1^2)/(2^2) = 1/4 = 0.25. Suppose he is unlucky and doesn't get the prize.
  • Due to his stubbornness, Noku must continue by putting his second ticket into machine 0.
  • The machine now computes that K=2, which means that Noku is guaranteed to get the prize with value 4.
  • Now, Noku must switch machines. He therefore moves to machine 1.
  • Noku puts the next ticket (his third one) into machine 1.
  • This machine computes that K=1. This means that Noku will only get its prize with probability 1/9. Suppose this time he's lucky and gets the prize (with value 5).
  • Noku switches machines again, returning back to machine 0.
  • Noku puts his last ticket into machine 0.
  • This is the first ticket that was put into this machine since it last gave out a prize. Hence, we are now back to having K=1, and therefore the probability of getting the next prize is 1/4. Suppose he does not win this prize.
In this scenario, Noku's total value of prizes is 4 + 5 = 9.
Returns: 292.20826703848974
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{1, 4, 9, 29, 35, 40, 49, 55, 63, 72}
Returns: 12321.244694733025
{1000000000, 1}
{1000000000, 1}
Returns: 21333.928554321847

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.

This problem was used for:
       2016 TCO Algorithm Round 2A - Division I, Level Three