Crawling in process... |
Description
![Download as PDF Download as PDF](https://i-blog.csdnimg.cn/blog_migrate/2ea975a8d421ce3226c46b0946fd5932.png)
Minimum Sum LCM
Minimum Sum LCM |
![\epsfbox{p10791.eps}](http://uva.onlinejudge.org/external/107/p10791.jpg)
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 3is 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 ( 1N
231 - 1).
Input is terminated by a case where N = 0. This case should not be processed. There can be at most100 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
Problem setter: Md. Kamruzzaman
Special Thanks: Shahriar Manzoor
Miguel Revilla 2004-12-10
题意:
输入正整数n(n<=2^31-1),找出至少两个正整数,使得它们的LCM为n,并且和最小。
思路:
这个题的结论我最开始只是一种感性认识:
n质因式分解为 p1^r1 * p2^r2 ... px^rx
则答案为x个数,p1^r1 + p2^r2 + .. px^rx 最小,且他们的LCM是为n的。然后试了一下WA了
后来发现一些情况:
1的时候是2
只有一个因子的时候要+1
要使用long long
证明:
对于a, b >= 2,有a * b >= a + b,对于分解的因子 pi^ri , pj^rj >= 2,pi^ri * pj^rj >= pi^ri + pj^rj
所以有理由按照上面那样构造答案。
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif
#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)
const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));
const int maxn = 100000 + 20;
int main() {
int kase = 0, n;
while(scanf("%d", &n) != EOF && n) {
kase++;
if(n == 1) {
printf("Case %d: 2\n", kase);
continue;
}
int m = (int) sqrt(n+0.5);
int flag = 0;
LL ans = 0;
for(int i=2; i<=m; i++) {
LL t = 1;
while(n % i == 0) {
t *= i;
n /= i;
}
if(t > 1) {
flag++;
ans += t;
}
}
if(n > 1) {
ans += n;
flag++;
}
if(flag == 1) ans++;
printf("Case %d: %lld\n", kase, ans);
}
return 0;
}