HDU - 6333 Harvest of Apples
题意:
T次询问,每次给出n,m。求sigma(k:0->m)C(n, k)。
题解:
用离线莫队来做。
令S(n,m) = sigma(k:0->m)C(n, k)。
S(n+1, m) = 2S(n, m) - C(n, m) S(n-1, m) = (S(n, m) + C(n-1, m)) / 2
S(n, m+1) = S(n, m) + C(n, m+1) S(n, m-1) = S(n, m) - C(n, m)
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9+;
const int N = 1e5+;
int t;
int blk;
int blg[N];
int fac[N], inv[N], tmp[N];
int l, r, now;
int ans[N];
struct ask {
int l, r, id;
bool operator < (const ask &a) {
return blg[l] == blg[a.l] ? r < a.r : l < a.l;
}
}q[N];
int C(int n, int m) {
int res = 1ll*fac[n]*inv[m]%mod;
res = 1ll*res*inv[n-m]%mod;
return res;
}
void update(int k, int t) {
if(t == ) {
if(~k) now = (2ll*now-C(l, r)+mod)%mod;
else now = 1ll*(now+C(l-, r))*tmp[]%mod;
}
else {
if(~k) now = (now+C(l, r))%mod;
else now = (now-C(l, r)+mod)%mod;
}
}
int main() {
fac[] = fac[] = tmp[] = inv[] = inv[] = ;
for(int i = ; i < N; i++) {
fac[i] = 1ll*fac[i-]*i%mod;
tmp[i] = 1ll*(mod-mod/i)*tmp[mod%i]%mod;
inv[i] = 1ll*inv[i-]*tmp[i]%mod;
}
scanf("%d", &t);
int up = ;
for(int i = ; i <= t; i++) {
scanf("%d%d", &q[i].l, &q[i].r);
up = max(up, q[i].r);
q[i].id = i;
}
blk = sqrt(t);
for(int i = ; i <= up; i++) blg[i] = (i-)/blk;
sort(q+, q+t+);
now = ;
l = , r = ;
for(int i = ; i <= t; i++) {
while(r > q[i].r) update(-, ), r--;
while(l < q[i].l) update(, ), l++;
while(l > q[i].l) update(-, ), l--;
while(r < q[i].r) r++, update(, );
ans[q[i].id] = now;
}
for(int i = ; i <= t; i++) printf("%d\n", ans[i]);
}
HDU - 6333 Harvest of Apples的更多相关文章
- HDU 6333 Harvest of Apples (分块、数论)
题目连接:Harvest of Apples 题意:给出一个n和m,求C(0,n)+C(1,n)+.....+C(m,n).(样例组数为1e5) 题解:首先先把阶乘和逆元预处理出来,这样就可O(1)将 ...
- HDU 6333.Problem B. Harvest of Apples-组合数C(n,0)到C(n,m)求和-组合数学(逆元)+莫队 ((2018 Multi-University Training Contest 4 1002))
2018 Multi-University Training Contest 4 6333.Problem B. Harvest of Apples 题意很好懂,就是组合数求和. 官方题解: 我来叨叨 ...
- hdu多校第4场 B Harvest of Apples(莫队)
Problem B. Harvest of Apples Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...
- 2018 Multi-University Training Contest 4 Problem B. Harvest of Apples 【莫队+排列组合+逆元预处理技巧】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6333 Problem B. Harvest of Apples Time Limit: 4000/200 ...
- Harvest of Apples
问题 B: Harvest of Apples 时间限制: 1 Sec 内存限制: 128 MB提交: 18 解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 Ther ...
- HDU 6333 莫队+组合数
Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K ...
- hdu6333 Harvest of Apples 离线+分块+组合数学(求组合数模板)
Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K ...
- hdu6333 Problem B. Harvest of Apples(组合数+莫队)
hdu6333 Problem B. Harvest of Apples 题目传送门 题意: 求(0,n)~(m,n)组合数之和 题解: C(n,m)=C(n-1,m-1)+C(n-1,m) 设 ...
- Problem B. Harvest of Apples HDU - 6333(莫队)
Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...
随机推荐
- Python Map 并行
Map是一个酷酷的小东西,也是在Python代码轻松引入并行的关键.对此不熟悉的人会认为map是从函数式语言(如Lisp)借鉴来的东西.map是一个函数 - 将另一个函数映射到一个序列上.例如: ur ...
- MySql 增加字段 删除字段 修改字段名称 修改字段类型
//1.增加一个字段 alter table user add COLUMN new1 VARCHAR(20) DEFAULT NULL; //增加一个字段,默认为空 alter table user ...
- C#中Mutex的用法
C#中Mutex是互斥锁,位于System.Threading 命名空间中. 顾名思义,它是一个互斥的对象,同一时间只有一个线程可以拥有它,该类还可用于进程间同步的同步基元. 如果当前有一个线程拥有它 ...
- 两个有序数组合并成一个有序数组(要求时间复杂度为O(n))
面试题: 怎样把两个有序数组合并成有序数组呢 逻辑步骤: 1.假设两个数组为A和B 2.A和B都是从小到大的顺序进行排列 ** 1.我们可以直接比较两个数组的首元素,哪个小就把这个小元素放入可变数组. ...
- python 函数定义顺序
#!/usr/bin/python # Hello World def order(): print("haha") print('Hello World!') order()
- HTML随笔3
1. *svg(可伸缩矢量图)标签画圆,其中r表示半径,cx和cy表示其圆心的坐标 <svg><circle r="100" cx="200" ...
- Qt-QML-C++交互实现文件IO系统
QMl是没有自己的文件IO控制的,这里如果我们需要对文件进行读写操作,那么就需要去C++或者JS完成交互,交互方式有多种,由于我还没有掌握,这里就不介绍具体的交互方式了.这里就简单说明一下我的实现过程 ...
- 韦大仙--python对文件操作 2--写入与修改
请大家看一段代码: yesterday2是我之前上个帖子创建的文件,为了方便大家看清我把本来的代码复制到下面: coding=utf-8 f=open("yesterday2",& ...
- Siki_Unity_1-6_C#编程初级教程(未学)
Unity 1-6 C#编程初级教程 任务1:C#和.Net框架 C#是.Net里的一个成分 2002年微软发布第一个.Net框架(多平台,行业标准,安全性) .Net框架 IDE编程工具 --产生- ...
- BOM / URL编码解码 / 浏览器存储
BOM 浏览器对象模型 BOM(Browser Object Model) 是指浏览器对象模型,是用于描述这种对象与对象之间层次关系的模型,浏览器对象模型提供了独立于内容的.可以与浏览器窗口进行互动的 ...