There are nn apples on a tree, numbered from 11 to nn. 
Count the number of ways to pick at most mm apples. 

Input

The first line of the input contains an integer TT (1≤T≤105)(1≤T≤105) denoting the number of test cases. 
Each test case consists of one line with two integers n,mn,m (1≤m≤n≤105)(1≤m≤n≤105). 
Output

For each test case, print an integer representing the number of ways modulo 109+7109+7.Sample Input

2
5 2
1000 500

Sample Output

16
924129523 题意:
给定多个n,m,求C(n,m)
思路:
数据范围比较大,不能进行预处理。
我们可以采用莫队算法解决这个查询问题。

当n变大时,按此试展开,再和原式相比较,便可以得出转换公式。
n变小同理。
k的变化直接加减即可。
注意:变化过程中,可能使n>m,这个时候计算C的函数直接返回0就好了。
逆元要预处理,否则会T。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int maxm = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-); struct node{
int l,r;
int id;
}a[maxm];
ll anss[maxm];
int block; bool cmp(node a,node b){
return (a.l/block!=b.l/block)?a.l<b.l:a.r<b.r;
}
int vis[];
ll q_pow(ll a,ll b){
ll ans=;
while(b){
if(b&){ans*=a;ans%=mod;}
a*=a;
a%=mod;
b>>=;
}
return ans;
}
ll cal[maxn];
ll inv[maxn];
ll C(int n,int m){
if(n<||m<||m>n){return ;}
return cal[n]*inv[m]%mod*inv[n-m]%mod;
} int main()
{ cal[]=;inv[]=;
for(int i=;i<maxn;i++){
cal[i]=cal[i-]*i;
cal[i]%=mod;
inv[i]=q_pow(cal[i],mod-);
inv[i]%=mod;
} int m;
scanf("%d",&m);
int mx=;
for(int i=;i<=m;i++){
scanf("%d%d",&a[i].l,&a[i].r);
a[i].id=i;
mx=max(mx,a[i].l);
}
block=sqrt(mx);
sort(a+,a++m,cmp); ll L=,R=;
ll ans=;
for(int i=;i<=m;i++){
while(L>a[i].l){
ans+=C(L-,R);
ans%=mod;
ans*=q_pow(,mod-);
ans%=mod;
L--;
}
while(R<a[i].r){
R++;
ans+=C(L,R);
ans%=mod;
}
while(L<a[i].l){
ans*=;
ans%=mod;
ans-=C(L,R);
ans+=mod+mod;
ans%=mod;
L++;
}
while(R>a[i].r){
ans-=C(L,R);
ans+=mod+mod;
ans%=mod;
R--;
}
ans%=mod;
anss[a[i].id]=ans;
} for(int i=;i<=m;i++){
printf("%lld\n",anss[i]);
}
return ;
}

HDU - 6333 Problem B. Harvest of Apples (莫队)的更多相关文章

  1. 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)).有公 ...

  2. Problem B. Harvest of Apples 莫队求组合数前缀和

    Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...

  3. HDU-6333 Problem B. Harvest of Apples 莫队

    HDU-6333 题意: 有n个不同的苹果,你最多可以拿m个,问有多少种取法,多组数据,组数和n,m都是1e5,所以打表也打不了. 思路: 这道题要用到组合数的性质,记S(n,m)为从n中最多取m个的 ...

  4. 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 题意很好懂,就是组合数求和. 官方题解: 我来叨叨 ...

  5. 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 ...

  6. 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)    设 ...

  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. 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. 热身训练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( ...

随机推荐

  1. 运行docker容器镜像2(指定容器启动时启动的脚本)

    docker中启动容器有以下两种情况. 第一种是通过 # docker run containerid 启动一个容器. 第二种是重新启动已经关闭的容器. # docker start containe ...

  2. 微信小程序组件——bindtap和catchtap的区别

    了解知识点 DOM模型是一个树形结构,在DOM模型中,HTML元素是有层次的.当一个HTML元素上产生一个事件时,该事件会在DOM树中元素节点与根节点之间按特定的顺序传播,路径所经过的节点都会收到该事 ...

  3. Oracle安装 卸载 和常见问题

    Oracle的安装   全局数据库名:orcl  口令:orcl 或者以第三方工具SQLplus为例 系统用户:sys 和 system  练习账户:scott (密码:tiger) 登录账户为:sy ...

  4. 16.libgdx根据配置文件生成布局(未完)

    思路: screen分为普通和复杂两种,普通的功能大部分是页面跳转以及简单的crud数据,复杂的单独弄出来 跳转普通的screen,直接根据配置文件调整设置 <layouts> <l ...

  5. 2017 ACM-ICPC 亚洲区(西安赛区)网络赛: B. Coin 【概率题】【数论】

    Bob has a not even coin(就是一个不均匀的硬币,朝上的概率不一定是1/2), every time he tosses the coin, the probability tha ...

  6. iOS如何才能在招聘中表现得靠谱?

    http://www.cocoachina.com/programmer/20150707/12414.html 近一年内陆续面试了不少人了,从面试者到面试官的转变让我对 iOS 招聘有了更多的感受. ...

  7. 解释型语言Python

  8. js获取当前日期及获取当前日期的前一天日期函数

    function getcurrentdate(){ //获取系统时间var LSTR_ndate=new Date();var LSTR_Year=LSTR_ndate.getFullYear(); ...

  9. H3C路由器SSH服务配置命令

  10. 自然语言处理课程(二):Jieba分词的原理及实例操作

    上节课,我们学习了自然语言处理课程(一):自然语言处理在网文改编市场的应用,了解了相关的基础理论.接下来,我们将要了解一些具体的.可操作的技术方法. 作为小说爱好者的你,是否有设想过通过一些计算机工具 ...