HDU - 6333 Problem B. Harvest of Apples (莫队+组合数学)
题意:计算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 ;
- }
HDU - 6333 Problem B. Harvest of Apples (莫队+组合数学)的更多相关文章
- HDU - 6333 Problem B. Harvest of Apples (莫队)
There are nn apples on a tree, numbered from 11 to nn. Count the number of ways to pick at most mm a ...
- Problem B. Harvest of Apples 莫队求组合数前缀和
Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...
- HDU-6333 Problem B. Harvest of Apples 莫队
HDU-6333 题意: 有n个不同的苹果,你最多可以拿m个,问有多少种取法,多组数据,组数和n,m都是1e5,所以打表也打不了. 思路: 这道题要用到组合数的性质,记S(n,m)为从n中最多取m个的 ...
- HDU 6333.Problem B. Harvest of Apples-组合数C(n,0)到C(n,m)求和-组合数学(逆元)+莫队 ((2018 Multi-University Training Contest 4 1002))
2018 Multi-University Training Contest 4 6333.Problem B. Harvest of Apples 题意很好懂,就是组合数求和. 官方题解: 我来叨叨 ...
- 2018 Multi-University Training Contest 4 Problem B. Harvest of Apples 【莫队+排列组合+逆元预处理技巧】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6333 Problem B. Harvest of Apples Time Limit: 4000/200 ...
- hdu6333 Problem B. Harvest of Apples(组合数+莫队)
hdu6333 Problem B. Harvest of Apples 题目传送门 题意: 求(0,n)~(m,n)组合数之和 题解: C(n,m)=C(n-1,m-1)+C(n-1,m) 设 ...
- hdu6333 Harvest of Apples 离线+分块+组合数学(求组合数模板)
Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K ...
- 【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples
http://acm.hdu.edu.cn/showproblem.php?pid=6333 莫队算法是一个离线区间分块瞎搞算法,只要满足:1.离线 2.可以O(1)从区间(L,R)更新到(L±1, ...
- Problem B. Harvest of Apples(杭电2018年多校+组合数+逆元+莫队)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 题目: 题意:求C(n,0)+C(n,1)+……+C(n,m)的值. 思路:由于t和n数值范围太 ...
随机推荐
- C++ 类模板三(类模版中的static关键字)
//类模版中的static关键字 #include<iostream> using namespace std; /* 类模板本质上是c++编译器根据类型参数创建了不同的类, c++编译器 ...
- C语言 函数指针一(函数指针的定义)
//函数指针 #include<stdio.h> #include<stdlib.h> #include<string.h> //函数指针类型跟数组类型非常相似 / ...
- Java多线程基础知识总结
2016-07-18 15:40:51 Java 多线程基础 1. 线程和进程 1.1 进程的概念 进程是表示资源分配的基本单位,又是调度运行的基本单位.例如,用户运行自己的程序,系统就创建一个进程, ...
- cobbler default system 网络安装时主机的menu上只有一个local选项
问题:使用cobbler default system 做pxe网络安装时,主机启动后安装menu上只有一个local选项,看不到对应的system名字 解决:cobbler default syst ...
- iOS 保存异常日志
// // AppDelegate.m // test // // Created by Chocolate. on 14-4-16. // Copyright (c) 2014年 redasen. ...
- iOS Xcode之SVN(remove git)
项目用SVN比较多,所以大家都把精力放在如何在XCODE上使用SVN. 配置SVN当然是很简单,但提交都默认出现git的提交窗,否则要到repositories界面去提交. 目前没有找到什 ...
- PAT 1007 Maximum Subsequence Sum(最长子段和)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- delphi --批量添加
公共批量添加方法 function BatchSQL(DC : TADOConnection; Qry : TADOQuery; StrSQL : TStrings): Boolean; var i ...
- 巨蟒python全栈开发flask4
1.偏函数 2.ThreadingLocal线程安全 空间换取时间 3.LocalStack 4.RunFlask+request 5.请求上文 6.请求下文
- JavaScript数据类型转换汇总
ECMAScirpt中的数据类型:undefined.Null.Boolean.Number.String.Object 对一个值使用typeof操作符可能返回下列某个字符串: number(数字). ...