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. 大数据生态,哪些框架需要全部启动,哪些只启动master,仅为汇总

    主从,只需要在master节点启动 hadoop hbase 单机启动 hive 其他,需要启动每个节点 zookeeper kafka flume presto

  2. hadoop17---RPC和Socket的区别

    RPC是在Socket的基础上实现的,它比socket需要更多的网络和系统资源.RPC(Remote Procedure Call,远程过程调用)是建立在Socket之上的,出于一种类比的愿望,在一台 ...

  3. 【纯代码】Swift相册照片选择-支持单选或多选

    // // NAPublishAlbumTableViewController.swift //// // Created by on 2019/3/23. // Copyright © 2019年 ...

  4. Spring boot + jdbc学习笔记

    pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www. ...

  5. Spring 是什么

  6. HTTP Message Handlers in ASP.NET Web API

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/http-message-handlers A message ha ...

  7. PAT1051. Pop Sequence (25)

    #include <iostream> #include <stack> using namespace std; int sizeMax,n,k; stack<int& ...

  8. 比较好的SQL

    DECLARE @Data NVARCHAR(30);DECLARE @Data2 NVARCHAR(30);SET @Data = @DataDate;SET @Data = CONVERT(CHA ...

  9. Codeforces Round #315 (Div. 2) C. Primes or Palindromes? 暴力

    C. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input st ...

  10. mysql中删除完全重复数据的准确SQL语句

    删除数据库中重复的记录,只保留一条 DELETE FROM tb_gps_records WHERE id NOT IN (SELECT bid FROM (SELECT min(id) as bid ...