Description
The binomial coefficient (mn) represents the number of ways to choose m items from n items. For example, when choosing two items from three items (1,2,3), there are three choices: (1,2), (1,3), and (2,3). By definition, the general formula for computing the binomial coefficient (mn) is:
(mn)=m!(n−m)!n!
where n!=1×2×⋯×n. In particular, 0!=1.
Xiaocong wants to know, given n, m, and k, among all 0≤i≤n, 0≤j≤min(i,m), how many pairs (i,j) satisfy k∣(ji).
The first line contains two integers t,k, where t is the number of test cases in this testdata, and k is as described in the problem.
Each of the next t lines contains two integers n,m, where n and m are as described in the problem.
Output t lines. Each line contains a single integer representing how many pairs (i,j) among all 0≤i≤n, 0≤j≤min(i,m) satisfy k∣(ji).
1 2
3 3
1
2 5
4 5
6 7
0
7
Hint
Sample 1 Explanation.
Among all possible cases, only (12)=2 is a multiple of 2.
Subtasks.
| Testpoint |
n |
m |
k |
t |
| 1 |
≤3 |
=2 |
=1 |
| 2 |
^ |
=3 |
≤104 |
| 3 |
≤7 |
=4 |
=1 |
| 4 |
^ |
=5 |
≤104 |
| 5 |
≤10 |
=6 |
=1 |
| 6 |
^ |
=7 |
≤104 |
| 7 |
≤20 |
≤100 |
=8 |
=1 |
| 8 |
^ |
=9 |
≤104 |
| 9 |
≤25 |
≤2000 |
=10 |
=1 |
| 10 |
^ |
=11 |
≤104 |
| 11 |
≤60 |
≤20 |
=12 |
=1 |
| 12 |
^ |
=13 |
≤104 |
| 13 |
≤100 |
≤25 |
=14 |
=1 |
| 14 |
^ |
^ |
=15 |
≤104 |
| 15 |
≤60 |
=16 |
=1 |
| 16 |
^ |
=17 |
≤104 |
| 17 |
≤2000 |
≤100 |
=18 |
=1 |
| 18 |
^ |
^ |
=19 |
≤104 |
| 19 |
≤2000 |
=20 |
=1 |
| 20 |
^ |
=21 |
≤104 |
Constraints.
For all testdata, it is guaranteed that 0≤n,m≤2×103, 1≤t≤104.
Translated by ChatGPT 5