题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333
分析:
代码:
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;
#define ll long long
#define mod 1000000007
const int maxn =100000+5;
ll ans[maxn],fac[maxn]={1,1},inv[maxn]={1,1},f[maxn]={1,1},len;
int T;
ll cal(ll a,ll b) //求组合数
{
if(b>a)
return 0;
return fac[a]*inv[b]%mod*inv[a-b]%mod;
}
void init()
{
for(int i=2; i<=maxn-1; i++)
{
fac[i]=fac[i-1]*i%mod;//阶乘
f[i]=(mod-mod/i)*f[mod%i]%mod;//逆元
inv[i]=inv[i-1]*f[i]%mod;//逆元的阶乘
}
}
struct Query
{
ll L,R,block;
int ID;
Query(){};
Query(ll l, ll r, int ID) :L(l), R(r), ID(ID){
block=l/len;
}
bool operator<(const Query rhs)const{
if(block ==rhs.block)
return R<rhs.R;
return block <rhs.block;
}
}queries[maxn];
int main()
{
len=sqrt(maxn);///块长度
init();
scanf("%d",&T);
for(int i=1; i<=T; i++)
{
ll l,r;
scanf("%lld %lld",&r,&l);
queries[i]=Query(l,r,i);
}
sort(queries+1,queries+T+1);
int l=0,r=1;
ll val=1;
for(int i=1; i<=T; i++)
{
while(r<queries[i].R)
{
val=(2ll*val%mod+mod-cal(r++,l))%mod;
}
while(r>queries[i].R)
{
val=(val+cal(--r,l))%mod*f[2]%mod;
}
while(l<queries[i].L)
{
val=(val+cal(r,++l))%mod;
}
while(l>queries[i].L)
{
val=(val+mod-cal(r,l--))%mod;
}
ans[queries[i].ID]=val;
}
for(int i=1; i<=T; i++)
printf("%lld\n",ans[i]);
return 0;
}