牛客网暑期ACM多校训练营(第九场)H:Prefix Sum(组合数学相关)

链接:https://www.nowcoder.com/acm/contest/147/H

时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
Niuniu has learned prefix sum and he found an interesting about prefix sum.

Let’s consider (k+1) arrays a[i] (0 <= i <= k)
The index of a[i] starts from 1.
a[i] is always the prefix sum of a[i-1].
“always” means a[i] will change when a[i-1] changes.
“prefix sum” means a[i][1] = a[i-1][1] and a[i][j] = a[i][j-1] + a[i-1][j] (j >= 2)

Initially, all elements in a[0] are 0.
There are two kinds of operations, which are modify and query.
For a modify operation, two integers x, y are given, and it means a[0][x] += y.
For a query operation, one integer x is given, and it means querying a[k][x].

As the result might be very large, you should output the result mod 1000000007.

输入描述:
The first line contains three integers, n, m, k.
n is the length of each array.
m is the number of operations.
k is the number of prefix sum.

In the following m lines, each line contains an operation.

If the first number is 0, then this is a change operation.
There will be two integers x, y after 0, which means a[0][x] += y;
If the first number is 1, then this is a query operation.

There will be one integer x after 1, which means querying a[k][x].

1 <= n <= 100000
1 <= m <= 100000
1 <= k <= 40
1 <= x <= n
0 <= y < 1000000007
输出描述:
For each query, you should output an integer, which is the result.
示例1
输入
4 11 3
0 1 1
0 3 1
1 1
1 2
1 3
1 4
0 3 1
1 1
1 2
1 3
1 4
输出
1
3
7
13
1
3
8
16
说明
For the first 4 queries, the (k+1) arrays are
1 0 1 0
1 1 2 2
1 2 4 6
1 3 7 13
For the last 4 queries, the (k+1) arrays are
1 0 2 0
1 1 3 3
1 2 5 8
1 3 8 16

题解:询问 x x x,则 a n s = ∑ 1 ≤ i ≤ x C x − i + k − 1 k − 1 ∗ a [ 0 ] [ i ] ans=\sum_{1\le i\le x}C_{x-i+k-1}^{k-1}*a[0][i] ans=1ixCxi+k1k1a[0][i]


关于如何得到上面的式子
我们先只看 a [ 0 ] [ 0 ] a[0][0] a[0][0]对整个数组的贡献,可以得到 a [ 0 ] [ 0 ] a[0][0] a[0][0]对数组的贡献系数如下
[ 1 1 1 1 1 ⋯ 1 2 3 4 ⋯ 1 3 6 ⋯ 1 4 10 ⋯ 1 5 … 1 … ] \left[ \begin{matrix} 1&1&1&1&1&\cdots\\ 1&2&3&4&\cdots \\ 1&3&6&\cdots \\ 1&4&10&\cdots \\ 1&5&\dots\\ 1& \dots\\ \end{matrix} \right] 1111111234513610141
如果把上面的矩阵顺时针翻转45°,其实就是杨辉三角。依此类推,那么我们就只需一一找出 a [ 0 ] [ i ] a[0][i] a[0][i]对答案的贡献系数并乘以 a [ 0 ] [ i ] a[0][i] a[0][i],累计求和就是答案。

为了方便,让 k = k − 1 k=k-1 k=k1.

a n s = ∑ 1 ≤ i ≤ x C x − i + k k ∗ a [ 0 ] [ i ] ans=\sum_{1\le i\le x}C_{x-i+k}^{k}*a[0][i] ans=1ixCxi+kka[0][i]
注意到组合恒等式,然后我们把组合数的定义推广到负数
C x − i + k k = ∑ 0 ≤ j ≤ k C x j ∗ C k − i k − j C_{x-i+k}^{k}=\sum_{0\le j\le k}C_x^j*C_{k-i}^{k-j} Cxi+kk=0jkCxjCkikj

a n s = ∑ 0 ≤ j ≤ k C x j ∑ 1 ≤ i ≤ x ( C k − i k − j ∗ a [ 0 ] [ i ] ) ans=\sum_{0\le j\le k}C_x^j\sum_{1\le i\le x}(C_{k-i}^{k-j}*a[0][i]) ans=0jkCxj1ix(Ckikja[0][i])

注意到最后是一个 C k − i k − j ∗ a [ 0 ] [ i ] C_{k−i}^{k−j}*a[0][i] Ckikja[0][i] 的前缀和,很容易想到用树状数组维护。

负数组合数公式 C − n m = C n + m − 1 m ( n > 0 ) C_{-n}^{m}=C_{n+m-1}^{m}(n>0) Cnm=Cn+m1m(n>0)

#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+10;
const int MOD=1e9+7;
const int INF=1e9+7;
const double PI=acos(-1.0);
typedef long long ll;
int A[44][MAX];
void add(int k,int x,int y,int n){while(x<=n){(A[k][x]+=y)%=MOD;x+=x&(-x);}}
int ask(int k,int x){int ans=0;while(x){(ans+=A[k][x])%=MOD;x-=x&(-x);}return ans;}
int inv[MAX];
ll C(int n,int m)//负数组合数也可由正常公式计算
{
    ll ans=1;
    for(int i=n;i>=(n-m+1);i--)ans=ans*i%MOD;
    for(int i=1;i<=m;i++)ans=ans*inv[i]%MOD;
    return (ans+MOD)%MOD;
}
int main()
{
    inv[1]=1;
    for(int i=2;i<=99;i++)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
    int n,m,k;
    cin>>n>>m>>k;
    k--;
    while(m--)
    {
        int op,x,y;
        scanf("%d",&op);
        if(op==0)
        {
            scanf("%d%d",&x,&y);
            for(int j=0;j<=k;j++)add(j,x,C(k-x,k-j)*y%MOD,n);
        }
        else
        {
            scanf("%d",&x);
            ll ans=0;
            for(int j=0;j<=k;j++,(ans+=MOD)%=MOD)ans+=C(x,j)*ask(j,x)%MOD;
            printf("%lld\n",ans);
        }
    }
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值