UVA 10791 Minimum Sum LCM

LCM (Least Common Multiple) of a set of integers is de ned 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 nd out a set of at least two positive in-
tegers whose LCM is N. As in nite such sequences are
possible, you have to pick the sequence whose summa-
tion 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 le contains at most 100 test cases. Each
test case consists of a positive integer N (1 N
231 �� 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 speci ed 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


【分析】
唯一分解定理。
设分解式为 n=a1^p1+a2^p2+…+am^pm(^表示几次方,不是异或),那么不难发现把每个 ai^pi 作为一个单独的整数时解最优。
这道题真是long long天坑…WA死了


【代码】

//UVA 10791 Minimum Sum LCM 
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=1e5;
int n,t;
int pri[mxn+5],cnt[mxn+5];
bool vis[mxn+5];
inline void getpri()
{
    int i,j;
    fo(i,2,mxn)
    {
        if(!vis[i]) pri[++pri[0]]=i;
        fo(j,1,pri[0])
        {
            if(i*pri[j]>mxn) break;
            vis[i*pri[j]]=1;
            if(i%pri[j]==0) 
              break;
        } 
    }
}
inline long long query(int x)
{
    if(x==1) return 2;
    memset(cnt,0,sizeof cnt);
    int i,j,flag=0;
    long long ans=0;
    for(i=1;i<=pri[0] && pri[i]<=x;i++)
    {
        while(x%pri[i]==0)
          cnt[i]++,x/=pri[i];
        if(cnt[i]) flag++;
        if(x==1) break;
    }
    fo(i,1,pri[0])
      if(cnt[i])
        ans+=pow(pri[i],cnt[i]);
    if(x>1) ans+=x,flag++;
    if(flag==1) ans++;
    return ans;
}
int main()
{
    int i,j;
    getpri();
    while(scanf("%d",&n) && n)
      printf("Case %d: %lld\n",++t,query(n));
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值