Partition into Teams
Time limit1sMemory limit256 MB
For n people each choosing red, blue, or spectator uniformly, compute 3^n times the probability that red wins, modulo a prime p.
- Level
Hard8 of 10
- Topics
- Combinatorics, Math, Number theory, Dynamic programming
- Solved
- No attempts yet
Problem
A company of people decided to play a game. Each person can either join the red team, join the blue team, or become a spectator. Each person makes a decision independently and picks one of the three options with equal probability. The team with more players wins; the game ends in a draw if both teams have the same number of players. Let be the probability that the red team wins. Find , where is prime.
Input
The only line of the input contains two integers and (, , is prime).
Output
Print one integer, the answer to the problem.