F - Minimum Sum LCM

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 the LCM of a set of positive integers. For example 12 can be expressed as the LCM 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 whose LCM 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 as LCM of 4 and 3 is 12 and 7 is the minimum possible summation. Input The input file contains at most 100 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 should not be processed. There can be at most 100 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

12 10 5 0

Sample Output

Case 1: 7

Case 2: 7

Case 3: 6

This card is the subject for a long long time ,,,,

Topic effect: to give you an integer n, n let you ask this factor constituted and minimal.

Solution: we need to decompose the only decomposition theorem for n is n = a1 ^ p1 * a2 ^ p2 * a3 ^ p3 ..., ai ^ pi optimal when used as a single integer.

Precautions:

A number of the decomposition of the quality factor must be greater than or equal to 2, for example 8 only 2 points out the value to be so for this type of sentence Laid

2 primes Laid sentence, not for prime ,, ,, Laid determination factorising

3 pairs of special sentence 1

Note 4 n, n decomposition may change when the size n, n advance so you want to save it

When the prime number decomposition. 5 TLE attention, can not i * i <n first taken to determine the number of root =, otherwise TLE

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long ll;

int main(){
    ll n,m;
    int k=0;
    while(~scanf("%lld",&n,&m)&&n){
        ll nn=n;
        if(n==1) {
            printf("Case %d: %lld\n",++k,n+1);
            continue ;
        }
        ll cnt=0;
        ll m=sqrt(n+1);
        ll ans=0;
        for(int i=2;i<=m;i++){
            if(n%i==0){
                cnt++;
                ll sum=1;
                while(n%i==0){
                    sum*=i;
                    n/=i;
                }
                ans= + SUM; 
            } 
        } 
        IF (n-> . 1 ) {// This determination step determines the next must be placed in front of 
            CNT ++ ; 
            ANS + = n-; 
        } 
        IF (CNT == . 1 || CNT == 0 ) { 
            the printf ( " Case% D: LLD% \ n- " , K ++, + NN . 1 ); 
        } 
        the else { 
            the printf ( " Case% D: LLD% \ n- " , ++ K, ANS); 
        } 
        
        
        
    } 
    
    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/Accepting/p/11361035.html