跳转至

问题 D: 最小公倍数的最小和

传送门:ZWGOJ | 洛谷 UVA10791 | UVA 10791

题目描述

原题面

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.

输入要求

The input file contains at most 100 test cases. Each test case consists of a positive integer \(N (1 \le N \le 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 of each test case should consist of a line starting with ‘\(\texttt{Case #: }\)’ where \(\texttt{#}\) 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.

样例

1
2
3
4
12
10
5
0
1
2
3
Case 1: 7
Case 2: 7
Case 3: 6

解法

解法和时间复杂度: (1)

  1. 此处时间复杂度为该解法的算法部分的时间复杂度,不是严谨的整题的时间复杂度
  • 数学 \(O(\sqrt n)\)

数学

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <bits/stdc++.h>
using namespace std;
#define int long long int

signed main() {
    int n, T = 0;
    while(scanf("%lld", &n) != EOF && n != 0) {
        T ++;
        if(n == 1) {printf("Case %lld: 2\n", T); continue; }
        int m = n, ans = 0, now = 0, k = 0;
        for(int i = 2; i * i <= n && m != 1; i ++) {
            now = 1;
            while(m % i == 0 && m != 1) {
                now *= i;
                m /= i;
            }
            if(now != 1) k ++, ans += now;
        }
        if(m != 1) {
            ans += m;
            k ++;
        }
        if(k == 1) printf("Case %lld: %lld\n", T, n + 1);
        else printf("Case %lld: %lld\n", T, ans);
    }
    return 0;
}