The 18th Zhejiang Provincial Collegiate Programming Contest
The 18th Zhejiang Provincial Collegiate Programming Contest
GYM链接 https://codeforces.com/gym/103055
F
题意:
给定两个整数\(n\)和\(m\),有两种操作:
- 当\(n\geq 2\)时,将\(n\)的值减少\(1\)。
- 将\(m\)的值增加\(1\)。
求最小操作数,使得\(n|m\)。
思路:
显然,当\(n\geq m\)时答案为\(n - m\), 下面我们来讨论当\(n < m\)时的情况:
我们假设当取得答案时的\(n\)的值为\(x\),对于此情况下,要使满足题意的\(m\)的值为\(x\times \lceil \frac {m} {x} \rceil\).
下面我们先来证明这个结论:
记此时的\(m\)为\(m_0\).
\(\because\) \(m\)%\(x\neq0\).
\(\therefore\) \(m = x\times q + c.(c < x)\) \(\Rightarrow q = \frac{m - c}{x}.\)
显然 \(m_0 = x \times(q + 1) = x \times (\frac {m - c}{x} + 1) = x \times (\frac{m - c + x}{x})\).
\(\because\) \(c < x\).
\(\therefore\) \(\frac{m - c + x}{x} = \lceil\)\(\frac{m}{x}\rceil\).
\(\therefore\) $m_0 = x \times $ \(\lceil\)\(\frac{m}{x}\rceil\).
所以对于给定的\(x\)我们的最终答案$ans = x \times $ \(\lceil\)\(\frac{m}{x}\rceil - m + n - x\),显然我们只要考虑 \(x \times \lceil \frac mx \rceil - x\).
那么问题就变成了求 \(f(x)_{min} = x \times \lceil \frac mx \rceil - x.(1 \leq x \leq n)\)
我们发现对于这个式子,我们除了从\(1\)到\(n\)去枚举,我们别无他法,但这显然时间复杂度较高,我们是不可以接受的,所以我们要继续化简它。
\(f(x) = x \times \lceil \frac mx \rceil - x = x \times \lfloor \frac{m + x -1}{x} \rfloor - x = x \times(\lfloor\frac{m + x -1 }{x} \rfloor - 1) = x \times(\lfloor\frac{m + x - x - 1}{x} \rfloor)= x \times \lfloor \frac {m-1}{x} \rfloor\)
到了这里,我们终于看到了一个熟悉的式子,上面这个式子我们可以使用整除分块来解决。
下面我们再来解释一下如何解决上面这个式子:
我们先忽略对\(m\)的减\(1\),我们很容易可以发现,对于固定的\(m\)和任意的\(x\),有相当连续一段的\(x\)对于\(\lfloor \frac {m }{x}\rfloor\)的值是一样的,我们把值相同的所有连续的\(x\)切割成一段,本题让我们求的是最小值,那我们只要枚举每一段的第一个数取最小值就可以了,那么究竟每一段右端是多少呢?
对于\(\forall x(x\leq m)\),我们要找到一个最大的\(j\),使得\(\lfloor \frac {m}{x}\rfloor\) = \(\lfloor \frac {m}{j}\rfloor\).
我们设\(k = \lfloor \frac {m}{x}\rfloor\),那么:
\(\lfloor \frac {m}{j}\rfloor = k \Leftrightarrow k \leq \frac mj < k + 1 \Leftrightarrow \frac {1}{k + 1} < \frac jm \leq \frac 1k \Leftrightarrow \frac{m}{k + 1} < j \leq \frac mk\),又因为\(j\)是整数,所以\(j_{max} = \lfloor \frac mk \rfloor = \lfloor \frac{m}{\lfloor \frac m x\rfloor} \rfloor\).
至此,我们终于找到了这个区间的右端。所以我们可以在\(O(\sqrt m)\) 的时间内枚举完成.
代码:
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
const double eps = 1e-6;
const ll N = 1e3 + 10;
const ll M = 4e6 + 10;
const ll INF = 1e8+10;
const ll mod = 1e9+7;
#define ywh666 std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define all(a) a.begin(),a.end()
int main(){
ywh666;
int t;
cin >> t;
while(t --){
int m, n;
cin >> n >> m ;
int mi = 0x3f3f3f3f;
if(n >= m){
cout << n - m << endl;
}else{
m -- ;
for(int l = 1, r ; l <= n ; l = r + 1){
r = min(n, m / (m / l));
mi = min(mi, (m / l) * l);
}
cout << mi + n - m - 1 << endl;
}
}
return 0 ;
}
The 18th Zhejiang Provincial Collegiate Programming Contest的更多相关文章
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Capture the Flag
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5503 The 12th Zhejiang Provincial ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5496 The 12th Zhejiang Provincial ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Lunch Time
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5499 The 12th Zhejiang Provincial ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Convert QWERTY to Dvorak
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5502 The 12th Zhejiang Provincial ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest May Day Holiday
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5500 The 12th Zhejiang Provincial ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Demacia of the Ancients
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5504 The 12th Zhejiang Provincial ...
- zjuoj The 12th Zhejiang Provincial Collegiate Programming Contest Ace of Aces
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5493 The 12th Zhejiang Provincial ...
- 140 - The 12th Zhejiang Provincial Collegiate Programming Contest(第二部分)
Floor Function Time Limit: 10 Seconds Memory Limit: 65536 KB a, b, c and d are all positive int ...
随机推荐
- 【文件系统】dumpe2fs命令
dumpe2fs - dump ext2/ext3/ext4 filesystem information dumpe2fs prints the super block and blocks gro ...
- mtu的原理和作用
MTU: 最大传输单元,是指一种通信协议的某一层上面所能通过的最大数据包大小,最大传输单元这个参数通常与通信接口有关. 因为协议数据单元的包头和包尾的长度是固定的,MTU越大,则一个协议数据单元的承载 ...
- 有关电控制图软件EPLAN的安装,下面有破解版本2.7
前段时间刚刚接触这一块,就安装个软件老是出问题,所以我通过自己的努力学会啦,来给正要学习EPLAN的同学发福利啦 15:07:48 安装包发放在百度网盘来自取呀 建议安装我勾选的这个哦 链接:htt ...
- 架构师成长之路也该了解的新一代微服务技术-ServiceMesh(上)
架构演进 发展历程 我们再来回顾一下架构发展历程,从前往后的顺序依次为单机小型机->垂直拆分->集群化负载均衡->服务化改造架构->服务治理->微服务时代 单机小型机:采 ...
- Flutter查漏补缺1
Flutter 基础知识查漏补缺 Hot reload原理 热重载分为这几个步骤 扫描项目改动:检查是否有新增,删除或者改动,直到找到上次编译后发生改变的dart代码 增量编译:找到改变的dart代码 ...
- tea加密算法及其变种的研究
tea 介绍 "TEA" 的全称为"Tiny Encryption Algorithm" 是1994年由英国剑桥大学的David j.wheeler发明的. T ...
- 描述一下 JVM 加载 class 文件的原理机制?
JVM 中类的装载是由类加载器(ClassLoader)和它的子类来实现的,Java 中的 类加载器是一个重要的 Java 运行时系统组件,它负责在运行时查找和装入类文件 中的类. 由于 Java 的 ...
- 转载:2017百度春季实习生五道编程题[全AC]
装载至:https://blog.csdn.net/zmdsjtu/article/details/70880761 1[编程题]买帽子 时间限制:1秒空间限制:32768K度度熊想去商场买一顶帽子, ...
- 学习Kvm(四)
安装KVM虚拟化 1.系统基础环境: [root@linux-node1 ~]# ip addr | grep inet | awk '{ print $2; }' | sed 's/\/.*$//' ...
- 深入 x64
本篇原文为 X64 Deep Dive,如果有良好的英文基础的能力,可以点击该链接进行阅读.本文为我个人:寂静的羽夏(wingsummer) 中文翻译,非机翻,著作权归原作者所有. 由于原文十 ...