我可以用动态规划来解决这个问题吗?
我如何在所有对 a
和 b
中找到具有“最小公倍数”LCM(a,b) = 498960 和“最大公约数”GDM(a, b ) = 12 一对具有最小总和a + b
?
我用 O(n^2) 时间解决了这个问题:
public class FindLcmAndGcdClass {
private int findGcd(int a, int b) {
if (a % b == 0) {
return b;
}
return findGcd(b, a % b);
}
private int findLcm(int a, int b, int gcd) {
return (a * b) / gcd;
}
private void run() {
int minSum = Integer.MAX_VALUE;
int foundNumberOne = 0;
int foundNumberTwo = 0;
for (int i = 12; i <= 498960; i += 12) {
for (int j = i; j <= 498960; j += 12) {
int gcd;
if (i < j) {
gcd = findGcd(j, i);
} else {
gcd = findGcd(i, j);
}
int lcm = findLcm(i, j, gcd);
if (gcd == 12 && lcm == 498960 && i + j < minSum) {
minSum = i + j;
foundNumberOne = i;
foundNumberTwo = j;
}
}
}
System.out.println(minSum);
System.out.println(foundNumberOne);
System.out.println(foundNumberTwo);
}
public static void main(String[] args) {
var o = new FindLcmAndGcdClass();
o.run();
}
}
而且执行速度非常慢!我想这个问题可以用动态规划来解决。任何人都可以帮助提供更快速的解决方案吗?
How I find among all pairs a
and b
with a "least common multiple" LCM(a,b) = 498960 and a "greatest common divisor" GDM(a, b) = 12 a pair with minimum sum a + b
?
I solved this with O(n^2) time:
public class FindLcmAndGcdClass {
private int findGcd(int a, int b) {
if (a % b == 0) {
return b;
}
return findGcd(b, a % b);
}
private int findLcm(int a, int b, int gcd) {
return (a * b) / gcd;
}
private void run() {
int minSum = Integer.MAX_VALUE;
int foundNumberOne = 0;
int foundNumberTwo = 0;
for (int i = 12; i <= 498960; i += 12) {
for (int j = i; j <= 498960; j += 12) {
int gcd;
if (i < j) {
gcd = findGcd(j, i);
} else {
gcd = findGcd(i, j);
}
int lcm = findLcm(i, j, gcd);
if (gcd == 12 && lcm == 498960 && i + j < minSum) {
minSum = i + j;
foundNumberOne = i;
foundNumberTwo = j;
}
}
}
System.out.println(minSum);
System.out.println(foundNumberOne);
System.out.println(foundNumberTwo);
}
public static void main(String[] args) {
var o = new FindLcmAndGcdClass();
o.run();
}
}
And it executes quite slowly! I guess the problem can be solved with Dynamic Programming. Can anyone help with more fast solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定这个问题是否可以用动态规划来解决,但我想到了一个时间复杂度
O(sqrt(LCM * GCD))
的解决方案。众所周知,对于任意两个整数 a 和 b,
LCM(a, b) * GCD(a, b) = a * b
。因此,你可以先计算gcd和lcm的乘积(本题为5987520)。然后对于sqrt(LCM * GCD)
下的所有因子,令a
为因子之一,则b = LCM * GCD / a
。测试 gcd(a, b) 是否 = 所需的 gcd,如果是,则计算和a + b
,然后找到和中的最小值,就完成了。I am not sure if this question can be solved with dynamic programming, but I think of a solution with time complexity
O(sqrt(LCM * GCD))
.It is well known that for any two integers a and b,
LCM(a, b) * GCD(a, b) = a * b
. Therefore, you can first calculate the product of the gcd and lcm, (which is 5987520 in this question). Then for all its factors undersqrt(LCM * GCD)
, leta
be one of the factors, thenb = LCM * GCD / a
. Test if gcd(a, b) = the required gcd, if so calculate the suma + b
, then find the minimum among the sums, and you are done.