QOJ.ac

QOJ

حد الوقت: 1 s حد الذاكرة: 512 MB مجموع النقاط: 100 قابلة للهجوم ✓

#13852. Combination Number Problem

الإحصائيات

The binomial coefficient $C_n^m$ represents the number of ways to choose $m$ items from $n$ items. For example, choosing two items from the three items $(1, 2, 3)$ can be done in $(1, 2), (1, 3), (2, 3)$, which are $3$ ways. Based on the definition of binomial coefficients, we can provide the general formula for $C_n^m$:

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

where $n! = 1 \times 2 \times \dots \times n$.

Given $n, m$ and $k$, for all $0 \le i \le n, 0 \le j \le \min(i, m)$, how many pairs $(i, j)$ satisfy that $C_i^j$ is a multiple of $k$?

Input

The first line contains two integers $t, k$, where $t$ is the total number of test cases, and $k$ is as defined above.

The next $t$ lines each contain two integers $n, m$, where $n, m$ are as defined above.

Output

Output $t$ lines, each containing an integer representing the number of pairs $(i, j)$ such that $0 \le i \le n, 0 \le j \le \min(i, m)$ and $C_i^j$ is a multiple of $k$.

Examples

Input 1

1 2
3 3

Output 1

1

Note 1

Among all possible cases, only $C_2^1 = 2$ is a multiple of $2$.

Input 2

2 5
4 5
6 7

Output 2

0
7

Subtasks

Test Case $n$ $m$ $k$ $t$
1 $\le 3$ $\le 3$ $= 2$ $= 1$
2 $= 3$ $\le 10^4$
3 $\le 7$ $\le 7$ $= 4$ $= 1$
4 $= 5$ $\le 10^4$
5 $\le 10$ $\le 10$ $= 6$ $= 1$
6 $= 7$ $\le 10^4$
7 $\le 20$ $\le 100$ $= 8$ $= 1$
8 $= 9$ $\le 10^4$
9 $\le 25$ $\le 2000$ $= 10$ $= 1$
10 $= 11$ $\le 10^4$
11 $\le 60$ $\le 20$ $= 12$ $= 1$
12 $= 13$ $\le 10^4$
13 $\le 100$ $\le 25$ $= 14$ $= 1$
14 $= 15$ $\le 10^4$
15 $\le 60$ $= 16$ $= 1$
16 $= 17$ $\le 10^4$
17 $\le 2000$ $\le 100$ $= 18$ $= 1$
18 $= 19$ $\le 10^4$
19 $\le 2000$ $= 20$ $= 1$
20 $= 21$ $\le 10^4$

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.