UVaOJ10791 - Minimum Sum LCM


10791 - Minimum Sum LCM

Time limit: 3.000 seconds \epsfbox{p10791.eps}

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 LCMof a set of positive integers. For example 12 can be expressed as the LCMof 112 or 1212 or 34 or 46 or 1234 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$ \le$N$ \le$231 - 1).

Input is terminated by a case where N = 0. This case should not be processed. There can be at most 100test 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



Problem setter: Md. Kamruzzaman
Special Thanks: Shahriar Manzoor

Miguel Revilla 2004-12-10

题目大意:

            输入几个数的最小公倍数,求这几个数的最小和。

解题方法:

       注意三点:

1、每个素因子组成的最大数加,如:12=(2*2)+ 3

2、区分单个素因子16=(2*2*2*2)+1

3、素因子只有1和本身的7=1+7=8,即本身是素数。

源代码:

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int N;
void slove() {
    int temp, cnt = 0, k;
    long long ans = 0;
    k = (int)sqrt(N + 1);
    for (int i = 2; i <= k; i ++) {
        if (N % i == 0) {
            temp = 1;
            while (N % i == 0) {
                temp *= i;
                N /= i;
            }
            ans += temp;
            cnt ++;
        }
    }
    if (N != 1 || cnt == 0) {
        ans += N;
        cnt ++;
    }
    if (cnt == 1) {
        ans ++;
    }
    printf("%lld\n", ans);
}
int main() {
    int nCase = 1;
    while (scanf("%d", &N) != EOF) {
        if (N == 0) break;
        else {
            printf("Case %d: ", nCase ++);
            slove();
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值