问题 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. Git提交与恢复

    Git提交与恢复 提交修改 git add --all # 提交所有修改文件 git add file file # 提交部分修改文件 $ git status On branch master Yo ...

  2. 2017 Gartner Hype Cycle for Emerging Technologies: AI, AR/VR, Digital Platforms

  3. [转] 深度探索Hyperledger技术与应用之超级账本的典型交易流程

    转自: https://blog.csdn.net/HiBlock/article/details/80212499 个人感觉对交易流程描述的比较清楚,转载以备查看. 1 典型交易流程 下图所示为Hy ...

  4. 洛谷P1655 小朋友的球(Stirling数)

    P1655 小朋友的球 题目描述 @发源于 小朋友最近特别喜欢球.有一天他脑子抽了,从口袋里拿出了N个不同的球,想把它们放到M个相同的盒子里,并且要求每个盒子中至少要有一个球,他好奇有几种放法,于是尝 ...

  5. LeetCode.908-最小差值 1(Smallest Range I)

    这是悦乐书的第348次更新,第372篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第213题(顺位题号是908).给定一个整数数组A,对于每个整数A[i],我们可以选择任 ...

  6. How to generate a CSR in Microsoft IIS 7

    How to generate a CSR in Microsoft IIS 7 To help you generate your CSR for Microsoft IIS 7 we've pre ...

  7. Tomcat 指定jdk

    Windows 下 修改 tomcat根目录/bin/setclasspath.bat 文件 如下: rem Otherwise either JRE or JDK are fine set JAVA ...

  8. 【VueJS】windows环境安装vue-cli及webpack并创建VueJS项目

    1. 安装node.js, Node.js安装包及源码下载地址为:https://nodejs.org/en/download/. 这次node.js不是主角,默认已安装好了,通过npm –v查看no ...

  9. 如何使用sass

    Sass 是对 CSS 的扩展,让 CSS 语言更强大.优雅. 它允许你使用变量.嵌套规则.mixin.导入等众多功能, 并且完全兼容 CSS 语法. Sass 有助于保持大型样式表结构良好, 同时也 ...

  10. 常见的div布局面试题

    题目1:如何让一个子元素在父元素里水平垂直居中? 方法1 .box{width:400px;height:400px;background:#ccc;position:relative;} .chil ...