D - Three Integers
https://codeforces.com/contest/1311/problem/D
本题题意:给出a,b,c三个数,a<=b<=c;
可以对三个数中任意一个进行+1或-1的操作;
问题:求出最少操作数使这些数满足:b整除a,c整除b
思路:题目中给出abc的范围只有1e4
所以我们可以通过枚举的方式来找出答案;
我们通过枚举b的大小,然后计算在b为k值得情况下,a,c为哪个数最优
暴力枚举出最优情况即可;
细节:在枚举b为k时,对于a,我们可以通过预处理出b的因子,然后枚举因子与原本的数的差值,找出最优即可;
而对于c,有以下情况:
1.对于b>c的情况,我们只需要让c等于b
2.对于c>b的情况,我们有两种可能,1.c已经整除b,这种需要的操作数为0
2.c没整除b,所以可能让c减少到为b的倍数,或者增大到b的倍数,两者枚举找操作数小的即可;
所以这道题的做法就是:先预处理出范围内的因子,然后枚举;
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define me(a,x) memset(a,x,sizeof a)
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define all(x) (x).begin(), (x).end()
#define pb(a) push_back(a)
#define paii pair<int,int>
#define pali pair<ll,int>
#define pail pair<int,ll>
#define pall pair<ll,ll>
#define fi first
#define se second
vector<int>g[];
int a,b,c;
void inist()
{
for(int i=;i<=;i++){
for(int j=;j<=/i;j++)
g[i*j].pb(i);
} }
int work(int& aa,int bb,int& cc)
{
int ans=;
int minn=;
int l=g[bb].size();
int x=aa;
for(int i=;i<l;i++){
if(minn>abs(g[bb][i]-aa)){
minn=abs(g[bb][i]-aa);
x=g[bb][i];
}
}
aa=x;
ans+=minn;
if(bb>cc){
ans+=abs(bb-cc);
cc=bb;
}
if(cc%bb<bb-cc%bb){
ans+=cc%bb;
cc-=cc%bb;
}
else{
ans+=bb-cc%bb;
cc+=bb-cc%bb;
}
return ans;
}
int main()
{
inist();
int t;
cin>>t;
while(t--){
int ans=;
int ansa,ansb,ansc;
cin>>a>>b>>c;
for(int i=;i<=;i++){
int a1=a,b1=i,c1=c;
int temp=abs(i-b);
temp+=work(a1,b1,c1);
if(temp<ans){
ans=temp;
ansa=a1;ansb=b1;ansc=c1;
}
}
cout<<ans<<endl;
cout<<ansa<<" "<<ansb<<" "<<ansc<<endl;
}
return ;
}
D - Three Integers的更多相关文章
- [LeetCode] Sum of Two Integers 两数之和
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- LeetCode Sum of Two Integers
原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...
- Nim Game,Reverse String,Sum of Two Integers
下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap o ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- LeetCode 371. Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- leetcode-【中等题】Divide Two Integers
题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...
- 解剖SQLSERVER 第十三篇 Integers在行压缩和页压缩里的存储格式揭秘(译)
解剖SQLSERVER 第十三篇 Integers在行压缩和页压缩里的存储格式揭秘(译) http://improve.dk/the-anatomy-of-row-amp-page-compre ...
随机推荐
- Laravel + Serverless Framework 快速创建 CMS 内容管理系统
今天,为大家带来一篇 Laravel + Serverless Framework 的综合实战,里面信息量有点多,大家仔细看哦- 首先,我来介绍下主要的本地环境吧: Git:不多说,只要会敲代码就应该 ...
- toj 3019 Hidden Password (最小表示法)
Hidden Password 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交: 53 测试通过: 19 描述 Some time the progr ...
- Git push时不需要总输入密码
遇到问题: 最近因为换了自己的邮箱密码后,每次push的时候都需要填写密码,账号.很烦 解决方法: [戎马半生的答案] (http://www.cnblogs.com/zhaoyu1995/p/650 ...
- JS-重写内置的call、apply、bind
首先看call和apply,第一个参数就是改变的this指向,写谁就是谁,如果是非严格模式下,传递null或undefined指向的也是window,二者唯一的区别是执行函数时,传递的参数方式不同,c ...
- StarUML之九、starUML的一些特殊属性的说明
UML的扩充性机制允许你在控制的方式下扩充UML语言. 这一类的机制包括:stereotype,标记值.约束. Stereotype扩充了UML的词汇表,允许你创建新的建筑块,这些建筑块从已有的继承而 ...
- vim配置markdown预览
注:系统为Centos7,安装了gnome桌面环境. 我的vim装了vimplus工具包,看代码一等一的方便,但是看md的时候还是有些不快乐.于是想找个插件实现markdown的预览. 尝试了几个vi ...
- npm常用命令和脚手架使用
最近前端同学使用最多的莫过于vue,angualr,react等热门前端框架了.那么就避免不掉的使用npm命令,本人就经常因为这三个脚手架的使用而不得不百度相应的npm命令,不胜其烦,,,因此就整理一 ...
- Java基础之五、Java编程思想(1-7)
一.对象导论 1:多态的可互换对象 面向对象程序设计语言使用了后期绑定的概念. 当向对象发送消息时,被调用的代码直到运行时才能确定.也叫动态绑定. 2:单根继承结构 所有的类最终都继承自单一的基类,这 ...
- MySQL必知存储引擎
Mysql存储引擎 1.MyISAM MySQL 5.0 之前的默认数据库引擎,最为常用.拥有较高的插入,查询速度,但不支持事务. 2.InnoDB事务型数据库的首选引擎,支持ACID事务,支持行级锁 ...
- win2012 挂载硬盘即增加新硬盘方法
这篇文章主要介绍了win2012 挂载硬盘即增加新硬盘方法,需要的朋友可以参考下 点击左下角的服务器管理图标 点击右上角的“工具”,再选择“计算机管理” 再点击“磁盘管理” 在磁盘1的按钮处单击右键, ...