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. Linux常用性能诊断命令详解

    top top命令动态地监视进程活动与系统负载等信息. 使用示例: top 效果如下图: 以上命令输出视图中分为两个区域,一个统计信息区,一个进程信息区. 统计信息区: 第一行信息依次为:系统时间.运 ...

  2. 5月7日 python学习总结 MySQL数据库(一)

    一.数据库介绍 1.数据库相关概念 数据库服务器(本质就是一台计算机,该计算机之上安装有数据库管理软件的服务端) 数据库管理系统RDBMS(本质就是一个C/S机构的套接字软件) 库(文件夹)===&g ...

  3. 时序数据库之InfluxDB的基本操作

    1.进入Influxdb的客户端 [root@activity_sentinel ~]# influx 2.数据库的操作 显示所有的数据库名 > show databases name: dat ...

  4. Vscode的使用小技巧

    命令行启动code 如果你的系统是Linux系统(我使用的是Ubuntu 16.04)这样就可以直接使用 code + filename来编辑文件(就像vi + filename) 如果你的系统是Ma ...

  5. mysql 在已存在的表中添加/删除字段约束AUTO_INCREMENT遇到的问题

    1. 在已存在的表中添加字段约束AUTO_INCREMENT修饰符 mysql> alter table user modify uid int auto_increment primary k ...

  6. MybatisPlus 多租户的常见问题

    mybatis plus :https://mp.baomidou.com/guide/interceptor-tenant-line.html 如果最终执行的sql出现select查询没有租户ID, ...

  7. innodb和myisam

    在Mysql数据库中,常用的引擎主要就是2个:Innodb和MyIASM.这篇文章将主要介绍这两个引擎,以及该如何去选择引擎,最后在提一下这2种引擎所使用的数据结构是什么. 首先介绍一下Innodb引 ...

  8. npm安装包出现UNMET DEPENDENCY报错

    出现这个内容应该是包损坏,导致npm无法正常解析,通过 npm ls 命令也可以看到UNMET DEPENDENCY在依赖包上出现,在输出的信息最后有一个错误信息 npm view pkg versi ...

  9. 学习docker(三)

    一.Docker介绍 1.docker容器 docker是宿主机的一个进程,通过namespace实现了资源隔离,通过cgroup实现了资源限制, 通过写时复制技术(copy-on-write)实现了 ...

  10. Heartbeat+DRBD+NFS

    添加路由心跳线 master: # route add -host 10.20.23.111 dev eth2 # echo "/sbin/route add -host 10.20.23. ...