hdu多校第4场 B Harvest of Apples(莫队)
- Problem B. Harvest of Apples
- Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others)
- Total Submission(s): Accepted Submission(s):
- Problem Description
- There are n apples on a tree, numbered from to n.
- Count the number of ways to pick at most m apples.
- Input
- The first line of the input contains an integer T (≤T≤) denoting the number of test cases.
- Each test case consists of one line with two integers n,m (≤m≤n≤).
- Output
- For each test case, print an integer representing the number of ways modulo +.
- Sample Input
- Sample Output
- Source
- Multi-University Training Contest
- Recommend
- chendu | We have carefully selected several similar problems for you:
求C(n,0)+C(n,1)+C(n,2)+.....+C(n,m);
设S(n,m)=C(n,0)+C(n,1)+C(n,2)+.....+C(n,m);
第一个式子易得,第二个式子:杨辉三角的 n,m=(n-1,m)+(n-1,m-1)
那么就是这一行等于上一行的都用了2次,只有第最后一个用了一次
所以减去c(n-1,m)
- #include<iostream>
- #include<stdio.h>
- #include<cmath>
- #include<algorithm>
- using namespace std;
- const int mod=1e9+7;
- #define ll long long
- const int maxn=1e5+7;
- ll jiecheng[maxn],inv[maxn];
- ll ans[maxn];
- int block;
- ll qsm(ll a,ll b)
- {
- ll ans=1;
- while(b){
- if(b&1)
- ans=ans*a%mod;
- a=a*a%mod;
- b>>=1;
- }
- return ans;
- }
- void init()
- {
- jiecheng[1] = 1;
- for(int i = 2; i < maxn; i++)
- jiecheng[i] = jiecheng[i-1] * i % mod;
- for(int i = 1; i < maxn; i++)
- inv[i] = qsm(jiecheng[i], mod-2);
- }
- struct node{
- int l,r;
- int i;
- }modui[maxn];
- bool cmp(node a,node b)
- {
- if(a.l/block==b.l/block)
- return a.r<b.r;
- return a.l<b.l;
- }
- ll C(ll n,ll m)
- {
- if(m == 0 || m == n) return 1;
- ll ans=1;
- ans=(jiecheng[n]*inv[m])%mod*inv[n-m];
- ans=ans%mod;
- return ans;
- }
- int main()
- {
- init();
- block = sqrt(maxn);
- int t;
- scanf("%d",&t);
- for(int i=0;i<t;i++)
- {
- scanf("%d%d",&modui[i].l,&modui[i].r);
- modui[i].i=i;
- }
- sort(modui,modui+t,cmp);
- int l=1,r=0;
- int sum=1;
- for(int i = 0; i < t; i++)
- {
- while(l < modui[i].l) sum = (2 * sum - C(l++, r) + mod) % mod;
- while(l > modui[i].l) sum = ((sum + C(--l, r))*inv[2]) % mod;
- while(r < modui[i].r) sum = (sum + C(l, ++r)) % mod;
- while(r > modui[i].r) sum = (sum - C(l, r--) + mod) % mod;
- ans[modui[i].i] = sum;
- }
- for(int i=0;i<t;i++)
- {
- printf("%lld\n",ans[i]);
- }
- return 0;
- }
hdu多校第4场 B Harvest of Apples(莫队)的更多相关文章
- 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)).有公 ...
- 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 莫队
HDU-6333 题意: 有n个不同的苹果,你最多可以拿m个,问有多少种取法,多组数据,组数和n,m都是1e5,所以打表也打不了. 思路: 这道题要用到组合数的性质,记S(n,m)为从n中最多取m个的 ...
- 2018 HDU多校第四场赛后补题
2018 HDU多校第四场赛后补题 自己学校出的毒瘤场..吃枣药丸 hdu中的题号是6332 - 6343. K. Expression in Memories 题意: 判断一个简化版的算术表达式是否 ...
- 2018 HDU多校第三场赛后补题
2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...
- Harvest of Apples (HDU多校第四场 B) (HDU 6333 ) 莫队 + 组合数 + 逆元
题意大致是有n个苹果,问你最多拿走m个苹果有多少种拿法.题目非常简单,就是求C(n,0)+...+C(n,m)的组合数的和,但是询问足足有1e5个,然后n,m都是1e5的范围,直接暴力的话肯定时间炸到 ...
- HDU 多校第四场题解
对于 D 题的原题意,出题人和验题人赛前都没有发现标算存在的问题,导致了许多选手的疑惑和时间的浪费,在此表示真诚的歉意! 预计难度分布: Easy - DJKL, Medium - ABCEG, Ha ...
- 【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples
http://acm.hdu.edu.cn/showproblem.php?pid=6333 莫队算法是一个离线区间分块瞎搞算法,只要满足:1.离线 2.可以O(1)从区间(L,R)更新到(L±1, ...
- HDU多校训练第一场 1012 Sequence
题目链接:acm.hdu.edu.cn/showproblem.php?pid=6589 题意:给出一个长度为n的数组,有m次操作,操作有3种1,2,3,问操作m次后的数组,输出i*a[i]的异或和 ...
随机推荐
- [js]this关键字代表当前执行的主体
点前是谁,this就是谁 <div id="div1" class="div1"></div> <div id="div ...
- mysql----------mysql5.7.11导入sql文件时报错This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled
1.导入sql文件出现如下错误. [Err] 1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in ...
- 今天整理了一下Winform用的UI插件信息
平时主要用了一下几个比较好的UI控件: 1:IrisSkin2 皮肤插件.这是一款与编程开发相关的素材资源,主要是提供一些采用IrisSkin2.dll控件进行软件窗口换肤的素材文件,包括一些GIF图 ...
- SqlServer父节点与子节点查询及递归
在最近老是用到这个SQL,所以记下来了: 1:创建表 CREATE TABLE [dbo].[BD_Booklet]( [ObjID] [int] IDENTITY(1,1) NOT NULL, [P ...
- delphi “div”、“mod”、“\”除法运算符的区别与使用方法(附带FORMAT使用方法)
Delphi中和除法相关的算术运算符有: div.mod和符号“\” 下面分别对他们的作用.操作数类型和返回值类型进行一下介绍: div:对2个整数进行除,取商,操作数需是integer类型,返回值也 ...
- 0001-20180421-自动化第一章-python基础学习笔记
======================学习python==================介绍: python种类: cpython(*),jpython,ironpython,rubypyth ...
- winform中的dateTimePicker控件设置默认值为空
winform中的dateTimePicker控件设置默认值为空 第一步:设置Format的属性值为“Custom” 第二步:设置CustomFormat的属性值为空,需要按一个空格键
- ANNOTATION 注解
注解(Annotation)很重要,未来的开发模式都是基于注解的,JPA是基于注解的,Spring2.5以上都是基于注解的,Hibernate3.x以后也是基于注解的,现在的Struts2有一部分也是 ...
- js 星星效果思路
//星星的效果思路 1.获取需要修改的元素 ul li 跟p 布局 2.给li 加移入事件 更改提示框显示, 3.给li 加移出事件 更改提示框隐藏 4.给li加索引值代表自己的序号 5.在li移入时 ...
- Android webview 调起H5微信支付
mWebView.setWebViewClient(new MyWebViewClient()); private class MyWebViewClient extends WebViewClien ...