UVA10791 Minimum Sum LCM(唯一分解定理)

UVA10791 Minimum Sum LCM

Description

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 ≤2^31 − 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

题解

题意

给一个数字n,范围在[1,2^23-1],这个n是一系列数字的最小公倍数,这一系列数字的个数至少为2

例如12,是1和12的最小公倍数,是3和4的最小公倍数,是1,2,3,4,6,12的最小公倍数,是12和12的最小公倍数………………

那么找出一个序列,使他们的和最小,上面的例子中,他们的和分别为13,7,28,24……显然最小和为7

思路

唯一分解定理,设唯一分解式为n=a1^p1*a2^p2……,不难发现每个ai^pi作为一个单独的整数使最优。

然而还有一些细节需要考虑:例如n=1时,答案为1+1 = 2;n只有一种因子时需要加个1,还要注意不要溢出。

代码

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

#define REP(i,n) for(int i=0;i<(n);i++)

int cnt;
ll sum;
int kase=0;
int n;

int main(){
    while(~scanf("%d",&n)){
        kase++;
        if(n==0)    break;
        cnt = 0;
        int mmax = sqrt(n)+1;
        sum = 0;
        for(int i=2;i<mmax;i++){
            if(n%i==0){
                ll tmp =1;
                cnt++;
                while(n%i==0){
                    tmp*=i;
                    n/=i;
                }
                sum+=tmp;
            }
        }
        if(n != 1 || cnt == 0) {
            cnt ++;
            sum += n;
        }
        if(cnt == 1)    sum ++;
        printf("Case %d: %lld\n",kase,sum);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/caomingpei/p/9585900.html