๋ฐฑ์ค (boj) ํ์ด์ฌ - 1182 ๋ฒ : ๋ถ๋ถ์์ด์ ํฉ
๋ฌธ์ ๋งํฌ :https://www.acmicpc.net/problem/1182
1182๋ฒ: ๋ถ๋ถ์์ด์ ํฉ
์ฒซ์งธ ์ค์ ์ ์์ ๊ฐ์๋ฅผ ๋ํ๋ด๋ N๊ณผ ์ ์ S๊ฐ ์ฃผ์ด์ง๋ค. (1 ≤ N ≤ 20, |S| ≤ 1,000,000) ๋์งธ ์ค์ N๊ฐ์ ์ ์๊ฐ ๋น ์นธ์ ์ฌ์ด์ ๋๊ณ ์ฃผ์ด์ง๋ค. ์ฃผ์ด์ง๋ ์ ์์ ์ ๋๊ฐ์ 100,000์ ๋์ง ์๋๋ค.
www.acmicpc.net
itertools์ combination์ ์ฌ์ฉํ์ฌ ํ์๋ค. (์๋์ผ๋ก n๊ฐ์ค์ r๊ฐ๋ฅผ ๋ฝ๋ ์กฐํฉ(combination)์ ๋ฐํํด์ฃผ๋... )
https://infinitt.tistory.com/114
์์ด๊ณผ ์กฐํฉ ( n! , nPr , nCr ) - (itertools) combinations, permutations + ์ค๋ณต์์ด, ์ค๋ณต์กฐํฉ
*n! (Factorial, ํฉํ ๋ฆฌ์ผ) 1๋ถํฐ ์์ฐ์ n๊น์ง์ ๋ชจ๋ ์๋ฅผ ์ฐจ๋ก๋๋ก ๊ณฑํ๋๊ฒ. (์ฃผ์ 0! = 1) math๋ชจ๋ ํ์ด์ฌ์์ ํฉํ ๋ฆฌ์ผ์ ๊ตฌํ ๋๋ math๋ชจ๋์ ์ด์ฉํ๋ฉด ๋ฉ๋๋ค. import math math.factorial(5) *nPr (perm..
infinitt.tistory.com
๋ฌธ์ ์์ N๊ณผ S๋ฅผ ์ ๋ ฅ์ผ๋ก ์ค๋ค.
N๊ฐ์ ์ ์๋ก ์ด๋ฃจ์ด์ง ์์ด์ค์, ?๊ฐ๋ฅผ ๋ฝ์ ๊ทธ ํฉ์ด S์ ๊ฐ์์ผ ํ๋ฏ๋ก ์กฐํฉ์ ์ฌ์ฉํ๋ฉด ๋๋ค.
from itertools import combinations
N, S = map(int,input().split())
arr = list(map(int,input().split()))
cnt = 0
for i in range(1,len(arr)+1):
answer = list(combinations(arr,i))
for idx in range(len(answer)):
sum_partier = sum(answer[idx])
if sum_partier == S :
cnt += 1
print(cnt)