Three Integers CodeForces - 1311D (暴力循环)

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

题意
每次加1或减1使B能被A整除,C能被B整除,输出执行这些操作(加1或减1)次数的最小数目以及经过操作改变后的A,B,C。

思路
根据题意可知:B=n* A,C=n1* B(也=n1* n* A),题目所给数据不是特别大即 1 ≤ a ≤ b ≤ c ≤ 10^4,所以可以暴力三层for循环!!!

基于上周比赛题中遇到这个题的简单版本,本以为只是取余问题,结果并不是,多加思考吧ok就这样!看代码吧!

代码

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        int num=99999999;
        int i,j,k,s,s1,s2,s3;
        for(i=1; i<=10000; i++)   //i<=10000是因为 A<10000
        {
            for(j=1; j*i<=20000; j++)  //此处20000是A+B<=20000,j相当于n
            {
                for(k=1; i*j*k<=20000; k++) //同理B+C<=20000,j*k相当n1
                {
                    s=abs(a-i)+abs(i*j-b)+abs(i*j*k-c);
                    if(num>s)
                    {
                        num=s;
                        s1=i;
                        s2=i*j;
                        s3=i*j*k;
                    }

                }
            }
        }
       printf("%d\n",num);
       printf("%d %d %d\n",s1,s2,s3);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Piink/article/details/105230562