Rock-Paper-Scissors
Time limit2sMemory limit512 MB
Given n rounds of random rock-paper-scissors and p prime, compute the expected GCD score of the win counts times 3^(2n), modulo p.
- Level
Medium7 of 10
- Topics
- Math, Number theory, Combinatorics, Probability
- Solved
- No attempts yet
Statement
Alice and Bob are going to play the famous game "Rock-Paper-Scissors". Both of them dislike thinking much, so both use a random strategy: each picks rock, paper, or scissors with equal probability.
They play the game times and then compute a score as follows. If Alice won times, Bob won times, and the remaining games were draws, the score is the greatest common divisor of and . If or is , the greatest common divisor of and is defined as .
Calculate the expected value of . The answer is always an integer. Because it may be very large, print its remainder modulo .
Input
The input contains two integers and (, , is prime).
Output
Print a single integer: the answer modulo .