QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 512 MB Total points: 100

#6157. Combination Number Problem

Statistics

As is well known, Xiao Cong is good at calculation, especially at calculating binomial coefficients. Xiao Cong now wants you to calculate the value of:

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

where $n, x, p$ are given integers, and $f(k)$ is a given polynomial of degree $m$, $f(k) = a_0 + a_1 k + a_2 k^2 + \cdots + a_m k^m$.

$\binom n k$ is the binomial coefficient, defined as $\binom n k = \frac{n!}{k!(n-k)!}$.

Input

The first line contains four non-negative integers $n, x, p, m$.

The second line contains $m + 1$ integers, representing $a_0, a_1, \dots, a_m$ respectively.

Output

Output a single integer representing the answer.

Examples

Input 1

5 1 10007 2
0 0 1

Output 1

240

Note 1

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

Since $x = 1$, $x^k$ is always $1$, so this term in 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$.

The final answer is:

$$ \sum_{k=0}^5 f(k) \times \binom 5 k = 0\times 1 + 1\times 5 + 4\times 10 + 9\times 10 + 16\times 5 + 25\times 1 = 240 $$

Input 2

996 233 998244353 5
5 4 13 16 20 15

Output 2

869469289

Input 3

See the provided files.

Subtasks

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

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

Test Point ID $n\le $ $m\le $ Other Special Constraints
$1\sim 3$ $1000$ $1000$ None
$4\sim 6$ $10^5$ $0$ $p$ is prime
$7\sim 8$ $10^9$ None
$9\sim 12$ $5$
$13\sim 16$ $1000$ $x=1$
$17\sim 20$ None

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.