In one move, you can add +1+1 or −1−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≤CA≤B≤C such that BB is divisible by AA and CC is divisible by BB .
You have to answer tt independent test cases.
The first line of the input contains one integer tt (1≤t≤1001≤t≤100 ) — the number of test cases.
The next tt lines describe test cases. Each test case is given on a separate line as three space-separated integers a,ba,b and cc (1≤a≤b≤c≤1041≤a≤b≤c≤104 ).
For each test case, print the answer. In the first line print resres — the minimum number of operations you have to perform to obtain three integers A≤B≤CA≤B≤C such that BB is divisible by AA and CC is divisible by BB . On the second line print any suitable triple A,BA,B and CC .
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
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
题解:
Tutorial
Let's iterate over all possible values of AA from 11 to 2a2a. It is obvious that AA cannot be bigger than 2a2a, else we can just move aa to 11. Then let's iterate over all possible multiples of AA from 11 to 2b2b. Let this number be BB. Then we can find CC as the nearest number to cc that is divisible by BB (we can check two nearest numbers to be sure). These numbers are C=⌊cB⌋⋅BC=⌊cB⌋⋅B and C=⌊cB⌋⋅B+BC=⌊cB⌋⋅B+B. Then we can update the answer with the found triple. Note that the only condition you need to check is that B≤CB≤C.
Time complexity: O(nlogn)O(nlogn) because of the sum of the harmonic series.
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--) {
int a, b, c;
cin >> a >> b >> c;
int ans = 1e9;
int A = -1, B = -1, C = -1;
//重点
for (int cA = 1; cA <= 2 * a; ++cA) {
for (int cB = cA; cB <= 2 * b; cB += cA) {
for (int i = 0; i < 2; ++i) {
int cC = cB * (c / cB) + i * cB;
int res = abs(cA - a) + abs(cB - b) + abs(cC - c);
if (ans > res) {
ans = res;
A = cA;
B = cB;
C = cC;
}
}