#P3746. [六省联考 2017] 组合数问题

    ID: 1315 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>数学递推2017各省省选矩阵乘法

[六省联考 2017] 组合数问题

Description

The binomial coefficient CnmC_n^m denotes the number of ways to choose mm items from nn distinct items. For example, choosing two items from (1,2,3)(1, 2, 3) has three possibilities: (1,2)(1, 2), (1,3)(1, 3), (2,3)(2, 3). By definition, a general formula to compute CnmC_n^m is:

Cnm=n!m! (nm)!C_n^m = \frac {n!} {m! \ (n - m)!}

where n!=1×2××nn! = 1 \times 2 \times \cdots \times n. (In particular, when n=0n = 0, n!=1n! = 1; when m>nm > n, Cnm=0C_n^m = 0.)

Xiaocong learned about the divisibility relationship between CijC_i^j and kk during NOIP, and now he wants to go further and study more properties of binomial coefficients. He noticed that whether CijC_i^j is a multiple of kk depends on whether CijmodkC_i^j \bmod k equals 00. This intriguing fact sparked his interest in the mod\mathrm{mod} operation (the remainder operation). Now Xiaocong chose four integers n,p,k,rn, p, k, r, and he wants to know

$$\left( \sum_{i = 0}^\infty C_{nk}^{ik + r} \right) \bmod p,$$

that is,

$$\left( C_{nk}^{r} + C_{nk}^{k + r} + C_{nk}^{2k + r} + \cdots + C_{nk}^{(n - 1)k + r} + C_{nk}^{nk + r} + \cdots \right) \bmod p.$$

Input Format

The first line contains four integers n,p,k,rn, p, k, r. All symbols have the meanings given in the problem statement.

Output Format

Output a single integer, the answer.

2 10007 2 0
8
20 10007 20 0
176

Hint

For 30%30\% of the test points, 1n,k301 \leq n, k \leq 30, and pp is prime.
For another 5%5\% of the test points, p=2p = 2.
For another 5%5\% of the test points, k=1k = 1.
For another 10%10\% of the test points, k=2k = 2.
For another 15%15\% of the test points, 1n1031 \leq n \leq 10^3, 1k501 \leq k \leq 50, and pp is prime.
For another 15%15\% of the test points, 1n×k1061 \leq n \times k \leq 10^6, and pp is prime.
For another 10%10\% of the test points, 1n1091 \leq n \leq 10^9, 1k501 \leq k \leq 50, and pp is prime.
For 100%100\% of the test points, 1n1091 \leq n \leq 10^9, 0r<k500 \leq r < k \leq 50, 2p23012 \leq p \leq 2^{30} - 1.

Translated by ChatGPT 5