The 18th Zhejiang Provincial Collegiate Programming Contest

GYM链接 https://codeforces.com/gym/103055

F

题意:

给定两个整数\(n\)和\(m\),有两种操作:

  1. 当\(n\geq 2\)时,将\(n\)的值减少\(1\)。
  2. 将\(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的更多相关文章

  1. 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 ...

  2. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...

  3. 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 ...

  4. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Lunch Time

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5499 The 12th Zhejiang Provincial ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. xssgame记录

    xss地址:http://www.xssgame.com/ 直接插入标签 构造语句,注意闭合 注意寻找输出点,这个会进行一次urlencode,和浏览器有关系,firefox过不了 javascrip ...

  2. 解决metasploit的module load fail

    解决metasploit的module load fail 在exploits文件夹下面新建一个文件夹test 把你要用的rb文件放进去 reload_all 就行了

  3. 论文解读(GMI)《Graph Representation Learning via Graphical Mutual Information Maximization》2

    Paper Information 论文作者:Zhen Peng.Wenbing Huang.Minnan Luo.Q. Zheng.Yu Rong.Tingyang Xu.Junzhou Huang ...

  4. 27.Java 飞机游戏小项目

    开篇 游戏项目基本功能开发 飞机类设计 炮弹类设计 碰撞检测设计 爆炸效果的实现 其他功能 计时功能 游戏项目基本功能开发 这里将会一步步实现游戏项目的基本功能. 使用 AWT 技术画出游戏主窗口 A ...

  5. Redis 集群会有写操作丢失吗?为什么?

    Redis 并不能保证数据的强一致性,这意味这在实际中集群在特定的条件下可能会丢失写操作.

  6. 学习zabbix(十)

    Zabbix 3.0 基础介绍 [一] 一.Zabbix介绍   zabbix 简介 Zabbix 是一个高度集成的网络监控解决方案,可以提供企业级的开源分布式监控解决方案,由一个国外的团队持续维护更 ...

  7. 错误问题之“Apache Log4j 漏洞,在版本为包含2.14以内!”

    漏洞概述 Apache Log4j是一个用于Java的日志记录库,其支持启动远程日志服务器. Log4j 1.2 中包含一个 SocketServer 类,该类容易受到不可信数据反序列化的影响,当侦听 ...

  8. IdentityServer4系列 | 混合模式

    一.前言 在上一篇关于授权码模式中, 已经介绍了关于授权码的基本内容,认识到这是一个拥有更为安全的机制,但这个仍然存在局限,虽然在文中我们说到通过后端的方式去获取token,这种由web服务器和授权服 ...

  9. 国际化相对时间格式化API:Intl.RelativeTimeFormat

    原文:The Intl.RelativeTimeFormat API 作者:Mathias Bynens(@mathias) 现代 Web 应用程序通常使用"昨天","4 ...

  10. react 实用项目分享-mock server

    使用react16+router4+mobx+koa2+mongodb做的mock平台 moapi-cli 本地工具版,一行命令 ,方便个人使用 安装 npm i moapi-cli -g 使用 mo ...