Codeforce 1311D - Three Integers(暴力枚举)

传送门

题意:

给三个整数 a , b , c a,b,c a,b,c
可以对每个数进行 + 1 +1 +1或者 − 1 -1 1任意次
使得三个数满足, b b b能被 a a a整除, c c c能被 b b b整除
求变化成这种关系的最小次数,并输出 a , b , c a,b,c a,b,c

思路:

数据只有1e4,直接暴力枚举即可
从1到1e4枚举a
然后枚举a的倍数b
然后枚举b的倍数c
T ∗ n ∗ l o g ( n ) ∗ l o g ( n ) T*n*log(n)*log(n) Tnlog(n)log(n)

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <math.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
typedef long long ll;
#define Pll make_pair
#define PB push_back
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int MAXN=2e5+50;
const int inf=0x3f3f3f3f;
const int M=10050;
int n,m,_;
void solve(){
    int a,b,c,x,y,z;
    int ans=inf;
    scanf("%d%d%d",&a,&b,&c);
    for(int i=1;i<=M;i++)
        for(int j=i;j<=M;j+=i)
            for(int k=j;k<=M;k+=j){
                int tmp=abs(a-i)+abs(b-j)+abs(c-k);
                if(ans>tmp){
                    ans=tmp;
                    x=i;
                    y=j;
                    z=k;
                }
            }
    printf("%d\n%d %d %d\n",ans,x,y,z);
}
int main()
{ 
    for(scanf("%d",&_);_;_--)solve();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值