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 ...
随机推荐
- python 内置模块-re
想要在python中使用正则表达式,就需要先导入re模块,正则表达式是一个强大的功能,可以为我们节省很多工作量. 一.元字符: 用一些具有特殊含义的符号表示特定种类的字符或位置. . 匹配除换 ...
- Hadoop 权限管理(转)
如下图,hadoop访问控制分为两级,其中ServiceLevel Authorization为系统级,用于控制是否可以访问指定的服务,例如用户/组是否可以向集群提交Job,它是最基础的访问控制,优先 ...
- 栈Stack --- 数组实现
栈最大的一个特点就是先进后出(FILO—First-In/Last-Out). /** * 栈:后进先出 * Created by fred on 2018/7/31. */ public class ...
- 65、salesforce的数据分页
<apex:page controller="PagingController"> <apex:form > <apex:pageBlock titl ...
- linux安装相关软件
XShell上传jdk文件到Linux并安装配置1.yum -y install lrzsz2.sudo rz选文件3.sudo tar -zxvf jdk-8u131-linux-x64.tar.g ...
- PAT_A1089#Insert or Merge
Source: PAT A1089 Insert or Merge (25 分) Description: According to Wikipedia: Insertion sort iterate ...
- Cocos2d-x之Scene
| 版权声明:本文为博主原创文章,未经博主允许不得转载. Scene场景也是cocos2dx中必不可少的元素,游戏中通常我们需要构建不同的场景(至少一个),游戏里关卡.版块的切换也就是一个一个场景 ...
- nmon 定时任务 监控资源
nmon命令: # ./nmon –f -s 30 –c 100 说明:-f 以文件的形式输出,默认输出是机器名+日期.nmon的格式,也可以用-F指定输出的文件名,例如: # ./nmon_x8 ...
- spark复习总结02
1.spark执行原理图 spark程序启动后创建sparkContext作为程序的入口,sparkContext可以与不同类的集群资源管理器(Cluster Manager)进行通信,从而获得程序运 ...
- MySQL源码编译与初始化
MySQL源码编译与初始化 链接:https://pan.baidu.com/s/1ANGg3Kd_28BzQrA5ya17fQ 提取码:ekpy 复制这段内容后打开百度网盘手机App,操作更方便哦 ...