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. fastCMS八大核心对象

    fastCMS内置system对象,该对象包含八大核心对象,应用于不同的操作场景,分别是: 1.system.string 对象(处理字符类操作) 2.system.number 对象(处理数字类操作 ...

  2. UML类图(Class Diagram)中类与类之间的关系及表示方式(转)

    源地址:https://blog.csdn.net/a19881029/article/details/8957441 ======================================== ...

  3. [HNOI2017]影魔

    题意: 给定 \(n\) 个数的排列,\(m\) 次询问,每次询问询问一个区间内所有子区间的贡献. 每个区间如果两个端点分别是最大值和次大值,我们就算 \(P1\) 的贡献. 如果两个端点一个是最大值 ...

  4. Nodejs基础之redis

    安装redis 模块 npm install redis 1 代码部分 const redis = require('redis') const client = redis.createClient ...

  5. c# html 导出word

    [CustomAuthorize]        public FileResult ExportQuestionCenterWord(SearchBaseQuestion search)       ...

  6. NFS服务搭建使用

    需求:由于线上业务有一些数据存在了Redis数据库和mysql数据库中了,导致了数据较大迁移起来比较麻烦,所以准备搭建NFS来做WEB的共享磁盘,存储这些数据. 服务端搭建: 查看本机关于nfs的包 ...

  7. Thunder团队第二周 - Scrum会议5

    Scrum会议5 小组名称:Thunder 项目名称:爱阅app Scrum Master:苗威 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传康 ...

  8. Java常用类之Math类

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

  9. IT启示录

    引用电影<夏洛特烦恼>中夏洛的一句话:"一直以来,我根本就不知道自己想要什么".可以说在写这篇博客之前我仍然没有考虑清楚之后的道路,即使早已明确了走游戏开发的道理,却不 ...

  10. alpha阶段个人总结(201521123031林庭亦)

    一.个人总结 第一部分:硬的问题 第二部分:软的问题,在成长路上学到了什么? 1 当你看到不靠谱的设计.糟糕的代码.过时的文档和测试用例的时候,不要想 "既然别人的代码已经这样了,我的代码也 ...