Submit | All submissions | Best solutions | Back to list |
DRUNK - Drunk Game |
Alice and Bob were getting bored and decided to play a game. The game involves a pile of coins. Both players take turns in picking a certain number of coins from the pile. Initially, the pile contains N coins. Alice makes the first move. The player who is unable to pick up any coins in his/her turn loses the game. Some of the details of the game are:
- In each turn a player can only decide on picking up 1, 4, 9, 16 coins from the pile.
- As both the players are drunk and not in complete control of their actions, even if they decide on picking up some coins, they might end up picking something else completely. So, if a person decides on picking up X coins from the pile, he/she might end up with (X - 1), X or (X + 1) coins with the relative probability of C1: C2: C3
- In each turn a player has to eventually pick up at least 1 coin.
Note that a player cannot decide on picking up X coins from the pile if the pile contains less than X coins at the time. Also, if a player decided on picking up X coins and it happens that picking up (X + 1) is not valid (this might happen if there are only X coins in the pile), then the relative probability distribution for the valid moves will be C1: C2. Similarly, if picking up (X - 1) coins is not valid, then the relative probability distribution for the valid moves will be C2: C3.
As both of them are not in their right minds, they sometimes want to win and sometimes want to lose. But Bob still wonders what is his probability of winning/losing the game (depending on what he wants for a particular game). Help him find out his probability!
Input
The first line of the input contains a single integer, T, which represents the number of test cases. Each test case contains 3 lines. The first line of each test contains a single integer, N, which represents the number of coins in the pile initially. The second line contains 3 integers C1, C2 and C3. The last line of each test contains 2 integers describing the mood of Alice and Bob respectively. 0 is for losing and 1 is for winning.
Output
For each test cases, the probability of winning/losing (depending on the test case) rounded to 4 places of precision.
Constraints
1 ≤ T ≤ 10
1 ≤ N ≤ 1000000
1 ≤ C1, C2, C3 ≤ 100000
Sample
Input: 2 1 1 2 3 0 0 5 1 2 3 0 1 Output: 1.0000 0.4333
Problem Setter: Sukhjashan Singh
Added by: | darkshadows |
Date: | 2014-01-28 |
Time limit: | 1s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 |