-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgcd_lcm.cpp
More file actions
239 lines (221 loc) · 8.81 KB
/
gcd_lcm.cpp
File metadata and controls
239 lines (221 loc) · 8.81 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
/*
GCD & LCM (Greatest Common Divisor & Least Common Multiple)
Mathematical Foundation:
Euclidean Algorithm: gcd(a,b) = gcd(b, a%b), gcd(a,0) = a
LCM: lcm(a,b) = (a*b) / gcd(a,b)
Extended Euclidean: ax + by = gcd(a,b)
Time: O(log(min(a,b)))
*/
#include <bits/stdc++.h>
using namespace std;
// GCD - Euclidean Algorithm
// Related LeetCode Problems:
// 1071. Greatest Common Divisor of Strings
// https://leetcode.com/problems/greatest-common-divisor-of-strings/
// 914. X of a Kind in a Deck of Cards
// https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/
// 1979. Find Greatest Common Divisor of Array
// https://leetcode.com/problems/find-greatest-common-divisor-of-array/
// 1952. Three Divisors
// https://leetcode.com/problems/three-divisors/
// 1370. Increasing Decreasing String
// https://leetcode.com/problems/increasing-decreasing-string/
long long gcd(long long a, long long b) {
return b ? gcd(b, a % b) : a;
}
// LCM using GCD
// Related LeetCode Problems:
// 2413. Smallest Even Multiple
// https://leetcode.com/problems/smallest-even-multiple/
// 1979. Find Greatest Common Divisor of Array
// https://leetcode.com/problems/find-greatest-common-divisor-of-array/
// 1071. Greatest Common Divisor of Strings
// https://leetcode.com/problems/greatest-common-divisor-of-strings/
// 914. X of a Kind in a Deck of Cards
// https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/
// 2001. Number of Pairs of Interchangeable Rectangles
// https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/
long long lcm(long long a, long long b) {
return a / gcd(a, b) * b;
}
// Extended Euclidean Algorithm
// Returns gcd(a,b) and finds x,y such that ax + by = gcd(a,b)
// Related LeetCode Problems:
// 365. Water and Jug Problem
// https://leetcode.com/problems/water-and-jug-problem/
// 1979. Find Greatest Common Divisor of Array
// https://leetcode.com/problems/find-greatest-common-divisor-of-array/
// 1071. Greatest Common Divisor of Strings
// https://leetcode.com/problems/greatest-common-divisor-of-strings/
// 2607. Make K-Subarray Sums Equal
// https://leetcode.com/problems/make-k-subarray-sums-equal/
long long extgcd(long long a, long long b, long long& x, long long& y) {
if (b == 0) {
x = 1; y = 0;
return a;
}
long long x1, y1;
long long g = extgcd(b, a % b, x1, y1);
x = y1;
y = x1 - (a / b) * y1;
return g;
}
// Modular Inverse using Extended Euclidean
// Related LeetCode Problems:
// 1979. Find Greatest Common Divisor of Array
// https://leetcode.com/problems/find-greatest-common-divisor-of-array/
// 365. Water and Jug Problem
// https://leetcode.com/problems/water-and-jug-problem/
// 2607. Make K-Subarray Sums Equal
// https://leetcode.com/problems/make-k-subarray-sums-equal/
// 1735. Count Ways to Make Array With Product
// https://leetcode.com/problems/count-ways-to-make-array-with-product/
// 1830. Minimum Number of Operations to Make String Sorted
// https://leetcode.com/problems/minimum-number-of-operations-to-make-string-sorted/
long long modinv(long long a, long long m) {
long long x, y;
long long g = extgcd(a, m, x, y);
return g == 1 ? (x % m + m) % m : -1;
}
// GCD of Array
// LeetCode: 1979. Find Greatest Common Divisor of Array
// https://leetcode.com/problems/find-greatest-common-divisor-of-array/
// Related:
// 1071. Greatest Common Divisor of Strings
// https://leetcode.com/problems/greatest-common-divisor-of-strings/
// 914. X of a Kind in a Deck of Cards
// https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/
// 1952. Three Divisors
// https://leetcode.com/problems/three-divisors/
// 1819. Number of Different Subsequences GCDs
// https://leetcode.com/problems/number-of-different-subsequences-gcds/
long long gcdArray(vector<long long>& arr) {
long long result = arr[0];
for (int i = 1; i < arr.size(); i++)
result = gcd(result, arr[i]);
return result;
}
// LCM of Array
// Related LeetCode Problems:
// 2413. Smallest Even Multiple
// https://leetcode.com/problems/smallest-even-multiple/
// 1979. Find Greatest Common Divisor of Array
// https://leetcode.com/problems/find-greatest-common-divisor-of-array/
// 2001. Number of Pairs of Interchangeable Rectangles
// https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/
// 1071. Greatest Common Divisor of Strings
// https://leetcode.com/problems/greatest-common-divisor-of-strings/
// 914. X of a Kind in a Deck of Cards
// https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/
long long lcmArray(vector<long long>& arr) {
long long result = arr[0];
for (int i = 1; i < arr.size(); i++)
result = lcm(result, arr[i]);
return result;
}
// Binary GCD (Stein's Algorithm) - for very large numbers
// Related LeetCode Problems:
// 1979. Find Greatest Common Divisor of Array
// https://leetcode.com/problems/find-greatest-common-divisor-of-array/
// 1071. Greatest Common Divisor of Strings
// https://leetcode.com/problems/greatest-common-divisor-of-strings/
// 914. X of a Kind in a Deck of Cards
// https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/
// 365. Water and Jug Problem
// https://leetcode.com/problems/water-and-jug-problem/
// 1819. Number of Different Subsequences GCDs
// https://leetcode.com/problems/number-of-different-subsequences-gcds/
long long binaryGCD(long long a, long long b) {
if (a == 0) return b;
if (b == 0) return a;
int k = 0;
while (((a | b) & 1) == 0) {
a >>= 1; b >>= 1; k++;
}
while ((a & 1) == 0) a >>= 1;
do {
while ((b & 1) == 0) b >>= 1;
if (a > b) swap(a, b);
b -= a;
} while (b);
return a << k;
}
// Coprime Check
// Related LeetCode Problems:
// 1979. Find Greatest Common Divisor of Array
// https://leetcode.com/problems/find-greatest-common-divisor-of-array/
// 1071. Greatest Common Divisor of Strings
// https://leetcode.com/problems/greatest-common-divisor-of-strings/
// 914. X of a Kind in a Deck of Cards
// https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/
// 1952. Three Divisors
// https://leetcode.com/problems/three-divisors/
// 1819. Number of Different Subsequences GCDs
// https://leetcode.com/problems/number-of-different-subsequences-gcds/
bool areCoprime(long long a, long long b) {
return gcd(a, b) == 1;
}
// Euler's Totient Function φ(n)
// 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/
// 1979. Find Greatest Common Divisor of Array
// https://leetcode.com/problems/find-greatest-common-divisor-of-array/
// 1735. Count Ways to Make Array With Product
// https://leetcode.com/problems/count-ways-to-make-array-with-product/
// 1830. Minimum Number of Operations to Make String Sorted
// https://leetcode.com/problems/minimum-number-of-operations-to-make-string-sorted/
long long phi(long long n) {
long long result = n;
for (long long p = 2; p * p <= n; p++) {
if (n % p == 0) {
while (n % p == 0) n /= p;
result -= result / p;
}
}
if (n > 1) result -= result / n;
return result;
}
// Linear Diophantine Equation: ax + by = c
// Returns true if solution exists
// Related LeetCode Problems:
// 365. Water and Jug Problem
// https://leetcode.com/problems/water-and-jug-problem/
// 2607. Make K-Subarray Sums Equal
// https://leetcode.com/problems/make-k-subarray-sums-equal/
// 1735. Count Ways to Make Array With Product
// https://leetcode.com/problems/count-ways-to-make-array-with-product/
// 1830. Minimum Number of Operations to Make String Sorted
// https://leetcode.com/problems/minimum-number-of-operations-to-make-string-sorted/
bool linearDiophantine(long long a, long long b, long long c, long long& x, long long& y) {
long long g = extgcd(abs(a), abs(b), x, y);
if (c % g != 0) return false;
x *= c / g;
y *= c / g;
if (a < 0) x = -x;
if (b < 0) y = -y;
return true;
}
// Chinese Remainder Theorem
// Solve: x ≡ r[i] (mod m[i]) for all i
// Related LeetCode Problems:
// 1735. Count Ways to Make Array With Product
// https://leetcode.com/problems/count-ways-to-make-array-with-product/
// 1830. Minimum Number of Operations to Make String Sorted
// https://leetcode.com/problems/minimum-number-of-operations-to-make-string-sorted/
// 2607. Make K-Subarray Sums Equal
// https://leetcode.com/problems/make-k-subarray-sums-equal/
// 365. Water and Jug Problem
// https://leetcode.com/problems/water-and-jug-problem/
long long crt(vector<long long>& r, vector<long long>& m) {
long long M = 1, result = 0;
for (long long mod : m) M *= mod;
for (int i = 0; i < m.size(); i++) {
long long Mi = M / m[i];
long long yi = modinv(Mi, m[i]);
result = (result + r[i] * Mi % M * yi % M) % M;
}
return (result + M) % M;
}