首页 > 代码库 > UVA - 10791 Minimum Sum LCM(最小公倍数的最小和)

UVA - 10791 Minimum Sum LCM(最小公倍数的最小和)

题意:输入整数n(1<=n<231),求至少两个正整数,使得它们的最小公倍数为n,且这些整数的和最小。输出最小的和。

分析:

1、将n分解为a1p1*a2p2……,每个aipi作为一个单独的整数时最优。

2、n为1时,len==0;n为素数时,len==1。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100 + 10;
const int MAXT = 10000 + 10;
using namespace std;
vector<LL> v;
void deal(LL n){//将n分解成因子
    v.clear();
    LL m = (LL)sqrt(n + 0.5);
    for(LL i = 2; i <= m; ++i){
        if(n % i == 0){//n中的质因子
            LL tmp = 1;
            while(n % i == 0 && n > 1){
                tmp *= i;
                n /= i;
            }
            v.push_back(tmp);//由质因子i合并成的因子
        }
        if(n <= 1) break;
    }
    if(n > 1) v.push_back(n);//素数本身
}
int main(){
    LL N;
    int kase = 0;
    while(scanf("%lld", &N) == 1){
        if(!N) return 0;
        deal(N);
        LL ans = 0;
        int len = v.size();
        if(len == 0 || len == 1){//1或素数
            ans = N + 1;
        }
        else{
            for(int i = 0; i < len; ++i){
                ans += v[i];
            }
        }
        printf("Case %d: %lld\n", ++kase, ans);
    }
    return 0;
}

  

UVA - 10791 Minimum Sum LCM(最小公倍数的最小和)