热身训练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基于角色的访问授权
详情访问官方文档 例如,以下代码将访问权限限制为属于角色成员的用户的任何操作 AdministrationController Administrator : [Authorize(Roles = & ...
- JS014. toFixed( )调试踩坑 - 浏览器思维 点常量 & 点运算符
Number.prototype.toFixed( ) 在观察toFixed()丢失精度问题,和对toFixed()方法重写的调试过程时,发现toFixed()对Number的识别有它自己的规则,并找 ...
- "指针"和"引用"大对比
相同点: 都能够直接引用对象,并对对象进行操作. 不同点: 指针 引用 指针类型的变量能够保存一个对象的地址 引用是一个对象的别名 可以为空nil,可以不初始化 不可以为空nil,必须初始化 当设计一 ...
- vue-cli 项目中使用 v-chart 及导出 chart 图片
安装: npm i v-charts echarts -S 组件中使用: 1 <template> 2 <div class="app-chart"> 3 ...
- 设置自启动nginx(适用于其他软件)(LinuxDeploy里的Ubuntu)
LinuxDeploy里的Ubuntu自启动nginx(适用于其他软件) 网上的教程是这样的,基本能用 1.编写脚本(这个文件及其内容安装Nginx后自动生成,没有的话内容自己Google) $ su ...
- jmeter性能实战
概述 性能测试: 通过并发工具请求服务器,提前发现性能问题,优化并解决 为什么做性能测试? 常规需求 用户反馈性能问题 项目对性能不放心 性能测试的最终目标:? 性能指标分析 多快好省 项目性能场景提 ...
- 鸿蒙内核源码分析(fork篇) | 一次调用,两次返回 | 百篇博客分析OpenHarmony源码 | v45.03
百篇博客系列篇.本篇为: v45.xx 鸿蒙内核源码分析(Fork篇) | 一次调用,两次返回 | 51.c.h .o 进程管理相关篇为: v02.xx 鸿蒙内核源码分析(进程管理篇) | 谁在管理内 ...
- docker run 参数
一.格式 docker run [OPTIONS] IMAGE [COMMAND] [ARG...] 二.OPTIONS 参数 简写, 名称参数 默认参数 描述 --add-host 添加自定义主机到 ...
- Python3入门系列之-----循环语句(for/while)
前言 for循环在Python中是用的比较多的一种循环方法,小伙伴需要熟练掌握它的使用 本章节将为大家介绍 Python 循环语句的使用.Python 中的循环语句有 for 和 while for循 ...
- scheduler源码分析——preempt抢占
前言 之前探讨scheduler的调度流程时,提及过preempt抢占机制,它发生在预选调度失败的时候,当时由于篇幅限制就没有展开细说. 回顾一下抢占流程的主要逻辑在DefaultPreemption ...