热身训练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个的 ...
随机推荐
- Identity角色管理三(编辑角色)
因只有角色名能修改故继续使用创建角色的视图模型 using System.ComponentModel; using System.ComponentModel.DataAnnotations; na ...
- Vue设置全局js/css样式
''' 配置全局js mian.js: import settings from '@/assets/js/settings' Vue.prototype.$settings = settings; ...
- Java单例-双重检查锁
问题引入 Java中实现单例模式,一般性的做法是如下方式: class Singleton { private static Singleton INSTANCE = null; private Si ...
- elasticsearch入门到放弃之elasticsearch-head
elasticsearch-head可理解为跟DBeaver一样是一个数据可视化工具,但是这个工具并没有理想中那么好用坑也是很多,我已经在我的github上fork了一份修改后的版本:https:// ...
- Java Web下MySQL数据库的增删改查(一)
以图书管理系统举例(jsp+servlet+bean) 1.数据库的连接 package db; import java.sql.Connection; import java.sql.DriverM ...
- 利用 Nginx 搭建小型的文件服务器
利用 Nginx 搭建小型的文件服务器 1.查看 Nginx 配置 android@localhost:/etc/nginx/conf.d$ nginx -hnginx version: nginx/ ...
- find_elements与find_element的区别
find_element不能使用len,find_elements可以使用len获取元素数量,判断页面有无某个元素,这个方法可以用来断言. 如添加用户后,判断是否添加成功. 删除用户后,判断是否删除成 ...
- Appium调试分析方法
在使用appium做自动化测试的时候,发现用例报错,如何排查原因? 查看appium日志 appium日志大概是分为以下部分 culr命令调试 在理解appium协议的基础上,可以直接用shell发送 ...
- GDOI 2021 退役记
Day -n 时常想自己不学OI会怎样,经常畏惧自己其实没有心里想的那样有能力,去机房来麻痹自己 从 3.21 始加大频率刷题,复习以前都学会,而现在都被抛在脑后的算法 反正都要退役了,成绩也得鲜亮点 ...
- 智汀家庭云-开发指南Golang: 插件模块
插件模块 当前所说的插件仅指设备类插件,插件为SA提供额外的设备发现和控制功能: 插件通过实现定义的grpc接口,以grpc服务的形式运行,提供接口给SA调用 插件同时需要http服务提供h5页面及静 ...