Problem B. Harvest of Apples

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2970    Accepted Submission(s): 1153

Problem Description
There are n apples on a tree, numbered from 1 to n.
Count the number of ways to pick at most m apples.
 
Input
The first line of the input contains an integer T (1≤T≤105) denoting the number of test cases.
Each test case consists of one line with two integers n,m (1≤m≤n≤105).
 
Output
For each test case, print an integer representing the number of ways modulo 109+7.
 
Sample Input
2
5 2
1000 500
 
Sample Output
16
924129523
 
Source
 
  经过观察可以发现,设S(m,n)=C(0,n)+C(1,n)+.....+C(m,n)的话,S(m,n+1)=2*S(m,n)-C(m,n) , S(m,n-1)=(S(m,n)+C(m,n-1))/2。
  S(m-1,n)=S(m,n)-C(m,n) ,S(m+1,n)=S(m,n)+C(m+1,n),也就是说我们能在O(1)求出来这四个式子,这样就可以用莫队处理了。
分块的时候我错把 q[i].m/M写成了 i/M导致一直T ,是对mi所在的块作为关键字而不是输入的次序。
  

 #include<bits/stdc++.h>
using namespace std;
#define LL long long
#define eps 1e-6
#define mod 1000000007
//LL mod=1e9+7;
const int maxn=+;
LL len,p[maxn]={,},inv[maxn]={,},p_inv[maxn]={,},ans[maxn];
int t;
struct Query{
LL n,m,blo;
int id;
bool operator<(const Query&C)const{
if(blo==C.blo) return n<C.n;
return blo<C.blo;
}
}q[maxn];
LL cal(LL a,LL b)
{
if(b>a)
return ;
return p[a]*p_inv[b]%mod*p_inv[a-b]%mod;
}
int main(){
int n,m,i,j,k;
len=sqrt(maxn);
for(i=;i<=;++i){
p[i]=p[i-]*i%mod;
inv[i]=(mod-mod/i)*inv[mod%i]%mod;
p_inv[i]=p_inv[i-]*inv[i]%mod;
}
scanf("%d",&t);
for(i=;i<=t;++i){
scanf("%lld %lld",&q[i].n,&q[i].m);
q[i].id=i;
q[i].blo=q[i].m/len;
}
sort(q+,q++t);
int L=,R=;
LL res=;
for(i=;i<=t;++i){
while(L<q[i].m){
res=(res+cal(R,++L))%mod;
}
while(L>q[i].m){
res=(res+mod-cal(R,L--))%mod;
}
while(R<q[i].n){
res=(res*+mod-cal(R++,L))%mod;
}
while(R>q[i].n){
res=(res+cal(--R,L))%mod*inv[]%mod;
}
ans[q[i].id]=res;
}
for(i=;i<=t;++i)printf("%lld\n",ans[i]);
return ;
}
 

hdu-6333-莫队的更多相关文章

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

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

  2. HDU 6333 莫队+组合数

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

  3. Hdu 5213-Lucky 莫队,容斥原理,分块

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5213 Lucky Time Limit: 6000/3000 MS (Java/Others)    Me ...

  4. HDU 4358 莫队算法+dfs序+离散化

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others)T ...

  5. HDU 4638 (莫队)

    题目链接:Problem - 4638 做了两天莫队和分块,留个模板吧. 当插入r的时候,设arr[r]代表r的位置的数字,判断vis[arr[r-1]]和vis[arr[r+1]]是否访问过,如果两 ...

  6. HDU 4638 莫队算法

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. hdu 5145(莫队算法+逆元)

    NPY and girls Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  8. HDU 6534 莫队+ 树状数组

    题意及思路:https://blog.csdn.net/tianyizhicheng/article/details/90369491 代码: #include <bits/stdc++.h&g ...

  9. HDU 5145 NPY and girls (莫队分块离线)

    题目地址:HDU 5145 莫队真的好奇妙.. 这种复杂度竟然仅仅有n*sqrt(n)... 裸的莫队分块,先离线.然后按左端点分块,按块数作为第一关键字排序.然后按r值作为第二关键字进行排序. 都是 ...

  10. 【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples

    http://acm.hdu.edu.cn/showproblem.php?pid=6333 莫队算法是一个离线区间分块瞎搞算法,只要满足:1.离线  2.可以O(1)从区间(L,R)更新到(L±1, ...

随机推荐

  1. 论文笔记之 SST: Single-Stream Temporal Action Proposals

    SST: Single-Stream Temporal Action Proposals 2017-06-11 14:28:00 本文提出一种 时间维度上的 proposal 方法,进行行为的识别.本 ...

  2. qt QRegExp使用(搬运工)

    设置正则表达式. 类似下面的 QRegExp 这里的用法就是用来检测QString等字符串错误的,例如文件名里面最好就不出现<>|/\:等,所以可以如下定义QRegExp rx(" ...

  3. Android天坑ImageView控件上下留白原因与解决

    ImageView控件上下留白 如下,误以为是padding的问题.搜索无果 后来发现是需要添加android:adjustViewBounds="true",调整ImageVie ...

  4. 【Django】Django-REST-Framework

    [创建简单的API] 1. cmd.exe >django-admin startproject django_rest>cd django_rest\django_rest>pyt ...

  5. JaveWeb 公司项目(5)----- Java获取当前时间的年月日以及同Thrift格式的转化

    随着项目进度的逐步完成,数据传输和界面基本上已经搭建完成,下面就是一些细节部分的修改 今天博文的主要内容说的是获取当前的时间和同Thrift类型的转化 和C#类似,java也有一个时间类Date,加载 ...

  6. 启动tomcat 报错:Neither the JAVA_HOME nor the JRE_HOME environment variable is defined

    [root@localhost META-INF]# systemctl start tomcat Job for tomcat.service failed because the control ...

  7. .NET Core 管道

    从用户发请求到服务器响应返回数据 请求从 Request进去    先经过 Middleware(中间件) 然后经过AuthoriationFilters授权验证(token验证和 多租户验证) 在经 ...

  8. => js 中箭头函数使用总结

    箭头函数感性认识 箭头函数 是在es6 中添加的一种规范 x => x * x 相当于 function(x){return x*x} 箭头函数相当于 匿名函数, 简化了函数的定义. 语言的发展 ...

  9. ArcFace2 #C 视频人脸比对教程

    请允许我大言不惭,叫做教程,特希望各位能指正.哦,我用的是vs2017.使用虹软技术 一.准备工作1.创建项目 2.添加EMGU.CV包 3.复制虹软的dll到项目 ,并设属性“复制到输出目录”为“如 ...

  10. dockerfile debian 和pip使用国内源

    python官方镜像是基于debian的.国内使用时定制一下,加快下载速度. 1 debian本身使用国内源 dockfile中: #国内debian源 ADD sources.list /etc/a ...