题目描述:
输入最小公约数c,求c的因子之积的最大值(a <= n && b <= n)
示例1:
输入:
10 12
输出:
24
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
ll a[N];
ll cnt;
ll gcd(ll a, ll b)
{
if (!b) return a;
return gcd(b, a%b);
}
void num(ll n) // 参数n是最小公倍数,这个函数的功能是求最小公倍数的所有约数
{
cnt = 0;
memset(a, 0, sizeof a);
int len = sqrt(n); // 开方,将枚举数量折半
for(int i = 1 ; i <= len ; i ++)
{
if(n % i == 0) // i是n的约数
{
a[cnt ++] = i; // 将i存入a数组
if(n / i != 0) a[cnt ++] = n / i; // n比i大的话,将与之配对的约数也存进a数组里面
}
}
}
int main()
{
ll n,c;
cin >> n >> c;
num(c);
ll res = 0;
for(int i = 0 ; i < cnt ; i ++)
{
for(int j = i ; j < cnt ; j ++)
{
if(a[i] <= n && a[j] <= n && (a[i] * a[j]) / gcd(a[i], a[j]) == c)
res = max(res,a[i] * a[j]);
}
}
if(res == 0) cout << "-1" << endl;
else cout << res << endl;
return 0;
}
该博客介绍了一种算法,输入是最小公倍数c,目标是找到c的因子之积的最大值,其中a和b都小于等于n。通过遍历和计算所有可能的因子组合,找到满足条件的最大乘积。示例展示了对于输入1012,其因子之积的最大值为24。提供的AC代码实现了这一逻辑,包括计算最小公倍数的所有约数并寻找符合条件的最大乘积。
1万+

被折叠的 条评论
为什么被折叠?



