Codeforces Round #624 (Div. 3)D. Three Integers

D. Three Integers
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given three integers a≤b≤c.

In one move, you can add +1 or −1 to any of these integers (i.e. increase or decrease any number by one). You can perform such operation any (possibly, zero) number of times, you can even perform this operation several times with one number. Note that you cannot make non-positive numbers using such operations.

You have to perform the minimum number of such operations in order to obtain three integers A≤B≤C such that B is divisible by A and C is divisible by B.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤100) — the number of test cases.

The next t lines describe test cases. Each test case is given on a separate line as three space-separated integers a,b and c (1≤a≤b≤c≤104).

Output
For each test case, print the answer. In the first line print res — the minimum number of operations you have to perform to obtain three integers A≤B≤C such that B is divisible by A and C is divisible by B. On the second line print any suitable triple A,B and C.

Example
inputCopy
8
1 2 3
123 321 456
5 10 15
15 18 21
100 100 101
1 22 29
3 19 38
6 30 46
outputCopy
1
1 1 3
102
114 228 456
4
4 8 16
6
18 18 18
1
100 100 100
7
1 22 22
2
1 19 38
8
6 24 48

题意:三个数 abc 操作有加一或者减一 求最小的操作数使得 b可以整除a c可以整除b
思路:涨姿势了 数据范围 1e4 没想到直接暴力枚举因子数 复杂度nlogn 询问大佬过后这应该是
nloglog (上限要开大点 不然会wa)

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define pb(x) push_back(x)
#define debug(x) cout<<"..........."<<x<<endl;
#define fi first
#define se second

const int N = 11005;
const int M = 2e5+5;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;


int main()
{
    int t ;

    cin >> t;

    while(t --)
    {
        int a,b,c;

        cin >> a >> b >> c;

        int minx = 0x3f3f3f3f;
        int x ,y,z;

        for(int i = 1;i <= N;i ++)
        {
            for(int j = i;j <= N;j += i)
            {
                for(int k = j;k <= N;k += j)
                {
                     int cost = abs(a - i) + abs(b - j) + abs(c - k);

                     if(minx >  cost)
                     {
                         minx = cost;
                         x = i,y = j,z = k;
                     }
                }
            }
        }

        cout << minx << '\n';
        cout << x << ' ' << y << ' ' << z << '\n';

    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值