QOJ.ac

QOJ

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

#4558. 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 three ways: $(1, 2), (1, 3), (2, 3)$. According to the definition of binomial coefficients, we can provide the general formula for calculating $C_n^m$:

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

where $n!=1\times2\times\cdots\times n$. (Additionally, $n!=1$ when $n=0$.)

Xiao Cong wants to know, given $n, m,$ and $k$, how many pairs $(i, j)$ satisfy $0\leq i\leq n$ and $0\leq j\leq \min(i, m)$ such that $C_i^j$ is a multiple of $k$.

The answer should be taken modulo $10^9 + 7$.

Input

The first line contains two integers $t$ and $k$, where $t$ represents the total number of test cases.

Each of the next $t$ lines contains two integers $n$ and $m$.

Output

Output $t$ lines, each containing an integer representing the number of pairs $(i, j)$ such that $0\leq i\leq n$ and $0\leq j\leq \min(i, m)$ where $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

Input 3

3 23
23333333 23333333
233333333 233333333
2333333333 2333333333

Output 3

851883128
959557926
680723120

Constraints

For $20\%$ of the test cases, $1\leq n, m\leq 100$;

For another $15\%$ of the test cases, $n\leq m$;

For another $15\%$ of the test cases, $k=2$;

For another $15\%$ of the test cases, $m\leq 10$;

For $100\%$ of the test cases, $1\leq n, m\leq 10^{18}, 1 \leq t, k\leq 100$, and $k$ is a prime number.

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.