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. 强化学习策略梯度方法之: REINFORCE 算法(从原理到代码实现)

    强化学习策略梯度方法之: REINFORCE 算法 (从原理到代码实现) 2018-04-01  15:15:42   最近在看policy gradient algorithm, 其中一种比较经典的 ...

  3. [AtCode 4104] Small and Large Integers

    题目链接:https://abc093.contest.atcoder.jp/tasks/abc093_b?lang=en 这个题虽然很水,但是还是很容易踩坑,比如我,直接想到了[b,a]之间的长度和 ...

  4. JS进阶系列之this (call、apply、bind)

    在javascript中,this的指向是在执行上下文的创建阶段确定的,其实只要知道不同执行方式下,this的指向分别是是什么,就能很好的掌握this这个让人摸不透的东西. 一.全局执行 全局执行又分 ...

  5. 关于导入geoserver 源码到Eclipse编译运行

    参考http://blog.csdn.net/gisshixisheng/article/details/43016443 和  http://blog.sina.com.cn/s/blog_6e37 ...

  6. _event_team

    EventId 事件ID TeamId 事件玩家分组,仅传送(0为联盟 1为部落)对线(防守为1,进攻为2),自定义阵营(_faction表自定义阵营ID),公会(公会guid) TeamName 事 ...

  7. UIUseImgWindow

    using System;using UnityEngine;using UnityEngine.UI;using UnityEditor;using System.Collections;using ...

  8. 切片对象的demo

    a = slice(, ) s = 'HelloWorld' print(a.indices(len(s))) for i in range(*a.indices(len(s))): print(s[ ...

  9. Selenium IDE使用

    基于版本Selenium IDE 3.2.2(注:该工具不常用,可以使用定位元素是否存在) Selenium IDE可以录制也很方便,当然录下来的经常回放不成功,需要自己调试就是了.它是只针对Web页 ...

  10. hive表的存储路径查找以及表的大小

    1.在hive中知道一个表的存储路径可以通过hive命令   desc formatted table_name 显示表的详细信息; 2.然后找到该表的存储路径 "Location:    ...