链接:https://ac.nowcoder.com/acm/contest/897/M
来源:牛客网
LCM
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
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 a×ba×b, the product of a and b.
Please tell Silly Slp the maximize value of a×ba×b. If a and b don't exist, print “-1”(without quotes).
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 a×ba×b, the product of a and b.
Please tell Silly Slp the maximize value of a×ba×b. If a and b don't exist, print “-1”(without quotes).
输入描述:
The first line contains an integer number T, the number of test cases.
ithith of each next T lines contains two numbers n and c(1≤n,c≤1061≤n,c≤106).
输出描述:
For each test case print a number, the maximize value.
示例1
输出
复制24 -1
题意:
给你一个n和一个c,
让你找两个数a,b,满足 a<=n,b<=n, lcm(a,b)=c
思路:
c=lcm(a,b)=a*b/gcd(a,b);
so, gcd(a,b) 是c的一个因子,
那么我们sqrt(c)的时间复杂度来枚举 c的因子i,我们假定i就是 gcd ,然后y=c/i
y=a*b/gcd(a,b)/gcd(a,b)
我们再来枚举 y的因子,如果y有两个因子 p,q, 满足gcd(p,q)==1 并且 p*gcd<=n,q*gcd<=n
那么 p,q就可以是 a/gcd(a,b), b/gcd(a,b)的一种可能,
p*gcd*q*gcd = a*b
我们枚举的时候取最大值,就可以得到我们想要的答案。
细节见代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #define ALL(x) (x).begin(), (x).end() #define rt return #define dll(x) scanf("%I64d",&x) #define xll(x) printf("%I64d\n",x) #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define db(x) cout<<"== [ "<<x<<" ] =="<<endl; using namespace std; typedef long long ll; ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;} inline void getInt(int* p); const int maxn = 1000010; const int inf = 0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ int main() { // freopen("D:\\common_text\\code_stream\\in.txt","r",stdin); //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout); int t; // gbtb; // cin>>t; scanf("%d", &t); while (t--) { ll n, c; scanf("%lld %lld", &n, &c); // cin>>n>>c; ll res = -1; for (ll i = 1; i * i <= c; i++) { if (c % i == 0) { ll y = c / i; //枚举gcd(a,b)=i a*b=c*i for (ll j = 1; j * j <= y; j++) { if (y % j == 0 && gcd(j, y / j) == 1 && j * i <= n && y / j * i <= n) { res = max(res, c * i); } } y = i; //枚举gcd(a,b)=c/i a*b=c*(c/i) for (ll j = 1; j * j <= y; j++) { if (y % j == 0 && gcd(j, y / j) == 1 && j * (c / i) <= n && y / j * (c / i) <= n) { res = max(res, c * (c / i)); } } } } printf("%lld\n", res); // cout<<ans<<endl; } return 0; } inline void getInt(int* p) { char ch; do { ch = getchar(); } while (ch == ' ' || ch == '\n'); if (ch == '-') { *p = -(getchar() - '0'); while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 - ch + '0'; } } else { *p = ch - '0'; while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 + ch - '0'; } } }