Problem Description
There are n apples on a tree, numbered from 1 to n.
Count the number of ways to pick at most m apples.
 
Input
The first line of the input contains an integer T (1≤T≤105) denoting the number of test cases.
Each test case consists of one line with two integers n,m (1≤m≤n≤105).
 
Output
For each test case, print an integer representing the number of ways modulo 109+7.
 
Sample Input
2
5 2
1000 500
 
Sample Output
16
924129523
 
Source
 
Recommend
 
 
这题刚写的时候 公式及其的容易推 C(n,0)+C(n,1)+C(n,2)+。。。+C(n,m)
然后一直在想能不能化简这一项一项的求 复杂度会爆炸的
 
最后的题解是莫队
C(n,m)=C(n-1,m-1)+C(n-1,m)   
设 S(n,m)=C(n,0)+C(n,1)+C(n,2)+。。。+C(n,m)
然后将S(n,m) 通过 第一个公式 拆项
最后化简 变为 S(n,m)=2*S(n-1,m)-C(n-1,m);
 
预处理阶乘逆元  然后就 OK了 
莫队大法好
 
 
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <set>
  7. #include <iostream>
  8. #include <map>
  9. #include <stack>
  10. #include <string>
  11. #include <vector>
  12. #define pi acos(-1.0)
  13. #define eps 1e-6
  14. #define fi first
  15. #define se second
  16. #define lson l,m,rt<<1
  17. #define rson m+1,r,rt<<1|1
  18. #define bug printf("******\n")
  19. #define mem(a,b) memset(a,b,sizeof(a))
  20. #define fuck(x) cout<<"["<<x<<"]"<<endl
  21. #define f(a) a*a
  22. #define sf(n) scanf("%d", &n)
  23. #define sff(a,b) scanf("%d %d", &a, &b)
  24. #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
  25. #define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
  26. #define pf printf
  27. #define FRE(i,a,b) for(i = a; i <= b; i++)
  28. #define FREE(i,a,b) for(i = a; i >= b; i--)
  29. #define FRL(i,a,b) for(i = a; i < b; i++)
  30. #define FRLL(i,a,b) for(i = a; i > b; i--)
  31. #define FIN freopen("DATA.txt","r",stdin)
  32. #define gcd(a,b) __gcd(a,b)
  33. #define lowbit(x) x&-x
  34. #pragma comment (linker,"/STACK:102400000,102400000")
  35. using namespace std;
  36. typedef long long LL;
  37. typedef unsigned long long ULL;
  38. const int INF = 0x7fffffff;
  39. const int mod = 1e9 + ;
  40. const int maxn = 1e5 + ;
  41. int t, sz;
  42. LL inv[maxn], a[maxn], b[maxn];
  43. struct node {
  44. int l, r, id;
  45. LL ans = ;
  46. } qu[maxn];
  47. int cmp(node a, node b) {
  48. return a.l / sz == b.l / sz ? a.r < b.r : a.l < b.l;
  49. }
  50. LL expmod(LL a, LL b) {
  51. LL ans = ;
  52. while(b) {
  53. if (b & ) ans = ans * a % mod;
  54. a = a * a % mod;
  55. b = b >> ;
  56. }
  57. return ans;
  58. }
  59. void init() {
  60. a[] = ;
  61. for (int i = ; i < maxn ; i++) a[i] = a[i - ] * i % mod;
  62. for (int i = ; i < maxn ; i++) b[i] = expmod(a[i], mod - );
  63. }
  64. LL C(int n, int m) {
  65. if (m > n || n < || m < ) return ;
  66. if (m == n || m == ) return ;
  67. return a[n] * b[m] % mod * b[n - m] % mod;
  68. }
  69.  
  70. int main() {
  71. init();
  72. sf(t);
  73. for (int i = ; i <= t ; i++) {
  74. sff(qu[i].l, qu[i].r);
  75. qu[i].id = i, qu[i].ans = ;
  76. }
  77. sz = sqrt(maxn);
  78. sort(qu + , qu + + t, cmp);
  79. LL sum = ;
  80. for (int i = , L = , R = ; i <= t ; i++) {
  81. while(L < qu[i].l) sum = ( * sum - C(L++, R) + mod) % mod;
  82. while(L > qu[i].l) sum = ((sum + C(--L, R)) * b[]) % mod;
  83. while(R < qu[i].r) sum = (sum + C(L, ++R)) % mod;
  84. while(R > qu[i].r) sum = (sum - C(L, R--) + mod) % mod;
  85. qu[qu[i].id].ans = sum;
  86. }
  87. for (int i = ; i <= t ; i++) printf("%lld\n", qu[i].ans);
  88. return ;
  89. }
 

Problem B. Harvest of Apples 莫队求组合数前缀和的更多相关文章

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

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

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

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

  4. 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)    设 ...

  5. 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数值范围太 ...

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

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

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

  8. 热身训练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( ...

  9. codeforce617E-XOR and Favorite Number莫队+异或前缀和

    传送门:http://codeforces.com/contest/617/problem/E 参考:https://blog.csdn.net/keyboarderqq/article/detail ...

随机推荐

  1. CSS3自定义字体

    原文摘自:https://www.cnblogs.com/moqiutao/archive/2015/12/23/5070463.html 总节: 1) 定义字体标准格式: @font-face { ...

  2. react项目总结

    1.基本框架 1.react+react-router4+redux3.7.2 2.css预编译使用sass 3.数据请求使用axios(原本是使用fetch,结果在ios10下报错) 4.ui组件库 ...

  3. python中的迭代器与生成器

    迭代器 迭代器的引入 假如我现在有一个列表l=['a','b','c','d','e'],我想取列表中的内容,那么有几种方式? 1.通过索引取值 ,如了l[0],l[1] 2.通过for循环取值 fo ...

  4. js如何判断客户端是iOS还是Android等移动终端

    判断原理:JavaScript是前端开发的主要语言,我们可以通过编写JavaScript程序来判断浏览器的类型及版本.JavaScript判断浏览器类型一般有两种办法,一种是根据各种浏览器独有的属性来 ...

  5. 1 wait notify

    wait/notify: wait()使线程停止,notify使wait状态的线程继续执行. wait()是Object类的方法,该方法用来将线程置入“预执行队列”,并在wait()方法处停止执行,直 ...

  6. CPU设计学习-流水线

    各种名词 标量流水线 超级流水线 超标量流水线与多发射技术 经典五级流水线 IF |Instruction Fetch,取指 ID |Instruction Decode,译码 EX |Execute ...

  7. day-17 L1和L2正则化的tensorflow示例

    机器学习中几乎都可以看到损失函数后面会添加一个额外项,常用的额外项一般有两种,一般英文称作ℓ1-norm和ℓ2-norm,中文称作L1正则化和L2正则化,或者L1范数和L2范数.L2范数也被称为权重衰 ...

  8. HTMLTestRunner解决UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128)

    其中HTML和数据库都是设置成utf-8格式编码,插入到数据库中是正确的,但是当读取出来的时候就会出错,原因就是python的str默认是ascii编码,和unicode编码冲突,就会报这个标题错误. ...

  9. Java常用类之Math类

    Java 的常用类Math类: java.lang.Math 提供了系列的静态方法用于科学计算,其方法的参数和返回值类型一般为 double 类型. 如: 1. public static final ...

  10. ubuntu 安装lua错误

    转自:http://www.cnblogs.com/softidea/archive/2016/03/02/5236498.html lua.c:80:31: fatal error: readlin ...