NC25732. LCM
描述
输入描述
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); } }