Dice Game
Time limit5sMemory limit1024 MB
Given a die with n faces, find the expected score of a game where Budada either keeps the first roll or XORs it with a second roll, both playing optimally, modulo 998244353.
- Level
Medium7 of 10
- Topics
- Probability, Bit manipulation, Math
- Solved
- No attempts yet
Problem
Putata and Budada are playing a game with a die that has faces. Every integer from to is written on exactly one face, and each face is equally likely to land up. In other words, a roll gives a uniform random integer between and .
The game has two rounds. In the first round, Putata rolls the die and gets an integer .
In the second round, Budada can choose one of the following:
- End the game. The score is .
- Roll the die again and let the result be . The game ends, and the score is , where is the bitwise exclusive-or operation.
Putata and Budada want to maximize the score, and both always make the best choice. For a given , compute the expected score of the game.
The answer can be written as an irreducible fraction with . Output the integer with and .
Input
The first line contains an integer (). Each of the next lines contains an integer (), which is one query.
Output
Output lines. Line contains the answer for the -th query.