1. Problem B. Harvest of Apples
  2.  
  3. Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others)
  4. Total Submission(s): Accepted Submission(s):
  5.  
  6. Problem Description
  7. There are n apples on a tree, numbered from to n.
  8. Count the number of ways to pick at most m apples.
  9.  
  10. Input
  11. The first line of the input contains an integer T (≤T≤) denoting the number of test cases.
  12. Each test case consists of one line with two integers n,m (≤mn≤).
  13.  
  14. Output
  15. For each test case, print an integer representing the number of ways modulo +.
  16.  
  17. Sample Input
  18.  
  19. Sample Output
  20.  
  21. Source
  22. Multi-University Training Contest
  23.  
  24. Recommend
  25. 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)

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<cmath>
  4. #include<algorithm>
  5. using namespace std;
  6. const int mod=1e9+7;
  7. #define ll long long
  8. const int maxn=1e5+7;
  9. ll jiecheng[maxn],inv[maxn];
  10. ll ans[maxn];
  11. int block;
  12. ll qsm(ll a,ll b)
  13. {
  14. ll ans=1;
  15. while(b){
  16. if(b&1)
  17. ans=ans*a%mod;
  18. a=a*a%mod;
  19. b>>=1;
  20. }
  21. return ans;
  22. }
  23. void init()
  24. {
  25. jiecheng[1] = 1;
  26. for(int i = 2; i < maxn; i++)
  27. jiecheng[i] = jiecheng[i-1] * i % mod;
  28. for(int i = 1; i < maxn; i++)
  29. inv[i] = qsm(jiecheng[i], mod-2);
  30. }
  31. struct node{
  32. int l,r;
  33. int i;
  34. }modui[maxn];
  35. bool cmp(node a,node b)
  36. {
  37. if(a.l/block==b.l/block)
  38. return a.r<b.r;
  39. return a.l<b.l;
  40. }
  41. ll C(ll n,ll m)
  42. {
  43.  
  44. if(m == 0 || m == n) return 1;
  45. ll ans=1;
  46. ans=(jiecheng[n]*inv[m])%mod*inv[n-m];
  47. ans=ans%mod;
  48. return ans;
  49. }
  50. int main()
  51. {
  52. init();
  53. block = sqrt(maxn);
  54. int t;
  55. scanf("%d",&t);
  56. for(int i=0;i<t;i++)
  57. {
  58. scanf("%d%d",&modui[i].l,&modui[i].r);
  59. modui[i].i=i;
  60. }
  61. sort(modui,modui+t,cmp);
  62. int l=1,r=0;
  63. int sum=1;
  64. for(int i = 0; i < t; i++)
  65. {
  66. while(l < modui[i].l) sum = (2 * sum - C(l++, r) + mod) % mod;
  67. while(l > modui[i].l) sum = ((sum + C(--l, r))*inv[2]) % mod;
  68. while(r < modui[i].r) sum = (sum + C(l, ++r)) % mod;
  69. while(r > modui[i].r) sum = (sum - C(l, r--) + mod) % mod;
  70. ans[modui[i].i] = sum;
  71. }
  72. for(int i=0;i<t;i++)
  73. {
  74. printf("%lld\n",ans[i]);
  75. }
  76.  
  77. return 0;
  78. }

hdu多校第4场 B Harvest of Apples(莫队)的更多相关文章

  1. 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)).有公 ...

  2. 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 ...

  3. HDU-6333 Problem B. Harvest of Apples 莫队

    HDU-6333 题意: 有n个不同的苹果,你最多可以拿m个,问有多少种取法,多组数据,组数和n,m都是1e5,所以打表也打不了. 思路: 这道题要用到组合数的性质,记S(n,m)为从n中最多取m个的 ...

  4. 2018 HDU多校第四场赛后补题

    2018 HDU多校第四场赛后补题 自己学校出的毒瘤场..吃枣药丸 hdu中的题号是6332 - 6343. K. Expression in Memories 题意: 判断一个简化版的算术表达式是否 ...

  5. 2018 HDU多校第三场赛后补题

    2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...

  6. Harvest of Apples (HDU多校第四场 B) (HDU 6333 ) 莫队 + 组合数 + 逆元

    题意大致是有n个苹果,问你最多拿走m个苹果有多少种拿法.题目非常简单,就是求C(n,0)+...+C(n,m)的组合数的和,但是询问足足有1e5个,然后n,m都是1e5的范围,直接暴力的话肯定时间炸到 ...

  7. HDU 多校第四场题解

    对于 D 题的原题意,出题人和验题人赛前都没有发现标算存在的问题,导致了许多选手的疑惑和时间的浪费,在此表示真诚的歉意! 预计难度分布: Easy - DJKL, Medium - ABCEG, Ha ...

  8. 【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples

    http://acm.hdu.edu.cn/showproblem.php?pid=6333 莫队算法是一个离线区间分块瞎搞算法,只要满足:1.离线  2.可以O(1)从区间(L,R)更新到(L±1, ...

  9. HDU多校训练第一场 1012 Sequence

    题目链接:acm.hdu.edu.cn/showproblem.php?pid=6589 题意:给出一个长度为n的数组,有m次操作,操作有3种1,2,3,问操作m次后的数组,输出i*a[i]的异或和 ...

随机推荐

  1. [js]this关键字代表当前执行的主体

    点前是谁,this就是谁 <div id="div1" class="div1"></div> <div id="div ...

  2. 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 ...

  3. 今天整理了一下Winform用的UI插件信息

    平时主要用了一下几个比较好的UI控件: 1:IrisSkin2 皮肤插件.这是一款与编程开发相关的素材资源,主要是提供一些采用IrisSkin2.dll控件进行软件窗口换肤的素材文件,包括一些GIF图 ...

  4. SqlServer父节点与子节点查询及递归

    在最近老是用到这个SQL,所以记下来了: 1:创建表 CREATE TABLE [dbo].[BD_Booklet]( [ObjID] [int] IDENTITY(1,1) NOT NULL, [P ...

  5. delphi “div”、“mod”、“\”除法运算符的区别与使用方法(附带FORMAT使用方法)

    Delphi中和除法相关的算术运算符有: div.mod和符号“\” 下面分别对他们的作用.操作数类型和返回值类型进行一下介绍: div:对2个整数进行除,取商,操作数需是integer类型,返回值也 ...

  6. 0001-20180421-自动化第一章-python基础学习笔记

    ======================学习python==================介绍: python种类: cpython(*),jpython,ironpython,rubypyth ...

  7. winform中的dateTimePicker控件设置默认值为空

    winform中的dateTimePicker控件设置默认值为空   第一步:设置Format的属性值为“Custom” 第二步:设置CustomFormat的属性值为空,需要按一个空格键

  8. ANNOTATION 注解

    注解(Annotation)很重要,未来的开发模式都是基于注解的,JPA是基于注解的,Spring2.5以上都是基于注解的,Hibernate3.x以后也是基于注解的,现在的Struts2有一部分也是 ...

  9. js 星星效果思路

    //星星的效果思路 1.获取需要修改的元素 ul li 跟p 布局 2.给li 加移入事件 更改提示框显示, 3.给li 加移出事件 更改提示框隐藏 4.给li加索引值代表自己的序号 5.在li移入时 ...

  10. Android webview 调起H5微信支付

    mWebView.setWebViewClient(new MyWebViewClient()); private class MyWebViewClient extends WebViewClien ...