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);
        }
    }
  • 相关阅读:
    ECMAScript 6学习笔记(二):let和块级作用域
    ECMAScript 6学习笔记(一):展开运算符
    JavaScript的作用域和闭包
    HTML中行内元素的竖直方向的padding和margin是否真的无效
    <input type="text"/>未输入时属性value的默认值--js学习之路
    Matlab 之 FFT的理解和应用
    .NET 5.0实现Consul服务注册
    面向对象编程思想(OOP)
    又再回归一次
    阿里云的OCS缓存机制
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12375341.html
Copyright © 2011-2022 走看看