链接 :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;
}