UVa 10791 - Minimum Sum LCM (数论 推理)

Crawling in process...

Description

Download as PDF


  Minimum Sum LCM 

\epsfbox{p10791.eps}

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 112 or 12,12 or 34 or 46 or 1234 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 3is 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$ \le$N$ \le$231 - 1).

Input is terminated by a case where N = 0. This case should not be processed. There can be at most100 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



Problem setter: Md. Kamruzzaman
Special Thanks: Shahriar Manzoor

Miguel Revilla 2004-12-10




题意:

输入正整数n(n<=2^31-1),找出至少两个正整数,使得它们的LCM为n,并且和最小。


思路:

这个题的结论我最开始只是一种感性认识:

n质因式分解为 p1^r1 * p2^r2 ... px^rx

则答案为x个数,p1^r1 + p2^r2 + .. px^rx 最小,且他们的LCM是为n的。然后试了一下WA了

后来发现一些情况:

1的时候是2

只有一个因子的时候要+1

要使用long long 


证明:

对于a, b >= 2,有a * b >=  a + b,对于分解的因子 pi^ri , pj^rj >= 2,pi^ri * pj^rj >= pi^ri + pj^rj

所以有理由按照上面那样构造答案。




#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 100000 + 20;

int main() {
    int kase = 0, n;

    while(scanf("%d", &n) != EOF && n) {
        kase++;
        if(n == 1) {
            printf("Case %d: 2\n", kase);
            continue;
        }
        int m = (int) sqrt(n+0.5);
        int flag = 0;
        LL ans = 0;
        for(int i=2; i<=m; i++) {
            LL t = 1;
            while(n % i == 0) {
                t *= i;
                n /= i;
            }
            if(t > 1) {
                flag++;
                ans += t;
            }
        }
        if(n > 1) {
            ans += n;
            flag++;
        }
        if(flag == 1) ans++;
        printf("Case %d: %lld\n", kase, ans);
    }

    return 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值