-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlpf.cpp
More file actions
45 lines (35 loc) · 739 Bytes
/
lpf.cpp
File metadata and controls
45 lines (35 loc) · 739 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>
using namespace std;
/*
Build: O(nlogn)
Factor: O(logn)
*/
const int maxn = 1e6;
int lpf[maxn+123] = {};
void build(){
for(int i = 1;i <= maxn;i++) lpf[i] = i;
for(int i = 2;i <= maxn;i++){
if (lpf[i] == i){
for(int j = 2;i*j <= maxn;j++){
int x = i*j;
if (lpf[x] == x) lpf[x] = i;
}
}
}
}
vector<int> factor(int x){
vector<int>primes;
while(x != 1){
primes.push_back(lpf[x]);
x /= lpf[x];
}
return primes;
}
int main(){
build();
int x = 123456;
cout << "Factors of " << x << ": " << endl;
for(auto p:factor(x)) cout << p << " ";
cout << endl;
return 0;
}