1. Problem
2. My Solution
X = int(sys.stdin.readline())
poles = [1,2,4,8,16,32,64]
for bit in range(1, 2**7):
sum = 0
count = 0
for i in range(7):
if bit & (1 << i):
sum += poles[i]
count += 1
if sum == X:
print(count)
break
3. Learned
1. Problem
2. My Solution
import sys
n,k = map(int,sys.stdin.readline().rstrip().split())
coins = []
target = k
count = 0
for _ in range(n):
coins.append(int(sys.stdin.readline()))
coins.sort(reverse=True)
for i in coins:
if target == 0:
break
elif i == 1:
count += target
break
elif target / i >= 1:
count += target // i
target = target % i
print(count)
1. Problem
2. My Solution
import sys
def fib(n):
if n == 0 :
result[0] += 1
return 0
elif n == 1:
result[1] += 1
return 1
elif dp[n] != -1:
return dp[n]
else:
return fib(n-1) + fib(n-2)
test_n = int(sys.stdin.readline())
for _ in range(test_n):
n = int(sys.stdin.readline())
result = [0,0]
dp = [-1] * 41
dp[0] = 0
dp[1] = 1
fib(n)
print(*result)
# dp[i][j] -> i = N, j = 0, 1
import sys
test_n = int(sys.stdin.readline())
for _ in range(test_n):
n = int(sys.stdin.readline())
dp = [[0,0] for _ in range(41)]
dp[0] = [1,0]
dp[1] = [0,1]
for i in range(2,41):
dp[i][0] = dp[i-1][0] + dp[i-2][0]
dp[i][1] = dp[i-1][1] + dp[i-2][1]
print(*dp[n])
3. Learned
1. Problem
2. My Solution
import sys
from collections import deque
def turn(num, direction):
visited[num] = True
left, right = gear[num][6], gear[num][2]
# 오른쪽 회전
if direction == 1:
gear[num].appendleft(gear[num].pop())
# 왼쪽 회전
else:
gear[num].append(gear[num].popleft())
# 현재 톱니바퀴의 왼쪽 극성과 왼쪽 톱니바퀴의 오른쪽 극성이 다르다면
if num-1 >= 0 and visited[num-1] == False and left != gear[num-1][2]:
turn(num-1, direction * -1)
# 현재 톱니바퀴의 오른쪽 극성과 오른쪽톱니바퀴의 왼쪽 극성이 다르다면
if num+1 < 4 and visited[num+1] == False and right != gear[num+1][6]:
turn(num+1, direction * -1)
gear = []
score = [1,2,4,8]
answer = 0
for i in range(4):
gear.append(deque(map(int,sys.stdin.readline().rstrip())))
k = int(sys.stdin.readline())
for _ in range(k):
i, j = map(int,sys.stdin.readline().rstrip().split())
visited = [False] * 4
turn(i-1, j)
for i in range(4):
answer += gear[i][0] * score[i]
print(answer)