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 ...
随机推荐
- NaviCat连接提示驱动程序IM004错误
今天一打开NaviCat提示驱动程序IM004错误,百度了好几种方法都不起作用,多次尝试后总结出一套方法: 一.到控制面板--卸载程序--卸载NaviCat Clien 注意可能有多个版本的,2008 ...
- 阿里一面,说说你对Mysql死锁的理解
又到了金三银四的时候,大家都按耐不住内心的躁动,我在这里给大家分享下之前面试中遇到的一个知识点(死锁问题),如有不足,欢迎大佬们指点指点. 1.什么是死锁? 死锁指的是在两个或两个以上不同的进程或线程 ...
- Activemq持久化之kahadb特性
介绍数据的持久化是很多系统都会涉及到的一个问题,尤其是redis,activemq这些数据主要是存储在内存中的.既然存在内存中,就会面临宕机时数据丢失的风险.这一问题的解决方案就是通过某种方式将数据写 ...
- Android 12(S) 图形显示系统 - 简单聊聊 SurfaceView 与 BufferQueue的关联(十三)
必读: Android 12(S) 图形显示系统 - 开篇 一.前言 前面的文章中,讲解的内容基本都是从我们提供的一个 native demo Android 12(S) 图形显示系统 - 示例应用( ...
- 《前端运维》二、Nginx--4代理、负载均衡与其他
一.代理服务 比较容易理解吧,简单来说.客户端访问服务器并不是直接访问的,而是通过中间代理服务器,代理服务器再去访问服务器.就像一个中转站一样,无论什么,只要从客户端到服务器,你就要通过我. 一)正向 ...
- 羽夏看Win系统内核——消息机制篇
写在前面 此系列是本人一个字一个字码出来的,包括示例和实验截图.由于系统内核的复杂性,故可能有错误或者不全面的地方,如有错误,欢迎批评指正,本教程将会长期更新. 如有好的建议,欢迎反馈.码字不易, ...
- 数据库篇:mysql锁详解
前言 sql事务的执行,如果需要锁定数据进行更新操作,则必定离不开锁 共享锁和排他锁 表锁 行锁 Record Lock 间隙锁 Gap Lock 行锁+间隙锁 Next-Key Lock 加锁场景( ...
- Vscode的使用小技巧
命令行启动code 如果你的系统是Linux系统(我使用的是Ubuntu 16.04)这样就可以直接使用 code + filename来编辑文件(就像vi + filename) 如果你的系统是Ma ...
- Nacos:Nacos与OpenFeign的对接使用
Nacos(三):Nacos与OpenFeign的对接使用 上篇文章中,简单介绍了如何在SpringCloud项目中接入Nacos作为注册中心,其中服务消费者是通过RestTemplate+Rib ...
- Java BlockingQueue是什么?
Java BlockingQueue是一个并发集合util包的一部分.BlockingQueue队列是一种支持操作,它等待元素变得可用时来检索,同样等待空间可用时来存储元素.