Dice Journey
You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.
Dice Journey
Problem Statement
There are squares, numbered from through . You start on square . Each of the squares from through has a die on it. The die on square is labeled with the integers from through , each occurring with equal probability. (Die rolls are independent of each other.) Until you reach square , you will repeat rolling a die on the square you are on. If the die on square rolls the integer , you go to square .
Find the expected value, modulo , of the number of times you roll a die.
It can be proved that the sought expected value is always a rational number. Additionally, if that value is represented using two coprime integers and , there is a unique integer such that and . Find this .
INPUT FORMAT (input arrives from the terminal / stdin):
- Line 1: A single integer, .
- Line 2: integers, for , separated by spaces.
OUTPUT FORMAT (print output to the terminal / stdout):
- Line 1: A single integer, the expected value modulo of the number of times you roll a die, represented as described in the problem statement.
SAMPLE INPUT 1:
3
1 1
SAMPLE OUTPUT 1:
4
Let's consider one possible sequence of events until we reach Square :
- You roll a on Square , which allows you to move to Square .
- On Square , you roll a , meaning you stay on Square .
- You roll a on Square next, allowing you to move to Square .
This sequence of events occurs with a probability of . It takes die rolls to reach Square in this case. However, considering all possible case, the average or expected number of times you roll a die is .
SAMPLE INPUT 2:
5
3 1 2 1
SAMPLE OUTPUT 2:
332748122
SCORING:
- Inputs 3:
- Inputs 4-30: No additional constraints.
HFI Coding Club 2023 November Contest
- Status
- Done
- Rule
- IOI
- Problem
- 4
- Start at
- 2023-11-16 18:45
- End at
- 2023-11-16 20:45
- Duration
- 2 hour(s)
- Host
- Partic.
- 21