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++ 函数模板二(函数模板重载)
//函数模板重载 #include<iostream> using namespace std; /* 函数模板会进行严格的类型匹配,模板类型不提供隐式类型转化 普通函数能够进行自动类型转 ...
- RGB565 转换 BMP24
今晚有心情,就做完了BMP16(RGB565) 转换成BMP24 的小程序.其中最关键的地方是: // k,WORD类型,是RGB565 的一个点的数据(可以认为是灰度). r=(k & 0x ...
- 【Python】求素数-稍加优化
print 'Find prime number smaller then input number \n' print 'Please input a number:' import datetim ...
- 好用的 Visual Studio插件
Image Optimizer(图片优化)插件 使用行业标准优化任何JPEG,PNG和gif(包括动画gif).可以做有损和无损的优化.
- android去权限反编译,签名,zipalign优化
反编译:上工具ApkTool 下载自行搜索google apktool github cd apktool目录 java -jar apktool_2.0.1.jar d xx.apk 生成xx目录 ...
- jquery动态生成html代码绑定事件
今天工作中需要在页面动态生成html代码,但发现新生成的代码的click事件失效了(非动态生成的代码已经绑定了click事件),于是在网上找了很多解决办法,很多都比较复杂,且使用的jquery都比较老 ...
- Import error: no module named cv2 错误解决方法
Windows: 将opencv安装目录下的cv2.pyd拷贝到Python安装目录里Lib中site-packages Linux: (1)将opencv安装目录下的cv2.so拷贝到Python安 ...
- pandas 读取文件
import pandas as pd import matplotlib.pyplot as plt data = pd.read_csv('G:timeCompare.txt', sep=' ', ...
- mysql的增量备份与全备的脚本
mysql全量备份.增量备份.开启mysql的logbin日志功能.在/etc/my.cnf文件中加入以下代码: [mysqld]log-bin = "/home/mysql/logbin. ...
- Asp.Net WebApi核心对象解析
在接着写Asp.Net WebApi核心对象解析(下篇)之前,还是一如既往的扯扯淡,元旦刚过,整个人还是处于晕的状态,一大早就来处理系统BUG,简直是坑爹(好在没让我元旦赶过来该BUG),队友挖的坑, ...