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

Astonishing Combinatorial Number
Time Limit: 8000 MSMemory Limit: 512000 K
Total Submit: 58(12 users)Total Accepted: 15(10 users)Rating: Special Judge: No
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:
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) .


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







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值