zoukankan      html  css  js  c++  java
  • Codeforces Round #624 (Div. 3) D. Three Integers

    You are given three integers abca≤b≤c .

    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 ABCA≤B≤C such that BB is divisible by AA and CC is divisible by BB .

    You have to answer tt independent test cases.

    Input

    The first line of the input contains one integer tt (1t1001≤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 (1abc1041≤a≤b≤c≤104 ).

    Output

    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 ABCA≤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 .

    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
    一开始想了半天再加上题目的rating1900+math的标签就以为是数论不敢做了,后来看大佬说是暴力...枚举有两种方式,一种是直接枚举A,B,C(注意里面两重循环 for(int j = i; j <= 15000; j += i)for(int k = j; k <= 15000; k += j)不要写++;一种是枚举倍数 for(k=1;i*j*k<=20000;k++)for(k=1;i*j*k<=20000;k++)。玄学范围看着枚举就行,看似O(n^3)实际上有了剪枝是到不了的。Div3别想的太复杂。
    #include <bits/stdc++.h>
    int a,b,c;
    using namespace std;
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            scanf("%d%d%d",&a,&b,&c);
            int A,B,C,i,j,k;
            
            int mmin=99999999;
            int tot;
            for(i=1;i<=10000;i++)
            {
                for(j=1;i*j<=20000;j++)
                {
                    for(k=1;i*j*k<=20000;k++)
                    {
                        tot=abs(a-i)+abs(b-j*i)+abs(c-i*j*k);
                        if(tot<mmin)
                        {
                            mmin=tot;
                            A=i;
                            B=i*j;
                            C=i*j*k;
                        }
                    }
                }
            }
            cout<<mmin<<endl;
            printf("%d %d %d
    ",A,B,C);
        }
    }
  • 相关阅读:
    【LeetCode-位运算】汉明距离总和
    python类的继承和重写
    单元测试unittest使用说明
    Java学习90
    Java学习89
    Java学习88
    Java学习87
    Java学习86
    Java学习85
    Java学习84
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12375341.html
Copyright © 2011-2022 走看看