๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
CS/์•Œ๊ณ ๋ฆฌ์ฆ˜

๋ฐฑ์ค€ 9461 ํŒŒ์ด์ฌ - ํŒŒ๋„๋ฐ˜ ์ˆ˜์—ด - ๋™์  ๊ณ„ํš๋ฒ•

by ๐ŸŒปโ™š 2021. 10. 8.

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

 

9461๋ฒˆ: ํŒŒ๋„๋ฐ˜ ์ˆ˜์—ด

์˜ค๋ฅธ์ชฝ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์‚ผ๊ฐํ˜•์ด ๋‚˜์„  ๋ชจ์–‘์œผ๋กœ ๋†“์—ฌ์ ธ ์žˆ๋‹ค. ์ฒซ ์‚ผ๊ฐํ˜•์€ ์ •์‚ผ๊ฐํ˜•์œผ๋กœ ๋ณ€์˜ ๊ธธ์ด๋Š” 1์ด๋‹ค. ๊ทธ ๋‹ค์Œ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณผ์ •์œผ๋กœ ์ •์‚ผ๊ฐํ˜•์„ ๊ณ„์† ์ถ”๊ฐ€ํ•œ๋‹ค. ๋‚˜์„ ์—์„œ ๊ฐ€์žฅ ๊ธด ๋ณ€์˜

www.acmicpc.net

ํ”ผ๋ณด๋‚˜์น˜์™€ ๋น„์Šทํ•˜๊ฒŒ ๊ทœ์น™์ด ์žˆ๋Š” ํŒŒ๋„๋ฐ˜ ์ˆ˜์—ด์˜ N๋ฒˆ์งธ ๊ฐ’์„ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์œ„์˜ ๊ทธ๋ฆผ์„ ๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด... ์ •์‚ผ๊ฐํ˜•์˜ ๋ณ€์„ ๋”ฐ๋ผ ๊ณ„์† ๋Š˜๋ ค๊ฐ€๋Š”๋ฐ... ์ •์‚ผ๊ฐํ˜•์˜ ๋ณ€์˜ ๊ธธ์ด๊ฐ€ ์ˆ˜์—ด์˜ ๊ฐ’์œผ๋กœ ๋“ค์–ด๊ฐ„๋‹ค.

 

1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16...

๊ฐ€์žฅ ํ•ต์‹ฌ์€ ํŒŒ๋„๋ฐ˜ ์ˆ˜์—ด์˜ ๊ทœ์น™์„ ์ฐพ๋Š” ๊ฒƒ์ด์—ˆ์Šต๋‹ˆ๋‹ค. 

์œ„์˜ ๊ทœ์น™์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทผ๋ฐ ์กฐ๊ฑด์œผ๋กœ n์ด 8๋ณด๋‹ค ํฌ๋‹ค๊ณ  ์ ์€ ์ด์œ ๋Š” ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์‚ผ๊ฐํ˜•์„ ๊ธฐ์ค€์œผ๋กœ ํ•œ ๊ฒƒ์ด๊ณ ... n์ด 5๋ณด๋‹ค ํฌ๋‹ค๋Š” ์กฐ๊ฑด์€ ํ•ด๋‹น ์ˆ˜์—ด์˜ ์ฒซ 3 ์ˆซ์ž๊ฐ€ ๋ชจ๋‘ 1์ด๊ธฐ ๋•Œ๋ฌธ์— 8์—์„œ 3์„ ๋บ€ ์‹ ์ž…๋‹ˆ๋‹ค.

 

 

Python ํ’€์ด 1

n > 8์œผ๋กœ ๊ฐ€์ •ํ•œ ํ’€์ด ์ž…๋‹ˆ๋‹ค.

import sys
read = sys.stdin.readline

cache = [0, 1, 1, 1, 2, 2, 3, 4, 5] + [0 for _ in range(100)]

for i in range(9, 101):
    cache[i] = cache[i-1] + cache[i-5]

for _ in range(int(read())):
    print(cache[int(read())])
  • ๋ฏธ๋ฆฌ ์ดˆ๊ธฐ๊ฐ’์„ 8๋ฒˆ์งธ ์š”์†Œ๊นŒ์ง€ ์ง€์ •ํ•ด์ฃผ๊ณ  ์ตœ๋Œ€ 100๊นŒ์ง€ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ์œ  ์žˆ๊ฒŒ ๋ฏธ๋ฆฌ ๋ฆฌ์ŠคํŠธ๋ฅผ 0์œผ๋กœ ์ฑ„์›Œ์คฌ์Šต๋‹ˆ๋‹ค.

 

Python ํ’€์ด 2

n > 5์œผ๋กœ ๊ฐ€์ •ํ•œ ํ’€์ด ์ž…๋‹ˆ๋‹ค.

import sys
read = sys.stdin.readline

cache = [0, 1, 1, 1, 2, 2] + [0 for _ in range(100)]

for i in range(6, 101):
    cache[i] = cache[i-1] + cache[i-5]

for _ in range(int(read())):
    print(cache[int(read())])

ํ’€์ด 1๊ณผ ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹จ, ์ดˆ๊ธฐ๊ฐ’์„ ์ค„์ด๊ณ  ๊ณ„์‚ฐ์„ ์‹œ์ž‘ํ•˜๋Š” ๋ฒ”์œ„๋ฅผ ์•ž๋‹น๊ฒผ์„ ๋ฟ์ž…๋‹ˆ๋‹ค.

 

๋Œ“๊ธ€