Hrbust 2325 Astonishing Combinatorial Number【前缀和+暴力】

来源:互联网 发布:淘宝店铺监管会怎么样 编辑:程序博客网 时间:2024/05/16 04:34

Astonishing Combinatorial NumberTime Limit: 8000 MSMemory Limit: 512000 KTotal Submit: 58(12 users)Total Accepted: 15(10 users)Rating: Special Judge: NoDescription

    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:
The first line contains two integers  t(1<=t<=10^4) and k(1<=k<=30).
Each of the next t lines contains two integers  n(1<=n<=2000) and m(1<=m<=2000) .


OutputEach test case should contain t lines. Each line has an integer indicate the answer of the problem.

Sample Input2
1 2
3 3
2 5
4 5
6 7Sample Output

1

0

7

HintIn 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);            }        }    }}