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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不会c语言

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值