[BOJ] #2758 ๋กœ๋˜ (Python, Dynamic Programming)

    ๋ฐ˜์‘ํ˜•

    ๐ŸŒฑ ๋ฌธ์ œ

     

    2758๋ฒˆ: ๋กœ๋˜

    ์„ ์˜์ด๋Š” ๋งค์ฃผ ์—„์ฒญ๋‚œ ๋ˆ์„ ๋กœ๋˜์— ํˆฌ์žํ•œ๋‹ค. ์„ ์˜์ด๊ฐ€ ํ•˜๋Š” ๋กœ๋˜๋Š” 1๋ถ€ํ„ฐ m๊นŒ์ง€ ์ˆซ์ž ์ค‘์— n๊ฐœ์˜ ์ˆ˜๋ฅผ ๊ณ ๋ฅด๋Š” ๋กœ๋˜์ด๋‹ค. ์ด๋ ‡๊ฒŒ ์—ด์‹ฌํžˆ ๋กœ๋˜๋ฅผ ํ•˜๋Š”๋ฐ, ์•„์ง๊นŒ์ง€ ํ•œ ๋ฒˆ๋„ ๋‹น์ฒจ๋˜์ง€ ์•Š์€ ์ด์œ ๋Š”

    www.acmicpc.net

    https://www.acmicpc.net/problem/2758

     

    ๐ŸŒฑ ํ’€์ด1 - ๋ฐฑํŠธ๋ž™ํ‚น ํ’€์ด(์‹œ๊ฐ„ ์ดˆ๊ณผ)

    m๊ฐœ์˜ ์ž์—ฐ ์ˆ˜ ์ค‘์—์„œ n๊ฐœ๋ฅผ ๊ณ ๋ฅด๋Š” ๋ฌธ์ œ๋ผ ๋‹น์—ฐํžˆ ๋ฐฑํŠธ๋ž™ํ‚น์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ณ  ์ฒ˜์Œ์—๋Š” ๋ฐฑํŠธ๋ž™ํ‚น์œผ๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.

    def backtracking(n, m, lotto):
        if len(lotto) == n - 1:
            global answer
            answer += (m - (lotto[-1] * 2) + 1)
            return
        for i in range(lotto[-1]*2, m + 1):
            tmp = i
            for j in range(n - len(lotto) - 1):
                tmp *= 2
                if tmp > m:
                    return
            lotto.append(i)
            backtracking(n, m, lotto)
            lotto.pop()
    
    
    t = int(input())
    for _ in range(t):
        n, m = map(int, input().split())
        answer = 0
        for i in range(1, m + 1):
            backtracking(n, m, [i])
        print(answer)

    ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋– ์„œ ์–ด๋–ป๊ฒŒ๋“  ์‹œ๊ฐ„ ์ดˆ๊ณผ๋ฅผ ์ค„์—ฌ๋ณด๊ณ ์ž ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

    • ๋งŒ์•ฝ ํ˜„์žฌ lotto์— ๋‹ด๊ฒจ ์žˆ๋Š” ์ˆซ์ž ์ค‘ ๋งˆ์ง€๋ง‰ ์ˆซ์ž๋กœ ๋‘ ๋ฐฐ์”ฉ ๋Š˜๋ ธ์„ ๋•Œ m๋ณด๋‹ค ์ปค์ง€๋ฉด ๋๊นŒ์ง€ lotto๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ ๋ฐ”๋กœ return
    • lotto๊ฐ€ n์ด ๋  ๋•Œ๊นŒ์ง€ ํƒ์ƒ‰ํ•˜์ง€ ์•Š๊ณ  n - 1์ด ๋  ๋•Œ๊นŒ์ง€ ํƒ์ƒ‰ (→ ์ด๊ฒŒ ๋ฌด์Šจ ์˜๋ฏธ์ธ์ง€,, ๋ฉ์ฒญํ•œ ๋‚˜..โ™ก) (๊ทผ๋ฐ ๊ฒฐ๊ตญ ํƒ์ƒ‰์ด ์•„๋‹ˆ๋ผ count๋ฅผ ์„ธ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผ ํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด์„œ ๊นจ๋‹ฌ์•˜์Šต๋‹ˆ๋‹ค.)

    ๊ฒฐ๊ตญ ํƒ์ƒ‰์ด ์•„๋‹ˆ๋ผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋กœ๋˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•ด๋†“๊ณ  ๊ณ„์‚ฐํ•˜๋Š” ์‹์œผ๋กœ ํ’€์–ด์•ผ ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์—ฌ ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ด์šฉํ•˜์—ฌ ํ’€์ด๋ฅผ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

     

    ๐ŸŒฑ ํ’€์ด2 - ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ’€์ด

    ์ด ๋ฌธ์ œ์—์„œ๋Š” DP ๋ฐฐ์—ด์ด 2์ฐจ์›์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด dp[i][j]์— j์ดํ•˜์˜ ์ž์—ฐ์ˆ˜๋กœ ๊ธธ์ด i์˜ ๋กœ๋˜๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์„ ์ €์žฅํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

    t = int(input())
    for _ in range(t):
        n, m = map(int, input().split())
    
        dp = [[0]*(m+1) for _ in range(n+1)]
        for i in range(m + 1):
            dp[1][i] = i
    
        for i in range(2, n + 1):
            for j in range(1, m + 1):
                dp[i][j] = dp[i][j-1]+dp[i-1][j//2]
        print(dp[n][m])
    • dp[i][j]์—๋Š” j์ดํ•˜์˜ ์ž์—ฐ์ˆ˜๋กœ ๋กœ๋˜ ์ˆซ์ž i๊ฐœ๋ฅผ ๊ณ ๋ฅด๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
    • dp[i][j] = dp[i][j-1] + dp[i-1][j//2]๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    • ์˜ˆ๋ฅผ ๋“ค์–ด, n=4์ด๊ณ , m=10์ธ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 10์ดํ•˜์˜ ์ž์—ฐ์ˆ˜๋กœ ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ๊ทœ์น™๋Œ€๋กœ ๋กœ๋˜ ๋ฒˆํ˜ธ 4๊ฐœ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ƒ๊ฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, dp[4][10]์„ ๊ตฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
      ์ด๋•Œ, dp[4][10]๋Š” dp[3][5] + dp[4][9]๋กœ ๊ณ„์‚ฐ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด 10์ดํ•˜์˜ ์ž์—ฐ์ˆ˜๋กœ ๋กœ๋˜ ๋ฒˆํ˜ธ 4๊ฐœ๋ฅผ ๊ณ ๋ฅด๋ฉด ๋˜๋ฏ€๋กœ 9์ดํ•˜์˜ ์ž์—ฐ์ˆ˜๋กœ 4๊ฐœ์˜ ๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ๊ณ ๋ฅด๋Š” ๊ฒฝ์šฐ๊ฐ€ ํฌํ•จ๋˜๊ณ , 5์ดํ•˜์˜ ์ž์—ฐ์ˆ˜๋กœ 3๊ฐœ๋ฅผ ๊ณจ๋ผ๋†“์€ ๊ฒฝ์šฐ์— 5์˜ 2๋ฐฐ์ธ 10์„ ์ถ”๊ฐ€๋กœ ๋„ฃ์–ด์ฃผ๋ฉด๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

     

    ๐ŸŒฑ ์ •๋‹ต ์ฝ”๋“œ

    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    # boj 2758 ๋กœ๋˜
    
    # DP ํ’€์ด
    t = int(input())
    for _ in range(t):
        n, m = map(int, input().split())
    
        dp = [[0]*(m+1) for _ in range(n+1)]
        for i in range(m + 1):
            dp[1][i] = i
    
        for i in range(2, n + 1):
            for j in range(1, m + 1):
                dp[i][j] = dp[i][j-1]+dp[i-1][j//2]
        print(dp[n][m])

    ์—ญ์‹œ DP๋ฌธ์ œ๋Š” ์ ํ™”์‹์„ ์ƒ๊ฐํ•ด๋‚ด๋ฉด ๊ตฌํ˜„์€ ์ •๋ง ์‰ฌ์šด๋ฐ, ์ ํ™”์‹์„ ์ƒ๊ฐํ•ด๋‚ด๋Š”๊ฒŒ ์ฐธ ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค..๐Ÿซ 

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€