首页 技术 正文
技术 2022年11月21日
0 收藏 549 点赞 4,162 浏览 1578 个字

题意:计算C(n,0)到C(n,m)的和,T(T<=1e5)组数据。

分析:预处理出阶乘和其逆元。但如果每次O(m)累加,那么会超时。

定义 S(n, m) = sigma(C(n,m))。有公式:S(n,m) = S(n,m-1) +C(n,m)以及S(n,m) = 2*S(n-1,m) – C(n-1,m)。

这样就可以在O(1)的时间中计算出S(n+1,m),S(n-1,m),S(n,m+1),S(n,m+1)。可以用莫队离线处理T组查询。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn =1e5+;
const int mod = 1e9+;
LL fac[maxn],inv[maxn];
LL rev2;
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;
}int pos[maxn];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];
LL res;
LL ans[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()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T,N,M,u,v,tmp,K;
int a,b;
pre();
int block = (int)sqrt(1.0*maxn);
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=;
int curL=,curR=;
for(int i=;i<=T;++i){
while(curL<Q[i].L) addN(++curL,curR); //ok
while(curR<Q[i].R) addM(curL,++curR); //ok
while(curL>Q[i].L) delN(curL--,curR); //ok
while(curR>Q[i].R) delM(curL,curR--); //ok
ans[Q[i].id] = res;
}
for(int i=;i<=T;++i) printf("%lld\n",ans[i]);
return ;
}
上一篇: PHP 权限管理
下一篇: hadoop09----线程池
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848