牛客网暑期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 ,则

ans=1ixCk1xi+k1a[0][i] a n s = ∑ 1 ≤ i ≤ x C x − i + k − 1 k − 1 ∗ a [ 0 ] [ i ]

为了方便,让 k=k1 k = k − 1 .
ans=1ixCkxi+ka[0][i] a n s = ∑ 1 ≤ i ≤ x C x − i + k k ∗ a [ 0 ] [ i ]

注意到组合恒等式,然后我们把组合数的定义推广到负数
Ckxi+k=0jkCjxCkjki C x − i + k k = ∑ 0 ≤ j ≤ k C x j ∗ C k − i k − j


ans=0jkCjx1ix(Ckjkia[0][i]) a n s = ∑ 0 ≤ j ≤ k C x j ∑ 1 ≤ i ≤ x ( C k − i k − j ∗ a [ 0 ] [ i ] )

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

负数组合数公式 Cmn=Cmn+m1(n>0) C − n m = C n + m − 1 m ( n > 0 )

#include
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;
}

你可能感兴趣的:(数据结构-线段树//树状数组,数学)