You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 유클리드 호제법 O(log(min(a,b)))intgcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
intlcm(int a, int b) {
return a / gcd(a, b) * b; // 오버플로우 방지
}
// C++17 이상: __gcd(a, b) 또는 gcd(a, b)
importmathmath.gcd(a, b)
math.lcm(a, b) # Python 3.9+
2. 소수 판별 & 에라토스테네스의 체
// 단일 소수 판별 O(√N)boolisPrime(int n) {
if (n < 2) returnfalse;
for (int i = 2; i * i <= n; i++)
if (n % i == 0) returnfalse;
returntrue;
}
// 에라토스테네스의 체 O(NloglogN)
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;
}