-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathprimes.cpp
37 lines (37 loc) · 1018 Bytes
/
primes.cpp
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
/* 12721, 13331, 14341, 75577, 123457, 222557, 556679
* 999983, 1097774749, 1076767633, 100102021, 999997771
* 1001010013, 1000512343, 987654361, 999991231
* 999888733, 98789101, 987777733, 999991921, 1010101333
* 1010102101, 1000000000039, 1000000000000037
* 2305843009213693951, 4611686018427387847
* 9223372036854775783, 18446744073709551557 */
int mu[ N ] , p_tbl[ N ];
vector<int> primes;
void sieve() {
mu[ 1 ] = p_tbl[ 1 ] = 1;
for( int i = 2 ; i < N ; i ++ ){
if( !p_tbl[ i ] ){
p_tbl[ i ] = i;
primes.push_back( i );
mu[ i ] = -1;
}
for( int p : primes ){
int x = i * p;
if( x >= M ) break;
p_tbl[ x ] = p;
mu[ x ] = -mu[ i ];
if( i % p == 0 ){
mu[ x ] = 0;
break;
} } } }
vector<int> factor( int x ){
vector<int> fac{ 1 };
while( x > 1 ){
int fn = SZ(fac), p = p_tbl[ x ], pos = 0;
while( x % p == 0 ){
x /= p;
for( int i = 0 ; i < fn ; i ++ )
fac.PB( fac[ pos ++ ] * p );
} }
return fac;
}