题意:给你一个整数N,求至少两个正整数,使得他们的最小公倍数是N,且这些整数的和最小,求和。
思路:我们可以用唯一分解定理来求解,发现N的每一个质因子单独作为一个整数的时候情况最优。
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define MAXN 110000
#define MAXE 5
#define INF 100000000
#define MOD 10001
#define LL long long
#define pi 3.14159
using namespace std;
bool prime[MAXN];
LL P[MAXN];
LL mi[MAXN];
int cnt = 0;
struct Node {
LL x;
LL y;
};
void is_prime() {
memset(prime, true, sizeof(prime));
for (int i = 2; i <= 100010; ++i) {
if (prime[i]) {
P[cnt++] = i;
for (int j = 2 * i; j <= 100010; j += i) {
prime[j] = false;
}
}
}
}
LL power(LL a, LL b) {
LL ans = 1;
while (b) {
if (b & 1) {
ans *= a;
}
b >>= 1;
a = a * a;
}
return ans;
}
int main() {
std::ios::sync_with_stdio(false);
is_prime();
LL n;
int kase = 0;
while (cin >> n && n) {
cout << "Case " << ++kase << ": ";
if (n == 1) {
cout << 2 << endl;
} else {
vector<Node> vec;
for (int i = 0; i < cnt; ++i) {
int ans = 0;
while (n % P[i] == 0) {
ans++;
n /= P[i];
}
if (ans >= 1) {
vec.push_back((Node) {P[i], ans});
}
}
if (n > 1) {
vec.push_back((Node) {n, 1});
}
LL sum = 0;
if (vec.size() == 1) {
sum += power(vec[0].x, vec[0].y);
sum += 1;
} else {
for (int i = 0; i < vec .size(); ++i) {
sum += power(vec[i].x, vec[i].y);
}
}
cout << sum << endl;
}
}
return 0;
}
/*
12
10
5
0
*/