JOI 春合宿 2007 day1-2 Factorial
問題
解説
n を以下のように素因数分解したときのことを考える.
このとき, もし指数
となり,
この操作を全ての素因数に対して行い, それらの最大値を取れば求める答えが得られる.
実装例
C++
cpp
#include <algorithm>
#include <iostream>
#include <map>
std::map<long long, int> prime_factor(long long n) {
std::map<long long, int> res;
for (long long x = 2; x * x <= n; x++) {
while (n % x == 0) {
++res[x];
n /= x;
}
}
if (n != 1) {
res[n] = 1;
}
return res;
}
int main() {
int n;
std::cin >> n;
std::map<long long, int> prime_factors = prime_factor(n);
long long ans = 0;
for (auto it = prime_factors.begin(); it != prime_factors.end(); it++) {
long long prime = it->first;
int e = it->second;
int cnt = 0;
for (int i = 1; i <= n; i++) {
long long m = prime * i;
ans = std::max(ans, m);
while (m % prime == 0) {
cnt++;
m /= prime;
}
if (cnt >= e) {
break;
}
}
}
std::cout << ans << "\n";
return 0;
}
Python
python
#!/usr/bin/env python3
def prime_factor(n):
res, x = {}, 2
while x * x <= n:
while n % x == 0:
res[x] = res.get(x, 0) + 1
n //= x
x += 1
if n != 1:
res[n] = 1
return res
n = int(input())
prime_factors = prime_factor(n)
ans = 0
for prime, e in prime_factors.items():
cnt = 0
x = 1
while cnt < e:
m = prime * x
x += 1
ans = max(ans, m)
while m % prime == 0:
cnt += 1
m //= prime
print(ans)