列表

详情


NC25732. LCM

描述

    Silly Slp knows nothing about number theory. One day he feels puzzled with the following problem.
    Give two positive integers n and c. Find a pair of positive integer (a, b), which satisfies both of a and b are no more than n and the lowest common multiple of them is c. Moreover, maximize , the product of a and b.
    Please tell Silly Slp the maximize value of . If a and b don't exist, print “-1”(without quotes).

输入描述

    The first line contains an integer number T, the number of test cases.
     of each next T lines contains two numbers n and c().

输出描述

For each test case print a number, the maximize value.

示例1

输入:

2
10 12
5 36

输出:

24
-1

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 64ms, 内存消耗: 488K, 提交时间: 2019-05-12 14:44:54

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> q;
int main()
{
	ll t;scanf("%lld",&t);
	while(t--)
	{
		ll n,c;scanf("%lld%lld",&n,&c);ll ans=-1;
		q.clear();
		for(ll i=1;i<=(ll)sqrt(c);i++)
		{
			if(c%i==0)q.push_back(i),q.push_back(c/i);
		}
		sort(q.begin(),q.end());
		for(ll i=0;i<q.size()&&q[i]<=n;i++)
		{
			for(ll j=0;j<q.size()&&q[j]<=n;j++)
			{
				if((q[i]*q[j]/(__gcd(q[i],q[j]))==c))
				ans=max(ans,q[i]*q[j]);
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}

C++11(clang++ 3.9) 解法, 执行用时: 43ms, 内存消耗: 480K, 提交时间: 2019-05-12 21:32:47

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[20000];
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		ll n,c;
		scanf("%lld%lld",&n,&c);
		int t=0;
		for(ll i=1;i*i<=c;i++){
			if(c%i==0)
				a[t++]=i,a[t++]=c/i;
		}
			
		ll ans=-1;
		for(int i=0;i<t;i++){
			for(int j=i;j<t;j++){
				ll g=__gcd(a[i],a[j]);
				if((a[i]*a[j]==1ll*c*g)&&a[i]<=n&&a[j]<=n)
					ans=max(ans,1ll*a[i]*a[j]);
			}
		}
		printf("%lld\n",ans);
	}
}

上一题