dev/algorithm
BOJ / 11727๋ฒ / 2×n ํ์ผ๋ง 2 [Go][Python3]
crscnt
2021. 1. 13. 21:00
๐ฉ๐ป๐ป ๋ฌธ์
11727๋ฒ: 2×n ํ์ผ๋ง 2
2×n ์ง์ฌ๊ฐํ์ 1×2, 2×1๊ณผ 2×2 ํ์ผ๋ก ์ฑ์ฐ๋ ๋ฐฉ๋ฒ์ ์๋ฅผ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค. ์๋ ๊ทธ๋ฆผ์ 2×17 ์ง์ฌ๊ฐํ์ ์ฑ์ด ํ๊ฐ์ง ์์ด๋ค.
www.acmicpc.net
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/11727
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n int
fmt.Fscanln(reader, &n)
var count = make([]int, n+1)
for i := 1; i < n+1; i++ {
if i == 1 {
count[i] = 1
} else if i == 2 {
count[i] = 3
} else {
count[i] = (count[i-1] + 2*count[i-2]) % 10007
}
}
fmt.Fprintln(writer, count[n])
}
๐จ Python3
# https://www.acmicpc.net/problem/11727
import sys
if __name__ == "__main__":
n = int(sys.stdin.readline())
count = [0 for i in range(n+1)]
for i in range(1, n+1):
if i == 1:
count[i] = 1
elif i == 2:
count[i] = 3
else:
count[i] = (count[i-1] + 2*count[i-2]) % 10007
print(count[n])
728x90