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 ...
随机推荐
- 2019 牛客暑期多校 第一场 H XOR (线性基)
题目:https://ac.nowcoder.com/acm/contest/881/H 题意:求一个集合内所有子集异或和为0的长度之和 思路:首先集合内异或和,这是线性基的一个明显标志,然后我们不管 ...
- eclipse 中的.classpath和.project文件的具体作用(转)
转载博客:http://www.cnblogs.com/panjun-Donet/archive/2010/08/25/1807780.html .project是项目文件,项目的结构都在其中定义,比 ...
- (转)使用OpenGL显示图像(三)绘制Shapes
绘制形状 编写:jdneo - 原文:http://developer.android.com/training/graphics/opengl/draw.html 在定义了使用OpenGL绘制的形状 ...
- 51NOD 1005
1005 大数加法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大 ...
- php 字符串 定界符 json_last_error()
字符串的3种赋值 1:单引号 $str = '111111111111 '; 2:双引号 $str =" 11111111111 "; 3:定界符 $str = <<& ...
- JS-监听整个页面上的DOM树变化
# [在线预览](https://jsfiddle.net/1010543618/fyf913t0/) ## 方法 - 使用<Web API 接口>的<MutationObserve ...
- PHP 3DES 加解密(CBC模式,pkcs5padding填充)
1.前言:项目中接入第三方支付遇到3DES加密,以前也没用过,搜了好多,都不适用,各种不对,后来自己结合搜到的终于弄正确了,检测地址:http://tool.chacuo.net/crypt3des. ...
- 从上一个页面跳入新页面时,如何拿URL中的参数
var url = document.URL; //获取当前页面的url var urlA = url.split('?');//以url中的问号进行分割; var goodscode = urlA[ ...
- Linux NIO 系列(04-1) select
目录 一.select 机制的优势 二.select API 介绍与使用 2.1 select 2.2 fd_set 集合操作 2.3 select 使用范例 三.深入理解 select 模型: 四. ...
- 二、检索语句 SELECT、ORDER BY、WHERE
介绍如何使用SELECT语句从表中检索一个或多个数据列 第二章: SELECT语句 SQL语句可以在一行给出,也可以分成许多行,分成多行更容易调试. 多条SQL语句必须以分号 分隔.多数DBMS不 ...