Codeforces Round #624 (Div. 3) problem D

Three Integer

You are given three integers a≤b≤c

In one move, you can add +1or −1to 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

题意
输入三个数a,b,c,找三个数A,B,C,使得abs(a - A) + abs(b - B) + abs(c - C)最小。

题解
遍历a从1到2a(2a取不到,因为假如B是2a的倍数,那么一定是a的倍数)。因为B<=C,C<=2c,所以B的遍历范围是1到2*c。

#include<bits/stdc++.h>

using namespace std;

int main() {    
	int  T;    
	scanf("%d", &T);    
	while(T--) {        
	int a, b, c;        
	int A, B, C;        
	scanf("%d%d%d", &a, &b, &c);        
	int ans =0x3f3f3f3f, cnt = 0;       
	for (int i = 1; i < 2 * a; i ++) {            
		for (int j = i; j < 2 * c; j += i) {                
			for (int k = j; k < 2*c; k += j) {                    
	 			cnt = abs(a - i) + abs(b - j) + abs(c - k);                   
	  			if(ans > cnt) {                        
	 				ans = cnt;                        
	 				A = i, B = j, C = k;                    
	  			}                
	   		}          
		}      
	}        
	printf("%d\n%d %d %d\n", ans, A, B, C);    
	}
}
发布了25 篇原创文章 · 获赞 24 · 访问量 577

猜你喜欢

转载自blog.csdn.net/rainbowsea_1/article/details/104561819