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
 
Recommend
chendu   |   We have carefully selected several similar problems for you:  6343 6342 6341 6340 6339 
 
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 100005//一开始是10005,超时
#define mod 1000000007
#define gep(i,a,b) for(int i=a;i<=b;i++)
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
ll fac[N]={,},inv[N]={,},f[N]={,};
ll c(ll a,ll b)
{
if(b>a) return ;
return fac[a]*inv[b]%mod*inv[a-b]%mod;
}
void init()
{
gep(i,,N-){
fac[i]=fac[i-]*i%mod;
f[i]=(mod-mod/i)*f[mod%i]%mod;
inv[i]=inv[i-]*f[i]%mod;
}
}
struct Ma{
ll n,m;
int id;
bool operator <(const Ma&a)const{
return n<a.n;//排序
}
}ma[N];
vector<Ma>ve[N];
ll ans[N];
int t;
int main()
{
init();
scanf("%d",&t);
ll mx=sqrt();
gep(i,,t){
scanf("%lld%lld",&ma[i].n,&ma[i].m);
ll x=ma[i].m/mx;
ma[i].id=i;
ve[x].push_back(ma[i]);//分块
}
gep(i,,mx){
if(!ve[i].size()) continue;
sort(ve[i].begin(),ve[i].end());//排序
int k=ve[i].size();
ll val=,ik=-,mn=ve[i][].n;//一次处理每组数据
/*
s(n,m):c(n,0)+c(n,1)+……c(n,m)
s(n,m)=2*s(n-1,m)-c(n-1,m)
*/
gep(j,,k-){
while(mn<ve[i][j].n) val=(2ll*val+mod-c(mn++,ik))%mod;//+mod
while(ik<ve[i][j].m) val=(val+c(mn,++ik))%mod;//,++ik,不是ik++,可以不加mod
while(ik>ve[i][j].m) val=(val+mod-c(mn,ik--))%mod;//+mod
ans[ve[i][j].id]=val;
}
}
gep(i,,t){
printf("%lld\n",ans[i]);
}
return ;
}

最新文章

  1. [转]安装 SciTE 报错 No package ‘gtk+-2.0′ found
  2. Linux命令速查手册,超详细Linux命令教程
  3. wex5 开机图片时间长
  4. South——谁说Django不能migrate!
  5. 信号之kill和raise函数
  6. win/linux 下使用 psutil 获取进程 CPU / memory / IO 占用信息
  7. jquery 中ajax的参数
  8. Java基础09 类数据与类方法
  9. Principle of Computing (Python)学习笔记(5) BFS Searching + Zombie Apocalypse
  10. [html5] 学习笔记-应用缓存与Web workers
  11. pseudocode of zigzag conversion
  12. FORM的静态提交
  13. Python代码缩进与测试模块
  14. CSS-动画巧用translated3d
  15. Python 全栈开发九 日志模块
  16. Future复习笔记
  17. oracle创建数据库步骤
  18. STM32 UART DMA实现未知数据长度接收
  19. 手游项目Crash的上报
  20. visual studio运行时库MT、MTd、MD、MDd的研究

热门文章

  1. PHP函数生成随机数
  2. thymeleaf中th:attr用法以及相关的thymeleaf基本表达式
  3. VS2010/OpenGL配置
  4. Docker的下载安装以及简单使用
  5. CRC检错技术原理
  6. Leet-code144. Binary Tree Preorder Traversal
  7. Linux之bash shell的学习
  8. jsp另外五大内置对象之config
  9. [uva]AncientMessages象形文字识别 (dfs求连通块)
  10. Android(java)学习笔记138:三重for循环的优化(Java面试题)