File tree 3 files changed +47
-14
lines changed
3 files changed +47
-14
lines changed Original file line number Diff line number Diff line change 1
1
2
+ /* *
3
+
4
+ Sieve of Eratosthenes
5
+ =====================
6
+
7
+ Time Complexity : O( N log(logN) )
8
+ Memory Complexity : O( N )
9
+
10
+ **/
11
+
2
12
/* * Which of the favors of your Lord will you deny ? **/
3
13
4
14
#include < bits/stdc++.h>
Original file line number Diff line number Diff line change 1
1
2
+ /* *
3
+
4
+ Bit Sieve
5
+ =========
6
+
7
+ Time Complexity : O( N log(logN) )
8
+ Memory Complexity : N bit
9
+
10
+ **/
11
+
2
12
/* * Which of the favors of your Lord will you deny ? **/
3
13
4
14
#include < bits/stdc++.h>
Original file line number Diff line number Diff line change 1
1
2
+ /* *
3
+
4
+ Segmented Sieve
5
+ ===============
6
+
7
+ Precalculating primes : O( sqrt(R) log(log(sqrt(R))) )
8
+ Later segmented sieve : O( (R-L+1) log(logR) )
9
+
10
+ So,
11
+ Time Complexity : O( (R-L+1) log(logR) + sqrt(R) log(log(sqrt(R))) )
12
+
13
+ **/
14
+
2
15
/* * Which of the favors of your Lord will you deny ? **/
3
16
4
17
#include < bits/stdc++.h>
@@ -67,17 +80,17 @@ string to_str(T x)
67
80
68
81
Segmented Sieve
69
82
70
- Range of [A,B ] ~ 10^5
83
+ Range of [L,R ] ~ 10^5
71
84
72
85
**/
73
86
74
- const int pnmax = 1e6 +10 ;
75
- const int pnmax2 = 1e5 +10 ; /* * diff of B and A **/
87
+ const int pnmax = 1e6 +10 ; /* * ~ sqrt(R) * */
88
+ const int pnmax2 = 1e5 +10 ; /* * diff of R and L **/
76
89
77
- LL LIM = 1e6 +5 ;
90
+ LL LIM = 1e6 +5 ; /* * ~ sqrt(R) * */
78
91
vector<LL>primes;
79
- bool isP[nmax ];
80
- bool isPFinal[nmax2 ];
92
+ bool isP[pnmax ];
93
+ bool isPFinal[pnmax2 ];
81
94
82
95
void sieve ()
83
96
{
@@ -95,31 +108,31 @@ void sieve()
95
108
}
96
109
}
97
110
98
- void segmented_sieve (LL A ,LL B )
111
+ void segmented_sieve (LL L ,LL R )
99
112
{
100
- for (LL i=0 ; i<=nmax2 ; i++)
113
+ for (LL i=0 ; i<=pnmax2 ; i++)
101
114
isPFinal[i]=true ;
102
115
103
116
for (LL i=0 ;i<primes.size ();i++)
104
117
{
105
118
LL p=primes[i];
106
119
LL j=p*p;
107
120
108
- if (j<A )
109
- j=((A +p-1 )/p)*p;
121
+ if (j<L )
122
+ j=((L +p-1 )/p)*p;
110
123
111
- for (;j<=B ;j+=p)
112
- isPFinal[j-A ]=false ;
124
+ for (;j<=R ;j+=p)
125
+ isPFinal[j-L ]=false ;
113
126
114
127
}
115
128
116
129
/* * print **/
117
- for (LL i=A ;i<=B ;i++)
130
+ for (LL i=L ;i<=R ;i++)
118
131
{
119
132
if (i==1LL )
120
133
continue ;
121
134
122
- if (isPFinal[i-A ])
135
+ if (isPFinal[i-L ])
123
136
cout<<i<<endl;
124
137
}
125
138
}
You can’t perform that action at this time.
0 commit comments