uva 10791 LCM的最小和

来源:互联网 发布:windows live免安装版 编辑:程序博客网 时间:2024/06/07 03:56

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

题目大意:
给n,使得两个以上的数的LCM是n,求这样的数的最小和,
思路:
n分解质因数,每个因素的幂相加即是和,
注意特殊情况:n=1或者n只有1个素因子

#include<stdio.h>#include<algorithm>#include<iostream>#include<cmath>#include<cstring>#include<vector>#include<queue>using namespace std;#define ll long longvoid factor(ll n,int* a,int* b,int &tot){    ll temp,i,now;    temp=(int)((double)sqrt(n)+1);    tot=0;    now=n;    for(i=2;i<temp;i++){        if(now%i==0){            a[++tot]=i;            b[tot]=0;            while(now%i==0){                ++b[tot];                now/=i;            }        }    }            if(now!=1){                a[++tot]=now;                b[tot]=1;            }}int quickpow(int a,int b){    int r=1,base=a;    while(b){        if(b&1)        r*=base;        base*=base;        b>>=1;    }    return r;}int a[10000],b[10000],tot;int main(){    ll n;    int k=0;    while(~scanf("%lld",&n),n){        memset(a,0,sizeof(a));        memset(b,0,sizeof(b));        factor(n,a,b,tot);        ll ans=0;        if(tot==1||n==1)ans=n+1;        else{        for(int i=1;i<=tot;i++){            ans+=quickpow(a[i],b[i]);        }        }        printf("Case %d: %lld\n",++k,ans);    }    return 0;}
0 0
原创粉丝点击