LCM (Least Common Multiple) of a set of integers is de ned 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
nd out a set of at least two positive in- tegers whose LCM is N . As
in nite such sequences are possible, you have to pick the sequence
whose summa- tion 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 le contains at most 100
test cases. Each test case consists of a positive integer N (1 N 2
31
设n=a1^p1 * a2^p2 * …。
不难看出,当ai^pi作为单独整数时最优【很显然,两个大于1的数的和一定不大于他们的积】。
特判一下n=1和n是质数的情况。
#include<cstdio>
#include<cstring>
#include<cmath>
long long solve(int n)
{
if (n==1) return 2;
int i,j,k,m=sqrt(n+0.5),x,y,z,cnt=0;
long long ans=0;
for (i=2;i<=m&&n>1;i++)
if (n%i==0)
{
cnt++;
x=1;
while (n%i==0)
{
n/=i;
x*=i;
}
ans+=x;
}
if (n>1)
{
cnt++;
ans+=n;
}
if (cnt==1) ans++;
return ans;
}
int main()
{
int n,K=0;
while (scanf("%d",&n)&&n)
printf("Case %d: %lld\n",++K,solve(n));
}