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%a<mark>0 && c%b</mark>0

思路:因为范围只有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;
}


全部评论

相关推荐

10-19 10:28
已编辑
成都理工大学 后端工程师
团孝子已上线feeling:面了很多家公司,能感受到目前只有小公司+外包喜欢问八股。大厂虽然也问八股,但是是从实习、项目中进行提问,并且大厂会问很深,面试官也会对你的回答进行思考➕追问,所以准备大厂面试前一定要备好相关资料。对于算法,我做的是codetop前100+力扣hot100+力扣高频150,面试中实感hot100就足够,基本上只要是hot100就秒答。对于项目和八股,我做的也是烂大街的星球项目,八股则是看小林和问ai,自己也写了很多技术博客和画了很多思维导图,并且自己也尝试用嘴巴说出来,不只停留于纸面。运气也很重要,必须要让面试官/HR看到简历才行,所以建议投递时间是下午两点。tl:第一岗位9.9&nbsp;投递9.10&nbsp;一面(一面评价:最近见过最强的大三,结束五分钟后约二面,都晚上九点了不下班吗)9.11&nbsp;二面(三道算法a出两道,反问评价:经验不够等横向,我实习生要啥经验)9.21挂(实习时间过短+其他原因,想要一年实习的,为什么不招个正职)第二岗位10.10投递10.11约面(主管打电话,说看到我之前投递记录了想要我挂qa职进去干后端,同意)10.14&nbsp;一面(无八股,主动说确实很强,意愿很强)10.16&nbsp;oc其余,友邦,东软,东华,惠择,用友oc已拒京东测开一面挂(投后端被测开捞)腾讯测试已拒(投后端被测开捞)ps:表扬惠择的主管面,没怎么问技术(可能是一面面试官沟通过了),全程一起讲大道理,解答了心中很多疑惑,也告诉我以面试官角度来看怎么选候选人,如果可以下次一定选惠择
HeaoDng:美团好像可以触发一面通
点赞 评论 收藏
分享
10-01 09:50
门头沟学院 Java
肖先生~:这个人真的很好,点赞
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务