#P6669. [清华集训 2016] 组合数问题

    ID: 7399 远端评测题 1000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2016数位 DPCTT(清华集训/北大集训)

[清华集训 2016] 组合数问题

Problem Description

The binomial coefficient CnmC_n^m represents the number of ways to choose mm items from nn items. For example, choosing two items from the three items (1,2,3)(1,2,3) can be done in three ways: (1,2)(1,2), (1,3)(1,3), and (2,3)(2,3). According to the definition of binomial coefficients, we have the general formula to compute CnmC_n^m:

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

Here, n!=1×2××nn!=1×2×⋯×n. (In addition, when n=0n=0, n!=1n!=1.)

Xiaocong wants to know: given nn, mm, and kk, among all 0in0 \le i \le n and 0jmin(i,m)0 \le j \le \min(i,m), how many pairs (i,j)(i,j) satisfy that CijC_i^j is a multiple of kk.

Output the answer modulo 109+710^9+7.

Input Format

The first line contains two integers t,kt,k, where tt is the total number of groups of testdata in this test point.

In the next tt lines, each line contains two integers n,mn,m.

Output Format

Output tt lines. Each line contains one integer: among all 0in0 \le i \le n and 0jmin(i,m)0 \le j \le \min(i,m), how many pairs (i,j)(i,j) satisfy that CijC_i^j is a multiple of kk.

1 2
3 3
1
2 5
4 5
6 7
0
7
3 23
23333333 23333333
233333333 233333333
2333333333 2333333333
851883128
959557926
680723120

Hint

Sample 11 Explanation

Among all cases, only C21=2C_{2}^{1}=2 is a multiple of 22.

Constraints and Notes

For 20%20\% of the test points, 1n,m1001 \le n,m \le 100.

For another 15%15\% of the test points, nmn \le m.

For another 15%15\% of the test points, k=2k=2.

For another 15%15\% of the test points, m10m \le 10.

For 100%100\% of the test points, 1n,m10181 \le n,m \le 10^{18}, 1t,k1001 \le t,k \le 100, and kk is a prime number.

Translated by ChatGPT 5