Codeforces Round #624 (Div. 3) D. Three Integers 数论

传送门

文章目录

题意:

给定 A , B , C A,B,C A,B,C,找到一个三元组 ( a , b , c ) (a,b,c) (a,b,c),使得 a a a b b b的因子, b b b c c c的因子,且 a b s ( A − a ) + a b s ( B − b ) + a b s ( C − c ) abs(A-a)+abs(B-b)+abs(C-c) abs(Aa)+abs(Bb)+abs(Cc)最小。
A , B , C ≤ 1 e 4 A,B,C\le 1e4 A,B,C1e4

思路:

注意到 a a a b b b的因子, b b b c c c的因子,所以考虑枚举 a a a,那么枚举 b b b就是 k ∗ a k*a ka,枚举 c c c就是 t ∗ b t*b tb,枚举上限为 2 ∗ c 2*c 2c,复杂度 O ( n l o g 2 n ) O(nlog^2n) O(nlog2n)
当然还可以枚举 b b b,之后枚举 b b b的因子,找到离 a a a最近的那个,让后再枚举 b b b的倍数,找到离 c c c最近的那个即可。

O ( n l o g n ) O(nlog^n) O(nlogn)

// Problem: D. Three Integers
// Contest: Codeforces - Codeforces Round #624 (Div. 3)
// URL: https://codeforces.com/contest/1311/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define pb push_back
#define mk make_pair
using namespace std;

typedef long long LL;
typedef pair<int,int> PII;

const int N=200010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int a,b,c;

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);
	
	int _; scanf("%d",&_);
	while(_--)
	{
		scanf("%d%d%d",&a,&b,&c);
		int a1,a2,a3,ans=INF;
		for(int i=1;i<=2*c;i++)
		{
			for(int j=i;j<=2*c;j+=i)
			{
				for(int k=j;k<=2*c;k+=j)
				{
					int dis=abs(i-a)+abs(j-b)+abs(k-c);
					if(ans>dis) ans=dis,a1=i,a2=j,a3=k;
				}
			}
		}
		printf("%d\n%d %d %d\n",ans,a1,a2,a3);
	}	
    

	return 0;
}
/*

*/

O ( n l o g n ) O(nlogn) O(nlogn)

// Problem: D. Three Integers
// Contest: Codeforces - Codeforces Round #624 (Div. 3)
// URL: https://codeforces.com/contest/1311/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define pb push_back
#define mk make_pair
using namespace std;

typedef long long LL;
typedef pair<int,int> PII;

const int N=200010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int a,b,c;
vector<int>d[N];

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);

	for(int i=1;i<N;i++)
		for(int j=i;j<N;j+=i)
			d[j].pb(i);
	
	int _; scanf("%d",&_);
	while(_--)
	{
		scanf("%d%d%d",&a,&b,&c);
		int a1,a2,a3,ans=INF;
		for(int i=1;i<=2*c;i++)
		{
			int x,y,z,cnt=0,mi=INF;
			for(auto xx:d[i]) if(abs(xx-a)<mi) mi=abs(xx-a),x=xx;
			cnt+=mi; mi=INF;
			for(int j=i;j<=2*c;j+=i) if(abs(j-c)<mi) mi=abs(j-c),y=j;
			cnt+=mi;
			cnt+=abs(i-b);
			if(cnt<ans) ans=cnt,a1=x,a2=i,a3=y;
		}
		printf("%d\n%d %d %d\n",ans,a1,a2,a3);
	}	
    

	return 0;
}
/*

*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值