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 ...
随机推荐
- swfupload原理总结
1.引入js(js内动态添加上传的文件并提交表单) 2.后台处理(将图片保存) 3.调用另一个js修改前台图片的地址(改为新的图片地址)
- WebView根据加载的内容来控制其高度
一.先设置WebView的高度为0,然后在其加载结束后的代理方法中根据contentSize设置其高度 //初始话一个UIWebView: self.webView = [[[UIWebView al ...
- ncnn添加自己的layer
ncnn 是tencent公司开源的神经网络前向计算框架,github地址: https://github.com/Tencent/ncnn 通过简单的步骤可以添加自己的layer, 比如用位运算实现 ...
- 总结 Sublime Text 3 无法安装 Package Control 插件的解决办法
Sublime Text 是一款非常好用的轻便的编辑器,可以安装很多插件,实现IDE的很多功能,着实是程序员的利器. 我安装的 Sublime Text 3 Build 3143 ,软件汉化,软件激活 ...
- PJzhang:安全小课堂-安全软件为什么很重要,看这里!
猫宁!!! 参考链接: http://www.360.cn/webzhuanti/mianyigongju.html https://www.freebuf.com/fevents/204100.ht ...
- SublimeText3 snippet 编写总结
SublimeText3 snippet 编写总结 SublimeText的snippet定义功能也十分强大, 类似VAssist. 在菜单tool->New Snippet中定义. 打开后显 ...
- Linux上传下载工具FileZilla(GNU软件) 文件传输和配置文件修改
- day2逻辑运算作业详解
1.day2题目 1.判断下列逻辑语句的True,False. 1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 &l ...
- ES6入门教程---变量和常量
ES6提出了两个新的声明变量的命令:let 和 const 1. 建议不再使用var,而使用let 和const .优先使用const. 在定义之后值是固定不变的,即为常量 常量的值不能修改,但是如果 ...
- shell下批量除去文件名中的空格
rename 's/ /_/g' * 上述命令可以将当前文件夹内所有文件的名字中得所有空格替换为_.其中g代表所有,如果不加g,如果文件名字中有多个空格,仅替换第一个.