-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprimes.cpp
More file actions
308 lines (285 loc) · 10.3 KB
/
primes.cpp
File metadata and controls
308 lines (285 loc) · 10.3 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
Prime Numbers & Factorization
Mathematical Foundation:
Sieve of Eratosthenes: Mark multiples of primes
Miller-Rabin: Probabilistic primality test
Trial Division: O(√n) factorization
Time: Sieve O(n log log n), Miller-Rabin O(k log³ n)
*/
#include <bits/stdc++.h>
using namespace std;
// Sieve of Eratosthenes
// Related LeetCode Problems:
// 204. Count Primes
// https://leetcode.com/problems/count-primes/
// 762. Prime Number of Set Bits in Binary Representation
// https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/
// 279. Perfect Squares
// https://leetcode.com/problems/perfect-squares/
// 264. Ugly Number II
// https://leetcode.com/problems/ugly-number-ii/
// 313. Super Ugly Number
// https://leetcode.com/problems/super-ugly-number/
// 650. 2 Keys Keyboard
// https://leetcode.com/problems/2-keys-keyboard/
// 952. Largest Component Size by Common Factor
// https://leetcode.com/problems/largest-component-size-by-common-factor/
vector<bool> sieve(int n) {
vector<bool> is_prime(n + 1, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i * i <= n; i++) {
if (is_prime[i]) {
for (int j = i * i; j <= n; j += i)
is_prime[j] = false;
}
}
return is_prime;
}
// Generate all primes up to n
// Related LeetCode Problems:
// 204. Count Primes
// https://leetcode.com/problems/count-primes/
// 762. Prime Number of Set Bits in Binary Representation
// https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/
// 952. Largest Component Size by Common Factor
// https://leetcode.com/problems/largest-component-size-by-common-factor/
// 279. Perfect Squares
// https://leetcode.com/problems/perfect-squares/
// 650. 2 Keys Keyboard
// https://leetcode.com/problems/2-keys-keyboard/
// 1998. GCD Sort of an Array
// https://leetcode.com/problems/gcd-sort-of-an-array/
vector<int> generatePrimes(int n) {
auto is_prime = sieve(n);
vector<int> primes;
for (int i = 2; i <= n; i++)
if (is_prime[i]) primes.push_back(i);
return primes;
}
// Segmented Sieve for large ranges
// Related LeetCode Problems:
// 204. Count Primes
// https://leetcode.com/problems/count-primes/
// 762. Prime Number of Set Bits in Binary Representation
// https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/
// 952. Largest Component Size by Common Factor
// https://leetcode.com/problems/largest-component-size-by-common-factor/
// 2601. Prime Subtraction Operation
// https://leetcode.com/problems/prime-subtraction-operation/
// 2614. Prime In Diagonal
// https://leetcode.com/problems/prime-in-diagonal/
vector<bool> segmentedSieve(long long L, long long R) {
long long lim = sqrt(R);
auto base_primes = generatePrimes(lim);
vector<bool> is_prime(R - L + 1, true);
for (int p : base_primes) {
long long start = max((long long)p * p, ((L + p - 1) / p) * p);
for (long long j = start; j <= R; j += p)
is_prime[j - L] = false;
}
if (L == 1) is_prime[0] = false;
return is_prime;
}
// Miller-Rabin Primality Test
// Related LeetCode Problems:
// 204. Count Primes
// https://leetcode.com/problems/count-primes/
// 762. Prime Number of Set Bits in Binary Representation
// https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/
// 2601. Prime Subtraction Operation
// https://leetcode.com/problems/prime-subtraction-operation/
// 2614. Prime In Diagonal
// https://leetcode.com/problems/prime-in-diagonal/
// 952. Largest Component Size by Common Factor
// https://leetcode.com/problems/largest-component-size-by-common-factor/
long long mulmod(long long a, long long b, long long mod) {
return (__int128)a * b % mod;
}
long long powmod(long long a, long long b, long long mod) {
long long res = 1;
while (b > 0) {
if (b & 1) res = mulmod(res, a, mod);
a = mulmod(a, a, mod);
b >>= 1;
}
return res;
}
bool millerRabin(long long n, long long a) {
if (n <= 1 || a >= n) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
long long d = n - 1;
int r = 0;
while (d % 2 == 0) {
d /= 2;
r++;
}
long long x = powmod(a, d, n);
if (x == 1 || x == n - 1) return true;
for (int i = 0; i < r - 1; i++) {
x = mulmod(x, x, n);
if (x == n - 1) return true;
}
return false;
}
bool isPrime(long long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
vector<long long> witnesses = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
for (long long a : witnesses) {
if (a >= n) break;
if (!millerRabin(n, a)) return false;
}
return true;
}
// Trial Division Factorization
// Related LeetCode Problems:
// 264. Ugly Number II
// https://leetcode.com/problems/ugly-number-ii/
// 263. Ugly Number
// https://leetcode.com/problems/ugly-number/
// 313. Super Ugly Number
// https://leetcode.com/problems/super-ugly-number/
// 650. 2 Keys Keyboard
// https://leetcode.com/problems/2-keys-keyboard/
// 952. Largest Component Size by Common Factor
// https://leetcode.com/problems/largest-component-size-by-common-factor/
// 1998. GCD Sort of an Array
// https://leetcode.com/problems/gcd-sort-of-an-array/
// 1819. Number of Different Subsequences GCDs
// https://leetcode.com/problems/number-of-different-subsequences-gcds/
// 1735. Count Ways to Make Array With Product
// https://leetcode.com/problems/count-ways-to-make-array-with-product/
vector<pair<long long, int>> factorize(long long n) {
vector<pair<long long, int>> factors;
for (long long i = 2; i * i <= n; i++) {
int cnt = 0;
while (n % i == 0) {
n /= i;
cnt++;
}
if (cnt > 0) factors.push_back({i, cnt});
}
if (n > 1) factors.push_back({n, 1});
return factors;
}
// Pollard's Rho Algorithm for large number factorization
// Related LeetCode Problems:
// 952. Largest Component Size by Common Factor
// https://leetcode.com/problems/largest-component-size-by-common-factor/
// 1998. GCD Sort of an Array
// https://leetcode.com/problems/gcd-sort-of-an-array/
// 1819. Number of Different Subsequences GCDs
// https://leetcode.com/problems/number-of-different-subsequences-gcds/
// 1735. Count Ways to Make Array With Product
// https://leetcode.com/problems/count-ways-to-make-array-with-product/
// 650. 2 Keys Keyboard
// https://leetcode.com/problems/2-keys-keyboard/
long long pollardRho(long long n) {
if (n % 2 == 0) return 2;
auto f = [&](long long x) { return (mulmod(x, x, n) + 1) % n; };
long long x = 2, y = 2, d = 1;
while (d == 1) {
x = f(x);
y = f(f(y));
d = __gcd(abs(x - y), n);
}
return d;
}
// Count divisors
// Related LeetCode Problems:
// 1952. Three Divisors
// https://leetcode.com/problems/three-divisors/
// 1819. Number of Different Subsequences GCDs
// https://leetcode.com/problems/number-of-different-subsequences-gcds/
// 2427. Number of Common Factors
// https://leetcode.com/problems/number-of-common-factors/
// 952. Largest Component Size by Common Factor
// https://leetcode.com/problems/largest-component-size-by-common-factor/
// 1998. GCD Sort of an Array
// https://leetcode.com/problems/gcd-sort-of-an-array/
// 650. 2 Keys Keyboard
// https://leetcode.com/problems/2-keys-keyboard/
int countDivisors(long long n) {
auto factors = factorize(n);
int count = 1;
for (auto [p, e] : factors)
count *= (e + 1);
return count;
}
// Sum of divisors
// Related LeetCode Problems:
// 1952. Three Divisors
// https://leetcode.com/problems/three-divisors/
// 1819. Number of Different Subsequences GCDs
// https://leetcode.com/problems/number-of-different-subsequences-gcds/
// 2427. Number of Common Factors
// https://leetcode.com/problems/number-of-common-factors/
// 952. Largest Component Size by Common Factor
// https://leetcode.com/problems/largest-component-size-by-common-factor/
// 1998. GCD Sort of an Array
// https://leetcode.com/problems/gcd-sort-of-an-array/
long long sumDivisors(long long n) {
auto factors = factorize(n);
long long sum = 1;
for (auto [p, e] : factors) {
long long term = (powmod(p, e + 1, LLONG_MAX) - 1) / (p - 1);
sum *= term;
}
return sum;
}
// Generate all divisors
// Related LeetCode Problems:
// 1952. Three Divisors
// https://leetcode.com/problems/three-divisors/
// 1819. Number of Different Subsequences GCDs
// https://leetcode.com/problems/number-of-different-subsequences-gcds/
// 2427. Number of Common Factors
// https://leetcode.com/problems/number-of-common-factors/
// 952. Largest Component Size by Common Factor
// https://leetcode.com/problems/largest-component-size-by-common-factor/
// 1998. GCD Sort of an Array
// https://leetcode.com/problems/gcd-sort-of-an-array/
// 1390. Four Divisors
// https://leetcode.com/problems/four-divisors/
// 1492. The kth Factor of n
// https://leetcode.com/problems/the-kth-factor-of-n/
vector<long long> getDivisors(long long n) {
vector<long long> divisors;
for (long long i = 1; i * i <= n; i++) {
if (n % i == 0) {
divisors.push_back(i);
if (i != n / i) divisors.push_back(n / i);
}
}
sort(divisors.begin(), divisors.end());
return divisors;
}
// Smallest Prime Factor for range [1,n]
// Related LeetCode Problems:
// 952. Largest Component Size by Common Factor
// https://leetcode.com/problems/largest-component-size-by-common-factor/
// 1998. GCD Sort of an Array
// https://leetcode.com/problems/gcd-sort-of-an-array/
// 1819. Number of Different Subsequences GCDs
// https://leetcode.com/problems/number-of-different-subsequences-gcds/
// 1735. Count Ways to Make Array With Product
// https://leetcode.com/problems/count-ways-to-make-array-with-product/
// 650. 2 Keys Keyboard
// https://leetcode.com/problems/2-keys-keyboard/
// 264. Ugly Number II
// https://leetcode.com/problems/ugly-number-ii/
// 313. Super Ugly Number
// https://leetcode.com/problems/super-ugly-number/
vector<int> spf(int n) {
vector<int> smallest(n + 1);
iota(smallest.begin(), smallest.end(), 0);
for (int i = 2; i * i <= n; i++) {
if (smallest[i] == i) {
for (int j = i * i; j <= n; j += i)
if (smallest[j] == j) smallest[j] = i;
}
}
return smallest;
}