반응형
https://www.acmicpc.net/problem/1010
조합 라이브러리를 사용하여 풀었더니 시간초과가 발생.
from itertools import combinations
t = int(input())
for _ in range(t):
# bCa 로 조합을 사용한다. b개에서 a개를 뽑는다
a, b = map(int, input().split())
arr = []
for i in range(1, b + 1):
arr.append(i)
print(len(list(combinations(arr, a))))
아래와 같은 풀이를 통해 시간초과해결
조합을 직접 구현하였음.
t = int(input())
for _ in range(t):
# bCa 로 조합을 사용한다. b개에서 a개를 뽑는다
x = 1
y = 1
a, b = map(int, input().split())
if a == 1 or b - a == 1:
print(b)
else:
for i in range(a):
x *= b
b -= 1
for j in range(a, 1, -1):
y *= j
print(round(x / y))
반응형
'알고리즘 풀이' 카테고리의 다른 글
[백준/파이썬] 2869번 : 달팽이는 올라가고 싶다 (0) | 2023.01.09 |
---|---|
[백준/파이썬] 1978번: 소수 찾기 (0) | 2023.01.06 |
[백준 / Python] 1654번: 랜선 자르기 - jini (0) | 2022.07.11 |
[백준 / Python] 10816번: 숫자 카드 2 - jini (0) | 2022.07.11 |
[백준 / Python] 1920번: 수 찾기- jini (0) | 2022.07.10 |
댓글