Russian Roulette (Hard)
Time limit5sMemory limit1024 MB
Given n players, c chambers, and k passes per empty shot, find the starting seat with the best survival chance and its probability modulo 1000000009.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Probability, Math
- Solved
- No attempts yet
Problem
You went out to celebrate with your friends. You all got drunk and decided that playing Russian roulette with a paintball gun sounds like a great idea.
The gun is a revolver with chambers (). Exactly of those chambers contain a paintball, and all others are empty.
The game has one other parameter: a positive integer constant .
At the beginning of the game, all players stand in a circle. Number the players 0 through in clockwise order. Player 0 takes the gun. The game repeats the following three steps until the gun becomes empty:
- The person holding the gun shoots themselves. They spin the cylinder to choose one of the chambers uniformly at random, point the gun at themselves, and pull the trigger.
- If the chosen chamber was empty, exactly rounds of passing follow. In each round, the holder passes the gun clockwise to the next player still in the game.
- If the chamber contained a paintball, the player is hit and loses the game. The gun passes clockwise to the next player still in the game, and the hit player leaves the game.
The winner is the only player who is not shot.
For given , , and , compute where you should stand at the beginning to maximize your probability of winning. Also compute the exact value of that probability.
Input
The first line contains an integer , the number of test cases. Each test case is preceded by a blank line.
Each case is a single line with the integers , , and . It is guaranteed that and .
Output
For each test case, print one line with two space-separated numbers. The first is the position at the beginning of the game that maximizes your probability of winning. The second is a number that encodes the probability of winning from that position.
If several positions are optimal, print the smallest one.
Let the maximum winning probability be the irreducible fraction . Print , where is the modular inverse of modulo . The maximum probability is guaranteed to be expressible as such a fraction, and the modular inverse of is guaranteed to exist.
Hint
The exact answer for the first test case is . Since , the second number on the first output line is .