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 ...
随机推荐
- 100个实用的CSS技巧,以及遇见的问题和解决方案。
前言 本篇文章将会持续更新,我会将我遇见的一些问题,和我看到的实用的CSS技巧记录下来,本篇文章会以图文混排的方式写下去.具体什么时候写完我也不清楚,反正我的目标是写100个. 本案例都是经本人实测 ...
- Cheatsheet: 2017 02.01 ~ 02.28
Web Debouncing and Throttling Explained Through Examples What is TypeScript? An Absolute Beginner's ...
- Win10自带Ubuntu子系统下Mysql安装踩坑记录
linux系统为win10自带Ubuntu子系统 错误的安装过程 我按照一般的方法安装mysql,安装步骤如下 1.升级源 $ sudo apt-get update 2.安装mysql $ sudo ...
- html元素 input各种输入限制
1.取消按钮按下时的虚线框,在input里添加属性值 hideFocus 或者 HideFocus=true <input type="submit" value=" ...
- lintcode 刷题记录··
单例模式,用C#实现过单例模式,python区别就是类里边的静态方法写法不一样,python叫类方法,函数之前加@classmethod class Solution: # @return: The ...
- Ant design 项目打包后报错:"Menu(or Flex) is not defined"
我的项目使用了ant-design 和 ant-design-mobile,在测试环境上没问题,但是打包发布之后控制台报错 Menu is not defined Flex is not define ...
- Linux下mongodb
Linux下mongodb安装: 新建mongodb文件夹 下载安装包 curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3. ...
- Python基础-继承与派生
一.继承 继承是一种创建新的类的方式,在python中,新建的类可以继承自一个或者多个父类,原始类称为基类或超类,新建的类称为派生类或子类. python中类的继承分为:单继承和多继承 class P ...
- C语言实现整数数组的逆置算法
读入100个整数到一个数组中,写出实现该数组进行逆置的算法. 方法一: 假设100个整数读入到数组a中,算法f1的思想是分别从数组两端依次将对应数进行交换,即a[i]与a[100 - i - 1]进行 ...
- Apache服务器运维笔记(3)----容器部分
1.<IfModule>容器 <IfModule>容器作用于模块,它会首先判断模块是否载入,然后再决定是否进行处理,也就是说只有当判断结果为真时才会执行容器内的指令,相反如果为 ...