(UVA - 10791)Minimum Sum LCM (唯一分解定理)

27 篇文章 0 订阅
9 篇文章 0 订阅

链接 :https://vjudge.net/problem/UVA-10791

分析:任意一个大于1的数都能用若干素因子的积来表示, 即唯一分解定理。

在这道题中用唯一分解定理, n=a1^p1*a2^p2……
发现,每个ai^pi作为一个单独的整数时满足题目条件 最小公倍数的最小和。

有两种情况需要特判:
1.n为素数时,输出直接是n+1
2.只有一种素因子或者有素因子大于sqrt(n)的时候 需要加上最后的n

ps:如果不用long long 的话还要注意n=2^31-1的时候,结果会溢出

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define mem(a,n) memset(a,n,sizeof(a))
typedef  long long LL;
const int INF=0x3f3f3f3f;
const int mod=1e4+1;
const int N=1e4+5;
LL solve(LL n)
{
    LL ans=0,cnt=0;
    for(LL i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            cnt++;
            LL t=1;
            while(n%i==0)
            {
                t*=i;
                n/=i;
            }
            ans+=t;
        }
    }
    if(!cnt) ans=n+1;
    else if(cnt==1||n!=1)
        ans+=n;
    return ans;
}
int main()
{
    int n,cas=1;
    while(~scanf("%d",&n)&&n)
    {
        LL ans=solve(n);
        printf("Case %d: %lld\n",cas++,ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值