热身训练1 Problem B. Harvest of Apples
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的更多相关文章
- 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 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 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 ...
- 【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples
http://acm.hdu.edu.cn/showproblem.php?pid=6333 莫队算法是一个离线区间分块瞎搞算法,只要满足:1.离线 2.可以O(1)从区间(L,R)更新到(L±1, ...
- 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数值范围太 ...
- 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)).有公 ...
- HDU-6333 Problem B. Harvest of Apples 莫队
HDU-6333 题意: 有n个不同的苹果,你最多可以拿m个,问有多少种取法,多组数据,组数和n,m都是1e5,所以打表也打不了. 思路: 这道题要用到组合数的性质,记S(n,m)为从n中最多取m个的 ...
随机推荐
- 加载映射文件几种方式和mapper接口注解执行sql语句
一.加载映射文件几种方式 二.mapper接口注解执行sql语句 就将xml中的sql语句放到注解的括号中就可以,一般只用于简单的sql语句合适:
- linu命令进阶篇
预备知识: 本实验要求实验者具备如下的相关知识. 前面我们学习了linux的文件系统,了解的文件系统的结构,也学了linux档案的属性和权限,以及其设定. 当我们执行命令操作一个文件的时候,却不知道这 ...
- C语言学习笔记---2.C语言数据类型
1.C语言基本数据类型 1.1 int类型 int类型是有符号整型,即int类型的值必须是整数,可以是正整数.负整数或零.其取值范围依计算机系统而异.一般而言,储存一个int要占用一个机器字长. 声明 ...
- Sonarqube C# 配置资料整理
c#配置方式: http://www.cnblogs.com/CoderAyu/p/9416376.html http://www.cnblogs.com/jingridong/p/6513884.h ...
- mysql忘记root密码连接本地库
http://www.cnblogs.com/zf2011/archive/2012/03/13/2393387.html 今天想做个小项目,决定用mysql数据库,但是好久没用mysql了,也忘掉了 ...
- python win32com 处理excle 写一个公共的类
利用win32com处理excle,这样可以不管文件是老的xls还是新的xlsx格式,非常方便. 类的源码如下: import win32com.client as win32 class easyE ...
- java版gRPC实战之六:客户端动态获取服务端地址
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 机器学习——softmax回归
softmax回归 前面介绍了线性回归模型适用于输出为连续值的情景.在另一类情景中,模型输出可以是一个像图像类别这样的离散值.对于这样的离散值预测问题,我们可以使用诸如 softmax 回归在内的分类 ...
- mac php安装扩展 如 seoole apcu
//下载 --安装 --复制扩展文件到对应目录 wget https://pecl.php.net/get/apcu-5.1.7.tgz tar -zvcf pcu-5.1.7.tgz cd 到解压目 ...
- [转载]Samba 4实现windows匿名访问Linux共享!
SMB(Server Messages Block,信息服务块). 由于NFS(网络文件系统)可以很好的完成Linux与Linux之间的数据共享,因而 Samba较多的用在了Linux与windows ...