QOJ.ac

QOJ

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

#6157. 组合数问题

统计

众所周知,小葱同学擅长计算,尤其擅长计算组合数。小葱现在希望你计算

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

的值。其中 $n, x, p$ 为给定的整数,$f(k)$ 为给定的一个 $m$ 次多项式 $f(k) = a_0 + a_1 k + a_2 k^2 + \cdots + a_m k^m$。

$\binom n k$ 为组合数,其值为 $\binom n k = \frac{n!}{k!(n-k)!}$。

输入格式

第一行四个非负整数 $n, x, p, m$。

第二行 $m + 1$ 个整数,分别代表 $a_0, a_1, \dots, a_m$。

输出格式

仅一行一个整数表示答案。

样例数据

样例 1 输入

5 1 10007 2
0 0 1

样例 1 输出

240

样例 1 解释

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

$x = 1$,故 $x^k$ 恒为 $1$,乘积中的该项可以忽略。

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

最终答案为:

$$ \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 $$

样例 2 输入

996 233 998244353 5
5 4 13 16 20 15

样例 2 输出

869469289

样例 3

见下发文件。

子任务

对于所有测试数据:$1\le n, x, p \le 10^9, 0\le a_i\le 10^9, 0\le m \le \min(n,1000)$。

每个测试点的具体限制见下表:

测试点编号 $n\le $ $m\le $ 其他特殊限制
$1\sim 3$ $1000$ $1000$
$4\sim 6$ $10^5$ $0$ $p$ 是质数
$7\sim 8$ $10^9$
$9\sim 12$ $5$
$13\sim 16$ $1000$ $x=1$
$17\sim 20$

Discussions

About Discussions

The discussion section is only for posting: Editorials, 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. Submitting multiple issues may cause your account to be banned.
  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.