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 ...
随机推荐
- ssm 关于mybatis启动报Result Maps collection already contains value for ...的问题总结
Result Maps collection already contains value for com.zhaike.mapping.ChapterMapper.BaseResultMap Err ...
- web服务器-nginx默认网站
web服务器-nginx默认网站 一 默认网站 server { listen 80; server_name localhost; location / { root html; index ind ...
- java的https的get请求
package com.wl.webservice; import java.io.InputStream; import java.net.HttpURLConnection; import jav ...
- SolidWorks在一个零件中设置不同的尺寸版本
问题 比如想设置一系列螺丝的长度,一个一个建零件非常麻烦,希望在一个零件中设置不同的长度尺寸版本 解决 比如想设置不同的拉伸长度,右键拉伸>配置特征 可以生成新配置,设置不同的D1参数,即可生成 ...
- pytorch方面
(113条消息) Pytorch基础:Torch.mul.Torch.mm与Torch.matmul的异同_名字填充中的博客-CSDN博客_pytorch torch.mul (111条消息) pyt ...
- 怎么根据Comparable方法中的compareTo方法的返回值的正负 判断升序 还是 降序?
public int compareTo(Student o) { return this.age - o.age; // 比较年龄(年龄的升序) } 应该理解成return (-1)×(thi ...
- spring中bean的五种作用域?Spring中的bean是线程安全的吗?
spring中bean的五种作用域 当通过spring容器创建一个Bean实例时,不仅可以完成Bean实例的实例化,还可以为Bean指定特定的作用域.Spring支持如下5种作用域: singleto ...
- 解释 Spring 支持的几种 bean 的作用域?
Spring 框架支持以下五种 bean 的作用域:singleton : bean 在每个 Spring ioc 容器中只有一个实例.prototype:一个 bean 的定义可以有多个实例.req ...
- 学习k8s(一)
一.安装及介绍 1.k8s架构 2.核心组件 3.其他组件 4.安装方式 yum安装: 1.5 最简单,版本低,适合学习 二进制安装: 最繁琐,可以用saltstack安装 kubeadm安装: 谷歌 ...
- 使用Google Closure Compiler高级压缩Javascript代码
背景 前端开发中,特别是移动端,Javascript代码压缩已经成为上线必备条件. 如今主流的Js代码压缩工具主要有: 1)Uglify http://lisperator.net/uglifyjs/ ...