题解 CF1311D 【Three Integers】

· · 题解

观察此题,发现数据范围不大,所以不考虑结论,直接上暴力。

令输入为 a,b,c 答案为 A,B,C。

先枚举 A,再枚举 A 的每一个倍数 B,最后的 C 肯定等于 c/B \times B 或 (c/b+1) \times B 进行枚举即可。

但还有一点,在极限数据中 B 的值可能大于 10000 所以 B 的上界最好开大一点(好像 10050 就够了)。

比如当 a=137,b=10000,c=10000 时,答案为 A=137,B=10001,C=10001,此时 B 就大于 10000。(因此错失 rank9)

#include<bits/stdc++.h>
using namespace std;
#define re register
inline int read(){
    re int t=0;
    re char v=getchar();
    while(v<'0')v=getchar();
    while(v>='0'){
        t=(t<<3)+(t<<1)+v-48;
        v=getchar();
    }
    return t;
}
int t,n,a,b,c,d,e,f,ans,x,y;
int main(){
    t=read();
    while(t--){
        ans=1e9;
        a=read();
        b=read();
        c=read();
        for(re int i=1;i<=10000;++i){
            for(re int j=i;j<=10000;j+=i){
                int tmp=c/j;
                if(abs(tmp*j-c)<abs((tmp+1)*j-c))x=tmp*j;
                else x=(tmp+1)*j;
                if(abs(i-a)+abs(j-b)+abs(x-c)<ans){
                    ans=abs(i-a)+abs(j-b)+abs(x-c);
                    d=i,e=j,f=x;
                }
            }
        }
        printf("%d\n%d %d %d\n",ans,d,e,f);
    }
}