Astonishing Combinatorial Number | ||||||
| ||||||
Description | ||||||
The combinatorial number CmnCnm in combinatorics, where it gives the number of the ways, disregarding order, the m objects can be chosen from among n objects; more formally, the number of m-element subsets (or m-combinations) of an n-element set. Now, there's a simple problem about this number: For three given number n, m and k, how many pairs of (i, j) will make the equation A.a be true. (0 <= i <= n, 0 <= j <= min(i, m)) Equation A.1: Cjimodk=0Cijmodk=0 | ||||||
Input | ||||||
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case: | ||||||
Output | ||||||
Each test case should contain t lines. Each line has an integer indicate the answer of the problem. | ||||||
Sample Input | ||||||
2 1 2 3 3 2 5 4 5 6 7 | ||||||
Sample Output | ||||||
1 0 7 | ||||||
Hint | ||||||
In first test case, only C12=2C21=2 is multiple of 2. | ||||||
Source | ||||||
"尚学堂杯"哈尔滨理工大学第七届程序设计竞赛 |
题目大意:
给你t个查询,每个查询询问Ci,j%k==0的个数(i<=n,j<=min(m,i))。
思路:
我们知道,Ci,j的求法有很多,这里数据范围并不大,而且模数不定,所以我们肯定要用求杨辉三角的方法来求Ci,j的。
那么对于每个k,我们预处理出来C【2000】【2000】表示Ci,j%k的值,然后我们维护一个前缀和,O(t*n)查询即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int c[2005][2005];
int sum[2005][2005];
void init(int z)
{
memset(sum,0,sizeof(sum));
memset(c,0,sizeof(c));
c[0][0]=1%z;
c[1][0]=1%z;
c[1][1]=1%z;
for(int i=2;i<=2002;i++)
{
c[i][0]=1%z;
for(int j=1;j<=i;j++)
{
c[i][j]=(c[i-1][j]+c[i-1][j-1])%z;
}
}
for(int i=1;i<=2002;i++)
{
for(int j=1;j<=2002;j++)
{
int tmp=0;
if(c[i][j]==0)tmp=1;
sum[i][j]=sum[i][j-1]+tmp;
}
}
}
int main()
{
int t;
while(~scanf("%d",&t))
{
while(t--)
{
int q,k;
scanf("%d%d",&q,&k);
init(k);
while(q--)
{
int n,m;
scanf("%d%d",&n,&m);
int output=0;
for(int i=1;i<=n;i++)
{
output+=sum[i][min(i,m)];
}
printf("%d\n",output);
}
}
}
}