#P2822. [NOIP 2016 提高组] 组合数问题

[NOIP 2016 提高组] 组合数问题

Description

The binomial coefficient (nm)\binom{n}{m} represents the number of ways to choose mm items from nn items. For example, when choosing two items from three items (1,2,3)(1, 2, 3), there are three choices: (1,2)(1, 2), (1,3)(1, 3), and (2,3)(2, 3). By definition, the general formula for computing the binomial coefficient (nm)\binom{n}{m} is:

(nm)=n!m!(nm)!\binom{n}{m}=\frac{n!}{m!(n-m)!}

where n!=1×2××nn!=1\times2\times\cdots\times n. In particular, 0!=10!=1.

Xiaocong wants to know, given nn, mm, and kk, among all 0in0 \leq i \leq n, 0jmin(i,m)0 \leq j \leq \min \left ( i, m \right ), how many pairs (i,j)(i, j) satisfy k(ij)k \mid \binom{i}{j}.

Input Format

The first line contains two integers t,kt, k, where tt is the number of test cases in this testdata, and kk is as described in the problem.

Each of the next tt lines contains two integers n,mn, m, where nn and mm are as described in the problem.

Output Format

Output tt lines. Each line contains a single integer representing how many pairs (i,j)(i, j) among all 0in0 \leq i \leq n, 0jmin(i,m)0 \leq j \leq \min \left ( i, m \right ) satisfy k(ij)k \mid \binom{i}{j}.

1 2
3 3
1
2 5
4 5
6 7
0
7

Hint

Sample 1 Explanation.

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

Subtasks.

Testpoint nn mm kk tt
11 3\le 3 =2=2 =1=1
22 ^ =3=3 104\le 10^4
33 7\le 7 =4=4 =1=1
44 ^ =5=5 104\le 10^4
55 10\le 10 =6=6 =1=1
66 ^ =7=7 104\le 10^4
77 20\le 20 100\le 100 =8=8 =1=1
88 ^ =9=9 104\le 10^4
99 25\le 25 2000\le 2000 =10=10 =1=1
1010 ^ =11=11 104\le 10^4
1111 60\le 60 20\le 20 =12=12 =1=1
1212 ^ =13=13 104\le 10^4
1313 100\le 100 25\le 25 =14=14 =1=1
1414 ^ ^ =15=15 104\le 10^4
1515 60\le 60 =16=16 =1=1
1616 ^ =17=17 104\le 10^4
1717 2000\le 2000 100\le 100 =18=18 =1=1
1818 ^ ^ =19=19 104\le 10^4
1919 2000\le 2000 =20=20 =1=1
2020 ^ =21=21 104\le 10^4

Constraints.

For all testdata, it is guaranteed that 0n,m2×1030 \leq n, m \leq 2 \times 10^3, 1t1041 \leq t \leq 10^4.

Translated by ChatGPT 5