๐Ÿง ๋ฐฑ์ค€ 2609 (์ตœ๋Œ€๊ณต์•ฝ์ˆ˜, ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜)

๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ 

๐Ÿค” ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜, ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๋Š” math ๋‚ด์žฅ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์žˆ๋Š” gcd, lcm ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์ž!  
from math import gcd, lcm


๐Ÿคซ  solution_2609

from math import gcd, lcm
x, y = map(int, input().split())
print(gcd(x,y), lcm(x,y), sep = "\n")

 

 

๐Ÿง ๋ฐฑ์ค€ 1676 (factorial)

๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ 

๐Ÿค” ํŒฉํ† ๋ฆฌ์–ผ์€ math ๋‚ด์žฅ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์žˆ๋Š” factorial ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์ž!
from math import factorial

๐Ÿค” ๋ฌธ์ž์—ด์ด๋‚˜ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•œ ๋’ค์—์„œ์˜ ์ ‘๊ทผ์€ reversed๋ฅผ ์• ์šฉํ•˜์ž!
for i in reversed(ans):


๐Ÿคซ  solution_1676

from math import factorial
ans = str(factorial(int(input())))
cnt = 0
for i in reversed(ans):
    if i == '0':
        cnt += 1
    else:
        break
print(cnt)

 

 

๐Ÿง ๋ฐฑ์ค€ 2004 (combination_์กฐํ•ฉ)

๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ 

๐Ÿค” ์กฐํ•ฉ๋ก ์˜ ์›๋ฆฌ, ์ฆ‰ ๋ณธ์งˆ์˜ ํŒŒ์•…์ด ์šฐ์„ ์ด๋‹ค!
0์˜ ๊ฐœ์ˆ˜ = 
min{
(M!์ด ๊ฐ€์ง€๋Š” 2์˜ ๊ฐœ์ˆ˜ - N!์ด ๊ฐ€์ง€๋Š” 2์˜ ๊ฐœ์ˆ˜ - (M-N)!์ด ๊ฐ–๋Š” 2์˜ ๊ฐœ์ˆ˜) 
                             /
(M!์ด ๊ฐ€์ง€๋Š” 5์˜ ๊ฐœ์ˆ˜ - N!์ด ๊ฐ€์ง€๋Š” 5์˜ ๊ฐœ์ˆ˜ - (M-N)!์ด ๊ฐ–๋Š” 5์˜ ๊ฐœ์ˆ˜)
}




๐Ÿคซ  solution_2004 (์‹œ๊ฐ„์ดˆ๊ณผ)

from math import factorial

def combination(n, m):
    return factorial(n) // (factorial(m) * factorial(n-m))

n, m = map(int, input().split())
num = str(combination(n,m))

cnt = 0
for i in reversed(num):
    if i == '0':
        cnt += 1
    else:
        break
print(cnt)



๐Ÿคซ  solution_2004

