题意:计算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. WebAPI发布IIS报错问题

    1.看IIS中处理程序映射中有没有注册:ExtensionlessUrlHandler-Integrated-4.0 没有的话需要在[运行]中注册:aspnet_regiis.exe 2.配置文件中要 ...

  2. Hive数据类型与文件存储格式

    Hive数据类型 基础数据类型: TINYINT,SMALLINT,INT,BIGINT,BOOLEAN,FLOAT,DOUBLE,STRING,BINARY,TIMESTAMP,DECIMAL,CH ...

  3. 《linux系统及其编程》实验课记录(二)

    实验 2:获取对使用命令的帮助 实验环境: 安装了 Red Hat Enterprise Linux 6.0 可运行系统,并且是成功验证系统.有另外一个无特权用户 student,密码 student ...

  4. ChemDraw Pro和ChemBio3D Ultra有什么区别

    作为ChemOffice 14的两个重要组件,ChemDraw Pro和ChemBio3D Ultra都有着自己的特点,ChemDraw Pro以绘制平面结构而ChemBio3D Ultra以绘制立体 ...

  5. 图像增强:直方图均衡和小波变换【matlab】

    直方图均衡:统计图像像素灰度的直方图分布.对其进行重新分配以使图像的直方图分布更加均衡. 小波变换:图像轮廓主要体现在低频部分,可以通过对低频分解系数进行增强处理,对高频分解系数进行衰减处理,达到图像 ...

  6. iOS开发之--蓝牙开发实战

    转载自:http://www.cnblogs.com/zyjzyj/p/6029968.html ,感谢英杰 前言 最近一直在开发关于蓝牙的功能,本来是不想写这一篇文章,因为网上关于ios蓝牙开发的文 ...

  7. 红黑树C++实现

    1 /* 2 * rbtree.h 3 * 1. 每个节点是红色或者黑色 4 * 2. 根节点是黑色 5 * 3. 每个叶子节点是黑色(该叶子节点就空的节点) 6 * 4. 如果一个节点是红色,则它的 ...

  8. Android 触摸及手势操作GestureDetector

    现在的智能手机不敢说百分百的都是触摸屏,也应该是百分之九九以上为触摸屏了,触摸屏为我们操作无键盘.无鼠标的手机系统带来了很多的便利.当用户触摸屏幕时会产生很多的触摸事件,down.up.move等等. ...

  9. delphi 快捷键的使用

    CTRL+SPACE 代码补全,很好用的(先改了输入法热键)CTRL+SHIFT+C 编写申明或者补上函数CTRL+SHIFT+↑(↓) 在过程.函数.事件内部, 可跳跃到相应的过程.函数.事件的定义 ...

  10. ActiveMQ+SpringMVC+Maven应用示例

    本文主要想通过一个WEB示例展示ActiveMQ在具体应用程序中是如何使用的,顺便简单介绍以下如何通过Eclipse搭建一个SpringMVC框架和Maven项目开发框架.下面首先展示一下项目最终的运 ...