def gcd(a,b):
return gcd(b, a%b) if a%b !=0 else b
def gcd2(a,b):
while a % b != 0: a, b = b, a%b
return b
import math
math.gcd(a,b)
import math
def lcm(a, b):
return a*b/math.gcd(a,b)
def isPrime(N):
for i in range(2, N):
if N%i == 0: return False
if i*i > N: break
return True
def prime_factorization(N):
p, fac = 2, []
while p**2 <= N:
if N%p == 0:
N //= p
fac.append(p)
else:
p += 1
if N > 1: fac.append(N) # 너무 커서 탈출한경우 이거 자체가 prime
return fac
def era_prime(N):
ck, ret = [0 for _ in range(N+1)], []
for i in range(2, N):
if ck[i] == 0 : ret.append(i)
else: continue
for j in range(i**2, N, i): ck[j] = 1
return ret
def prime_factorization_2(N , p):
fac = []
for i in p:
if N == 1 or N > i*i : break
while N%i == 0:
fac.append(i)
N //= i
return fac
def era_factor_count(N):
A = [0 for _ in range(N+1)]
for i in range(2, N):
for j in range(i, N, i):
A[j] += 1
return A
def era_factor_sum(N):
A = [0 for _ in range(N+1)]
for i in range(2, N):
for j in range(i, N, i):
A[j] += i
return A
def era_factorization(N):
A = [0 for _ in range(N+1)]
for i in range(2, N):
if A[i]: continue
for j in range(i, N, i):
A[j] = i
return A
# 소인수 분해
A = era_factorization(100)
N = 84
while A[N] != 0:
print(A[N])
N //= A[N]
$a ^ {11} = a ^ 1 * a ^ 2 * a ^ 8$ 을 이용
def pow_adv(a, b, mod):
ret = 1
while b > 0:
if b % 2: ret = ret*a%mod
a, b = a*a%mod, b//2
return ret
pow(a, b, 1000000007)
pow 가 압도적으로 빠름 !