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 —— 树状数组+懵逼的组合恒等式相关推荐
- Codeforces Round #782 (Div. 2)-D. Reverse Sort Sum(树状数组)
题目链接:Problem - D - Codeforces 题意:给定一个 01 序列 ,对 区间分别进行递增排序,得到 个序列,现给定 序列, ,即所有序列第 位的和.求给定 序列所对应的原 ...
- Codeforces数学1600----day1[同余定理,树状数组+两次二分,,组合计数]
1.C. Kuroni and Impossible Calculation **知识点:同余定理 ** #include <iostream> #include <cstdio&g ...
- LeetCode Range Sum Query - Mutable(树状数组、线段树)
问题:给出一个整数数组,求出数组从索引i到j范围内元素的总和.update(i,val)将下标i的数值更新为val 思路:第一种方式是直接根据定义,计算总和时直接计算从i到j的和 第二种方式是使用树状 ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...
- Hihocoer 1336 - Matrix Sum 二维树状数组
题意 给我们个n*n的矩阵 有m个操作让我们 操作有两种 Add x y v对(x,y)上的元素加v Sum x1 y1 x2 y2 对(x1,y1)为左上角(x2,y2)为右下角的矩阵求和并mod ...
- 10.20 Bzoj 3155 Preprefix sum(树状数组)
Bzoj 3155 Preprefix sum 题意: 长度为n的序列,m次操作 操作有两种:1.询问前缀和的前缀和:2.修改某个ai的值 解题思路: S1=a1S_1=a_1S1=a1 S2=a ...
- E. Tyler and Strings(组合计数 + 树状数组/线段树)(带重复元素的全排列)
题目链接 主要思路还是比较好想的,一些细节公式预处理比较难搞- 参考题解 分析 从前往后遍历,如果s[]剩下的数中,s[i] < t[i]则对答案是有贡献的: 贡献就是后面位置[i+1 ~ n] ...
- Tyler and Strings(树状数组/排列组合/dp)
题目 题意:给定两个数组s,ts,ts,t,现重排列数组sss,使得数组sss小于ttt.问有多少种排列方式. 参考 代码源自cwxzh #include<bits/stdc++.h> u ...
- 【BZOJ 3155】Preprefix sum(树状数组)
显然是不能直接开两个树状数组维护 前缀和,前缀和的前缀和.因为一旦对a[i]进行修改,将会影响许多位前缀和的前缀和 我们考虑对式子变一下形 Qi =S1+S2+S3+...+Si =a1+a1+a2+ ...
- 花神游历各国 题解(小清新线段树/树状数组+并查集)
题面 众所周知,这是一道小清新线段树 然而可以用树状数组水过去且跑得飞快 看到区间开方第一反应肯定是线段树懒标记区间修改之类的,但是这个东西似乎确凿不可维护 所以考虑暴力循环单点修改->T飞 于 ...
最新文章
- php array product,PHP array_product()用法及代码示例
- 经典技术书籍分享(5):《WCF技术内幕》封面和购买地址
- DBDesigner 4 与 MySql 5 不能连接主要是驱动的原因
- php sorcket_PHP: Sockets - Manual
- 努比亚手机浏览器 安全证书失效_浏览器提示“该站点安全证书的吊销信息不可用”的解决方法-...
- 有哪些皮一下就很开心的句子?
- 第三届《麻省理工科技评论》EmTech China峰会召开,全球新兴科技智慧风暴席卷京城
- Halcon缺陷检测——差分
- c语言课程设计做科普,【图片】发几个C语言课程设计源代码(恭喜自己当上技术小吧主)【东华理工大学吧】_百度贴吧...
- jQuery.param() 函数详解
- 《初级会计电算化实用教程(金蝶KIS专业版)》一1.5 课后习题
- (附源码)springboot基于Web的儿童教育管理系统的开发毕业设计281442
- zzuli oj 1077
- 如何解决移动端300ms延迟的问题
- NLP 学习教程 第一节 简介
- 关于tink的碰撞检测类【1】
- python使用代理爬虫_python爬虫requests使用代理ip
- 使用Bootstrap建立网站微金所——头部
- 一滴水泛起行业波纹,惠民保给保险业带来的启示录
- 统计学中,Z值与T值怎么换算 T-score transform Z-score