#P4456. [CQOI2018] 交错序列
[CQOI2018] 交错序列
Description
We call a sequence consisting only of an "alternating sequence" if and only if there are no adjacent 's (adjacent 's are allowed). For example, 000, 001, 101 are alternating sequences, while 110 is not.
For an alternating sequence of length , let the counts of and be and , respectively. Given parameters , define the characteristic value of a sequence as . Note that any integer to the -th power equals (including ).
Obviously there may be multiple alternating sequences of length . We want to know the remainder modulo (where is a given prime) of the sum of characteristic values over all alternating sequences of length .
For example, all alternating sequences of length are: 000, 001, 010, 100, 101. When , we can compute: $3^1 \times 0^2 + 2^1 \times 1^2 + 2^1 \times 1^2 + 2^1 \times 1^2 + 1^1 \times 2^2 = 10$.
Input Format
The input contains one line with four space-separated integers .
Output Format
Output one line containing the result .
3 1 2 1009
10
4 3 2 1009
204
Hint
For of the testdata, .
For of the testdata, , , .
Translated by ChatGPT 5
京公网安备 11011102002149号