๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm

[๋ฐฑ์ค€] 11726๋ฒˆ: 2xn ํƒ€์ผ๋ง(DP)

by ์ฃผ๋ฐœ2 2020. 2. 25.
๋ฐ˜์‘ํ˜•

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

 

11726๋ฒˆ: 2×n ํƒ€์ผ๋ง

2×n ํฌ๊ธฐ์˜ ์ง์‚ฌ๊ฐํ˜•์„ 1×2, 2×1 ํƒ€์ผ๋กœ ์ฑ„์šฐ๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์•„๋ž˜ ๊ทธ๋ฆผ์€ 2×5 ํฌ๊ธฐ์˜ ์ง์‚ฌ๊ฐํ˜•์„ ์ฑ„์šด ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์˜ ์˜ˆ์ด๋‹ค.

www.acmicpc.net

์ฝ”๋“œ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(bf.readLine());
		int[] dp = new int[1001];
		dp[1] = 1;
		dp[2] = 2;
		for(int i=3; i<=1000; i++) {
			dp[i] = (dp[i-2] + dp[i-1]) % 10007;
		}
        
		System.out.println(dp[n]);
		bf.close();
	}

}

ํ’€์ด

DP๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.

์œ„ ๊ทธ๋ฆผ์„ ๋ณด๋ฉด ๋งจ ์˜ค๋ฅธ์ชฝ์— ๋†“์„ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š”

2x1 ํƒ€์ผ์„ ์„ธ๋กœ๋กœ ํ•˜๋‚˜๋งŒ ๋†“๋Š” ๊ฒฝ์šฐ -> d[n-1]

2x2 ํƒ€์ผ์„ ๊ฐ€๋กœ๋กœ ๋‘๊ฐœ ๋†“๋Š” ๊ฒฝ์šฐ -> d[n-2]๋กœ,

d[n] = d[n-1] + d[n-2] ์˜ ์ ํ™”์‹์ด ๋‚˜์˜จ๋‹ค.(ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด)

 

https://antaehyeon.github.io/devlog/2018/05/08/%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98-2xn%ED%83%80%EC%9D%BC%EB%A7%81-(11726)/

 

๋˜ํ•œ ์ฃผ์˜ํ• ์ ์€, n์ด 1000๊นŒ์ง€ ์žˆ์œผ๋ฏ€๋กœ ์ ํ™”์‹์˜ ๊ฒฐ๊ณผ์—์„œ 10007์˜ ๋‚˜๋จธ์ง€๋ฅผ ์ €์žฅํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด int ๋ฐฐ์—ด์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ๋”ํ• ๋•Œ, 10007์„ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ์ €์žฅํ•˜๋ฉด ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€