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 or 12, 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 print 4+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
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 OutputCase 1: 7
Case 2: 7
Case 3: 6
题意:一串序列(至少2个数字组成)的LCM(最小公倍数)为N,求这串序列和的最小值。
思路:分解质因数,如12=2*2*3,结果为2*2+3,24=2*2*2*3,结果为2*2*2+3,如果只有1种质因子或N为1时结果要再加1。
# include <stdio.h>
int main()
{
long long n, tmp, sum, flag;
int icase = 1;
while(~scanf("%lld",&n),n)
{
if(n==1)
printf("Case %d: 2\n",icase++);
else
{
flag = sum = 0;
for(long long i=2; i*i<=n; ++i)
{
if(n%i==0)
{
tmp = 1;
++flag;
for(;n%i==0;n/=i)
tmp *= i;
sum += tmp;
}
}
if(n > 1)
{
++flag;
sum += n;
}
printf("Case %d: %lld\n",icase++, flag>1?sum:sum+1);
}
}
return 0;
}
本文介绍了一个算法问题:给定一个正整数N,找出一组至少包含两个正整数的序列,该序列的最小公倍数(LCM)等于N,并使这些整数的和达到最小值。文章提供了完整的解析思路及C语言实现代码。
390

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



