@

目录

HDU6333:传送门

题意:求组合数前m项的和。

在线分块or离线莫队

分块

重要的一个定理:

\[C_{n}^{m} = 0\;\;m > n
\]

\[C_{n}^{m} = C_{a}^{0}\times C_{b}^{m}+C_{a}^{1}\times C_{b}^{m-1}...+C_{a}^{m}\times C_{b}^{0}\;\;\;a+b = n
\]

\[ans = \sum_{i=0}^m C_n^i=\sum_{i=0}^m(C_a^i\times \sum_{j=0}^{m-i} C_b^j)\;\;a+b=n
\]

然后分块处理,b为整块的大小,块内枚举i求解。

#include<bits/stdc++.h>
#define lson rt<<1
#define rson rt<<1|1
#define all(x) (x).begin(),(x).end()
#define mme(a,b) memset((a),(b),sizeof((a)))
#define fuck(x) cout<<"* "<<x<<"\n"
#define iis std::ios::sync_with_stdio(false)
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int MXN = 1e5 + 7;
const int MXE = 1e6 + 7;
const int INF = 0x3f3f3f3f;
const LL MOD = 1e9 + 7;
const LL mod = 1e9 + 7; int n;
LL l, r; const int MX = 1e5 + 7;
LL F[MX], invF[MX];
LL ksm(LL a, LL b){
LL res = 1;
for(;b;b>>=1,a=a*a%mod){
if(b&1)res = res * a % mod;
}
return res;
}
void init() {
F[0] = 1;
for (int i = 1; i < MX; i++) F[i] = F[i - 1] * i % mod;
invF[MX - 1] = ksm(F[MX - 1], mod - 2);
for (int i = MX - 2; i >= 0; i--) invF[i] = invF[i + 1] * (i + 1) % mod;
}
LL COMB(int n, int m) {
if(n == m)return 1;
if(n < m) return 0;
return F[n]*invF[m]%mod*invF[n-m]%mod;
} LL bpre[505][MXN];//bpre[i][j] = sigma(COMB(500*i,k)), k从[0, j]
int blocks = 500;
void yuchuli(){
for(int i = 1; i < MXN/blocks; ++i){
for(int j = 0; j < MXN; ++j){
if(j==0)bpre[i][j] = COMB(blocks*i, j);
else bpre[i][j] = (bpre[i][j-1] + COMB(blocks*i, j))%MOD;
}
}
} int main(int argc, char const *argv[]){
#ifndef ONLINE_JUDGE
freopen("E://ADpan//in.in", "r", stdin);
//freopen("E://ADpan//out.out", "w", stdout);
#endif
init();
yuchuli();
int tim = 1;
scanf("%d", &tim);
while(tim--){
scanf("%lld%lld", &r, &l);
LL p = r/blocks, re = r - p * blocks, ans = 0;
LL tmp = min(l, re), limit = p*500;
//printf("%lld %lld\n", p, re);
if(p == 0) p = 1, limit = 0;
for(int i = 0; i <= tmp; ++i){
ans = (ans + COMB(re, i)*bpre[p][min(l-i, limit)])%MOD;
}
printf("%lld\n", ans);
}
return 0;
}

莫队

\(S_n^m=\sum C_n^m=Cn^0+Cn^1...+Cn^m\)

\(S_n^{m-1}=\sum C_n^{m-1}=Cn^0+Cn^1...+Cn^{m-1}=S_n^m-C_n^m\)

\(S_n^{m+1}=\sum C_n^{m+1}=Cn^0+Cn^1...+Cn^{m+1}=S_n^m+C_n^{m+1}\)

\(S_{n-1}^m=\sum C_{n-1}^m=C_{n-1}^0+C_{n-1}^1...+C_{n-1}^m=(S_n^m+C_{n-1}^m)\div 2\)

\(S_{n+1}^m=\sum C_{n+1}^m=C_{n+1}^0+C_{n+1}^1...+C_{n+1}^m
=C_{n}^0+(C_{n}^0+C_n^1)+...+(C_n^{m-1}+C_n^m)
=2\times S_n^m-C_n^m\)

若已知S(m,n),则可在O(1)的时间内得到S(m-1,n),S(m+1,n),S(m,n-1),S(m,n+1),莫队即可。

