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

๋ฐฑ์ค€ 1003 ํŒŒ์ด์ฌ - ํ”ผ๋ณด๋‚˜์น˜ ํ•จ์ˆ˜ - ๋™์  ๊ณ„ํš๋ฒ•

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

๋ฐฑ์ค€ 1003 ํŒŒ์ด์ฌ

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

 

1003๋ฒˆ: ํ”ผ๋ณด๋‚˜์น˜ ํ•จ์ˆ˜

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค 0์ด ์ถœ๋ ฅ๋˜๋Š” ํšŸ์ˆ˜์™€ 1์ด ์ถœ๋ ฅ๋˜๋Š” ํšŸ์ˆ˜๋ฅผ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net

์žฌ๊ท€๋ฅผ ํ†ตํ•œ ํ”ผ๋ณด๋‚˜์น˜๋ฅผ ๊ตฌํ•˜๊ณ  0๊ณผ 1๊นŒ์ง€ ๋‚ด๋ ค๊ฐ”์„ ๋•Œ 0๊ณผ 1์„ ์ถœ๋ ฅํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

์ด๋•Œ 0๊ณผ 1์„ ์ถœ๋ ฅํ•œ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ์ด๋‹ค.

 

ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์„ ์žฌ๊ท€๋กœ ์ฃผ๋กœ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ... ๋™์  ๊ณ„ํš๋ฒ•์œผ๋กœ๋„ ์ถฉ๋ถ„ํžˆ ๊ฐ€๋Šฅํ•˜๋‹ค. ํŠนํžˆ ์ด๋ฒˆ ๋ฌธ์ œ์—์„œ๋Š” ์ถœ๋ ฅ๋˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—.... ์ˆซ์ž N์—๋Œ€ํ•ด์„œ N-1๊ณผ N-2 ๋ฒˆ์งธ์— ์ถœ๋ ฅํ•œ 0๊ณผ 1์˜ ์ˆ˜๋ฅผ ํ•ฉ์‚ฐํ•˜๋ฉด ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

 

 

Python ํ’€์ด

import sys
read = sys.stdin.readline

cache = {0: [1,0], 1:[0,1]}
for i in range(2, 41):
    cache[i] = [cache[i-2][j] + cache[i-1][j] for j in range(2)]

T = int(read())
for _ in range(T):
    print(' '.join(map(str, cache[int(read())])))
  • ์ดˆ๊ธฐ ๊ฐ’์œผ๋กœ ์ง€์ •ํ•  0๊ณผ 1์ผ ๋•Œ ์ถœ๋ ฅํ•˜๋Š” ํšŸ์ˆ˜๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ์— ๋‹ด๋Š”๋‹ค.
  • N์€ ์ตœ๋Œ€ 40๊นŒ์ง€๋ผ๋Š” ์ œํ•œ์ด ์žˆ์–ด ๊ทธ๋ƒฅ ๋ฏธ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“ค์–ด ๋†“๋Š”๋‹ค.

๋Œ“๊ธ€