Topic description
The combination number CMNCNM represents the number of options for selecting mm items from an item in NN. For example, two items from (1,2,3) (1,2,3) three items can have (1,2), (1,3), (2,3) (1,2), (1,3), (2,3) the three options. According to the definition of combinatorial number, we can give a general formula for calculating the CMNCNM of combinatorial number:
cmn=n!m! (N−M)!
cnm=n!m! (N−M)!
Which n!=1x2x⋯xnn!=1x2x⋯xn, in particular, defines 0!=10!=1.
Shallot want to know if given N,mn,m and KK, for all 0≤i≤n,0≤j≤min (i,m) 0≤i≤n,0≤j≤min (i,m) There are many pairs (i,j) (i,j) to satisfy CMNCNM is a multiple of KK.
Input format
The first line has two integers t,kt,k, where TT
Represents the total number of test data for this test point, KK meaning see problem description.
The next TT line is two integers per line n,mn,m, where the meaning of n,mn,m is described in the question.
Output format
The TT row, one integer per line, represents how many pairs (i,j) (i,j) in all 0≤i≤n,0≤j≤min (i,m) 0≤i≤n,0≤j≤min (i,m) meet the CMNCNM is a multiple of KK.
Sample One
Input
1 2
3 3
Output
1
Explanation
In all possible cases, only c12c21 is a multiple of 22.
Sample Two
Input
2 5
4 5
6 7
Output
0
7
train of Thought
Yang Hui's triangle
NUM[I][J] The number of combinations of J selected in the I number
The first line of each row is the number of rows, and the diagonal is 1,
Initialize the recursive value, each equal to its top + upper-left value
TOT[I][J] is how many of the first J in line I can be prefixed and maintained by K
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=2000+50;
Long long NUM[MAXN][MAXN],TOT[MAXN][MAXN];
int ans,n,m,t,k;
int main () {
scanf ("%d%d", &t,&k);
for (int i=1;i<=2005;i++) {
num[i][1]=i%k;
num[i][i]=1;
}
for (int i=1;i<=2005;i++)
(int j=2;j<i;j++) {
num[i][j]= (num[i-1][j]+num[i-1][j-1])%k;
for (int i=1;i<=2010;i++)
(int j=1;j<=i;j++) {
if (num[i][j]%k==0)
tot[i][j]=1;
TOT[I][J]+=TOT[I][J-1];
}
while (t--) {
scanf ("%d%d", &n,&m);
ans=0;
for (int i=1;i<=n;i++)
ans+=tot[i][min (i,m)];
printf ("%d\n", ans);
}
return 0;
}