HDOJ:6333-Problem B. Harvest of Apples(组合数学+莫队算法+逆元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333
解题心得:
- 这个题可以说是十分精彩了,首先推组合数学的公式,其中一个很重要的公式是Cnm = Cmn-1 + Cm-1n-1 这个公式知道杨辉三角的都明白,但是一看发现似乎没啥用。但是可以以这个公式为基础继续推演下去。
- 设Snm = Cn1 + Cn2 + Cn3 + ...... Cnm 然后继续使用上面的基本公式可以化成
- Sn m-1 = Sn m - Cn m
Sn m+1 = Sn m + Cn m+1
Sn+1 m = 2Sn m - Cn m
Sn m = (Sn+1 m + Cn m)/2 - 这些个公式看起来没啥用,但是知道S的关系之间可以通过O(1)的复杂度来进行转化,这样就可以将105次询问离线化处理,也就是用莫队来维护。神奇阿,想破脑袋就是想不到阿。里面的除法取余的操作用逆元来处理,逆元可以先全与预处理出来。
//
// ┏┛ ┻━━━━━┛ ┻┓
// ┃ ┃
// ┃ ━ ┃
// ┃ ┳┛ ┗┳ ┃
// ┃ ┃
// ┃ ┻ ┃
// ┃ ┃
// ┗━┓ ┏━━━┛
// ┃ ┃ 神兽保佑
// ┃ ┃ 代码无BUG!
// ┃ ┗━━━━━━━━━┓
// ┃ ┣┓
// ┃ ┏┛
// ┗━┓ ┓ ┏━━━┳ ┓ ┏━┛
// ┃ ┫ ┫ ┃ ┫ ┫
// ┗━┻━┛ ┗━┻━┛
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+;
const int MOD = 1e9+;
typedef long long ll; struct Query {
ll n, m, B, pos, ans;
}q[maxn]; ll unit, ans, fac[maxn], inv[maxn], t, rev2; ll quick_pow(ll a, ll b) {
ll res = ;
while(b) {
if(b&)
res = res * a % MOD;
a = a * a % MOD;
b >>= ;
}
return res;
} void get_fac() { //预处理出阶乘和逆元
fac[] = ;
fac[] =;
for(int i=;i<maxn;i++) {
fac[i] = i * fac[i-] % MOD;
} //神奇的操作
inv[maxn-] = quick_pow(fac[maxn-], MOD-);
for(int i=maxn-;i>=;i--) {
inv[i] = inv[i+] * (i + ) % MOD;
}
} bool cmp1(Query a, Query b) {
if(a.B == b.B)
return a.m < b.m;
return a.B < b.B;
} bool cmp2(Query a, Query b) {
return a.pos < b.pos;
} void init() {
unit = sqrt(maxn);
get_fac();
scanf("%lld", &t);
for(int i=;i<t;i++) {
scanf("%lld%lld", &q[i].n, &q[i].m);
q[i].B = q[i].n/unit;
q[i].pos = i;
}
rev2 = quick_pow(, MOD-);
sort(q, q+t, cmp1);
} ll C(ll n, ll m) {//得到c(n, m)的组合
ll ans = fac[n] * inv[n-m] % MOD * inv[m] % MOD;
return ans;
} void addL(ll l, ll r) {
ans = (( * ans % MOD) + MOD - C(l, r)) % MOD;
} void cutL(ll l, ll r) {
ans = (ans + C(l-, r) % MOD) * rev2 % MOD;
} void addR(ll l, ll r) {
ans = (ans + C(l, r+)) % MOD;
} void cutR(ll l, ll r) {
ans = (ans + MOD - C(l, r)) % MOD;
} int main() {
init();
ll l=, r = ;
ans = ;
for(int i=;i<t;i++) {//离线莫队处理
int L = q[i].n;
int R = q[i].m;
while(l < L) addL(l++, r);
while(l > L) cutL(l--, r);
while(r > R) cutR(l, r--);
while(r < R) addR(l, r++); q[i].ans = ans;
}
sort(q, q+t, cmp2);
for(int i=;i<t;i++) {
printf("%lld\n",q[i].ans);
}
return ;
}
HDOJ:6333-Problem B. Harvest of Apples(组合数学+莫队算法+逆元)的更多相关文章
- 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 ...
- 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多校第4场 B Harvest of Apples(莫队)
Problem B. Harvest of Apples Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...
- HDU - 6333 Problem B. Harvest of Apples (莫队+组合数学)
题意:计算C(n,0)到C(n,m)的和,T(T<=1e5)组数据. 分析:预处理出阶乘和其逆元.但如果每次O(m)累加,那么会超时. 定义 S(n, m) = sigma(C(n,m)).有公 ...
- 【HDOJ6333】Harvest of Apples(莫队)
题意: 给定T组询问,每组有两个数字n和m,求sigma i=0..m c(n,i) 答案对1e9+7取模 T<=1e5 1<=n,m<=1e5 思路: 注意要先变n再变m,否则会因 ...
- 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) 设 ...
- 【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples
http://acm.hdu.edu.cn/showproblem.php?pid=6333 莫队算法是一个离线区间分块瞎搞算法,只要满足:1.离线 2.可以O(1)从区间(L,R)更新到(L±1, ...
- BZOJ 3339: Rmq Problem 莫队算法
3339: Rmq Problem 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3339 Description n个数,m次询问l,r ...
随机推荐
- February 6 2017 Week 6 Monday
There are no shortcuts to any place worth going. 任何值得去的地方,都没有捷径. Several years ago, I climbed the Hu ...
- Entity Framework: 主从表的增删改
1.根据主表添加从表数据 var dest = (from d in context.Destinations where d.Name == "Bali" select d).S ...
- Entity FrameWork Code First 配置关系
Has方法与With方法 A.HasRequired(a => a.B).WithOptional(b => b.A);上面一句配置意思就是A类包含B类一个不为null的实例,B类包含A类 ...
- 51nod 1019 逆序数(逆序数+离散化)
在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数. 如2 4 3 1中,2 1,4 3,4 1,3 1是 ...
- PHP语言开发微信公众平台(订阅号)之curl命令(补充)
在之前的一篇随笔中,博主在调用curl命令上传文件时会经常出现上传方法过时的情况.如下图所示: 所以,我们只需要把上传方法换成创建CURLFile 类即可.如下所示 $ch = curl_init() ...
- 清除IE下input的叉叉
很多时候,我们在开发过程中,设计师会在输入框后加上定制的清除图标,但是在IE下有IE自带的清除图标,肯定是不美观的. <style> ::-ms-clear, ::-ms-reveal{d ...
- Multiply Strings 字符串相乘
http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...
- 用LinkedList list实现栈的功能
package homework; public class Dog extends Pet { String strain = "dogxx"; int love=80; pub ...
- mongo安装跟启动
mongo下载地址:http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.9.tgz
- type和 #define
1.#define INT8U unsigned char : 用INT8U代替unsigned char 2.typedef typedef int size; 此声明定义了一个int的同义字 ...