Codeforces 1311 D. Three Integers(暴力枚举)

D. Three Integers

You are given three integers a b c a≤b≤c .

In one move, you can add + 1 +1 or 1 −1 to any of these integers ( i . e 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 A≤B≤C such that B B is divisible by A A and C is divisible by B B .

You have to answer t independent test cases.

Input

The first line of the input contains one integer t ( 1 t 100 ) 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 a,b and c ( 1 a b c 1 0 4 ) c (1≤a≤b≤c≤10^4)

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 A≤B≤C such that B B is divisible by A A and C C is divisible by B B . On the second line print any suitable triple A , B A,B and C 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

解题思路:

给了 a b c a、b、c 三个数,现在你可以对任意一个数进行任意次数的 + 1 +1 1 -1
问你最少操作次数 让 b % a = 0 , c % b = 0 b\%a=0 , c\%b=0
a , b , c a,b,c 改变的最大范围也就是 [ 1 , 10000 ] [1,10000] ,直接枚举就可以,但是 1000 0 3 10000^3 是会超时的。这里稍微优化一下。

AC代码:

const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int N = 2e5 + 10;
int t;
int n, m;
int res, len, pre, now;
int a, b, c;
ll ans;
int a1, a2, a3;
int main()
{
    sd(t);
    while (t--)
    {
        sddd(a, b, c);
        ans = INF;
        rep(i, 1, 11000)
        {
            for (int j = 1; i * j <= 11000; j++)
            {
                for (int k = 1; i * j * k <= 11000; k++)
                {
                    if (ans > abs(i - a) + abs(i * j - b) + abs(i * j * k - c))
                    {
                        ans = abs(i - a) + abs(i * j - b) + abs(i * j * k - c);
                        a1 = i, a2 = i * j, a3 = i * j * k;
                    }
                }
            }
        }
        pld(ans);
        cout << a1 << ' ' << a2 << ' ' << a3 << '\n';
    }
    return 0;
}
发布了683 篇原创文章 · 获赞 410 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/qq_43627087/article/details/104492030