ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

1208๋ฒˆ: ๋ถ€๋ถ„์ˆ˜์—ด์˜ ํ•ฉ 2

์ฒซ์งธ ์ค„์— ์ •์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” N๊ณผ ์ •์ˆ˜ S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 40, |S| ≤ 1,000,000) ๋‘˜์งธ ์ค„์— N๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜์˜ ์ ˆ๋Œ“๊ฐ’์€ 100,000์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ์•ž์„œ ๋‹ค๋ฃฌ ๋ถ€๋ถ„์ˆ˜์—ด์˜ ํ•ฉ ๋ฌธ์ œ์—์„œ N์˜ ๋ฒ”์œ„๊ฐ€ 40์œผ๋กœ ์ฆ๊ฐ€ํ•œ ๋ฌธ์ œ์ด๋‹ค. ๋”ฐ๋ผ์„œ ๊ธฐ์กด์˜ ํ’€์ด๋Š” ์ด ๋ฌธ์ œ์— ์ ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด ๋ฌธ์ œ๋Š” ํ•œ ๋ฒˆ์— N์— ๋Œ€ํ•œ ๋ถ€๋ถ„์ˆ˜์—ด์„ ๋ชจ๋‘ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ์ˆ˜์—ด์˜ ์ขŒ/์šฐ์ธก์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋ถ€๋ถ„์ ์œผ๋กœ ๊ตฌํ•œ ํ›„์— ์ขŒ์ธก์˜ ์ž‘์€ ๊ฐ’ + ์šฐ์ธก์˜ ํฐ ๊ฐ’๋ถ€ํ„ฐ ์ ์ฐจ์ ์œผ๋กœ ๊ณ„์‚ฐํ•ด S์™€ ๋งŒ์กฑํ•˜๋Š”์ง€ ์ฐพ๊ฒŒ ๋˜๋ฉด ์‹œ๊ฐ„ ์•ˆ์— ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

def dfs(idx, end_idx, subtotal, ret):
    if idx == end_idx:
        ret.append(subtotal)
        return

    dfs(idx + 1, end_idx, subtotal, ret)
    dfs(idx + 1, end_idx, subtotal + nums[idx], ret)

    return sorted(ret), len(ret)


if __name__ == "__main__":
    answer = 0
    n, s = map(int, stdin.readline().split())
    nums = list(map(int, stdin.readline().split()))

    left, len_l = dfs(0, n // 2, 0, [])
    right, len_r = dfs(n // 2, n, 0, [])
    
    # ์•„๋ž˜ ๋ถ€๋ถ„์€ ์ƒ๋žต 

 ๋ถ€๋ถ„์ˆ˜์—ด์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ์ฒ˜์Œ์—๋Š” ์œ„์™€ ๊ฐ™์ด DFS๋กœ ์ž‘์„ฑํ•˜์—ฌ ๋ถ€๋ถ„์ˆ˜์—ด์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•˜์˜€๋‹ค. ์งˆ๋ฌธ ๊ฒŒ์‹œํŒ์— ๋ณด๋ฉด N์ด 40์ธ ๊ฒฝ์šฐ์˜ ์˜ˆ์‹œ๊ฐ€ ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. 

 

 ์œ„์˜ ์˜ˆ์‹œ๋ฅผ ํ•ด๋‹น ์ฝ”๋“œ๋กœ ๋Œ๋ ค๋ณด๋ฉด ์•„๋ฌด๋ฆฌ ๊ธฐ๋‹ค๋ ค๋„ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š”๋‹ค....๐Ÿ˜‚ ๊ณ ๋ฏผ์„ ํ•˜๊ณ  ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ•œ ๊ฒฐ๊ณผ, ์ ˆ๋ฐ˜์„ ๋‚˜๋ˆˆ ํ›„ ์ขŒ์ธก์— ๋Œ€ํ•œ ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ํ•ฉ์„ ๊ตฌํ–ˆ๋‹ค๋ฉด, ์šฐ์ธก์— ๋Œ€ํ•œ ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ํ•ฉ์„ ๊ตฌํ•˜๋ฉด์„œ ๋‹ต์„ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ํšจ์œจ์ ์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

 ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌํ•˜์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • ์ฃผ์–ด์ง„ ์ˆœ์—ด(์ˆ˜๋“ค)์„ ์ ˆ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ„์–ด ์ขŒ์ธก์— ๋Œ€ํ•œ ๋ถ€๋ถ„์ˆ˜์—ด์˜ ํ•ฉ์„ ๊ตฌํ•˜๊ณ  ์ด๋ฅผ ์นด์šดํŠธํ•œ๋‹ค.
    • ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ์ •๋ ฌ์— ๋Œ€ํ•œ ๋ถ€๋ถ„๋„ ์ฒ˜๋ฆฌํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.
  • (S - ์šฐ์ธก ๋ถ€๋ถ„์ˆ˜์—ด์˜ ํ•ฉ)์ด ํ˜„์žฌ ๋”•์…”๋„ˆ๋ฆฌ์— ์นด์šดํŠธ๋œ ๊ฒŒ ์žˆ์œผ๋ฉด ์ •๋‹ต์œผ๋กœ ๋ฐ˜์˜ํ•œ๋‹ค.
    • ๋งŒ์•ฝ, ๋”•์…”๋„ˆ๋ฆฌ์— ์นด์šดํŠธ๋˜์ง€ ์•Š์€ ๊ฐ’์„ ์–ป๊ณ ์ž ํ•œ๋‹ค๋ฉด 0์ด ๋ฐ˜์˜๋œ๋‹ค.
  • ํ•ด๋‹น ๋ฐฉ์‹์€ ์ขŒ์ธก, ์šฐ์ธก์„ ๊ตฌํ•œ ํ›„์— ๋‹ค์‹œ while๋ฌธ์„ ๋Œ๋ฉฐ ์ขŒ์ธก๊ณผ ์šฐ์ธก์˜ ํ•ฉ์ด S๋ฅผ ๋งŒ์กฑํ•˜๋Š”์ง€ ์ฒดํฌํ•˜๋Š” ๋ฐฉ์‹๋ณด๋‹ค ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ •๋ ฌ์— ๋Œ€ํ•œ ์˜ค๋ฒ„ํ—ค๋“œ๋„ ์—†์„ ๊ฒƒ์œผ๋กœ ์ƒ๊ฐ๋œ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
from collections import defaultdict


def dfs(idx, end_idx, subtotal, direction):
    global answer

    if idx == end_idx:
        if direction == "right":
            answer += left[s - subtotal]
        else:
            left[subtotal] += 1
        return

    dfs(idx + 1, end_idx, subtotal, direction)
    dfs(idx + 1, end_idx, subtotal + nums[idx], direction)


if __name__ == "__main__":
    answer = 0
    n, s = map(int, stdin.readline().split())
    nums = list(map(int, stdin.readline().split()))
    left = defaultdict(int)

    dfs(0, n//2, 0, "left")
    dfs(n//2, n, 0, "right")

    print(answer if s != 0 else answer - 1)

 `defaultdict`๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด value์˜ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์–ด, ๋ณ„๋„์˜ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š” ์—†๋‹ค.

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€