#P4456. [CQOI2018] 交错序列

    ID: 3388 远端评测题 1000ms 500MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2018重庆各省省选枚举,暴力二项式定理构造

[CQOI2018] 交错序列

Description

We call a sequence consisting only of 0,10,1 an "alternating sequence" if and only if there are no adjacent 11's (adjacent 00's are allowed). For example, 000, 001, 101 are alternating sequences, while 110 is not.

For an alternating sequence of length nn, let the counts of 00 and 11 be xx and yy, respectively. Given parameters a,ba,b, define the characteristic value of a sequence as xaybx^a y^b. Note that any integer to the 00-th power equals 11 (including 00=10^0 = 1).

Obviously there may be multiple alternating sequences of length nn. We want to know the remainder modulo mm (where mm is a given prime) of the sum of characteristic values over all alternating sequences of length nn.

For example, all alternating sequences of length 33 are: 000, 001, 010, 100, 101. When a=1,b=2a = 1, b = 2, 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 n,a,b,mn,a,b,m.

Output Format

Output one line containing the result modm\bmod m.

3 1 2 1009
10
4 3 2 1009
204

Hint

For 30%30\% of the testdata, 1n151 \le n \le 15.

For 100%100\% of the testdata, 1n1071 \le n \le 10^7, 1a,b451 \le a,b \le 45, 107m<10810^7 \le m < 10^8.

Translated by ChatGPT 5