M, N = map(int, input().split())
def countNum(N, num):
    cnt = 0
    divNum = num
    while(N >= divNum):
        cnt = cnt + (N // divNum)
        divNum *= num
    return cnt

# 0์˜ ๊ฐœ์ˆ˜ = min(M!์ด ๊ฐ€์ง€๋Š” 2์˜ ๊ฐœ์ˆ˜ - N!์ด ๊ฐ€์ง€๋Š” 2์˜ ๊ฐœ์ˆ˜ - (M-N)!์ด ๊ฐ–๋Š” 2์˜ ๊ฐœ์ˆ˜  /  M!์ด ๊ฐ€์ง€๋Š” 5์˜ ๊ฐœ์ˆ˜ - N!์ด ๊ฐ€์ง€๋Š” 5์˜ ๊ฐœ์ˆ˜ - (M-N)!์ด ๊ฐ–๋Š” 5์˜ ๊ฐœ์ˆ˜)
print(min(countNum(M, 5) - countNum(N, 5) - countNum(M-N, 5), countNum(M, 2) - countNum(N, 2) - countNum(M-N, 2)))

 

 

๐Ÿง ๋ฐฑ์ค€ 11051 (์ดํ•ญ์ •๋ฆฌ)

๐Ÿคซ  solution_2004

from math import factorial
n, k = map(int, input().split())
print((factorial(n) // (factorial(k) * factorial(n - k))) % 10007)

์กฐํ•ฉ์˜ ๊ฒฝ์šฐ n! / k! (n-k)! ์œผ๋กœ ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•˜๊ธฐ์— ์œ„์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

๐Ÿง ๋ฐฑ์ค€ 2981 (combination_์กฐํ•ฉ)  Gold IV


๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ _ ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•

 

๐Ÿค” Algorithm
๋‚˜๋ˆ—์…ˆ์—์„œ ๋ชซ๊ณผ ๋‚˜๋จธ์ง€๊ฐ€ ์–ด๋–ป๊ฒŒ ๋‚˜์˜ค๋Š”์ง€,
 ๋ณธ์งˆ์˜ ํŒŒ์•…์ด ์šฐ์„ ์ด๋‹ค!

์œ„์˜ ์˜ˆ์ œ์ž…๋ ฅ 2์˜ ์ผ๋ถ€๋ฅผ ์˜ˆ๋กœ ๋“ค์–ด ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณผ์ •์„ ์„ค๋ช…ํ•ด ๋ณด๊ฒ ๋‹ค.
# 17 = 5*(3) + '2'
# 83 = 27*(3) + '2'

# 3 = (number - 'rest')*๋ชซ
# ์ฆ‰, ์ˆซ์ž 2๊ฐœ๋ฅผ ๋นผ์ฃผ๋ฉด? => ()๊ด„ํ˜ธ ๊ฐ’์„ ์ธ์ˆ˜๋กœ ๊ฐ–๋Š” ์ˆซ์ž๊ฐ€ ๋‚˜์˜ด
# ex) 83-17 = 66์˜ ๊ณต์•ฝ์ˆ˜ = 2 3 6 11 22 33 66
# ex) 17- 5 = 12์˜ ๊ณต์•ฝ์ˆ˜ = 2 3 4 6 12
# ex) 17-14 = 3์˜ ๊ณต์•ฝ์ˆ˜ = 3
# ์ฆ‰, ์ตœ์ข…์ ์œผ๋กœ ์œ„์˜ ๋ฆฌ์ŠคํŠธ๋“ค์˜ ๊ณต์•ฝ์ˆ˜๋Š” 3์ด๋ฏ€๋กœ 3์„ ์ถœ๋ ฅํ•˜๋ฉด ๋จ.




๐Ÿค”Algorithm ๊ณผ์ •
์œ„์—์„œ ์„ค๋ช…ํ•œ ๋ฐ”์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ•  ๊ฒƒ์ด๋‹ค.
1. ์ž…๋ ฅ๋œ num๊ฐ„์˜ ์ฐจ์ด๋ฅผ ์ €์žฅํ•˜๋Š” num_interval ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๊ณ  ์ €์žฅํ•œ๋‹ค.
2. ans๋Š” num_interval์˜ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋ฅผ ๊ตฌํ•˜์—ฌ ๋„ฃ์–ด์ฃผ๊ธฐ ์œ„ํ•œ ๋ฆฌ์ŠคํŠธ์ด๋‹ค.
3. gcd(*num_interval)์„ ํ†ตํ•ด ๋ชจ๋“  num_interval์˜ ์›์†Œ๋“ค์„ ๋น„๊ต, ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋ฅผ ๊ตฌํ•ด์ค€๋‹ค.
4. ans์— ์ €์žฅ๋œ 1์„ ์ œ์™ธํ•œ ์•ฝ์ˆ˜๊ฐ€ ๋‹ต์ด ๋˜๋ฏ€๋กœ ์•ฝ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ divisorํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•ด์ค€๋‹ค.
5. divisorํ•จ์ˆ˜์—์„œ๋Š” 2๋ถ€ํ„ฐ n๊นŒ์ง€๋ฅผ ๋Œ๋ฉด์„œ ๋‚˜๋จธ์ง€๊ฐ€ 0์ด ๋  ๋•Œ, i๊ฐ’์„ appendํ•ด์ค€๋‹ค.
6. ์ด๋ ‡๊ฒŒ ๋ชจ์ธ answer ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋ฐ”๋กœ 1์„ ์ œ์™ธํ•œ ์•ฝ์ˆ˜๊ฐ€ ์ €์žฅ๋˜์žˆ๋Š” ๋ฆฌ์ŠคํŠธ์ด๋‹ค.
7. ์ด ํ•จ์ˆ˜์— ans์— ์ €์žฅ๋œ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜์˜ ์›์†Œ๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ  ํ•จ์ˆ˜๋กœ ๋„์ถœ๋œ ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋ฅผ ๋ฝ‘์•„์ค€๋‹ค.





๐Ÿคซ  solution_2981

from math import gcd

N = int(input())
num = [int(input()) for _ in range(N)]

num_interval = []
ans = []
for i in range(1, N):
    num_interval.append(abs(num[i] - num[i-1]))
ans.append(gcd(*num_interval))

def divisor(n):
    answer = []
    for i in range(2, n+1):
        if n%i == 0:
            answer.append(i)
    return answer

print(*divisor(*ans))

 

 

๐Ÿง ๋ฐฑ์ค€ 2877 (2์ง„์ˆ˜) Gold V

๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ _  format() 

>>> format(42, 'b') # 2์ง„์ˆ˜
'101010'
>>> format(42, 'o') # 8์ง„์ˆ˜
'52'
>>> format(42, 'x') # 16์ง„์ˆ˜
'2a



๐Ÿค” Algorithm 



๐Ÿคซ  solution_ 2877

n = format(int(input()) + 1, 'b') # k+1 ์ˆซ์ž๋ฅผ 2์ง„์ˆ˜('b')๋กœ ๋ณ€ํ™˜
print(n[1:].replace('0', '4').replace('1', '7'))

 

 

 

 

 

๐Ÿง ๋ฐฑ์ค€ 1158 (์š”์„ธํ‘ธ์Šค ๋ฌธ์ œ)

๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ _  deque 

๐Ÿค” collections์˜ deque๋ฅผ ์ด์šฉํ•˜์ž!  
from collections import deque
dq = deque()

# dq์˜ ๋’ค(์˜ค๋ฅธ์ชฝ)์— ๊ฐ’ ์ถ”๊ฐ€, ์‚ญ์ œ
dq.append(num)
dq.pop()

# dq์˜ ์•ž(์™ผ์ชฝ)์— ๊ฐ’ ์ถ”๊ฐ€
dq.appendleft(num)
dq.popleft()

# dq์•ˆ์˜ ๊ฐ’๋“ค ํšŒ์ „ (์›ํ˜• ํ๋ฅผ ์ด์šฉ)
dq.rotate(์–‘์ˆ˜)  = ์–‘์ˆ˜ ์นธ์”ฉ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฐ€๋ฆฐ๋‹ค.
dq.rotate(์Œ์ˆ˜)  = ์Œ์ˆ˜ ์นธ์”ฉ ์™ผ์ชฝ์œผ๋กœ ๋ฐ€๋ฆฐ๋‹ค.

print(dq)  # deque([1, 2, 3, 4, 5])

dq.rotate(1)
print(dq)  # deque([5, 1, 2, 3, 4])

dq.rotate(-1)
print(dq)  # deque([1, 2, 3, 4, 5])

dq.rotate(-1)
print(dq)  # deque([2, 3, 4, 5, 1])

 


๐Ÿคซ  solution_1158

from collections import deque

n, k = map(int, input().split())
dq = deque([i for i in range(1, n+1)])

ans = []
while dq:
    dq.rotate(-k+1)
    ans.append(dq.popleft())  # popleft: ๋ฑ์˜ ์•ž(์™ผ์ชฝ)๊ฐ’ ์‚ญ์ œํ›„ ๋ฐ˜ํ™˜

print("<" + ", ".join(map(str, ans)) + ">")

 

 

+ Recent posts