CodeForces 1372B Omkar and Last Class of Math (数学+贪心)

题意:给出一个n,求满足a+b=n且lcm(a, b)最小。

题解:思维
a+b=n且lcm(a, b)最小,那么a和b一定要满足倍数关系,且lcm=max(a, b)要尽可能小。

若为偶数,a=b=n/2,这肯定让lcm最小。

若为奇数,我们只要找到n的除1外的最小质因子x,a=n/x,b=n-a。
因为这样算出的a肯定不超过n/2,也就不超过b,所以max(a, b)=b,且a是尽可能大的,那么max(a, b)=b会尽可能小,这也就满足了我们一开始设置的条件。至于倍数关系,其实我们把n分成了x份,a占1份,b占x-1份,必然是倍数关系。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
using namespace std;
int t, n;
int main() {
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		if (n & 1) {
			int flag = 0;
			for (int i = 2; i * i <= n; i++) {
				if (n % i == 0) {
					printf("%d %d\n", n / i, n - (n / i));
					flag = 1;
					break;
				}
			}
			if (!flag) printf("%d %d\n", 1, n - 1);
		}
		else printf("%d %d\n", n / 2, n / 2);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值