UVA

来源:互联网 发布:网络电视有哪些功能 编辑:程序博客网 时间:2024/05/22 06:47

题意:

 求至少两个数的最小公倍数是n的这些数的最小和。这里用到了唯一分解定理,最小公倍数为n,可以说明这些数可以是n的因子。

本题有好几个特殊处理,当n为1时,答案是1+1=2,还有n只有一个因子的时候需要加一个1,还要注意n=2^31-1不要溢出。本题精度longlong,有些要强制转换一下,否则会出错,一直过不了。

LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is amultiple of all integers of that set. It is interesting to note that any positive integer can be expressedas the LCM of a set of positive integers. For example12 can be expressed as theLCM of1,12 or12,12 or3,4 or4,6 or1,2,3,4 etc.

In this problem, you will be given a positive integerN. You have to find out a set of at least two positive in-tegers whoseLCM isN . As infinite such sequences arepossible, you have to pick the sequence whose summa-tion of elements is minimum. We will be quite happyif you just print the summation of the elements of thisset. So, for N = 12, you should print4+3 = 7 asLCMof 4and 3is 12and 7is the minimum possiblesummation.

Input

The input file contains at most 100test cases. Eachtest case consists of a positive integerN (1N2311).

Input is terminated by a case where N= 0. Thiscase should not be processed. There can be at most100test cases.

Output

Output of each test case should consist of a line starting with ‘Case#:
number. It should be followed by the summation as specified in the problem statement. Look at theoutput for sample input for details.

Sample Input

121050

Sample Output

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

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<queue>#include<set>#include<cstdlib>#include<cmath>using namespace std;#define ll long long#define inf 0x3f3f3f3f#define rep3(i,a) for(int i=0;i<a;i++)#define rep1(i,a,b) for(int i=a;i<=b;i++)#define rep2(i,a,b) for(int i=a;i>=b;i--)#define mem(x) memset(x,0,sizeof(x))#define sfd(a) scanf("%d",&a)#define sfld(a) scanf("%lld",&a)#define sfs(a) scanf("%s",s)#define oi(a) printf("%d",a)#define ol(a) printf("%lld",a)#define maxn 10000int main(){    int n;    int time=1;    while(sfd(n)&&n)    {        ll sum=0;        printf("Case %d: ",time++);        int f=0;        int m=(int)(sqrt((double)n)+10);        for(int i=2;i<=m;i++)        {            if(n%i==0)            {                f++;                int t=1;                while(n%i==0)                {                    t*=i;                    n/=i;                                    }                sum+=(ll)t;            }        }        if(f==0)        cout<<(ll)n+1<<endl;        else if(f==1||n!=1)        cout<<sum+(ll)n<<endl;        else        cout<<sum<<endl;    }    return 0;}


原创粉丝点击