UVa 10791

链接:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1732

题意:

输入整数n(1≤n<2^31),求至少两个正整数,使得它们的最小公倍数为n,且这些整数的和最小。输出最小的和。

分析:

设唯一分解式n=(a1^p1)*(a2^p2)…,不难发现每个(ai^pi)作为一个单独的整数时最优。
注意几个特殊情况:n=1时答案为1+1=2;n只有一种质因子时需要加个1,还有n=2^31-1时不要溢出。

代码:

 1 import java.io.*;
 2 import java.util.*;
 3 
 4 public class Main {
 5     static int divideAll(int n[], int d) {
 6         int old = n[0];
 7         while(n[0] % d == 0) n[0] /= d;
 8         return old / n[0];
 9     }
10     
11     static long solve(int arn) {
12         if(arn == 1) return 2;
13         int n[] = {arn};
14         int pf = 0, u = (int)Math.sqrt(n[0] + 0.5);
15         long ans = 0;
16         for(int i = 2; i < u; i++) {
17             if(n[0] % i == 0) {
18                 ans += divideAll(n, i);
19                 pf++; // 质因子(prime_factor)个数
20             }
21         }
22         if(n[0] > 1) { ans += n[0];  pf++; }
23         if(pf < 2) ans++;
24         return ans;
25     }
26     
27     public static void main(String args[]) throws Exception {
28         Scanner cin = new Scanner(new BufferedInputStream(System.in));
29         for(int cases = 1; ; cases++) {
30             int n = cin.nextInt();
31             if(n == 0) break;
32             System.out.printf("Case %d: %d
", cases, solve(n));
33         }
34         cin.close();
35     }
36 }
原文地址:https://www.cnblogs.com/hkxy125/p/8857764.html