luogu#P5075. [JSOI2012] 分零食

    ID: 4105 远端评测题 1000ms 250MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>动态规划 DP2012各省省选江苏O2优化快速傅里叶变换 FFT

[JSOI2012] 分零食

Problem Description

This is the joyful Jingxiang River, and this is a joyful kindergarten.

Today is February 14, Tuesday. On this special day, the teacher leads the children to dance and laugh happily. The principal bought a large amount of snacks from a snack shop next to the kindergarten and decided to give them to the children. Hearing this news, all the children quietly lined up. Everyone knows that the principal does not like naughty kids.

The children stand in a single line in order. There are AA children, and there are three shared joy coefficients OO, SS, and UU. If a child receives xx candies, then her happiness is f(x)=Ox2+Sx+Uf(x)=Ox^2+Sx+U.

Now the principal starts handing out candies. There are MM candies in total. Some children may receive no candies. For those children who receive no candies, the happiness is 11. If a child receives no candies, then all children behind her also receive no candies (that is, the children who receive no candies must form a consecutive suffix at the end of the line).

All candy-distribution plans are equally likely. The question is: in expectation, what is the product of the happiness values of all children? Student Daidai quickly came up with an idea: as long as he knows the total number of plans TT and the sum SS of the happiness-product over all plans, he can get the answer ans=STans=\frac{S}{T}. He has already computed TT, but he does not know how to compute SS. Can you tell him?

Because the answer is very large, you only need to output SmodPS \bmod P.

Postscript:

Although everyone knows that even if you know TT and SmodPS \bmod P, there is still no way to know the expected product of the happiness values of all children. However, when Daidai realized this, he was already completely hopeless.

Input Format

The first line contains two integers, MM and PP.

The second line contains an integer AA.

The third line contains an integer OO.

The fourth line contains an integer SS.

The fifth line contains an integer UU.

Output Format

Output one integer SS. Since the answer may be very large, you only need to output SmodPS \bmod P.

4 100
4
1
0
0

63

Hint

Sample Explanation:

The function is f(x)=x2f(x)=x^2. There are 44 snacks and 44 students. If only the first student receives candies, the happiness is 1616. If the first two students receive candies, then the possible happiness values (for all distributions) are 9,9,169, 9, 16 in order. If three students receive candies, the happiness values are 4,4,44, 4, 4. In the last case, every student receives snacks, and the happiness is 11. Adding them up gives S=63S=63.

Constraints:

For 40%40\% of the testdata, M150M \leq 150.
For 60%60\% of the testdata, M2000M \leq 2000.
For 80%80\% of the testdata, M6000M \leq 6000.
For 100%100\% of the testdata, $M \leq 10000, P \leq 255, A \leq 10^8, O \leq 4, S \leq 300, U \leq 100$.

Translated by ChatGPT 5