热身训练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个的 ...
随机推荐
- Spring-图解
- vue 工作随笔
现在工作要做一个电商项目,将工3作的笔记记在这儿,以后方便结总 本套项目用的前端方案 是: vue vue-router Element -ui Axios Echarts 后端技术采用node.js ...
- kubectl工具的windows\linux安装方法
kubectl 安装 下载kubectl二进制文件 curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.10.0 ...
- Java-枚举(Enum)
1.枚举概述 枚举是一个被命名的整型常数的集合,用于声明一组带标识符的常熟.当一个变量有几种固定可能的取值时,就可以将其定义为枚举类型. 1.1 声明枚举 Java中枚举是一个特殊的类,使用enum关 ...
- 搭建GIT仓库
- 【noip1998】题解:2的幂次方
思路:设递归函数dfs(x)用于输出x的幂次方 最容易的思路:0不输出,1输出为2(0),2输出2,剩下的递归执行. 每一次递归:例如7,拆分为4+3,先拆出最大的是2的次方的数出来,输出4,再把3分 ...
- java web 项目中web.xml 详解
web.xml详述: web.xml 是每个Java web 项目的必备文件,又叫做部署描述符,Servlet规范中定义的,是web应用的配置文件. 概念: .部署描述符文件就像所有XML文件一样,必 ...
- 环形链表II
题目描述: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 p ...
- CentOS7安装Docker遇到的问题笔记
笔记/朱季谦 以下是笔者本人学习搭建docker过程当中记录的一些实践笔记,过程当中也遇到了一些坑,但都解决了,就此记录,留作以后再次搭建时可以直接参考. 一.首先,先检查CentOS版本,保证在Ce ...
- Centos 7 设置 SFTP
近期要给服务器设置一个SFTP用户,可以上传删除修改的SFTP,但是禁止该用户SSH登录.这里记录下来 先升级 来源: https://blog.csdn.net/fenglailea/article ...