Problem B. Harvest of Apples(杭电2018年多校+组合数+逆元+莫队)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333
题目:

题意:求C(n,0)+C(n,1)+……+C(n,m)的值。
思路:由于t和n数值范围太大,所以此题查询复杂度不能太高,由组合数的将前k项求和可以推出
,从而可以转换成莫队的区间查询,将n当成l,m当成r即可。此题需要注意,对于求组合数得用o(1)的方法求,也就是阶乘相除的方法,对于分母我们得求逆元,因而借助欧拉定理。
代码实现如下:
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = 1e9 + ;
const int maxn = 1e5 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f; int t, block;
ll sum;
ll a[maxn], b[maxn]; struct node {
int l, r, id;
ll ans;
bool operator < (const node & x) const {
return (l - ) / block == (x.l - ) / block ? r < x.r : l < x.l;
}
}ask[maxn]; ll Mod_Pow(ll x, ll n) {
ll res = ;
while(n > ) {
if(n & ) res = res * x % mod;
x = x * x % mod;
n >>= ;
}
return res;
} void init() {
a[] = ;
for(int i = ; i < maxn; i++) a[i] = a[i-] * i % mod;
for(int i = ; i < maxn; i++) b[i] = Mod_Pow(a[i], mod - );
} ll C(int n, int m) {
if(n < || m < || m > n) return ;
if(m == || m == n) return ;
return a[n] * b[n-m] % mod * b[m] % mod;
} int main() {
//FIN;
init();
scanf("%d", &t);
block = sqrt(maxn);
sum = ;
for(int i = ; i <= t; i++) {
scanf("%d%d", &ask[i].l, &ask[i].r);
ask[i].id = i;
}
sort(ask + , ask + t + );
for(int i = , l = , r = ; i <= t; i++) {
while(l < ask[i].l) sum = ( * sum - C(l++, r) + mod) % mod;
while(l > ask[i].l) sum = ((sum + C(--l, r)) * b[]) % mod;
while(r < ask[i].r) sum = (sum + C(l, ++r)) % mod;
while(r > ask[i].r) sum = (sum - C(l, r--) + mod) % mod;
ask[ask[i].id].ans = sum;
}
for(int i = ; i <= t; i++) {
printf("%lld\n", ask[i].ans);
}
return ;
}
Problem B. Harvest of Apples(杭电2018年多校+组合数+逆元+莫队)的更多相关文章
- Problem E. Matrix from Arrays(杭电2018年多校第四场+思维+打表找循环节)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6336 题目: 题意:给你一个l个元素的数组a,用题目中的程序构造一个新的矩阵,询问q次,问以(x1,y ...
- 杭电2018暑假多校第一场 D Distinct Values hdu6301 贪心
Distinct Values Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 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 ...
- 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 题意很好懂,就是组合数求和. 官方题解: 我来叨叨 ...
- 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 莫队求组合数前缀和
Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...
- HDU - 6333 Problem B. Harvest of Apples (莫队)
There are nn apples on a tree, numbered from 11 to nn. Count the number of ways to pick at most mm a ...
- 【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples
http://acm.hdu.edu.cn/showproblem.php?pid=6333 莫队算法是一个离线区间分块瞎搞算法,只要满足:1.离线 2.可以O(1)从区间(L,R)更新到(L±1, ...
- Problem L. Visual Cube(杭电多校2018年第三场+模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6330 题目: 题意:给你长宽高,让你画出一个正方体. 思路:模拟即可,湘潭邀请赛热身赛原题,不过比那个 ...
随机推荐
- LintCode-50.数组剔除元素后的乘积
数组剔除元素后的乘积 给定一个整数数组A. 定义B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], 计算B的时候请不要使用除法. 样例 给出A=[1 ...
- <Effective C++>读书摘要--Inheritance and Object-Oriented Design<二>
<Item 36> Never redefine an inherited non-virtual function 1.如下代码通过不同指针调用同一个对象的同一个函数会产生不同的行为Th ...
- 【Docker 命令】- build命令
docker build 命令用于使用 Dockerfile 创建镜像. 语法 docker build [OPTIONS] PATH | URL | - OPTIONS说明: --build-arg ...
- oracle RAC集群启动和关闭
oracle 11G RAC集群启动和关闭:1.停止数据库$srvctl stop database –d oradb2.停止集群服务关闭集群需要root运行#/u01/11.2.0/grid/bin ...
- 数字证书认证这点事, SSL/TLS,OpenSSL
1.概念 数字证书 HTTPS请求时,Server发给浏览器的认证数据,用私钥签名,并且告诉浏览器公钥,利用公钥解密签名,确认Server身份. 证书还会指明相应的CA,CA能确认证书是否真的是CA颁 ...
- 《Effective C#》快速笔记(三)- 使用 C# 表达设计
目录 二十一.限制类型的可见性 二十二.通过定义并实现接口替代继承 二十三.理解接口方法和虚方法的区别 二十四.用委托实现回调 二十五.用事件模式实现通知 二十六.避免返回对内部类对象的引用 二十七. ...
- 发送tcp的时候,数据包是如何拷贝的
发送数据包的时候,用户态的数据包是如何拷贝到内核的kiovec msghd 结构体 icmp是走sock吗? 每一个skb_buffer的大小都是固定的吗?所以有skb_available这样的函数 ...
- hdu 2050 折线分割平面 (递推)
折线分割平面 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- Windows系统Unity3D中的快捷键
Windows系统Unity3D中的快捷键 组合键 键 功能 File 文件 Ctrl N New Scene 新建场景 Ctrl O Open Scene 打开场景 Ctrl S Sav ...
- Signal函数
Signal函数: 这个函数是一种系统调用,就是告诉系统发生中断的时候用该干嘛.第一个参数就是信号的编号,第二个参数就是信号的指针. 原型: #include <signal.h> voi ...