#P6620. [省选联考 2020 A 卷] 组合数问题

[省选联考 2020 A 卷] 组合数问题

Description

As everyone knows, student Xiaocong is good at calculations, especially at computing binomial coefficients. Now Xiaocong wants you to compute

$$\left(\sum_{k=0}^{n}f(k)\times x^k\times \binom{n}{k}\right)\bmod p$$

where nn, xx, and pp are given integers, and f(k)f(k) is a given degree mm polynomial f(k)=a0+a1k+a2k2++amkmf(k) = a_0 + a_1k + a_2k^2 + \cdots + a_mk^m. (nk)\binom{n}{k} is the binomial coefficient, defined as (nk)=n!k!(nk)!\binom{n}{k} = \frac{n!}{k!(n-k)!}.

Input Format

The first line contains four non-negative integers nn, xx, pp, mm.

The second line contains m+1m + 1 integers, representing a0a_0, a1a_1, \cdots, ama_m.

Output Format

Output one integer in a single line, which is the answer.

5 1 10007 2
0 0 1
240
996 233 998244353 5
5 4 13 16 20 15
869469289

Hint

Sample 1 Explanation

$f(0) = 0,f(1) = 1,f(2) = 4,f(3) = 9,f(4) = 16,f(5) = 25$.

x=1x = 1, so xkx^k is always 11, and this factor in each term of the product can be ignored.

$\binom 5 0 = 1, \binom 5 1 = 5, \binom 5 2 = 10, \binom 5 3 = 10, \binom 5 4 = 5, \binom 5 5 = 1$.

Sample 3

See the attached files problem3.in and problem3.ans.

Constraints and Hints

For all testdata: $1\le n, x, p \le 10^9, 0\le a_i\le 10^9, 0\le m \le \min(n,1000)$.

The specific limits for each test point are shown in the table below:

Test Point ID nn\le mm\le Other Special Limits
131\sim 3 10001000
464\sim 6 10510^5 00 pp is prime.
787\sim 8 10910^9
9129\sim 12 55
131613\sim 16 10001000 x=1x=1
172017\sim 20

Translated by ChatGPT 5