Three Integers

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

input
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
output
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

暴力搜索,关键是确定搜索范围。
以x为例,假设存在存在最优解使得x>a[1]*2,则令x=1也能满足条件并且结果会变好,矛盾.故x在[1,a[1]*2]范围内.
只需枚举x,y,求z复杂度为常数.

#include<bits/stdc++.h>

using namespace std;
struct {
    int a, b, c, t;
} ans;
int T, a[4];

int main() {
    cin >> T;
    while (T--) {
        ans.t = 0x3f3f3f3f;
        for (int i = 1; i <= 3; i++)scanf("%d", &a[i]);
        for (int x = 1; x <= a[1] * 2; x++)
            for (int y = x; y <= a[2] * 2; y += x) {
                int t = a[3] / y, z1 = y * t, z2 = y * (t + 1);
                int res = abs(a[1] - x) + abs(a[2] - y);
                if (res + abs(z1 - a[3]) < ans.t)
                    ans = {x, y, z1, res + abs(z1 - a[3])};
                if (res + abs(z2 - a[3]) < ans.t)
                    ans = {x, y, z2, res + abs(z2 - a[3])};
            }
        printf("%d\n%d %d %d\n", ans.t, ans.a, ans.b, ans.c);
    }
    return 0;
}
发布了360 篇原创文章 · 获赞 28 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_45323960/article/details/105132731