HDU6333 求组合数前m项的和
@
HDU6333:传送门
题意:求组合数前m项的和。
在线分块or离线莫队
分块
重要的一个定理:
\]
\]
\]
然后分块处理,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项的和的更多相关文章
- POJ 2478 Farey Sequence(欧拉函数前n项和)
A - Farey Sequence Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- 39. 求分数序列前N项和
求分数序列前N项和 #include <stdio.h> int main() { int i, n; double numerator, denominator, item, sum, ...
- 20. 求阶乘序列前N项和
求阶乘序列前N项和 #include <stdio.h> double fact(int n); int main() { int i, n; double item, sum; whil ...
- 19. 求平方根序列前N项和
求平方根序列前N项和 #include <stdio.h> #include <math.h> int main() { int i, n; double item, sum; ...
- 18. 求交错序列前N项和
求交错序列前N项和 #include <stdio.h> int main() { int numerator, denominator, flag, i, n; double item, ...
- 12. 求简单交错序列前N项和
求简单交错序列前N项和 #include <stdio.h> int main() { int denominator, flag, i, n; double item, sum; whi ...
- 11. 求奇数分之一序列前N项和
求奇数分之一序列前N项和 #include <stdio.h> int main() { int denominator, i, n; double item, sum; while (s ...
- 10. 求N分之一序列前N项和
求N分之一序列前N项和 #include <stdio.h> int main() { int i, n; double item, sum; while (scanf("%d& ...
- 递归函数练习:输出菲波拉契(Fibonacci)数列的前N项数据
/*====================================================================== 著名的菲波拉契(Fibonacci)数列,其第一项为0 ...
随机推荐
- 【Flutter学习】页面布局之其它布局处理
一,概述 Flutter中拥有30多种预定义的布局widget,常用的有Container.Padding.Center.Flex.Row.Colum.ListView.GridView.按照< ...
- uni-app获取元素宽高封装
getElSize(id) { //得到元素的size return new Promise((res, rej) => { uni.createSelectorQuery().select(' ...
- 2019ccpc秦皇岛/Gym102361 F Forest Program 仙人掌上dfs
题意: 某地沙漠化严重,沙漠里长了很多仙人掌,现在要让你删掉仙人掌的一些边让它的所有连通分量都是树,就完成了沙漠绿化(什么鬼逻辑?)让你计算删边的方案数. 仙人掌是一种特殊的图,它的每一条边只属于1或 ...
- postgresql的规则系统
" class="wiz-editor-body wiz-readonly" contenteditable="false"> Postgres ...
- 8086汇编和Win32汇编
8086汇编是指在某环境下汇编编译产生的程序,用机器去执行每条指令的长度为16位(可小于16),如DOS操作系统:WIN32汇编是32位环境下的汇编,如Windows(Windows也有64位的,XP ...
- 公司-ofo:ofo
ylbtech-公司-ofo:ofo ofo小黄车是一个无桩共享单车出行平台,缔造了“无桩单车共享”模式,致力于解决城市出行问题.用户只需在微信公众号或App扫一扫车上的二维码或直接输入对应车牌号,即 ...
- sqlserver2012分页注意事项
SELECT orderid, orderdate, custid, empid FROM Sales.Orders ORDER BY orderdate, orderid OFFSET 600 RO ...
- Spring JAR下载地址
包含3.2版本及以上 http://repo.spring.io/libs-release-local/org/springframework/spring/ 包含从2.0开始的所有版本 http:/ ...
- python基础【第六篇】
list列表 基本结构 lst =[1,2,3,5,6] 为什么学列表? 列表能够存储比字符串更多的数据 列表能够存储各种数据类型 列表的注意点 列表是有序的 列表是可变的,支持索引,切片,步 切片后 ...
- 代码编译与反编译 (.py文件与.pyc文件互转)
# 将.py文件转化为.pyc文件,实现代码隐藏的需要,转化后的.pyc文件将在当前目录的__pycache__文件夹下. # .pyc文件的使用与.py文件的使用相同. .py -> .pyc ...