UVA 10791 Minimum Sum LCM (數論)

來源:互聯網
上載者:User

標籤: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 theLCM of a set of positive integers. For example 12 can be expressed as theLCM 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 whoseLCM 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 asLCM of 4 and 3 is 12 and7 is the minimum possible summation.

Input 

The input file contains at most 100 test cases. Each test case consists of a positive integerN ( 1N231 - 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 

 121050

Sample Output 

 Case 1: 7Case 2: 7Case 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;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.