1372B - Omkar and Last Class of Math

链接:

https://codeforces.com/problemset/problem/1372/B

题意:

给一个数n,让a+b=n,且a,b最大公倍数(LCM(a,b))最小

解:

对于一个n,设他的最小非1因子Ymin,和他的最大非1因子Ymax=n/Ymin

所有n由Ymin*Ymax组成

这边要在a+b=n的情况下LCM最小,那么n-a/a就要是整数(成倍)且越小越好

那么因为n由Ymin个Ymax组成,所以1个Ymax和Ymin-1个Ymax最合适,倍数为Ymin-1倍

偶数时推得最小非1因子Ymin=2,直接推出答案n/2,n/2

奇数,就找最小非1因子,找不到答案就是1和n-1

PS:A<=B LCM最小值B,当A是B因子,或A=B

实际代码:

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define csh(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long int ll;
int gcd(int a,int b)
{
	if(b==0) return a;
	if(a<b) swap(a,b);
	return a%b==0?b:gcd(b,a%b);
}
int lcm(int a,int b)
{
	if(a<b) swap(a,b);
	return a*b/gcd(a,b);
}
int main()
{
	int T;
	cin>>T;
	for(int f=1;f<=T;f++)
	{
		int n;
		cin>>n;
		if( !(n&1) ) cout<<n/2<<" "<<n/2<<endl;
		else
		{
			int ans=1;
			for(int i=2;i<=n/i;i++)
			{
				if(n%i==0)
				{
					ans=n/i;
					break;
				}
			}
			cout<<ans<<" "<<n-ans<<endl;
		}
	}
}

限制:

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值