luogu#P5165. xtq的棋盘
xtq的棋盘
Background
Since second grade, xtq has loved board games.
Problem Description
xtq has a chessboard with row and columns, numbered from to from left to right. At the initial moment, there is a piece at position .
xtq will perform random operations over time. Specifically, if in a given second the piece is not at , then with probability he moves the piece one cell to the left, and with probability he moves it one cell to the right; otherwise (when the piece is at ), he will definitely move the piece one cell to the left.
Now xtq wants to ask you: what is the expected number of seconds until the piece can reach ? Since the answer may be very large, and to avoid unnecessary precision errors, you only need to output the answer modulo (it can be proven that the answer must be a rational number).
Input Format
One line with four positive integers .
Here, mean .
Output Format
One line with one positive integer, representing the expected number of moves modulo .
3 1 1 3
13
Hint
For of the testdata, , , and it is guaranteed that the answer is an integer before taking modulo.
For of the testdata, .
For of the testdata, .
For of the testdata, , , and are coprime.
In addition, among all testdata points, of the testdata satisfies .
The modulo of a rational number with respect to a prime is defined as follows:
Let the result of modulo be . Then it must satisfy and .
It is guaranteed that for of the testdata, such an satisfying the requirements always exists.
Translated by ChatGPT 5