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 ...
随机推荐
- Entity Framework: 主从表的增删改
1.根据主表添加从表数据 var dest = (from d in context.Destinations where d.Name == "Bali" select d).S ...
- LINQPad 编译调试C#代码的工具推荐
LinqPad介绍 学习C#代码的好帮手,很容易调试C#代码片段. LINQPad 4 支持.NET Framework 4.0 / 4.5 ,专业调试LINQ,lambda等特性,完全取代Snipp ...
- mongodb启动和关闭
mongodb的启动 mongod --dbpath=/data/mongodb/data --logpath=/data/mongodb/log/33988.log --port 33988 --f ...
- C#利用NPOI处理excel的类 NPOIHelper.cs
个人的NPOIHelp类,包括datatable导出到excel,dataset导出到excel,excel导入到datatable,excel导入到dataset, 更新excel中的数据,验证导入 ...
- 8.spring:事务管理(上):Spring的数据库编程、编程式事务管理
Spring的数据库编程 Spring框架提供了JDBC模板模式------>JdbcTemplate 简化了开发,在开发中并不经常是使用 实际开发更多使用的是Hibernate和MyBatis ...
- js 获取网页宽/高度
网页可见区域宽度: document.body.clientWidth 网页可见区域高度: document.body.clientHeight 网页可见区域宽度: document.body.off ...
- The App Life Cycle & The Main Function
The App Life Cycle Apps are a sophisticated interplay between your custom code and the system framew ...
- IDEA 相关问题
IntelliJ Idea 常用快捷键列表 Alt+回车 导入包,自动修正Ctrl+N 查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L 格式化代码 Ctrl+Alt+O 优化导 ...
- C# 对WinForm应用程序的App.config的加密
默认情况下,我们需要对App.config文件里的connectionStrings片断进行加密处理,ASP.NET IIS 注册工具 (Aspnet_regiis.exe)可以胜任这个工作,但这个工 ...
- 用cookie实现记住用户名和密码
1.当第一次发送请求时,在jsp页面并不能获取cookie对象,第一次是addCookie,之后再请求时才能获得. session和sessionid在服务器端生成的时候,同时把sessionID放在 ...