题意:求几个最小公倍数为n的数的最小和
1、每个素因子组成的最大数加,如:12=(2*2)+ 3
2、区分单个素因子16=(2*2*2*2)+1
3、素因子只有1和本身的7=1+7=8,即本身是素数。
CODE:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <ctype.h>
using namespace std;
long long n;
long long solve(long long n)
{
long long tempn;
long long ans = 0, cnt = 0;
int m = (int)(sqrt(n)+0.5);
tempn = n;
for(int i = 2; i <= m; i++) if(tempn%i == 0)
{
int temp = 1;
while(tempn%i == 0)
{
temp *= i;
tempn /= i;
}
ans += temp;
cnt++;
}
if(tempn == n) return (n+1); //本身是素数
if(tempn != 1) //可能最后一个数不等于1
{
ans += tempn;
return ans;
}
if(cnt == 1) ans++; //单个素因子
return ans;
}
int main()
{
int times = 0;
while(scanf("%lld", &n) && n)
{
printf("Case %d: %lld\n", ++times, solve(n));
}
return 0;
}
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <ctype.h>
using namespace std;
long long n;
long long solve(long long n)
{
long long tempn;
long long ans = 0, cnt = 0;
int m = (int)(sqrt(n)+0.5);
tempn = n;
for(int i = 2; i <= m; i++) if(tempn%i == 0)
{
int temp = 1;
while(tempn%i == 0)
{
temp *= i;
tempn /= i;
}
ans += temp;
cnt++;
}
if(tempn == n) return (n+1); //本身是素数
if(tempn != 1) //可能最后一个数不等于1
{
ans += tempn;
return ans;
}
if(cnt == 1) ans++; //单个素因子
return ans;
}
int main()
{
int times = 0;
while(scanf("%lld", &n) && n)
{
printf("Case %d: %lld\n", ++times, solve(n));
}
return 0;
}