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. html 基础--一般标签

    <html>    --开始标签 <head> 网页上的控制信息 <title>页面标题</title> </head> <body& ...

  2. redis 系列文章推荐

    推荐博客: Redis在linux上的安装: http://www.open-open.com/lib/view/open1426468117367.html Redis的三种启动方式: http:/ ...

  3. qplot()函数的详细用法

    qplot()函数的详细用法: library(ggplot2) # 测试数据集,ggplot2内置的钻石数据qplot(carat, price, data = diamonds)dsmall &l ...

  4. Django学习笔记之Django中间件

    准备 我们在前面的课程中已经学会了给视图函数加装饰器来判断是用户是否登录,把没有登录的用户请求跳转到登录页面.我们通过给几个特定视图函数加装饰器实现了这个需求.但是以后添加的视图函数可能也需要加上装饰 ...

  5. 菜单Menu

    <Menu HorizontalAlignment="> <MenuItem Header="文件"> <MenuItem Header=& ...

  6. 20145109 《Java程序设计》第三周学习总结

    20145109 <Java程序设计>第三周学习总结 教材学习内容总结 Chapter 4 Object 4.1 Class & Object definition of clas ...

  7. springBean集合注入的方法

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  8. Apache HTTP Server——官网下载

    Windows版 Apache 2.4.x OpenSSL 1.0.2 VC14  ——Apache 2.4.34 x64(注:x64就是64位,x86就是32位) https://www.apach ...

  9. 403.14-Forbidden Web 服务器被配置为不列出此目录的内容

    第二次碰到这个问题了,记录一下 解决方案:1. 运行->cmd 2. cd  C:\Windows\Microsoft.NET\Framework64\v4.0.30319 3. aspnet_ ...

  10. Scala- Double类型工具类

    格式化分数,按照指定小数位四舍五入工具类 package com.rz.util object NumberUtils { /** * 格式化小数 * @param num Double对象 * @p ...