UVa 10791 - Minimum Sum LCM

来源:互联网 发布:怎样评判数据库的好坏 编辑:程序博客网 时间:2024/05/16 15:26

题意:

在LCM的所有约数组成的序列中找到一个序列使得LCM = n,且序列和最小。


算法:

1、如果n=1或者n为素数,则序列和为n+1。

2、将n分解质因子,把相同的因子相乘作为处理后的因子。然后将处理后得到的因子直接相加。


详解可参看http://www.cnblogs.com/scau20110726/archive/2013/01/18/2866101.html


#include<cstdio>#include<iostream>#include<cstring>#include<cmath>using namespace std;typedef long long ll;ll a[110];int c;void solve(ll x){    ll m = (ll)sqrt(x+0.5); //不加这个要从2遍历到n,TLE    c = 0;    for(ll i=2;i<=m && x>1;i++)    {        if(!(x%i))        {            ll fac = 1;            while(x>1 && !(x%i))            {                fac = fac*i;                x/=i;            }            a[c++] = fac;        }    }    if(x>1)   //!        a[c++] = x;}int main(){    int cas=1;    ll ans,n;    while(scanf("%lld",&n)!=EOF)    {        if(n==0) break;        ans = 0;        solve(n);        if(c==1 || n==1)            printf("Case %d: %lld\n",cas++,n+1);        else        {            for(int i=0;i<c;i++)                ans+=a[i];            printf("Case %d: %lld\n",cas++,ans);        }    }    return 0;}


0 0