#include<bits/stdc++.h>
#define lson rt<<1
#define rson rt<<1|1
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define lowbit(x) (x&(-(x)))
#define mme(a,b) memset((a),(b),sizeof((a)))
#define test printf("**-**\n")
#define fuck(x) cout<<"* "<<x<<"\n"
#define iis std::ios::sync_with_stdio(false)
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int MXN = 2e5 + 7;
const int MXE = 1e6 + 7;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7; int n; const int MX = 1e5 + 5;
LL F[MX], invF[MX];
LL ksm(LL a, LL b){
LL res = 1;
for(;b;b>>=1,a=a*a%mod){
if(b&1)res = res * a % mod;
}
return res;
}
void init() {
F[0] = 1;
for (int i = 1; i < MX; i++) F[i] = F[i - 1] * i % mod;
invF[MX - 1] = ksm(F[MX - 1], mod - 2);
for (int i = MX - 2; i >= 0; --i) invF[i] = invF[i + 1] * (i + 1) % mod;
}
LL COMB(LL n, LL m) {
if(n == m)return 1;
if(n < m) return 0;
return F[n]*invF[m]%mod*invF[n-m]%mod;
} struct lp{
int l, r, id;
}cw[MX];
int belong[MX];
LL ans, Ans[MX];
bool cmp(lp &a,lp &b){
if(belong[a.l]!=belong[b.l])return belong[a.l]<belong[b.l];
return a.r<b.r;
} int main(){
#ifndef ONLINE_JUDGE
freopen("E://ADpan//in.in", "r", stdin);
//freopen("E://ADpan//out.out", "w", stdout);
#endif
init();
scanf("%d", &n);
int block = sqrt(MX*1.0);
for(int i = 1; i < MX; ++i)belong[i] = (i-1)/block;
for(int i = 1; i <= n; ++i){
scanf("%d%d", &cw[i].r, &cw[i].l);
cw[i].id = i;
}
sort(cw+1,cw+n+1,cmp);
int L = 1, R = 0;
ans = 1LL;
LL two = ksm(2LL, mod-2);
for(int i = 1; i <= n; ++i){
while(R<cw[i].r)ans = ((ans * 2LL - COMB(R++, L))%mod+mod)%mod ;
while(R>cw[i].r)ans = (ans + COMB(--R, L))*two%mod ;
while(L<cw[i].l)ans = (ans + COMB(R,++L))%mod ;
while(L>cw[i].l)ans = (ans - COMB(R,L--) + mod)%mod ;
Ans[cw[i].id] = ans;
}
for(int i = 1; i <= n; ++i){
printf("%lld\n", Ans[i]);
}
return 0;
}

HDU6333 求组合数前m项的和的更多相关文章

  1. POJ 2478 Farey Sequence(欧拉函数前n项和)

    A - Farey Sequence Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  2. 39. 求分数序列前N项和

    求分数序列前N项和 #include <stdio.h> int main() { int i, n; double numerator, denominator, item, sum, ...

  3. 20. 求阶乘序列前N项和

    求阶乘序列前N项和 #include <stdio.h> double fact(int n); int main() { int i, n; double item, sum; whil ...

  4. 19. 求平方根序列前N项和

    求平方根序列前N项和 #include <stdio.h> #include <math.h> int main() { int i, n; double item, sum; ...

  5. 18. 求交错序列前N项和

    求交错序列前N项和 #include <stdio.h> int main() { int numerator, denominator, flag, i, n; double item, ...

  6. 12. 求简单交错序列前N项和

    求简单交错序列前N项和 #include <stdio.h> int main() { int denominator, flag, i, n; double item, sum; whi ...

  7. 11. 求奇数分之一序列前N项和

    求奇数分之一序列前N项和 #include <stdio.h> int main() { int denominator, i, n; double item, sum; while (s ...

  8. 10. 求N分之一序列前N项和

    求N分之一序列前N项和 #include <stdio.h> int main() { int i, n; double item, sum; while (scanf("%d& ...

  9. 递归函数练习:输出菲波拉契(Fibonacci)数列的前N项数据

    /*====================================================================== 著名的菲波拉契(Fibonacci)数列,其第一项为0 ...

随机推荐

  1. javascript全量匹配屏蔽词

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 52、saleforce 导入csv文件

    Load Data Using the Custom Object Import Wizard 1. 2. 3. 4. 5. 6.然后就导入成功了

  3. %matplotlib inline 被注释掉后,pycharm不能生成图

    目录 问题描述 解决方案 @ 问题描述 在 jupyter 编译器中 程序的开头,有这么一行 %matplotlib inline import numpy as np import matplotl ...

  4. C++异常处理try、catch 没有finally

    程序的错误大致可以分为三种,分别是语法错误.逻辑错误和运行时错误: 1) 语法错误在编译和链接阶段就能发现,只有 100% 符合语法规则的代码才能生成可执行程序.语法错误是最容易发现.最容易定位.最容 ...

  5. WinDows应急响应基础

    文件排查 开机启动有无异常文件 msconfig 敏感的文件路径 %WINDIR% %WINDIR%\SYSTEM32\ %TEMP% %LOCALAPPDATA% %APPDATA% 用户目录 新建 ...

  6. 什么是索引?Mysql目前主要的几种索引类型

    一.索引 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的My ...

  7. java并发锁ReentrantReadWriteLock读写锁源码分析

    1.ReentrantReadWriterLock 基础 所谓读写锁,是对访问资源共享锁和排斥锁,一般的重入性语义为如果对资源加了写锁,其他线程无法再获得写锁与读锁,但是持有写锁的线程,可以对资源加读 ...

  8. python基础【第十篇】

    Python文件操作 1.常规格式 f = open(file="文件所在路径/文件名",mode="操作模式",encoding="选择的编码&qu ...

  9. python配置文件configparser详解

    Python中一般需要配置文件,配置文件一般以.cfg, .conf, .ini结尾.配置文件可以将数据库抽离到以 .ini(Windows)结尾的文件中,这样做的优点在于可在配置文件中添加多个数据库 ...

  10. BUUCTF 派大星的烦恼

    这道题做的累死了,题目关键在于思路,这里将做题的完整思路记下来.题目给了一张bmp,用010打开可以看出题目关键就在于这一段D和“,保存出来 "DD"DD""& ...