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 题目: 题意:给你长宽高,让你画出一个正方体. 思路:模拟即可,湘潭邀请赛热身赛原题,不过比那个 ...
随机推荐
- NSDate常用的日期操作
// 当前时间创建NSDate NSDate *myDate = [NSDate date]; NSLog(@"myDate = %@",myDate); //从现在开始的24小时 ...
- PAT 甲级 1101 Quick Sort
https://pintia.cn/problem-sets/994805342720868352/problems/994805366343188480 There is a classical p ...
- 利用Fiddler,解密wireshark抓的HTTPS包
背景介绍 HTTPS加密方式介绍 浏览器-->SSL Client Hello(我支持这些加密方式)-->服务器 浏览器<-SLL Server Hello(就用这种加密,然后下面是 ...
- IPython 4.0发布:Jupyter和IPython分离后的首个版本
IPython 4.0日前正式发布,这是IPython分离成IPython和Jupyter后的第一个重要版本. 更新Jupyter的快捷方式是: pip install --upgrade jupyt ...
- html/css/javascript知识点集锦;完全小白开搞web编程
知识点集锦 1.在html模板的代码中会有下面一类: {% block title %} Blog entries {% endblock %}这里的 block 和 endblock 是神马? {% ...
- uva1086 The Ministers' Major Mess
题意:有n 个议案,m 个大臣,每个大臣会对其中的ki 个议案投票,为赞成或反对.现要你判断是否存在一种方案,使得每个大臣有大于一半的投票被满足.若存在,还需判断某个议案是不是一定要通过,或者一定不能 ...
- [CF613D]Kingdom and its Cities
description 题面 data range \[n, q,\sum k\le 10^5\] solution 还是虚树的练手题 \(f[0/1][u]\)表示\(u\)的子树内,\(u\)是否 ...
- bzoj3489: A simple rmq problem (主席树)
//========================== 蒟蒻Macaulish:http://www.cnblogs.com/Macaulish/ 转载要声明! //=============== ...
- BZOJ5292 & 洛谷4457 & LOJ2513:[BJOI2018]治疗之雨——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5292 https://www.luogu.org/problemnew/show/P4457 ht ...
- BZOJ1486:[HNOI2009]最小圈——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=1486 https://www.luogu.org/problemnew/show/P3199 题面 ...