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了 
莫队大法好
 
 
 #include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("DATA.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x7fffffff;
const int mod = 1e9 + ;
const int maxn = 1e5 + ;
int t, sz;
LL inv[maxn], a[maxn], b[maxn];
struct node {
int l, r, id;
LL ans = ;
} qu[maxn];
int cmp(node a, node b) {
return a.l / sz == b.l / sz ? a.r < b.r : a.l < b.l;
}
LL expmod(LL a, LL b) {
LL ans = ;
while(b) {
if (b & ) ans = ans * a % mod;
a = a * a % mod;
b = b >> ;
}
return ans;
}
void init() {
a[] = ;
for (int i = ; i < maxn ; i++) a[i] = a[i - ] * i % mod;
for (int i = ; i < maxn ; i++) b[i] = expmod(a[i], mod - );
}
LL C(int n, int m) {
if (m > n || n < || m < ) return ;
if (m == n || m == ) return ;
return a[n] * b[m] % mod * b[n - m] % mod;
} int main() {
init();
sf(t);
for (int i = ; i <= t ; i++) {
sff(qu[i].l, qu[i].r);
qu[i].id = i, qu[i].ans = ;
}
sz = sqrt(maxn);
sort(qu + , qu + + t, cmp);
LL sum = ;
for (int i = , L = , R = ; i <= t ; i++) {
while(L < qu[i].l) sum = ( * sum - C(L++, R) + mod) % mod;
while(L > qu[i].l) sum = ((sum + C(--L, R)) * b[]) % mod;
while(R < qu[i].r) sum = (sum + C(L, ++R)) % mod;
while(R > qu[i].r) sum = (sum - C(L, R--) + mod) % mod;
qu[qu[i].id].ans = sum;
}
for (int i = ; i <= t ; i++) printf("%lld\n", qu[i].ans);
return ;
}
 

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. Java异常层次结构

    1. 如果是不可查异常(unchecked exception),即Error.RuntimeException或它们的子类,那么可以不使用throws关键字来声明要抛出的异常,编译仍能顺利通过,但在 ...

  2. spark写入ES(动态模板)

    使用es-hadoop插件,主要使用elasticsearch-spark-20_2.11-6.2.x.jar 官网:https://www.elastic.co/guide/en/elasticse ...

  3. [Clr via C#读书笔记]Cp1CLR执行模型

    Cp1CLR执行模型 本章的概念点 CLR=Common Language Runtime 内存管理,程序集加载,安全性,异常处理和线程同步.CLR是基础,支持着面向它的各种语言.各种语言会被对应的编 ...

  4. springMVC对jsp页面的数据进行校验

    一. 使用注解校验 a) 引入校验依赖包 <dependency> <groupId>javax.validation</groupId> <artifact ...

  5. 菜鸟之路——机器学习之决策树个人理解及Python实现

    最近开始学习机器学习,以下会记录我学习中遇到的问题以及我个人的理解 决策树算法,网上很多介绍,在这不复制粘贴.下面解释几个关键词就好. 信息熵(entropy):就是信息不确定性的多少 H(x)=-Σ ...

  6. UVa 1225 - Digit Counting - ACM/ICPC Danang 2007 解题报告 - C语言

    1.题目大意 把前n$(n\le 10000)$个整数顺次写在一起:12345678910111213……计算0~9各出现了多少次. 2.思路 第一想法是打表,然而觉得稍微有点暴力.不过暂时没有想到更 ...

  7. vscode开发智能合约

    开发工具 EOS 开发终极神器-vscode (你绝对找不到的干货) lome · 2018年04月19日 · 最后由 18636292520 回复于 2018年09月15日 · 15672 次阅读 ...

  8. Entity Framework 基本概念

    概念 LINQ to Entities 一种 LINQ 技术,使开发人员可以使用 LINQ 表达式和 LINQ 标准查询运算符,针对实体数据模型 (EDM) 对象上下文创建灵活的强类型化查询. ESQ ...

  9. 自测之Lesson14:多线程编程

    题目:创建一个线程,并理清主线程结束时会发生什么. 代码: #include <stdio.h> #include <pthread.h> #include <unist ...

  10. Android中的回调Callback

    回调就是外部设置一个方法给一个对象, 这个对象可以执行外部设置的方法, 通常这个方法是定义在接口中的抽象方法, 外部设置的时候直接设置这个接口对象即可. 例如给安卓添加按钮点击事件, 我们创建了OnC ...