问题 B: Harvest of Apples

时间限制: 1 Sec  内存限制: 128 MB
提交: 18  解决: 11
[提交] [状态] [讨论版] [命题人:admin]

题目描述

There are n apples on a tree, numbered from 1 to n.
Count the number of ways to pick at most m apples.

输入

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

输出

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

样例输入

2
5 2
1000 500

样例输出

16
924129523

莫队(分块),组合数学。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
const int mod=1e9+;
ll fac[maxn],inv[maxn],ans[maxn];
ll rev2,res;
int pos[maxn];
ll qpow(ll b,int n)
{
ll res=;
while(n)
{
if(n&) res=res*b%mod;
b=b*b%mod;
n>>=;
}
return res;
}
ll Comb(int n,int k)
{
return fac[n]*inv[k]%mod*inv[n-k]%mod;
}
void pre()
{
rev2=qpow(,mod-);
fac[]=fac[]=;
for(int i=; i<maxn; ++i) fac[i]=i*fac[i-]%mod;
inv[maxn-]=qpow(fac[maxn-],mod-);
for(int i=maxn-;i>=;i--) inv[i]=inv[i+]*(i+)%mod;
}
struct Query
{
int L,R,id;
bool operator <(const Query& p) const
{
if(pos[L]==pos[p.L]) return R<p.R;
return L<p.L;
}
} Q[maxn];
inline void addN(int posL,int posR)
{
res=(*res%mod-Comb(posL-,posR)+mod)%mod;
}
inline void addM(int posL,int posR)
{
res=(res+Comb(posL,posR))%mod;
}
inline void delN(int posL,int posR)
{
res=(res+Comb(posL-,posR))%mod*rev2%mod;
}
inline void delM(int posL,int posR)
{
res=(res-Comb(posL,posR)+mod)%mod;
}
int main()
{
int T,curL,curR;
int block=(int)sqrt(1.0*maxn);
pre();
scanf("%d",&T);
for(int i=;i<=T;++i)
{
scanf("%d %d",&Q[i].L,&Q[i].R);
pos[i]=i/block;
Q[i].id=i;
}
sort(Q+,Q+T+);
res=;
curL=,curR=;
for(int i=;i<=T;++i)
{
while(curL<Q[i].L) addN(++curL,curR);
while(curR<Q[i].R) addM(curL,++curR);
while(curL>Q[i].L) delN(curL--,curR);
while(curR>Q[i].R) delM(curL,curR--);
ans[Q[i].id] = res;
}
for(int i=;i<=T;++i) printf("%lld\n",ans[i]);
return ;
}

Harvest of Apples的更多相关文章

  1. hdu多校第4场 B Harvest of Apples(莫队)

    Problem B. Harvest of Apples Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...

  2. HDU 6333 Harvest of Apples (分块、数论)

    题目连接:Harvest of Apples 题意:给出一个n和m,求C(0,n)+C(1,n)+.....+C(m,n).(样例组数为1e5) 题解:首先先把阶乘和逆元预处理出来,这样就可O(1)将 ...

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

  4. hdu6333 Harvest of Apples 离线+分块+组合数学(求组合数模板)

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

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

  6. Problem B. Harvest of Apples HDU - 6333(莫队)

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

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

  8. HDU - 6333:Harvest of Apples (组合数前缀和&莫队)

    There are n n apples on a tree, numbered from 1 1 to n n . Count the number of ways to pick at most ...

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

随机推荐

  1. 使用CTE生成辅助表(数字或时间)等

    数字辅助表: , ;WITH Digital_Rangs(Digital) AS ( SELECT @start_digital UNION ALL FROM [Digital_Rangs] WHER ...

  2. ASP.NET Core MVC 视图

    ASP.NET Core MVC中视图的知识和ASP.NET MVC有很多相似之处,学习难度较低.以下内容主要体现了编程中模块化的思想,模块化才应是我们关注的重点. Layout 布局用于提供各个页面 ...

  3. (转)深度学习(Deep Learning, DL)的相关资料总结

    from:http://blog.sciencenet.cn/blog-830496-679604.html 深度学习(Deep Learning,DL)的相关资料总结 有人认为DL是人工智能的一场革 ...

  4. 洛谷P4768 [NOI2018]归程(克鲁斯卡尔重构树+最短路)

    传送门 前置技能,克鲁斯卡尔重构树 我们按道路的高度建一个最大生成树,然后建好克鲁斯卡尔重构树 那么我们需要知道一颗子树内到1点距离最近是多少(除此之外到子树内任何一个点都不需要代价) 可以一开始直接 ...

  5. 【工具篇】Sublime Text 2/3 安装汉化破解、插件包安装教程详解

    Sublime Text概述: Sublime Text是一个代码编辑器,也是HTML和散文先进的文本编辑器. 漂亮的用户界面和非凡的功能,例如:迷你地图,多选择,Python插件,代码段等等. 完全 ...

  6. Python基础:模块化来搭项目

    简单模块化 import 最好在最顶端 sys.path.append("..")表示把当前程序所在位置向上提了一级 在python3规范中,__init__.py并不是必须的. ...

  7. 本机和虚拟机互联 设置静态IP vmware 虚拟网络 桥接 NAT 仅主机 自定义

  8. 引擎基本服务接口API介绍

    Slickflow.NET 开源工作流引擎基础介绍(一) -- 引擎基本服务接口API介绍 https://www.cnblogs.com/slickflow/p/4807227.html 工作流术语 ...

  9. NET高性能IO

    System.IO.Pipelines: .NET高性能IO https://www.cnblogs.com/xxfy1/p/9290235.html System.IO.Pipelines是一个新的 ...

  10. Exception in thread "main" java.lang.UnsupportedClassVersionError: com/google/common/base/Function : Unsupported major.minor version 52.0的解决办法(图文详解)

    不多说,直接上干货! 问题详情 Exception in thread "main" java.lang.UnsupportedClassVersionError: com/goo ...