๐ŸŒฑ → ๐ŸŒณ

[Baekjoon] 2404. ๋‹จ์œ„ ๋ถ„์ˆ˜๋กœ ๋ถ„ํ•  - python ๋ณธ๋ฌธ

Algorithms

[Baekjoon] 2404. ๋‹จ์œ„ ๋ถ„์ˆ˜๋กœ ๋ถ„ํ•  - python

BAY 2023. 9. 28. 16:30
728x90

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

 

2404๋ฒˆ: ๋‹จ์œ„ ๋ถ„์ˆ˜๋กœ ๋ถ„ํ• 

์ฒซ์งธ ์ค„์— ์–‘์˜ ์ •์ˆ˜ p, q, a, n์ด ์ž…๋ ฅ๋œ๋‹ค. (1 ≤ p, q ≤ 800, 1 ≤ a ≤ 12000, 1 ≤ n ≤ 7)

www.acmicpc.net

๋ฌธ์ œ ์ •๋ณด

๋ฌธ์ œ ์œ ํ˜• ๋‚œ์ด๋„ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„ ํ•ด๊ฒฐ ์œ ๋ฌด
๋ฐฑํŠธ๋ž˜ํ‚น ์‹ค๋ฒ„1 1.2h o

์„ค๊ณ„ ๋ฐฉ๋ฒ•

๋ฐฑํŠธ๋ž˜ํ‚น

๊ตฌํ•ด์•ผ ํ•˜๋Š” ํƒ€๊นƒ ๋ถ„์ˆ˜์—์„œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋นผ๊ฐ€๋ฉด์„œ ํ’€์ดํ•จ

์ฝ”๋“œ

์‹œ๊ฐ„์ดˆ๊ณผ ..

from fractions import Fraction
import sys
input = sys.stdin.readline

p, q, a, n = map(int, input().split())
target = Fraction(p, q)

def backtrack(remaining, start, depth, product): # ๋‚จ์€ ์–‘, ๋‹จ์œ„๋ถ„์ˆ˜ ๋ถ„๋ชจ, ๋ถ„์ˆ˜๊ฐฏ์ˆ˜, ๋‹จ์œ„๋ถ„์ˆ˜ ๋ถ„๋ชจ ๊ณฑ
    # ๋‚จ์€ ์–‘์ด 0๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜, ๋‹จ์œ„๋ถ„์ˆ˜ ๋ถ„๋ชจ ๊ณฑ์ด a๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜, ๋ถ„์ˆ˜๊ฐฏ์ˆ˜๊ฐ€ n๋ณด๋‹ค ํฌ๋ฉด return 0
    if remaining < 0 or product > a or depth > n: 
        return 0
    # ๋‚จ์€ ์–‘์ด 0์ด๊ณ , ๋ถ„์ˆ˜ ๊ฐœ์ˆ˜๊ฐ€ n๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด return 1
    if remaining == 0 and depth <= n: 
        return 1
    count = 0
    for i in range(start, a + 1):
        unit_fraction = Fraction(1,i)
        if remaining-unit_fraction < 0 or product * i > a:
            continue
        # 1๋ถ€ํ„ฐ a๊นŒ์ง€ ๋Œ๋ฉด์„œ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋งŒ count
        count += backtrack(remaining - unit_fraction, i, depth + 1, product * i) 
    return count

print(backtrack(target, 1, 0, 1))

 

์ •๋‹ต ์ฝ”๋“œ !

from fractions import Fraction
import sys
input = sys.stdin.readline

p, q, a, n = map(int, input().split())
target = Fraction(p, q)

def backtrack(remaining, start, depth, product): 
    if remaining < 0 or product > a or depth > n: 
        return 0
    if remaining == 0 and depth <= n: 
        return 1
    count = 0
    for i in range(start, int(a / product) + 1): # ๋ฒ”์œ„ ์ˆ˜์ •
        unit_fraction = Fraction(1,i)
        # ๋‚จ์€ ์–‘ - ๋‹จ์œ„ ๋ถ„์ˆ˜๊ฐ€ 0๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜, ๋ถ„๋ชจ ๊ณฑ์ด a๋ฅผ ๋„˜์œผ๋ฉด continue
        if remaining-unit_fraction < 0 or product * i > a:
            continue
        count += backtrack(remaining - unit_fraction, i, depth + 1, product * i) 
    return count

