#P8688. [蓝桥杯 2019 省 A] 组合数问题

    ID: 7690 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2019数位 dpLucas 定理蓝桥杯省赛

[蓝桥杯 2019 省 A] 组合数问题

Description

Given n,m,kn, m, k, find how many pairs (i,j)(i, j) satisfy 1in1 \le i \le n, 0jmin(i,m)0 \le j \le \min(i, m), and (ij)0(modk){i\choose j} \equiv 0\pmod{k}, where kk is a prime number. Here (ij){i\choose j} is a binomial coefficient, which means the number of ways to choose jj elements from ii distinct elements to form a set.

Input Format

The first line contains two numbers t,kt, k, where tt means this test point contains tt queries, and the meaning of kk is the same as above.

The next tt lines each contain two integers n,mn, m, representing one query.

Output Format

Output tt lines, each line containing one integer representing the corresponding answer. Since the answer may be very large, output the remainder of the answer modulo 109+710^9+7.

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 Explanation]

Among all possible cases, only (21)=2{2 \choose 1} = 2 is a multiple of 22.

[Constraints]

For all test cases, 1k1081 \le k \le 10^8, 1t1051 \le t \le 10^5, 1n,m10181 \le n, m \le 10^{18}, and kk is a prime number.

During judging, 1010 test cases will be used to test your program. The limits for each test case are as follows:

Lanqiao Cup 2019 Provincial Contest A Group, Problem J.

Translated by ChatGPT 5