10791 - Minimum Sum LCM
Time limit: 3.000 seconds
LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is a multiple of all integers of that set. It is interesting to note that any positive integer can be expressed as the LCM of a set of positive integers. For example 12 can be expressed as the LCM of 1, 12 or12, 12 or 3, 4 or 4, 6 or 1, 2, 3, 4 etc.
In this problem, you will be given a positive integer N. You have to find out a set of at least two positive integers whose LCM is N. As infinite such sequences are possible, you have to pick the sequence whose summation of elements is minimum. We will be quite happy if you just print the summation of the elements of this set. So, for N = 12, you should print4+3 = 7 as LCM of 4 and 3 is 12 and 7 is the minimum possible summation.
Input
The input file contains at most 100 test cases. Each test case consists of a positive integer N ( 1
N
231 - 1).
Input is terminated by a case where N = 0. This case should not be processed. There can be at most 100 test cases.
Output
Output of each test case should consist of a line starting with `Case #: ' where # is the test case number. It should be followed by the summation as specified in the problem statement. Look at the output for sample input for details.
Sample Input
12 10 5 0
Sample Output
Case 1: 7 Case 2: 7 Case 3: 6
思路:素因子分解
完整代码:
/*0.022s*/
#include <cstdio>
#include <cmath>
int main(void)
{
int n, nCase = 0;
int maxi, temp, f, i;
long long ans;
while (scanf("%d", &n), n)
{
maxi = (int)sqrt(n);
ans = 0;
f = 0;
for (i = 2; i <= maxi; i++)
if (n % i == 0)
{
f++;
temp = 1;
while (n % i == 0)
{
temp *= i;
n /= i;
}
ans += temp;
}
///拾遗
if (f == 0)
ans = (long long)n + 1;
else if (n > 1 || f == 1)
ans += n;
printf("Case %d: %lld\n", ++nCase, ans);
}
return 0;
}

本文探讨了如何解决LCM问题,即找出一组正整数的LCM为给定值N的情况下,使这组数之和最小。通过素因子分解的方法,我们能够有效地找到这样的数列,并计算其最小可能的和。

446

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



