暴力枚举

You are given three integers a≤b≤ca≤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 A≤B≤CA≤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 (1≤t≤1001≤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 (1≤a≤b≤c≤1041≤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 A≤B≤CA≤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.
对于给定的三个数a,b,c,可以进行+1或者-1操作,最后要满足b是a的倍数,c是b的倍数,求满足这个要求所需要的最小操作数,就是枚举的数与a,b,c的差值最小。
因为数据给了1e4,所以直接暴力三层循环枚举,按照倍数枚举,最后复杂度不会是O(n^3),a枚举到10000,b枚举到20000,一般不会超过这个。
因为 假如移动后b是a的k1倍,那么 b=k1a,c=k2b=k1k2a
我们只需枚举 a移动的次数q,然后遍历(a+q)的倍数j,那么b移动为(a+q)*j-b,遍历b为c的倍数 k ,c移动为(a+q)jk-c;

for(int i=1;i<=10000;i++)
        {
            for(int j=1;j*i<=20000;j++)
            {
                for(int k=1;i*j*k<=20000;k++)
                {
                    int sum=abs(a-i)+abs(j*i-b)+abs(i*j*k-c);
                    if(ans>sum)
                    {
                        ans=sum;
                        x=i;
                        y=i*j;
                        z=i*j*k;
                    }
                }
            }
        }

这里移动后 i为a, ij为b, ij*k=c ;
b,c<20000;

在这里插入代码片
#include <bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
int main()
{

	int t,a,b,c;
	int x,y,z;
	scanf("%d",&t);
	while(t--)
    {
       scanf("%d%d%d",&a,&b,&c);
        int ans=inf;
        for(int i=1;i<=10000;i++)
        {
            for(int j=1;j*i<=20000;j++)
            {
                for(int k=1;i*j*k<=20000;k++)
                {
                    int sum=abs(a-i)+abs(j*i-b)+abs(i*j*k-c);
                    if(ans>sum)
                    {
                        ans=sum;
                        x=i;
                        y=i*j;
                        z=i*j*k;
                    }
                }
            }
        }
        cout<<ans<<endl;
        cout<<x<<" "<<y<<" "<<z<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Starry_Sky_Dream

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

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

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

打赏作者

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

抵扣说明:

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

余额充值