-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linear sieve v2.cpp
112 lines (74 loc) · 2.42 KB
/
Linear sieve v2.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
Copyright (C) Pawel Masluch, 2021. All rights reserved.
Bibliography:
1. https://eduinf.waw.pl/inf/alg/001_search/0012.php
2. https://codeforces.com/blog/entry/54090
3. https://cp-algorithms.com/algebra/prime-sieve-linear.html
4. https://www.cs.utexas.edu/users/misra/scannedPdf.dir/linearSieve.pdf
*/
#include<cstdio>
#include<vector>
typedef long long LL;
typedef std::pair <int, int> PII;
typedef std::pair <PII, int> PIII;
typedef std::vector <PIII> VPIII;
#define REP(i,a,b) for(int i=a; i<=b; ++i)
#define MP std::make_pair
#define prime first.first
#define exponent first.second
#define powerOfPrime second
const int MAX_N = 100000000;
/*
smallestPrimeFactors[x] = (p, alfa, p^alfa) iff:
1. p^alfa | x
2. ![ p^(alfa+1) | x ]
3. p is smallest prime such that p|x
*/
VPIII smallestPrimeFactors(MAX_N+1);
void linearSieve(int n){ // we find smallest prime factors, for x in {0,1,2,...,n} and MAX_N >= n >= 2
REP(i,0,1){
smallestPrimeFactors[i] = MP( MP(0, 0), 1 ); // by definition
}
REP(i,2,n){
smallestPrimeFactors[i] = MP( MP(i, 1), i ); // initially, we assume that all numbers i in {2,...,n} are primes so smallestPrimeFactors[i] = (i, 1, i^1=i)
}
int p=2, q=2;
while( p*q <= n ){
LL P = p;
int k = 1;
while( P*q <= n ){ // P*q = p^k * q <= n
// P*q = p^k * q is composite
smallestPrimeFactors[ P*q ].prime = p;
smallestPrimeFactors[ P*q ].exponent = k;
smallestPrimeFactors[ P*q ].powerOfPrime = P; // p^k
if( q == p ){
++smallestPrimeFactors[ P*q ].exponent; // k+1
smallestPrimeFactors[ P*q ].powerOfPrime *= p; // p^(k+1)
}
P *= p;
++k;
}
// we find the smallest number, thought cuurently as prime, bigger than q
do{
++q;
}
while( smallestPrimeFactors[q].prime < q );
if( p*q > n ){ // p*q > n
// we find the smallest number, thought cuurently as prime, bigger than q
do{
++p;
}
while( smallestPrimeFactors[p].prime < p );
q = p;
}
}
}
int main(){
int n; // we find smallest prime factors, for x in {0,1,2,...,n} and MAX_N >= n >= 2
scanf( "%d", &n );
linearSieve(n);
REP(i,std::max(2,n-100),n){
printf( "smallestPrimeFactors[%d] = (%d, %d, %d)\n", i, smallestPrimeFactors[i].prime, smallestPrimeFactors[i].exponent, smallestPrimeFactors[i].powerOfPrime );
}
return 0;
}