hdu 6333
2024-08-30 05:20:45
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.
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).
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
5 2
1000 500
Sample Output
16
924129523
924129523
Source
Recommend
#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 ;
}
最新文章
- [转]安装 SciTE 报错 No package ‘gtk+-2.0′ found
- Linux命令速查手册,超详细Linux命令教程
- wex5 开机图片时间长
- South——谁说Django不能migrate!
- 信号之kill和raise函数
- win/linux 下使用 psutil 获取进程 CPU / memory / IO 占用信息
- jquery 中ajax的参数
- Java基础09 类数据与类方法
- Principle of Computing (Python)学习笔记(5) BFS Searching + Zombie Apocalypse
- [html5] 学习笔记-应用缓存与Web workers
- pseudocode of zigzag conversion
- FORM的静态提交
- Python代码缩进与测试模块
- CSS-动画巧用translated3d
- Python 全栈开发九 日志模块
- Future复习笔记
- oracle创建数据库步骤
- STM32 UART DMA实现未知数据长度接收
- 手游项目Crash的上报
- visual studio运行时库MT、MTd、MD、MDd的研究