码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 10791 Minimum Sum LCM (数论)

时间:2014-08-18 23:38:13      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   os   io   strong   for   ar   

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.

Input 

The input file contains at most 100 test cases. Each test case consists of a positive integer N ( 1bubuko.com,布布扣Nbubuko.com,布布扣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 

 
12
10
5
0

Sample Output 

 
Case 1: 7
Case 2: 7
Case 3: 6
真不想吐槽这道题,前前后后花了2个小时(唯一分解定理的运用)
I64d竟然wa,lld过的,就因为这卡了1个小时。。。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
#include<cmath>
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
int main()
{
   LL n;
   int l=0;
   while(~scanf("%lld",&n)&&n)
   {
       LL ans=0;
       int num=0;
       int m=(int)sqrt(n);
       for(int i=2;i<=m;i++)
       {
           if(n%i==0)
           {
               num++;
               LL sum=1;
               while(n%i==0)
               {
                   n/=i;
                   sum*=i;
               }
               ans+=sum;
//               cout<<ans<<endl;
           }
       }
       if(num==0)
          ans=(LL)n+1;
       else if(n>1||num==1)
          ans+=n;
       printf("Case %d: %lld\n",++l,ans);
   }
   return 0;
}



UVA 10791 Minimum Sum LCM (数论),布布扣,bubuko.com

UVA 10791 Minimum Sum LCM (数论)

标签:style   http   color   os   io   strong   for   ar   

原文地址:http://blog.csdn.net/u013582254/article/details/38668691

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!