Codeforces Round #655 (Div. 2)——B. Omkar and Last Class of Math

Codeforces Round #655 (Div. 2)——B. Omkar and Last Class of Math_第1张图片题目链接
大概意思是:将输入的数n分解为a+b,但要求LCM(a,b)最小

题解思路

  • n%2==0 即n为偶数
    • 则a=b=n/2,此时LCM(a,b)=n/2取得最小
  • n%2!=0 即n为奇数且非质数
    • 这个则需要找到n的最大公约数(通过 n/最小公约数 )
    • a=n/最小公约数,b=n-最大公约数,LCM(a,b)=b
  • 如果为质数
    • a =1,b=n-1,LCM(a,b)=b
#include
using namespace std;
int main(void)
{
	int T;
	cin>>T;
	while(T--)
	{
		long long int n,ans1,ans2;
		int flag=0;
		cin>>n;
		if(n%2==0)
		{
			cout<<n/2<<" "<<n/2<<endl;
		}
		else
		{
			for(long long int i=2;i*i<=n;i++)  //找到最小公约数 
			{
				if(n%i==0)
				{
					flag=1;
					ans1 = n/i;
					ans2 = n-n/i;
					break;
				}
			}
			if(flag==1)
			{
				cout<<ans1<<" "<<ans2<<endl;
			}
			else
				cout<<1<<" "<<n-1<<endl; 
		}
	}
	return 0;
 } 

你可能感兴趣的:(算法)