Codeforces Round #624 (Div. 3)D.Three Integers

D. Three Integers
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given three integersa≤b≤c.

In one move, you can add+1or−1toanyof 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 integersA≤B≤Csuch thatBis divisible byAandCis divisible byB.

You have to answertindependent test cases.

Input
The first line of the input contains one integert (1≤t≤100) — the number of test cases.

The nexttlines describe test cases.Each test case is given on a separate line as three space-separated integersa,bandc (1≤a≤b≤c≤104).

Output
For each test case, print the answer.In the first line printres— the minimum number of operations you have to perform to obtain three integersA≤B≤Csuch thatBis divisible byAandCis divisible byB.On the second line printanysuitable tripleA,BandC.

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三個數,現在你可以對任意一個數進行任意次數的+1 和-1
問你最少操作次數 讓b%a0 && c%b0

思路:因爲範圍只有1e4 我們先處理出來1e4以內所有數的因子
然後因爲b是a的倍數,b是c的因子,我們考慮去 枚舉中間數b 記作bb
這樣b和改變後的值bb直接的差值就是對b進行的操作次數
對於a呢 我們去找bb的因子 找到最小改變的次數繼續加上來
對於c 我們先去比較b和c的大小 然後去看c=kbb+r 考慮去掉餘數 或者加上數
使得k
bb=c即可

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define me(a,x) memset(a,x,sizeof a)
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define all(x) (x).begin(), (x).end()
#define pb(a) push_back(a)
#define paii pair<int,int>
#define pali pair<ll,int>
#define pail pair<int,ll>
#define pall pair<ll,ll>
#define fi first
#define se second
vector<int>g[20005];
int a,b,c;
void inist()
{
    for(int i=1;i<=20000;i++)
    {
        for(int j=1;j<=20000/i;j++)
            g[i*j].pb(i);
    }

}
int work(int& aa,int bb,int& cc)
{
    int ans=0;
    int minn=1000000;
    int l=g[bb].size();
    int x=aa;
    for(int i=0;i<l;i++)
    {
        if(minn>abs(g[bb][i]-aa))
        {
            minn=abs(g[bb][i]-aa);
            x=g[bb][i];
        }
    }
    aa=x;
    ans+=minn;
    if(bb>cc)
    {
        ans+=abs(bb-cc);
        cc=bb;
    }
    if(cc%bb<bb-cc%bb)
    {
        ans+=cc%bb;
        cc-=cc%bb;
    }
    else
    {
        ans+=bb-cc%bb;
        cc+=bb-cc%bb;
    }
    return ans;
}
int main()
{
    inist();
    int t;
    cin>>t;
    while(t--)
    {
        int ans=100000;
        int ansa,ansb,ansc;
        cin>>a>>b>>c;
        for(int i=1;i<=20000;i++)
        {
            int a1=a,b1=i,c1=c;
            int temp=abs(i-b);
            temp+=work(a1,b1,c1);
            if(temp<ans)
            {
                ans=temp;
                ansa=a1;ansb=b1;ansc=c1;
            }
        }
        cout<<ans<<endl;
        cout<<ansa<<" "<<ansb<<" "<<ansc<<endl;
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章