QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 512 MB Total points: 100

#4558. 组合数问题

統計

组合数 $C_n^m$ 表示的是从 $n$ 个物品中选出 $m$ 个物品的方案数。举个例子,从 $(1,2,3)$ 三个物品中选择两个物品可以有 $(1,2),(1,3),(2,3)$ 这三种选择方法。根据组合数的定义,我们可以给出计算组合数 $C_n^m$ 的一般公式:

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

其中 $n!=1\times2\times\cdots\times n$。(额外的,当 $n=0$ 时, $n!=1$)

小葱想知道如果给定 $n,m$ 和 $k$,对于所有的 $0\leq i\leq n,0\leq j\leq \min \left ( i, m \right )$ 有多少对 $(i,j)$ 满足 $C_i^j$ 是 $k$ 的倍数。

答案对 $10^9 + 7$ 取模。

输入格式

第一行有两个整数 $t,k$,其中 $t$ 代表该测试点总共有多少组测试数据。

接下来 $t$ 行每行两个整数 $n,m$。

输出格式

$t$ 行,每行一个整数代表所有的 $0\leq i\leq n,0\leq j\leq \min \left ( i, m \right )$ 中有多少对 $(i,j)$ 满足 $C_i^j$ 是 $k$ 的倍数。

样例一

input

1 2
3 3

output

1

explanation

在所有可能的情况中,只有 $C_2^1=2$ 是 $2$ 的倍数。

样例二

input

2 5
4 5
6 7

output

0
7

样例三

input

3 23
23333333 23333333
233333333 233333333
2333333333 2333333333

output

851883128
959557926
680723120

限制与约定

对于 $20\%$ 的测试点,$1\leq n,m\leq 100$;

对于另外 $15\%$ 的测试点,$n\leq m$;

对于另外 $15\%$ 的测试点, $k=2$;

对于另外 $15\%$ 的测试点, $m\leq 10$;

对于 $100\%$ 的测试点, $1\leq n,m\leq 10^{18},1 \leq t,k\leq 100$,且 $k$ 是一个质数。

About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.