UVA10791 Minimum Sum LCM 质因数分解

来源:互联网 发布:微信收费数据共享 编辑:程序博客网 时间:2024/04/28 14:09


Minimum Sum LCM
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

Submit Status

Description

Download as PDF


  Minimum Sum LCM 

\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 LCM of a set of positive integers. For example 12 can be expressed as the LCM of 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 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 

 121050

Sample Output 

 Case 1: 7Case 2: 7Case 3: 6



Problem setter: Md. Kamruzzaman
Special Thanks: Shahriar Manzoor
Miguel Revilla 2004-12-10

Source

Root :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 10. Maths :: Examples
Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 6. Mathematical Concepts and Methods
Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 2. Mathematics :: Basic Problems
Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Mathematics :: Number Theory :: Working with Prime Factors

Root :: Prominent Problemsetters :: Md. Kamruzzaman (KZaman)
Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Mathematics :: Number Theory :: Working with Prime Factors

Submit Status


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;typedef long long int LL;LL n;LL ans(LL n){    LL ret=0,num=0;    if(n==1) return 2LL;    LL m=sqrt(n+0.5);    for(LL i=2;i<=m&&n!=1;i++)    {        if(n%i) continue;        LL temp=1LL;        num++;        while(n%i==0)        {            n/=i;temp*=i;        }        ret+=temp;    }    if(n!=1)    {        num++; ret+=n;    }    if(num<2) ret+=1;    return ret;}int main(){    int cas=1;    while(scanf("%lld",&n)!=EOF&&n)    {        printf("Case %d: %lld\n",cas++,ans(n));    }    return 0;}





0 0