UVa 10791 (unique decomposition) Minimum Sum LCM

Source: Internet
Author: User

Test instructions

Enter N to find at least two positive integers, so that the least common multiple of these numbers is n and minimum.

Analysis:

The decomposition of the n is, apparently alone, as an item, and the smallest.

Here are two tips:

    • Starting from 2, the constant addition of n, until it cannot be divisible. This eliminates the problem of prime judgment and shortens the amount of code. Because the first 2 of all n is out of the factor, then there will be no n 4 = = 0 of the case, so that all except n is a prime number.
    • From 2 in addition to n until sqrt (n), if n is not 1, then the "remaining" is at this time n the largest mass factor. Reduce the number of cycles.
1#include <cstdio>2#include <cmath>3 4typedefLong LongLL;5 6 intMainvoid)7 {8     intN, Kase =0;9      while(SCANF ("%d", &n) = =1&&N)Ten     { OneLL ans =0; A         intm = sqrt (n +0.5); -         intpcnt =0; -          the         if(n = =1) -         { -printf"Case %d:2\n", ++Kase); -             Continue; +         } -          +          for(inti =2; I <= m; ++i) A         { at             if(n% i = =0) -             { -pcnt++; -                 inttemp =1; -                  while(n% i = =0) -                 { inN/=i; -Temp *=i; to                 } +                 if(Temp >1) ans + =temp; -             } the         } *         if(N >1) $         {Panax Notoginsengpcnt++; -Ans + =N; the         } +         if(Pcnt <=1) ans++; A          theprintf"Case %d:%lld\n", ++Kase, ans); +          -     } $      $     return 0; -}
code June

UVa 10791 (unique decomposition) Minimum Sum LCM

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.