-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrivo.cpp
140 lines (125 loc) · 3.15 KB
/
crivo.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
#define FOR(cont,max) for(int (cont)=0; (cont)<(int)(max);(cont)++)
#define FOR2(cont,start,max) for(int (cont)=(start); (cont)<(int)(max);(cont)++)
#define ABS(x) (((x)> 0 ) ? (x) :-(x))
#define MAX(x,y) (((x)>(y)) ? (x) : (y))
#define MIN(x,y) (((x)<(y)) ? (x) : (y))
#define BETWEEN(x,a,b) (((x)>=(a)) && ((x)<(b)))
#define SWAP(a,b) int _temp_=(a);(a)=(b);(b)=_temp_;
#define RAND(max) (rand()%(max))
#define RAND2(min,max) (min)+(rand()%((max)-(min)))
void sieveEuler(vector<int>& out,int n);
void sieveErathostenes(vector<int>& out,int n);
void sieveErathostenes2(vector<int>& out,int n);
int main(){
vector<int> v;
int x,n;
scanf(" %d %d\n",&x,&n);
if (x==1)sieveEuler(v,n);
else if(x==2)sieveErathostenes2(v,n);
else sieveErathostenes(v,n);
//cout << "Primes = {";
//for(auto i:v)cout << i << ", ";
//cout << "}\n";
}
void sieveEuler(vector<int>& out,int n){
bool v[n+1];
v[2]=true;
for(int j=3;j<=n;j+=2)v[j]=true;
for(int j=4;j<=n;j+=2)v[j]=false;
for(int i=2;i*i<=n;){
if(v[i]){
out.push_back(i);
v[i]=false;
while(i<=n && !v[i])i++;
for(int j=i;j*j<=n;j++){
if(v[j]){
v[i*j]=false;
}
}
}
}
for(int i=out[out.size()-1]+1;i<=n;i++){
if(v[i])out.push_back(i);
}
}
void sieveErathostenes(vector<int>& out,int n){
bool v[n+1];
out.push_back(2);
for(int j=3;j<=n;j+=2)v[j]=true;
for(int j=4;j<=n;j+=2)v[j]=false;
int i;
for(i=3;i*i<=n;i+=2){
if(v[i]){
out.push_back(i);
for(int j=i*i;j<=n;j+=i){
v[j]=false;
}
}
}
for(;i<=n;i++){
if(v[i])out.push_back(i);
}
}
struct node{
node(int max,int x,node* point[]):val{x},del{false},next{NULL}{
//cout << "constructor node max " << max << " x " << val << endl;
if(x<max)next = new node(max,x+1,point);
//cout << "node " << val << " = " << this << " next = " << next << endl;
point[x]=this;
}
int val;
bool del;
node* next;
};
void deleteNode(node* n){
delete n->next;
delete n;
}
void sieveErathostenes2(vector<int>& out,int n){
node* point[n+1];
node* first = new node(n,2,point);
//cout << "finished constructor" << endl;
out.push_back(2);
//cout << "deleting mod 2" << endl;
for(int j=4;j<=n;j+=2){
point[j-1]->next = point[j]->next;
delete point[j];
point[j]=NULL;
}
//cout << "deleted mod 2" << endl;
auto i=point[3];
while(1){
//cout << "i = " << i->val << endl;
out.push_back(i->val);
//cout << "out += i" << endl;
if(!(i->next))break;
//cout << i << " -> next = " << i->next << endl;
i=i->next;
if((i->val * i->val) > n)continue;
//cout << i << endl;
//cout << "i = " << i->val << endl;
auto j=i;
//cout << j << endl;
//cout << "j = " << j->val << endl;
while(j->val * j->val <= n){
//cout << "j = " << j->val << endl;
//cout << "deleting point[" << i->val << "*" << j->val << "]" << endl;
int del=i->val*j->val;
point[del]->del=true;
point[del]=NULL;
//cout << "deleted point[" << i->val << "*" << j->val << "]" << endl;
j=j->next;
}
for(auto x=point[3];x->next;x=x->next){
if(x->next->del){
auto temp = x->next;
x->next = x->next->next;
delete temp;
}
}
}
deleteNode(first);
}