题目 | Factorial and Multiple
Denso Create Programming Contest 2022 Winter(AtCoder Beginner Contest 280)
D - Factorial and Multiple
https://atcoder.jp/contests/abc280/tasks/abc280_d
Time Limit: 2 sec / Memory Limit: 1024 MB
Score :
Problem Statement
You are given an integer
Find the minimum positive integer
Here,
Constraints
is an integer.
Input
The input is given from Standard Input in the following format:
K
Output
Print the minimum positive integer
Sample Input 1
30
Sample Output 1
5
Therefore,
Sample Input 2
123456789011
Sample Output 2
123456789011
Sample Input 3
280
Sample Output 3
7
题解
(这题比赛的时候用不正常的方法混过了,现在补一遍正解)
该题要分解质因数,这点应该比较好想,不过之后的处理方式才是重点。即:
对于分解好的数
我们可以单独考虑每一个质数,对于
如
那么如何求到最小的呢?分解质因数后能保证时间复杂度为
如
考虑完单个质数之后,整个
代码
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 10;
int prime[MAXN], primesize = 0;
bool not_prime[MAXN];
void init_prime()
{
not_prime[0] = not_prime[1] = true;
for (int i = 2; i < MAXN; i++)
{
if (!not_prime[i])
prime[primesize++] = i;
for (int j = 0; j < primesize && i * prime[j] < MAXN; j++)
{
not_prime[i * prime[j]] = true;
if (i % prime[j] == 0)
break;
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
init_prime();
ll K;
cin >> K;
ll ans = 0;
for (int i = 0; i < primesize; i++)
{
ll prm = prime[i], cnt = 0;
if (prm * prm > K)
break;
while (!(K % prm))
{
K /= prm;
cnt++;
}
ll n = 0;
while (cnt > 0)
{
n += prm;
int x = n;
while (!(x % prm))
{
x /= prm;
cnt--;
}
}
ans = max(ans, n);
}
cout << max(ans, K) << endl;
return 0;
}
本文采用 CC BY-SA 4.0 许可,本文 Markdown 源码:Haotian-BiJi