UVa 10791 Minimum Sum LCM

Source: Internet
Author: User

Math problems

Test instructions (just because he read the wrong test instructions and WA once): Give a number n, the range in [1,2^23-1], this n is a series of numbers of least common multiple, this series of numbers is at least 2

For example 12, is the least common multiple of 1 and 12, is 3 and 4 of the least common multiple, is the least common multiple of 1,2,3,4,6,12, is 12 and 12 least common multiple ...

So find out a sequence to make them and the smallest, above examples, their and respectively, 13,7,28,24 ... Apparently minimum and 7

The topic started with no clue, wrote a mob search procedure to find the answer, and then at a glance

First, let's say we know a series of digital a1,a2,a3......an, their LCM is N, so when are they the best solution, when they're 22 coprime?

To make it easier for us to explain the problem in two numbers.

The LCM of A and B is n,gcd is M, then n=a/m*b, their and is sum=a+b;

If M is not 1 (i.e. A and B are not coprime), then why don't we optimize it and change a to a=a/m? , changes in the LCM of A and B remain n, but their and apparently reduced

So we get the most important of a nature, to want to a1,a2,a3......an and the smallest, to ensure that they 22 coprime, as long as there is no coprime of two number, it can be nearly one step optimization

So how do we guarantee 22 coprime? The method is actually very simple, the direct decomposition of the mass factor

For example, 24=2*2*2*3, can only be decomposed into 8 and 3, because there are 3 2, 3 2 must be together, if this 3 2, this appears to have two number will have a public quality factor 2, and will make the two number of LCM is not 24

Again, for example 72=2*2*2*3*3, can only be divided into 8 and 9, because 3 2 and 2 3 can not be separated, they must at once

So, we decompose a number n into a mass factor, and by the way a processing, in addition to clean a qualitative factor, they multiply as a factor, after processing will be a number of factors, they also meet the nature of 22 coprime

And then a further analysis.

For example, 264600=8*27*25*49, just from 3 2, 3 3, 2 5, 2 7, the processing of the resulting factor, then 8,27,25,49 LCM is 264600, and 22 coprime, they have to deal with it? No need, directly add them up is the answer we want! Why is it? 8,27,25,49 These numbers can be multiplied, no matter how well, the final number of their LCM is still n, but multiply it is obviously more than the direct addition of a lot more!

So we've got the solution to this problem.

1. Decompose a number into a mass factor and multiply the same factor as a treated factor

2. Adding multiple factors directly to the process is the answer

3. Because the topic says that as long as two numbers are needed, we need to be careful about 1 and primes. For prime numbers, we can only decompose a factor on its own, for 11 factors are not decomposed (we do not take 1 as a factor), their answer is n+1, because only 1 and N of the LCM is n

#include <cstdio> #include <cmath> #define INF 0x3f3f3f3f#define N 110long long f[n],ccount;void init (long Long N) {    long long m= (long Long) sqrt (n+0.5);    ccount=0;    For (long long i=2;i<=m&&n>1;i++) {        ///Quality factor if (! (n%i)) Can decompose I this qualitative factor        {            long long fac=1;            while (!) ( n%i) && n>1)            {fac*=i; n/=i;}            F[CCOUNT++]=FAC; FAC is a processed factor        }    if (n>1) f[ccount++]=n;} int main () {    long long N,ans;    int case=0,i;    while (scanf ("%lld", &n)!=eof && N) {        init (n);/        ////////////n = = n is the prime number or 1            ans=n+1;        else for            (ans=0,i=0;i<ccount;i++)            ans+=f[i];        printf ("Case%d:%lld\n", ++case,ans);    }    return 0;}


UVa 10791 Minimum Sum LCM

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.