http://acm.hdu.edu.cn/showproblem.php?pid=6333

题意: 求 C(0,n)+C(1,n)+...+C(m,n)

分析:

这道题,我们令s(m,n) = C(0,n)+C(1,n)+...+C(m,n)

那么这道题就变成求各种s(m, n)

于是,莫队这个算法便可浮现在脑海里!

我们现在需要用O(1)的时间转移式子

s(m,n)=s(m-1,n)+C(m,n)

s(m,n)=s(m+1,n)-C(m+1,n)

S(m,n)=2*s(m,n-1)-C(m,n-1)  ps:这个推导的方法,可以从“杨辉三角”中,轻松看出

S(m,n)=(s(m,n+1)+C(m,n))/2

ok,这道题AC了

接下来便是莫队板子了!

#include<bits/stdc++.h>
using namespace std;
#define re register int
#define LL long long
#define int long long
const int N=1e5+5;
const LL mo=1e9+7;
int blo[N]; LL fac[N], inv[N], iv[N];
struct node{int a, b, id;}ask[N];
bool cmp(const node&x, const node&y)
{
if(blo[x.a] == blo[y.a]) return x.b < y.b;
return blo[x.a] < blo[y.a];
}
inline void init()
{
fac[0] = fac[1] = iv[1] = inv[1] = inv[0] = 1ll;
for(re i=2, sq=sqrt(100000);i<=100000;++i)
{
iv[i] = mo - mo / i * iv[mo%i] % mo;
inv[i] = inv[i-1] * iv[i] % mo;
fac[i] = fac[i-1] * i % mo;
blo[i] = (i-1) / sq + 1;
}
}
inline LL getc(const int x, const int y)
{
if(x > y) return 0;
return fac[y] * inv[x] % mo * inv[y-x] % mo;
}
int lt, rt; LL Tot;
inline void Del1()
{
Tot = ((Tot - getc(lt, rt)) % mo + mo) % mo;
lt --;
}
inline void Add1()
{
lt ++;
Tot = ((Tot + getc(lt, rt)) % mo + mo) % mo;
}
inline void Del2()
{
rt --;
Tot = ((Tot + getc(lt, rt)) % mo * iv[2]) % mo;
}
inline void Add2()
{
Tot = ((2 * Tot % mo - getc(lt, rt)) % mo + mo) % mo;
rt ++;
}
LL ans[N];
signed main()
{
init();
int m;
scanf("%lld",&m); for(re i=1;i<=m;++i)
{
scanf("%lld%lld",&ask[i].b,&ask[i].a);
ask[i].id = i;
}
sort(ask+1, ask+1+m, cmp);
lt=0; rt=0; Tot=1;
for(re i=1;i<=m;++i)
{
while(rt < ask[i].b) Add2();
while(lt > ask[i].a) Del1();
while(rt > ask[i].b) Del2();
while(lt < ask[i].a) Add1();
ans[ask[i].id] = Tot;
}
for(re i=1;i<=m;++i) printf("%lld\n", ans[i]);
}

热身训练1 Problem B. Harvest of Apples的更多相关文章

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

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

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

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

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

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

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

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

  8. 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)).有公 ...

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

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

随机推荐

  1. Sonarqube C# 配置资料整理

    c#配置方式: http://www.cnblogs.com/CoderAyu/p/9416376.html http://www.cnblogs.com/jingridong/p/6513884.h ...

  2. 关联数组VS索引数组

    关联数组和常规说的数组类似,它包含标量抄数据,可用索引值来单独选择这些数据,和常规数组不同的是, 关联数组的索引值不是非负的整数而是任意的标量袭.这些标量称为百Keys,可以在以后用于检索数组中的数值 ...

  3. 【转】shell脚本中echo显示内容带颜色的实现方法

    shell脚本中echo显示内容带颜色的实现方法 shell脚本里使用echo输出颜色  

  4. Python增强下git那长长的指令

    场景 现如今有点规模的公司都使用GitFlow模式进行分支管理.虽然插件给我们带来了非常大的方便,但切换分支.找分支.起分支还是那么的麻烦 需求 在社会主次国家,每个生活在底层的劳动人民,他们默默的工 ...

  5. 树莓派修改默认pi帐号亲测有效

    # 树莓派修改默认pi帐号亲测有效### 1.我的树莓派机型:3B+,系统:Raspbian桌面标准版,连接的屏幕:电视机..###2.打开树莓派LX终端,快捷键:Ctrl+Alt+t ###3.输入 ...

  6. TP6出现错误 No input file specified.

    解决办法是打开public下的.htaccess文件, 把:RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] 改为:RewriteRule ^(.*)$ index ...

  7. dede调用文章内第一张原始图片(非缩略图)的实现方法

    第一步,修改include/extend.func.php文件,最下面插入函数,查询的是文章附加表,如需查询图片集什么的,改表名即可 //取原图地址 function GetFirstImg($arc ...

  8. Docker系列(10)- 常用命令小结

    #橙色前面笔记已记录,黑色后面笔记将完善#勤加练习!!!attach Attach to a running container # 当前 shell 下 attach 连接指定运行镜像 build ...

  9. find_elements与find_element的区别

    find_element不能使用len,find_elements可以使用len获取元素数量,判断页面有无某个元素,这个方法可以用来断言. 如添加用户后,判断是否添加成功. 删除用户后,判断是否删除成 ...

  10. docker efk(filebeat+logstash+es+kibana)

    ​ 1.系统架构 ​ 通常我们说的elastic stack,也就是elk,通过es 收集日志数据,存到elasticsearch,最后通过kibana进行统计分析,但是elastic公司后续又推出了 ...