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 ...
随机推荐
- leaflet结合geoserver利用WFS服务实现图层删除功能(附源码下载)
前言 leaflet 入门开发系列环境知识点了解: leaflet api文档介绍,详细介绍 leaflet 每个类的函数以及属性等等 leaflet 在线例子 leaflet 插件,leaflet ...
- Android布局管理器-使用LinearLayout实现简单的登录窗口布局
场景 Android布局管理器-从实例入手学习相对布局管理器的使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1038389 ...
- PG数据库常用操作
全量迁移 备份数据 $ pg_dump -h 172.19.235.145 -U <username> -d <database> > 20180704_dbpe.sql ...
- count(1)比count(*)效率高?
SELECT COUNT(*) FROM table_name是个再常见不过的统计需求了. 本文带你了解下Mysql的COUNT函数. 一.COUNT函数 关于COUNT函数,在MySQL官网中有详细 ...
- 《自拍教程22》wget_文件下载工具
wget用途介绍 日常测试过程中,我们可以用wget命令,来下载一些资源文件. wget是一个很好文件下载命令, Linux操作系统下,自带wget命令. Windows操作系统下,需要自己去下载并配 ...
- NB-Iot和GPRS信号通信模式的对比
NB-Iot和GPRS信号通信模式的对比
- 树莓派搭载CentOS7系统初始配置
系统属性: 树莓派型号:3b SD:32GB 系统:CentOS-Userland-7-armv7hl-RaspberryPI-Minimal-1908-sda.raw 开机配置: 连接树莓派: 配件 ...
- go 面向对象
结构体 创建结构体变量和访问结构体字段 package main import "fmt" //创建结构体变量和访问结构体字段 type Person struct { Name ...
- mysql在node中的一些操作
mysql 服务: a) 安装wamp|xamp 开启 mysql服务 b) 安装mysql 开启服务 库操作: 客户端:软件操作(UI工具) wamp的客户端是phpmyadmin navicat ...
- 重启防火墙(iptables)命令#service iptable restart失效
Redirecting to /bin/systemctl restart iptables.ser linux下执行防火墙相关指令报错: Redirecting to /bin/systemctl ...