问题 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 ;
}

最新文章

  1. struts1和struts2的区别
  2. python函数应用
  3. 【.NET应用技巧】Asp.NET MVC 4 设置IIS下调试
  4. 给div设置一个关闭按钮.
  5. NavBarControl 左侧菜单
  6. Shortcut Collapse project or projects in the Solution Explorer Microsoft Visual Studio 2008
  7. Vim键盘布局
  8. VBS虚拟键码
  9. Java语言基本语法(一)————关键字&amp;标识符(Java语言标识符命名规范&amp;Java语言的包名、类名、接口名、变量名、函数名、常量名命名规则 )
  10. 米扑科技的开源项目:sitemap-php 自动生成网站地图
  11. Java---实现邮件发送
  12. DCDC设计指南二
  13. mysql删除大表更快的drop table办法
  14. Selenium HTMLTestRunner 执行测试成功但无法生成报告
  15. Unsafe 学习和源码阅读
  16. MySQL 0Day漏洞出现 该漏洞可以拿到本地Root权限
  17. elk收集windows日志
  18. install virtualenv
  19. Assets/FollowDestination.cs(6,13): error CS0246: The type or namespace name `NavMeshAgent&#39; could not be found. Are you missing `UnityEngine.AI&#39; using directive?的解决方案
  20. 在 Ubuntu上使用 MySQL

热门文章

  1. C++开源库(一) ----libConfig详解
  2. Keras实现MNIST分类
  3. 洛谷P2680 运输计划(树上差分+二分)
  4. HTML5新标签介绍
  5. 本机和虚拟机互联 设置静态IP vmware 虚拟网络 桥接 NAT 仅主机 自定义
  6. JSPs only permit GET POST or HEAD的解决方案(REST风格)
  7. 牛客假日团队赛2 A.买一送一
  8. QDU第一届程序设计大赛——E到I题解法(非官方题解)
  9. 洛谷P4719 【模板】动态dp
  10. 寒假作业第二组P&amp;&amp;Q&amp;&amp;R题解