题意:计算C(n,0)到C(n,m)的和,T(T<=1e5)组数据。

分析:预处理出阶乘和其逆元。但如果每次O(m)累加,那么会超时。

定义 S(n, m) = sigma(C(n,m))。有公式:S(n,m) = S(n,m-1) +C(n,m)以及S(n,m) = 2*S(n-1,m) - C(n-1,m)。

这样就可以在O(1)的时间中计算出S(n+1,m),S(n-1,m),S(n,m+1),S(n,m+1)。可以用莫队离线处理T组查询。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn =1e5+;
const int mod = 1e9+;
LL fac[maxn],inv[maxn];
LL rev2;
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;
} int pos[maxn]; 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];
LL res;
LL ans[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()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T,N,M,u,v,tmp,K;
int a,b;
pre();
int block = (int)sqrt(1.0*maxn);
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=;
int curL=,curR=;
for(int i=;i<=T;++i){
while(curL<Q[i].L) addN(++curL,curR); //ok
while(curR<Q[i].R) addM(curL,++curR); //ok
while(curL>Q[i].L) delN(curL--,curR); //ok
while(curR>Q[i].R) delM(curL,curR--); //ok
ans[Q[i].id] = res;
}
for(int i=;i<=T;++i) printf("%lld\n",ans[i]);
return ;
}

HDU - 6333 Problem B. Harvest of Apples (莫队+组合数学)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. HDU-6333 Problem B. Harvest of Apples 莫队

    HDU-6333 题意: 有n个不同的苹果,你最多可以拿m个,问有多少种取法,多组数据,组数和n,m都是1e5,所以打表也打不了. 思路: 这道题要用到组合数的性质,记S(n,m)为从n中最多取m个的 ...

  4. 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 题意很好懂,就是组合数求和. 官方题解: 我来叨叨 ...

  5. 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 ...

  6. 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)    设 ...

  7. hdu6333 Harvest of Apples 离线+分块+组合数学(求组合数模板)

    Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K ...

  8. 【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples

    http://acm.hdu.edu.cn/showproblem.php?pid=6333 莫队算法是一个离线区间分块瞎搞算法,只要满足:1.离线  2.可以O(1)从区间(L,R)更新到(L±1, ...

  9. 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数值范围太 ...

随机推荐

  1. Java释出的时候,AWT作为Java最弱的组件受到不小的批评

    Java释出的时候,AWT作为Java最弱的组件受到不小的批评. 最根本的缺点是AWT在原生的用户界面之上仅提供了一个非常薄的抽象层. 例如,生成一个AWT的 复选框会导致AWT直接调用下层原生例程来 ...

  2. PHP 程序员面试常问的问题

    1. Include 与 require的区别,require和require_once的效率哪个高? Php在遇到include时就解释一次,如果页面中出现10次include,php就解释10次, ...

  3. python 国内镜像

    pipy国内镜像目前有: http://pypi.douban.com/  豆瓣 http://pypi.hustunique.com/  华中理工大学 http://pypi.sdutlinux.o ...

  4. 使用Beautifulsoup去除特定标签

    使用Beautifulsoup去除特定标签 试用了Beautifulsoup,的确是个神器. 在抓取到网页时,会出现很多不想要的内容,例如<script>标签,利用beautifulsou ...

  5. linux 安装php 然后跟nginx整合

    php-fpm安装配置 nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端. nginx一般是把请求发fastcgi管理 ...

  6. [LintCode] O(1)检测2的幂次

    class Solution { public: /* * @param n: An integer * @return: True or false */ bool checkPowerOf2(in ...

  7. Cocos2d-x学习笔记:CCSprite的使用总结

    一.精灵创建及初始化 备注:默认锚点ccp(0.5,0.5),默认位置 ccp(0,0),contentSize为精灵图片尺寸 1.从图片文件创建 2.从帧缓存创建: 3.初始化及自定义大小 4. 从 ...

  8. Jmeter--正则表达式提取值

    博客首页:http://www.cnblogs.com/fqfanqi/ 设置界面如下: Apply to:应用范围的选择: Field to check:检查的领域,即需要提取内容的地方. 下面是各 ...

  9. HDU 1796 How many integers can you find(容斥原理)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  10. POJ 1423 Greatest Common Increasing Subsequence【裸LCIS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1423 http://acm.hust.edu.cn/vjudge/contest/view.action ...