There are n n apples on a tree, numbered from 1 1 to n n .
Count the number of ways to pick at most m m

apples.

InputThe first line of the input contains an integer T T

(1≤T≤10 5 ) (1≤T≤105)

denoting the number of test cases.
Each test case consists of one line with two integers n,m n,m

(1≤m≤n≤10 5 ) (1≤m≤n≤105)

.
OutputFor each test case, print an integer representing the number of ways modulo 10 9 +7 109+7

.Sample Input

2
5 2
1000 500

Sample Output

16
924129523

题意:T组询问,每次给出N, M,求C(N,0)+C(N,1)+...C(N,M);

思路:前缀和没有什么特别的公式, 所以我们考虑询问之间的关系:

易得,F(N,M)=F(N,M-1)+C(N,M);

由杨辉三角,可得, F(N,M)=2*F(N-1,M)-C(N-1,M);

然后就可以跑莫队了.

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
const int Mod=1e9+;
int f[maxn],rev[maxn],ans[maxn],B;
struct in{ int l,r,id,g; }s[maxn];
bool cmp(in w,in v){ if(w.g==v.g) return w.r<v.r; return w.g<v.g ;}
int qpow(int a,int x){
int res=; while(x){
if(x&) res=(ll) res*a%Mod;
a=(ll)a*a%Mod; x>>=;
} return res;
}
void prepare()
{
f[]=rev[]=;
rep(i,,maxn-) f[i]=(ll)f[i-]*i%Mod;
rev[]=qpow(f[],Mod-);
for(int i=maxn-;i>=;i--) rev[i]=(ll)rev[i+]*(i+)%Mod;
}
int C(int n,int m){
if(n<m) return ;
return (ll)f[n]*rev[n-m]%Mod*rev[m]%Mod;
}
int main()
{
prepare(); B=sqrt();
int N; scanf("%d",&N);
rep(i,,N) scanf("%d%d",&s[i].l,&s[i].r),s[i].id=i,s[i].g=s[i].l/B;
sort(s+,s+N+,cmp);
int L=,R=,res=;
rep(i,,N){
while(L<s[i].l) res=((*res-C(L++,R))%Mod+Mod)%Mod;
while(L>s[i].l) res=(ll)(res+C(--L,R))%Mod*rev[]%Mod;
while(R<s[i].r) res=(res+C(L,++R))%Mod;
while(R>s[i].r) res=(res-C(L,R--)+Mod)%Mod;
ans[s[i].id]=res;
}
rep(i,,N) printf("%d\n",ans[i]);
return ;
}

HDU - 6333:Harvest of Apples (组合数前缀和&莫队)的更多相关文章

  1. HDU 6333 Harvest of Apples (分块、数论)

    题目连接:Harvest of Apples 题意:给出一个n和m,求C(0,n)+C(1,n)+.....+C(m,n).(样例组数为1e5) 题解:首先先把阶乘和逆元预处理出来,这样就可O(1)将 ...

  2. HDU - 6333 Harvest of Apples

    题意: T次询问,每次给出n,m.求sigma(k:0->m)C(n, k). 题解: 用离线莫队来做. 令S(n,m) = sigma(k:0->m)C(n, k). S(n+1, m) ...

  3. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  4. 2018.08.12 bzoj5301: [Cqoi2018]异或序列(前缀和+莫队)

    传送门 简单的异或前缀和处理+莫队统计答案. 惊奇的发现无论开不开long long都能跑过... 代码: #include<bits/stdc++.h> #define N 100005 ...

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

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

  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. HDU 6333.Problem B. Harvest of Apples-组合数C(n,0)到C(n,m)求和-组合数学(逆元)+莫队 ((2018 Multi-University Training Contest 4 1002))

    2018 Multi-University Training Contest 4 6333.Problem B. Harvest of Apples 题意很好懂,就是组合数求和. 官方题解: 我来叨叨 ...

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

  9. HDU 6333 莫队+组合数

    Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K ...

随机推荐

  1. ES6 Promise 让异步函数顺序执行

    应用 ES6 的 内置对象 Promise, 让异步函数 按顺序执行的例子 如下: 上边 是四个用Promise 处理过的 异步执行的函数: fn1.fn2.fn3.fn4 下面,让其按顺序执行 如下 ...

  2. json数据结构

    JSON即JavaScript Object Natation,它是一种轻量级的数据交换格式,非常适合服务器与JavaScript的交互.JSON易于人阅读和编写.同时也易于机器解析和生成.JSON采 ...

  3. DATEDIFF() 返回2个日期之间的间隔

    SELECT DATEDIFF(day,'2008-12-29','2008-12-30') AS DiffDate 解释: 第一个参数 day 表示 返回2个日期间隔的 个间隔类型 是 日期:结果 ...

  4. ES6 随记(3.3)-- 数组的拓展

    上一章请见: 1. ES6 随记(1)-- let 与 const 2. ES6 随记(2)-- 解构赋值 3. ES6 随记(3.1)-- 字符串的拓展 4. ES6 随记(3.2)-- 正则的拓展 ...

  5. Python3:input()输入函数的用法

    Python3:input()输入函数的用法 一.简介 input这个函数,第一个参数是提示语,它默认是空的.在我们使用input的时候,会从标准输入中读取一个string,即字符串(请注意,这里很重 ...

  6. [BZOJ1587]叶子合并leaves

    Description 在一个美丽的秋天,丽丽每天都经过的花园小巷落满了树叶,她决定把树叶堆成K堆,小巷是笔直的 共有N片树叶(树叶排列也是笔直的),每片树叶都有一个重量值,并且每两片想邻的树叶之间的 ...

  7. [洛谷3041]视频游戏的连击Video Game Combos

    题目描述 Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the only v ...

  8. JNI_Z_01_获取Clazz

    1. 为了能够在C/C++中使用Java类,jni.h头文件中专门定义了jclass类型来表示Java中的Class类(ZC: 就是Clazz) 2. 2.1.JNIEXPORT void JNICA ...

  9. LeetCode第[21][23]题(Java):Merge Sorted Lists

    题目:合并两个已排序链表 难度:Easy 题目内容: Merge two sorted linked lists and return it as a new list. The new list s ...

  10. ssh登录CentOS服务器(Aliyun)

    ssh登录 (一).在本机使用命令生成ssh密钥:(-C代表注释,如果有多个密钥,加个注释就会很好区分) ssh-keygen -t rsa -C "mingwei" (二).将本 ...