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. AOP联盟通知类型和Spring编写代理半自动

    一.cglib功能更强大 二.Spring核心jar包 三.AOP联盟通知 三.代码实现Spring半自动代理 1.环绕通知的切面 2.bean.xml配置 3.创建bean容器,获取bean,即已经 ...

  2. java版gRPC实战之三:服务端流

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  3. Jenkins教程(七)实现 GitLab 提交/合并代码触发构建

    楔子 最近公司推行统一构建平台(基于 Jenkins + Kubernetes 插件创建 slave),原来部门自建的 Jenkins 不让用了. 迁移上统一构建平台的最大阻力是前端模块发布的问题: ...

  4. jsp页面动态获取系统时间

    最近在做练习时碰到了这样一个问题:"读者选择查询图书相应信息,跳转到书目的详细信息界面,当可借阅数量大于零,点击借阅按钮,提示用户借阅成功,并显示归还日期(三个月),否则提示用户该书可借阅数 ...

  5. PHP的Mcrypt加密扩展知识了解

    今天我们来学习的是 PHP 中的一个过时的扩展 Mcrypt .在 PHP7 之前,这个扩展是随 PHP 安装包一起内置发布的,但是现在新版本的 PHP 中已经没有了,需要使用这个扩展的话我们需要单独 ...

  6. 配置Orchard Core 最新的包资源

    添加预览包源 在本文中,我们将添加一个指向预览包的新包源. 与从主分支构建的NuGet上的代码相比,每次在dev分支上提交一些代码时都会构建预览包. 它们是最新的版本,但不是最稳定的,可以包含突破性的 ...

  7. 重新嫁接rm命令

    ### 重定义rm命令 #### 定义回收站目录trash_path='~/.trash'# 判断 $trash_path 定义的文件是否存在,如果不存在,那么就创建 $trash_path.if [ ...

  8. AT3945-[ARC092D]Two Faced Edges【dfs】

    正题 题目链接:https://www.luogu.com.cn/problem/AT3945 题目大意 \(n\)个点\(m\)条边的一张图,对于每条边求它翻转后强连通分量数量是否变化. \(1\l ...

  9. P4389-付公主的背包【生成函数,多项式exp】

    正题 题目链接:https://www.luogu.com.cn/problem/P4389 题目大意 \(n\)种物品,第\(i\)种大小为\(v_i\),数量无限.对于每个\(s\in[1,m]\ ...

  10. 数据库InnoDB和MyISAMYSQL的区别

    1.nnoDB支持事务,MyISAM不支持,这一点是非常之重要.事务是一种高级的处理方式,如在一些列增删改中只要哪个出错还可以回滚还原,而MyISAM就不可以了. 2.MyISAM适合查询以及插入为主 ...