Uva--10791(数论,唯一分解)
2014-09-02 16:21:26
Minimum Sum LCM |
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 theLCM 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 ( 1N231 - 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)n 本身为素数(2)质因子数只有一个的情况(3)n 为 2^31 - 1的情况。
1 #include <cstdio> 2 #include <cmath> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 typedef long long ll; 7 8 int main(){ 9 int Case = 0,n,m,v; 10 ll ans; 11 while(scanf("%d",&n) != EOF && n){ 12 ans = 0; 13 m = sqrt(1.0 * n) + 2; 14 int cnt = 0,tn = n; 15 for(int i = 2; i <= m; ++i) if(n % i == 0){ 16 ++cnt; 17 v = 1; 18 while(n % i == 0){ 19 n /= i; 20 v *= i; 21 } 22 ans += (ll)v; 23 if(n == 1) break; 24 } 25 if(tn == n) ans = (ll)tn + 1; 26 else if(cnt == 1 || n != 1) ans += (ll)n; 27 printf("Case %lld: %lld\n",++Case,ans); 28 } 29 return 0; 30 }
版本2:
1 /************************************************************************* 2 > File Name: 10791.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Tue 02 Sep 2014 12:00:18 AM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 typedef long long ll; 16 17 int n,cnt,aprime[100005],prime[10005]; 18 ll ans; 19 20 void Erato(int n){ 21 int m = (int)sqrt(n + 0.5); 22 for(int i = 2; i <= m; ++i) if(!aprime[i]) 23 for(int j = i * i; j <= n; j += i) aprime[j] = 1; 24 } 25 26 void Pre(){ 27 cnt = 0; 28 for(int i = 2; i <= 100000; ++i) if(!aprime[i]) 29 prime[cnt++] = i; 30 } 31 32 int main(){ 33 Erato(100000); 34 Pre(); 35 int Case = 0,v; 36 while(scanf("%d",&n) != EOF && n){ 37 printf("Case %d: ",++Case); 38 ans = 0; 39 int num = 0,tn = n; 40 for(int i = 0; i < cnt; ++i) if(n % prime[i] == 0){ 41 ++num; 42 v = 1; 43 while(n % prime[i] == 0){ 44 v *= prime[i]; 45 n /= prime[i]; 46 } 47 ans += (ll)v; 48 if(n == 1) break; 49 } 50 if(tn == n) ans = (ll)tn + 1; 51 else if(num == 1 || n != 1) ans += (ll)n; 52 printf("%lld\n",ans); 53 } 54 return 0; 55 }