print(backtrack(target, 1, 0, 1))

์–ด์ฐจํ”ผ product๊ฐ€ a๋ณด๋‹ค ํฌ๋ฉด ๋ชป ๋Œ๊ธฐ ๋•Œ๋ฌธ์— for๋ฌธ์„ ๋„๋Š” ๋ฒ”์œ„๋ฅผ (start, a+1) → (start, a//product + 1) ๋กœ ๋ณ€๊ฒฝํ•ด ์ฃผ์—ˆ๋‹ค

์žฌ๊ท€๋„๋Š” ํšŸ์ˆ˜๋ฅผ ์ค„์ž„์œผ๋กœ์„œ ์‹œ๊ฐ„์ดˆ๊ณผ ๊ทน๋ณตํ•จ

++ ๋‚จ์€์–‘-๋‹จ์œ„๋ถ„์ˆ˜ 0๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ๊ฐ€ ๋Œ€๋ถ€๋ถ„์ด๋ผ ๋ฏธ๋ฆฌ ์ฒ˜๋ฆฌํ•ด์คŒ์œผ๋กœ์„œ backtracking ๋„๋Š” ํšŸ์ˆ˜ ์ค„์ž„ 

 

๊ทผ๋ฐ ๋ถ„๋ช… ๋” ์ข‹์€ ํ’€์ด๊ฐ€ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค

์ ˆ๋Œ€ 1๋ถ€ํ„ฐ a๊นŒ์ง€ ๋„๋Š” ํ’€์ด์ผ๋ฆฌ ์—†์Œ .. 

๋ฉ”๋ชจ๋ฆฌ๋ž‘ ์‹œ๊ฐ„์ด ๋…ธ๋ ฅ์ด ๊ฐ€์ƒํ•ด์„œ ์ •๋‹ต์ฒ˜๋ฆฌํ•ด์ค€ ์ˆ˜์ค€

(์Šคํ„ฐ๋””์—์„œ ๋‹ค๋ฅธ ๋ถ„๋“ค ํ‘ธ์‹  ๊ฑฐ ๋ณด๊ณ  ๋‹ค์‹œ ํ’€์–ด๋ณด์ž .. >!)

 

์‹œ๊ฐ„ ๋ณต์žก๋„

O(2^n) ? O(a^n) ..?

์–ด๋ ค์› ๋˜ ์ 

๋ฐฑํŠธ๋ž˜ํ‚น ํ’€๋‹ค๋ณด๋ฉด ํ•จ์ˆ˜ ์•ˆ์—์„œ ๋‚ด๊ฐ€ ๋Œ๊ณ  ์žˆ๋Š” ๋“ฏํ•œ ๋Š๋‚Œ์„ ๋ฐ›์Œ

๋งŽ์€ ๋ฌธ์ œ ํ’€๋ฉด์„œ ์ต์ˆ™ํ•ด์ง€์ž 

์ฐธ๊ณ 

python์—์„œ 1/3+1/3์ด๋ž‘ 1/2+1/12+1/12ํ•˜๋ฉด ๊ฐ๊ฐ 0.6666666์ด๋ž‘ 0.6666667 ์ด๋ ‡๊ฒŒ ๊ณ„์‚ฐ๋˜์–ด์„œ ์ •ํ™•ํ•œ ๊ฐ’์„ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค

๋”ฐ๋ผ์„œ fractions ๋ชจ๋“ˆ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์Œ (๋ณด๋ฆฌ๋‹˜ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹น.. ) 

  1. Import the "fractions" module
from fractions import Fraction
  1. Create a fraction object
Fraction(3, 4) # 3/4

 

 

728x90