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. OpenTelemetry-可观察性的新时代

    有幸在2019KubeCon上海站听到Steve Flanders关于OpenTelemetry的演讲,之前Ops领域两个网红项目OpenTracing和OpenCensus终于走到了一起,可观察性统 ...

  2. bzoj1295 最长距离

    Description windy有一块矩形土地,被分为 N*M 块 1*1 的小格子. 有的格子含有障碍物. 如果从格子A可以走到格子B,那么两个格子的距离就为两个格子中心的欧几里德距离. 如果从格 ...

  3. jq方法的注意点

    当jq方法里面引用的ajax方法和其它方法时,就需要把ajax改为同步,通过ajax方法返回值来判断下一步执行那个方法,你不做判断,浏览器编译执行的时候不会不会按你想的从上之下执行下来. 当安卓手机跟 ...

  4. Linux终端常用命令(一)

    基本操作 展示全部的环境变量 export 搜索可执行文件.源文件 whereis ls 在环境变量中搜索可执行文件,并打印完整路径 which ls 展示用户命令,系统调用.库函数等 whatis ...

  5. DFS-生日蛋糕

    生日蛋糕 一道深搜题,看了这两个博客才懂的. http://blog.csdn.net/blesslzh0108/article/details/53486168 http://blog.csdn.n ...

  6. spring调试

    spring-beans DefaultListableBeanFactory preInstantiateSingletons:650 RequestMappingHandlerMapping Ab ...

  7. nodeJs学习-14 mysql数据库学习、Navicat管理工具

    数据库: MySQL   免费.性能非常不错 缺点:集群.容灾稍微弱一点 Oracle 收费.大型应用.金融级.性能非常不错.集群.容灾非常强 缺点:贵 mySQL安装教程--nodeJsz智能社视频 ...

  8. hdu1848 sg打表

    果然是神器. #include<stdio.h> #include<string.h> #define maxn 1002 ],sg[maxn],hash[maxn]; voi ...

  9. LeetCode93 Restore IP Addresses

    题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...

  10. VSCode 设置 CPP 代码风格

    VSCode 设置 CPP 代码风格 按 Ctrl+, 打开设置,输入 format 找到. { BasedOnStyle: Google, IndentWidth: 4 }