Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 15988๋ฒˆ - 1,2,3 ๋”ํ•˜๊ธฐ 3 (์Šค์œ„ํ”„ํŠธ dp ๊ธฐ๋ณธ๋ฌธ์ œ, ๋ฐฑ์ค€ 9095๋ฒˆ)

๊ฐ์ž ๐Ÿฅ” 2022. 3. 19. 00:32
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ๋งํฌ

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

 

15988๋ฒˆ: 1, 2, 3 ๋”ํ•˜๊ธฐ 3

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค, n์„ 1, 2, 3์˜ ํ•ฉ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ 1,000,000,009๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net

 

ํ’€์ด idea

์•„๋‹ˆ ํ’€๋‹ค๋ณด๋‹ˆ, 1,2,3 ๋”ํ•˜๊ธฐ ๊ด€๋ จํ•œ ์‹œ๋ฆฌ์ฆˆ ๋ฌธ์ œ๋ฅผ ๋ช‡๊ฐœ ํ’€์–ด๋ดค์œผ๋ฏ€๋กœ, ๋” ๋ณต์žกํ•ด์•ผ๋˜๋Š”๋ฐ ์ด๊ฒŒ ๊ทœ์น™์ด๋ผ๊ณ ? ํ•˜๋ฉด์„œ ์• ๋งค๋ชจํ˜ธํ•˜๊ฒŒ ํ’€์—ˆ๋˜ ๋ฌธ์ œ์˜€๋‹ค. ๊ทผ๋ฐ ์•„๋‹ˆ๋‚˜ ๋‹ค๋ฅผ๊นŒ 1,2,3๋”ํ•˜๊ธฐ (๋ฐฑ์ค€ 9095๋ฒˆ) ๋ฌธ์ œ๋ž‘ ๊ทธ๋ƒฅ ๋™์ผํ•œ ๋ฌธ์ œ์˜€๋‹ค. ใ…‹ใ…‹ใ…‹ใ…‹ ๊ดœํžˆ ์ซ„์•˜๋„ค...

https://didu-story.tistory.com/225 

 

[๋ฐฑ์ค€] (Swift) 9095๋ฒˆ - 1,2,3 ๋”ํ•˜๊ธฐ (์Šค์œ„ํ”„ํŠธ ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ’€์ด)

๋ฌธ์ œ ๋งํฌ https://www.acmicpc.net/problem/9095 9095๋ฒˆ: 1, 2, 3 ๋”ํ•˜๊ธฐ ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค, n์„ 1, 2, 3์˜ ํ•ฉ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. www.acmicpc.net ๋‚ด๊ฐ€ ํ‘ผ ํ’€์ด - ๋งž์•˜์Šต๋‹ˆ๋‹ค! dp ๋ฌธ์ œ๋ฅผ..

didu-story.tistory.com

์ž์„ธํ•œ ํ’€์ด๋Š” ์œ„ ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ ํ•˜์ž. ์™„์ „ํžˆ ๊ฐ™์€๋ฌธ์ œ๋‹ค. ๊ทธ๋ž˜๋„ ๋‹ค์‹œ ํ’€์–ด๋ณด๋Š”๋ฐ ์กฐ๊ฑด์„ ๋นผ๋จน๊ธฐ๋„ํ•˜๊ณ  ํ•˜๋ฉด์„œ 4๋ฒˆ์ •๋„ ํ‹€๋ฆฌ๊ณ  ๋ฌธ์ œ๋ฅผ ๋งž์ท„๋‹ค. ใ…‹ใ…‹ ์  ์žฅ!! ์—ญ์‹œ dp ๋Š” ๋ฐ˜๋ณตํ•™์Šต์ด ์ค‘์š”ํ•œ๊ฒƒ๊ฐ™๋‹ค. ๊ฐ™์€๋ฌธ์ œ๋ฅผ ๋˜‘๊ฐ™์ด ํ’€์–ด๋„ ํ‹€๋ฆฌ๊ธฐ๋„ ํ•˜๋Š”๊ตฌ๋‚˜~~

์•„ 6๋ฒˆ๋งŒ์— ๋งž์ท„๊ตฌ๋‚˜.ใ…‹ใ…‹ใ…‹ใ…‹ ๋งˆ์ง€๋ง‰ ์กฐ๊ฑด 1000000009๋กœ ๋‚˜๋ˆ ์ฃผ๋Š”๊ฒƒ์„ ๋นผ๋จน๊ธฐ๋„ํ•˜๊ณ , if ๋ฌธ์œผ๋กœ 0, 1, 2, 3 ์ผ๋•Œ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ  ์ „์ฒด ๋‹ค for ๋ฌธ์„ ๋Œ๋ ค๋ฒ„๋ ค์„œ index ๋ฒ”์œ„ ๋ฐ– ์—๋Ÿฌ๊ฐ€ ๋‚˜๊ธฐ๋„ ํ–ˆ๋‹ค.ใ…‹ใ…‹ใ…‹ใ…‹ ์œผ์•™! ํ•œ๋ฒˆ์— ์™œ ๋ชป๋งž์ถฐ ใ… ใ…  

 

์†Œ์Šค์ฝ”๋“œ

let n = Int(readLine()!)!

for _ in 0..<n {
    let num = Int(readLine()!)!
    var dp = Array(repeating: 0, count: num+1)
    
    if num == 0 {
        print(0)
    } else if num == 1 {
        print(1)
    } else if num == 2 {
        print(2)
    } else if num == 3 {
        print(4)
    } else {
        dp[1] = 1
        dp[2] = 2
        dp[3] = 4
        
        for j in 4..<num+1 {
            dp[j] = (dp[j-1] + dp[j-2] + dp[j-3]) % 1000000009
        }
        print(dp[num])
    }
}

 

๋ฐ˜์‘ํ˜•