Prefix Sum —— 树状数组+懵逼的组合恒等式

2024-04-07 01:18

本文主要是介绍Prefix Sum —— 树状数组+懵逼的组合恒等式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

链接:https://www.nowcoder.com/acm/contest/147/H
来源:牛客网

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

//这是个有问题的博客
这道题做了很久很久,还是没做出来,完全不知道组合数用树状数组怎么维护,各种骚操作都失败。就连看别人的代码也是一脸懵逼,最后才发现这和树状数组其实没有什么特别大的关系,它就是用来存一下数据的,接下来就是令人绝望的组合恒等式
这里写图片描述
我也不知道这个是什么玩意,以后搞懂了在说(数论不是我负责的,以后也不会懂),先记着。
它每一个树状数组记录的是杨辉三角中的一排,就算有负数也会加上一个mod,在query的时候会消掉,就比如说要求的答案是21,他是通过1*0+3*1+3*4+1*6这么过来的。。。其实我也不懂,调试别人代码的时候发现的这个规律

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod=1e9+7;
const int maxn=1e5+5;
ll c[55];
ll sum[55][maxn];
int n,m,k;
int lowbit(int x)
{return x&(-x);
}
void add(int i,int pos,int val)
{for(int j=pos;j<=n;j+=lowbit(j))sum[i][j]=(sum[i][j]+val)%mod;
}
int query(int i,int pos)
{int ans=0;for(int j=pos;j>=1;j-=lowbit(j))ans=(ans+sum[i][j])%mod;return ans;
}
int main()
{c[1]=1;for(int i=2;i<=50;i++)c[i]=(ll)(mod-mod/i)*c[mod%i]%mod;scanf("%d%d%d",&n,&m,&k);k-=1;int q,pos,val;while(m--){scanf("%d",&q);if(q==0){scanf("%d%d",&pos,&val);ll u=1;for(int i=0;i<=k;i++){add(i,pos,(ll)val*u%mod);u=(u*(k-pos-i)%mod+mod)%mod*c[i+1]%mod;}}else{scanf("%d",&pos);ll u=1,ans=0;for(int i=0;i<=k;i++){ans=(ans+u*query(k-i,pos))%mod;u=u*(pos-i)%mod*c[i+1]%mod;}printf("%lld\n",ans);}}return 0;
}

这篇关于Prefix Sum —— 树状数组+懵逼的组合恒等式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/881305

相关文章

C++原地删除有序数组重复项的N种方法

《C++原地删除有序数组重复项的N种方法》给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度,不要使用额外的数组空间,你必须在原地修改输入数组并在使用O(... 目录一、问题二、问题分析三、算法实现四、问题变体:最多保留两次五、分析和代码实现5.1、问题分析5.

Java中数组转换为列表的两种实现方式(超简单)

《Java中数组转换为列表的两种实现方式(超简单)》本文介绍了在Java中将数组转换为列表的两种常见方法使用Arrays.asList和Java8的StreamAPI,Arrays.asList方法简... 目录1. 使用Java Collections框架(Arrays.asList)1.1 示例代码1.

C++一个数组赋值给另一个数组方式

《C++一个数组赋值给另一个数组方式》文章介绍了三种在C++中将一个数组赋值给另一个数组的方法:使用循环逐个元素赋值、使用标准库函数std::copy或std::memcpy以及使用标准库容器,每种方... 目录C++一个数组赋值给另一个数组循环遍历赋值使用标准库中的函数 std::copy 或 std::

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

Java 字符数组转字符串的常用方法

《Java字符数组转字符串的常用方法》文章总结了在Java中将字符数组转换为字符串的几种常用方法,包括使用String构造函数、String.valueOf()方法、StringBuilder以及A... 目录1. 使用String构造函数1.1 基本转换方法1.2 注意事项2. 使用String.valu

JAVA中整型数组、字符串数组、整型数和字符串 的创建与转换的方法

《JAVA中整型数组、字符串数组、整型数和字符串的创建与转换的方法》本文介绍了Java中字符串、字符数组和整型数组的创建方法,以及它们之间的转换方法,还详细讲解了字符串中的一些常用方法,如index... 目录一、字符串、字符数组和整型数组的创建1、字符串的创建方法1.1 通过引用字符数组来创建字符串1.2

vue如何监听对象或者数组某个属性的变化详解

《vue如何监听对象或者数组某个属性的变化详解》这篇文章主要给大家介绍了关于vue如何监听对象或者数组某个属性的变化,在Vue.js中可以通过watch监听属性变化并动态修改其他属性的值,watch通... 目录前言用watch监听深度监听使用计算属性watch和计算属性的区别在vue 3中使用watchE

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu4869(逆元+求组合数)

//输入n,m,n表示翻牌的次数,m表示牌的数目,求经过n次操作后共有几种状态#include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdlib.h>#includ