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/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 4043 Accepted Submission(s): 1560
Count the number of ways to pick at most m apples.
Each test case consists of one line with two integers n,m (1≤m≤n≤105).
题意概括:
有 N 个苹果,问最多选 m 个苹果的方案有多少种?
解题思路:
大佬讲的很好了。
https://blog.csdn.net/codeswarrior/article/details/81359075
推出四个公式,算是一道比较裸的莫队了。
Sm−1n=Smn−CmnSnm−1=Snm−Cnm
Sm+1n=Smn+Cm+1nSnm+1=Snm+Cnm+1(或者Smn=Sm−1n+CmnSnm=Snm−1+Cnm)
Smn+1=2Smn−CmnSn+1m=2Snm−Cnm
Smn−1=Smn+Cmn−12Sn−1m=Snm+Cn−1m2(或者Smn=Smn+1+Cmn2Snm=Sn+1m+Cnm2)
需要注意的点较多:
1、精度问题,注意数据范围
2、为了保证精度,除法需要转换为逆元,预处理逆元的技巧
AC code:
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<set>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const LL MOD = 1e9+;
const int MAXN = 1e5+;
LL N, M;
LL fac[MAXN], inv[MAXN];
struct Query
{
int L, R, id, block;
bool operator < (const Query &p)const{
if(block == p.block) return R < p.R;
return block < p.block;
}
}Q[MAXN];
LL res, rev2;
LL ans[MAXN]; LL q_pow(LL a, LL b)
{
LL pans = 1LL;
while(b){
if(b&) pans = pans*a%MOD;
b>>=1LL;
a = a*a%MOD;
}
return pans;
} LL C(int n, int k)
{
return fac[n]*inv[k]%MOD*inv[n-k]%MOD;
} void init()
{
rev2 = q_pow(, MOD-); // 2的逆元
fac[] = fac[] = ;
for(LL i = ; i < MAXN; i++){ //预处理阶乘
fac[i] = fac[i-]*i%MOD;
} inv[MAXN-] = q_pow(fac[MAXN-], MOD-); //逆推预处理阶乘的逆元
for(int i = MAXN-; i >= ; i--){
inv[i] = inv[i+]*(i+)%MOD;
}
} void addN(int posL, int posR)
{
res = (*res%MOD-C(posL-, posR)%MOD + MOD)%MOD;
} void addM(int posL, int posR)
{
res = (res+C(posL, posR))%MOD;
} void delN(int posL, int posR)
{
res = (res+C(posL-, posR))%MOD*rev2%MOD;
} void delM(int posL, int posR)
{
res = (res - C(posL, posR) + MOD)%MOD;
} int main()
{
int T_case;
init();
int len = (int)sqrt(MAXN*1.0);
scanf("%d", &T_case);
for(int i = ; i <= T_case; i++){
scanf("%d%d", &Q[i].L, &Q[i].R);
Q[i].id = i;
Q[i].block = Q[i].L/len;
}
sort(Q+, Q++T_case);
res = ;
int curL = , curR = ;
for(int i = ; i <= T_case; i++){
while(curL < Q[i].L) addN(++curL, curR);
while(curR < Q[i].R) addM(curL, ++curR);
while(curL > Q[i].L) delN(curL--, curR);
while(curR > Q[i].R) delM(curL, curR--);
ans[Q[i].id] = res;
}
for(int i = ; i <= T_case; i++){
printf("%lld\n", ans[i]);
} return ; }
2018 Multi-University Training Contest 4 Problem B. Harvest of Apples 【莫队+排列组合+逆元预处理技巧】的更多相关文章
- 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 (莫队+组合数学)
题意:计算C(n,0)到C(n,m)的和,T(T<=1e5)组数据. 分析:预处理出阶乘和其逆元.但如果每次O(m)累加,那么会超时. 定义 S(n, m) = sigma(C(n,m)).有公 ...
- HDU-6333 Problem B. Harvest of Apples 莫队
HDU-6333 题意: 有n个不同的苹果,你最多可以拿m个,问有多少种取法,多组数据,组数和n,m都是1e5,所以打表也打不了. 思路: 这道题要用到组合数的性质,记S(n,m)为从n中最多取m个的 ...
- 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) 设 ...
- HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 【单调队列优化】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6319 Problem A. Ascending Rating Time Limit: 10000/500 ...
- 2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/100 ...
- 2018 Multi-University Training Contest 4 Problem K. Expression in Memories 【模拟】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6342 Problem K. Expression in Memories Time Limit: 200 ...
- 2018 Multi-University Training Contest 4 Problem E. Matrix from Arrays 【打表+二维前缀和】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6336 Problem E. Matrix from Arrays Time Limit: 4000/20 ...
- 2018 Multi-University Training Contest 4 Problem L. Graph Theory Homework 【YY】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6343 Problem L. Graph Theory Homework Time Limit: 2000 ...
随机推荐
- What is the difference between modified duration, effective duration and duration?
Macaulay Duration (traditionally just called Duration) The formula usually used to calculate a bond' ...
- 深入理解 JSON
我们先来看一个JS中常见的JS对象序列化成JSON字符串的问题,请问,以下JS对象通过JSON.stringify后的字符串是怎样的?先不要急着复制粘贴到控制台,先自己打开一个代码编辑器或者纸,写写看 ...
- Bookstrap4 学习(一)
容器 container 是最基本的lagyout 元素, 并且当使用默认的Grid 系统时, containers 是必须的. <div class="container" ...
- nodejs图片裁剪、缩放、水印
关于nodejs下图片的裁剪.水印,网上的模块很多,主要如下: gm:https://github.com/aheckmann/gm node-canvas:https://github.com/Au ...
- .netcore2.0 有关配置
1.在部署WebApi 或者网站时常用的2个配置数据库连接字符串.绑定Url地址 2.# 数据库连接字符串配置: 默认的配置文件 appsettings.json 添加配置节点: "Conn ...
- UNION ALL 视图 'ImprotHIS2012.dbo.ImportHISData' 不可更新,因为没有找到分区依据列。 Severity 16 State 12
-- 3 更正措施,使约束check一次 Alter Table ImprotHIS_Bak_2011.dbo.ImportHISData with check Check Constraint al ...
- 响应式(2)——bootstrap的响应式
<meta name="viewport" content="width=device-width,user-scalable=no"/> < ...
- ArcGIS for JavaScript 关于路径开发的一些记录(三)
最近被一个bug困扰了两天~ 我新发布了一个NAserver(路径分析服务),但是放在之前的代码里面发现不能生成路径.经过我的调试发现并没有代码并没有报错,并且能够返回所生成路径的Graphic la ...
- Netty入门3之----Decoder和Encoder
Netty强大的地方,是他能方便的实现自定义协议的网络传输.在上一篇文章中,通过使用Netty封装好的工具类,实现了简单的http服务器.在接下来的文章中,我们看看怎么使用他来搭建自定义协议的服务 ...
- ns2.35-classifier.cc
line143:recv() /* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */ /* * Copyri ...