UVA 10791 Minimum Sum LCM

Source: Internet
Author: User

Original title:
LCM (Least Common multiple) of a set of integers is defined as the minimum number, which is a
Multiple of all integers of this set. It's interesting to note, any positive integer can expressed as the LCM of a set of positive integers. For example-expressed as the LCM of 1, or 3, 4 or 4, 6 or 1, 2, 3, 4 etc.
In this problem, you Would be given a positive integer N. You have the to find out a set of at least, positive integers whose LCM is N. As infinite such sequences is possible and you has to pick the sequence whose summation of elements is minimum. We'll be quite happy if your just print the summation of the elements of this set. So, for N = A, you should print 4+3 = 7 as LCM of 4 and 3 are and 7 is the minimum possible summation.

The input file contains at most of the test cases. Each test case consists of a positive integer N (1≤n≤2^31−1). Input is terminated by a case where N = 0. This case is should not being processed. There can is at the most test cases.

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

Sample Input

Sample Output

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

#include <bits/stdc++.h> using namespace std;
Vector<long long> ans;
    Long long decomposition (long long x) {if (x==2) return 3;
    if (x==1) return 2;
    Ans.clear ();
    Long Long t=x; Long Long inde= (long Long) sqrt ((double) x*1.0) +1;//for (int. i=2;i<=inde&&t>1;i++) {while (t%
            i==0&&t>1) {t/=i;
        Ans.push_back (i);
    }} if (t>1)//Ans.push_back (t);
    if (Ans.size () ==0) return x+1;
    Long Long sum=0;
    Long Long tmp=ans[0],pre=ans[0];
        for (int i=1;i<ans.size (); i++) {if (ans[i]==tmp) pre*=ans[i];
            else {sum+=pre;
    }} if (sum==0) sum=pre+1;
    else Sum+=pre;
return sum;
    } int main () {Ios::sync_with_stdio (false);
    Long Long n,k=1;
        while (cin>>n,n) {long ans=decomposition (n); cout<< "
    Case "<<k++<<": "<<ans<<endl;
} return 0;

The question is really giving me the wrong flower = =!
First, if the least common multiple of two numbers is n, then n must be decomposed into a heap of coprime multiplied by the factor. If a number can decompose the coprime factor X and Y. Then X^n and y^m must also be coprime. So the sum of the prime factor corresponding to the second power of decomposition is the smallest sum, otherwise it will not produce least common multiple n. Here the case of prime number and 1 is noted. The
also takes the time complexity O (sqrt (n)) algorithm, and the long Long data type. Otherwise, the tle and re

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.