[洛谷 2822]组合数问题---二项式定理+前缀和

本文探讨了如何计算给定n、m和k时,满足条件C_ij是k的倍数的(i, j)对的数量。通过使用杨辉三角及前缀和优化,实现了高效的求解。

题目描述

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

Cmn=n!m!(nm)! C n m = n ! m ! ( n − m ) !

其中 n!=1×2××n n ! = 1 × 2 × · · · × n

小葱想知道如果给定n,m和k,对于所有的 0<=i<=n0<=j<=min(i,m) 0 <= i <= n , 0 <= j <= m i n ( i , m ) 有多少对 (i,j)满足 Cji C i j ​ ​ k k 的倍数。
输入输出格式
输入格式:

第一行有两个整数t,k,其中t代表该测试点总共有多少组测试数据,k的意义见 【问题描述】。

接下来t行每行两个整数n,m,其中n,m的意义见【问题描述】。

输出格式:

t行,每行一个整数代表答案。

输入输出样例
输入样例#1:

1 2
3 3

输出样例#1:

1

输入样例#2:

2 5
4 5
6 7

输出样例#2:

0
7

分析

  作为去年没做出来的题,不想多说什么...
  直接杨辉三角即可,再加入一点优化:
    1.直接%k,当f[i][j]为0时,ans++
    2.利用前缀和,优化答案统计

代码

#include <cstdio>
#include <cstdlib>
#define open(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
#define close fclose(stdin); fclose(stdout); 
using namespace std;

int mo;
int f[2005][2005];
int s[2005][2005];

inline int read()
{
    int k=1;
    int sum=0;
    char c=getchar();
    for(;'0'>c || c>'9' ;c=getchar())
        if(c=='-') k=-1;
    for(;'0'<=c && c<='9';c=getchar())
        sum=sum*10+c-'0';
    return sum*k;
}

inline void write(int x)
{
    if(x<0) { putchar('-'); x*=-1; }
    if(x>9) write(x/10);
    putchar(x%10+'0');
}

inline void get_f()
{
    for(int i=0;i<=2000;++i)
    for(int j=0;j<=i;++j)
    {
        if(i==j || !j)
            f[i][j]=1;
        else
            f[i][j]=f[i-1][j-1]+f[i-1][j];
        if(f[i][j]>=mo) f[i][j]-=mo;
        if(!f[i][j]) s[i][j]=1;
        s[i][j]+=(j?s[i][j-1]:0);
    }
}

int main()
{
    open("2822");
    int T=read();
    mo=read();
    get_f();
    for(int x,y,ans;T;--T)
    {
        x=read();
        y=read();
        ans=0;
        for(int i=0;i<=x;++i)//其实前缀和还可以再优化变为O(1)
            ans+=s[i][i<y?i:y];
        write(ans);
        putchar('\n');
    }
    close;
    return 0;
}
You are given positive integers N,K, and an integer sequence of length N: A=(A 1 ​ ,A 2 ​ ,…,A N ​ ). Find 1≤l≤r≤N ∑ ​ ( l≤i≤r ∑ ​ A i ​ ) K , modulo 998244353. Constraints 1≤N≤2×10 5 1≤K≤10 0≤A i ​ <998244353 All input values are integers. Input The input is given from Standard Input in the following format: N K A 1 ​ A 2 ​ … A N ​ Output Print the answer. Sample Input 1 Copy 3 2 3 1 2 Sample Output 1 Copy 75 The value is A 1 2 ​ +A 2 2 ​ +A 3 2 ​ +(A 1 ​ +A 2 ​ ) 2 +(A 2 ​ +A 3 ​ ) 2 +(A 1 ​ +A 2 ​ +A 3 ​ ) 2 =3 2 +1 2 +2 2 +4 2 +3 2 +6 2 =75. Sample Input 2 Copy 1 10 0 Sample Output 2 Copy 0 Sample Input 3 Copy 10 5 91 59 85 60 57 72 12 3 27 16 Sample Output 3 Copy 428633385 Be sure to find the sum modulo 998244353. C++AC代码,中文!!!过样例,时间复杂度O(NK)。提示: 代码解释 前缀和预处理:计算数组的前缀和,使得每个元素表示从开头到当前位置的和。 幂次预处理:计算每个前缀和的各个幂次,以便后续快速计算。 后缀和预处理:计算每个幂次的后缀和,用于快速累加子数组的贡献。 组合数预处理:计算组合数C(K, t),用于多项式展开中的系数。 多项式展开计算:利用多项式定理展开每个子数组的和的K次方,并通过预处理的后缀和数组快速累加结果,最终得到答案。 通过这种方法,我们能够将时间复杂度优化到O(NK),适用于较大的输入规模。
03-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值