Harvest of Apples
问题 B: Harvest of Apples
时间限制: 1 Sec 内存限制: 128 MB
提交: 18 解决: 11
[提交] [状态] [讨论版] [命题人:admin]
题目描述
Count the number of ways to pick at most m apples.
输入
Each test case consists of one line with two integers n,m (1≤m≤n≤105).
输出
样例输入
2
5 2
1000 500
样例输出
16
924129523
莫队(分块),组合数学。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
const int mod=1e9+;
ll fac[maxn],inv[maxn],ans[maxn];
ll rev2,res;
int pos[maxn];
ll qpow(ll b,int n)
{
ll res=;
while(n)
{
if(n&) res=res*b%mod;
b=b*b%mod;
n>>=;
}
return res;
}
ll Comb(int n,int k)
{
return fac[n]*inv[k]%mod*inv[n-k]%mod;
}
void pre()
{
rev2=qpow(,mod-);
fac[]=fac[]=;
for(int i=; i<maxn; ++i) fac[i]=i*fac[i-]%mod;
inv[maxn-]=qpow(fac[maxn-],mod-);
for(int i=maxn-;i>=;i--) inv[i]=inv[i+]*(i+)%mod;
}
struct Query
{
int L,R,id;
bool operator <(const Query& p) const
{
if(pos[L]==pos[p.L]) return R<p.R;
return L<p.L;
}
} Q[maxn];
inline void addN(int posL,int posR)
{
res=(*res%mod-Comb(posL-,posR)+mod)%mod;
}
inline void addM(int posL,int posR)
{
res=(res+Comb(posL,posR))%mod;
}
inline void delN(int posL,int posR)
{
res=(res+Comb(posL-,posR))%mod*rev2%mod;
}
inline void delM(int posL,int posR)
{
res=(res-Comb(posL,posR)+mod)%mod;
}
int main()
{
int T,curL,curR;
int block=(int)sqrt(1.0*maxn);
pre();
scanf("%d",&T);
for(int i=;i<=T;++i)
{
scanf("%d %d",&Q[i].L,&Q[i].R);
pos[i]=i/block;
Q[i].id=i;
}
sort(Q+,Q+T+);
res=;
curL=,curR=;
for(int i=;i<=T;++i)
{
while(curL<Q[i].L) addN(++curL,curR);
while(curR<Q[i].R) addM(curL,++curR);
while(curL>Q[i].L) delN(curL--,curR);
while(curR>Q[i].R) delM(curL,curR--);
ans[Q[i].id] = res;
}
for(int i=;i<=T;++i) printf("%lld\n",ans[i]);
return ;
}
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 ...
- HDU 6333 Harvest of Apples (分块、数论)
题目连接:Harvest of Apples 题意:给出一个n和m,求C(0,n)+C(1,n)+.....+C(m,n).(样例组数为1e5) 题解:首先先把阶乘和逆元预处理出来,这样就可O(1)将 ...
- 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 ...
- 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 ...
- 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:Harvest of Apples (组合数前缀和&莫队)
There are n n apples on a tree, numbered from 1 1 to n n . Count the number of ways to pick at most ...
- 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 ...
随机推荐
- Linux 错误集锦
1. CentOS 7 运行yum时出现/var/run/yum.pid已被锁定,PID为xxxx的另一个程序正在运行的问题解决 解决办法: rm -f /var/run/yum.pid,删除文件后再 ...
- TCP/IP之大明邮差
大明王朝天启四年, 清晨. 天色刚蒙蒙亮,我就赶着装满货物的马车来到了南城门, 这里是集中处理货物的地方 , 一队一队的马车都来到这里, 城头的士兵带着头盔,身披盔甲, 手持长枪, 虎视眈眈的注视这下 ...
- ue4 2游戏构架相关
官网:游戏性指南 https://docs.unrealengine.com/latest/CHN/Gameplay/index.html 深入UE4剖析源码,浅出GameEngine设计理念,比较好 ...
- unite2017《Unity企业级支持案例与分析》
在今天举办的Unite2017开发者大会上,Unity大中华区技术支持总监张黎明以"Unity企业级支持案例与分析"为主题进行了分享. 以下为演讲实录: 张黎明:非常感谢大家来参加 ...
- IT兄弟连 Java语法教程 Java开发环境 配置Java环境变量
在安装完JDK后,我们需要配置系统的环境变量,否则在控制台中使用JDK为我们提供的工具时将出现如图12所示的错误信息. 图12 未配置环境变量时执行java命令提示的错误信息 当出现这样的提示信息时 ...
- bash快捷键光标移动到行首行尾等
ctrl键组合ctrl+a:光标移到行首.ctrl+b:光标左移一个字母ctrl+c:杀死当前进程.ctrl+d:退出当前 Shell.ctrl+e:光标移到行尾.ctrl+h:删除光标前一个字符,同 ...
- js 获取两个日期相差的天数--自定义方法
//获取两个日期的相差天数 datedifference=function(sDate1, sDate2) { var dateSpan, tempDate, iDays; sDate1 = Date ...
- 2019-CCPC广东省赛总结
2018年11月第一次参加ICPC区域赛青岛赛区,打铁了! 2019年5月第一次参加CCPC广东省赛,4题滚粗,C题莫队TLE13发,只拿了个铜牌! 教训总结: 比赛时千万不能犹豫,不能犹豫,不能犹豫 ...
- Hive进阶_汇总
=========================================================================== 第2章 Hive数据的导入 使用Load语句执行 ...
- Mysql和oracle字段类型与java对象类型对应表收藏
https://blog.csdn.net/michaelzhou224/article/details/16827029 Mysql Oracle Java BIGINT NUMBER(19,0) ...