UVa 10791 Minimum Sum LCM (number theory & element factorization)

Source: Internet
Author: User
Tags time limit

10791-minimum Sum LCM

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php? Option=com_onlinejudge&itemid=8&category=467&page=show_problem&problem=17 32

LCM (least Common multiple) of a set of integers is defined as the minimum number, which are a multiple of all int Egers of that set. It is interesting to the positive integer can expressed as the LCM of a set of positive integers. For Example can expressed as the LCM of 1, or 3, 4 or 4, 6 or 1, 2, 3, 4 etc.

In the problem, you'll be given a positive integer N. You are have to find out a set of in least two positive integers whose LCM is N. As infinite such sequences are possible, you are have to pick the sequence whose the summation of-is elements. We are quite happy if you are just print the summation of the elements of this set. So, for N = should print4+3 = 7 as LCM of 4 and 3 are 7 is the minimum possible summation.

Input

The input file contains at most test cases. Each test case consists of a positive integer n (1n2 to 1).

The Input is terminated by a case where N = 0. This case should is processed. There can be at most test cases.

Output

Output of each test case should consist's 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 in the output for sample input for details.

Sample Input

5
0

Sample Output

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

Thinking: Element factorization

Complete code:

/*0.022s*/
  
#include <cstdio>
#include <cmath>
  
int main (void)
{
    int n, ncase = 0;
    int maxi, temp, F, I;
    Long ans;
    while (scanf ("%d", &n), N)
    {
        maxi = (int) sqrt (n);
        Ans = 0;
        f = 0;
        for (i = 2; I <= maxi i++)
            if (n% i = = 0)
            {
                f++;
                temp = 1;
                while (n% i = = 0)
                {
                    temp *= i;
                    n/= i;
                }
                Ans + temp;
            }
  
        Supplement
        if (f = = 0)
            ans = (long long) n + 1;
        else if (n > 1 | | f = = 1)
            ans + = n;
        printf ("Case%d:%lld\n", ++ncase, ans);
    }
    return 0;
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.