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

[๋ฐฑ์ค€] 11727๋ฒˆ: 2xn ํƒ€์ผ๋ง 2

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

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

 

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

์ฒซ์งธ ์ค„์— 2×n ํฌ๊ธฐ์˜ ์ง์‚ฌ๊ฐํ˜•์„ ์ฑ„์šฐ๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ 10,007๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net

์ฝ”๋“œ

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		int n = scan.nextInt();
		int[] dp = new int[1001];
		dp[1] = 1;
		dp[2] = 3;
		for(int i=3; i<1001; i++) 
			dp[i] = (dp[i-1] + (2*dp[i-2])) % 10007;
		
		System.out.println(dp[n]);
		scan.close();
	}

}

ํ’€์ด

2xnํƒ€์ผ๋ง 

์œ„ ๋ฌธ์ œ๋Š” 1x2 , 2x1 ํƒ€์ผ๋กœ ์ฑ„์šฐ์ง€๋งŒ ์ด ๋ฌธ์ œ๋Š” 2x1 , 2x2 ํƒ€์ผ๋กœ ์ฑ„์šฐ๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜์ด๋‹ค.

 

2x1์ด๋ผ๊ณ  ํ•ด์„œ ๊ธธ์ญ‰ํ•œ ์„ธ๋กœ ์ง์‚ฌ๊ฐํ˜•๋งŒ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ, ์ด ์„ธ๋กœ๋ฅผ ๋ˆ•ํžˆ๋ฉด ๊ฐ€๋กœ ์ง์‚ฌ๊ฐํ˜•๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

๋งˆ์ง€๋ง‰ ๋„ค๋ชจ๋ฅผ 2X1 ์ง์‚ฌ๊ฐํ˜• ํ•˜๋‚˜๋กœ ๋†“๋Š” ๊ฒฝ์šฐ -> dp[n-1]

๋งˆ์ง€๋ง‰ ๋„ค๋ชจ๋ฅผ 1X2 ์ง์‚ฌ๊ฐํ˜• ๋‘๊ฐœ๋กœ ๋†“๋Š” ๊ฒฝ์šฐ -> dp[n-2]

 

๊ทธ๋ฆฌ๊ณ  ์ถ”๊ฐ€์ ์œผ๋กœ ๋งˆ์ง€๋ง‰์œผ๋กœ ๋„ค๋ชจ๋ฅผ 2x2 ์ง์‚ฌ๊ฐํ˜• ํ•œ๊ฐœ๋กœ ๋†“๋Š”๊ฒฝ์šฐ -> dp[n-2] ์˜ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ ์ตœ์ข… ์ ํ™”์‹์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

dp[n] = dp[n-1] + dp[n-2] + dp[n-2]

dp[n] = dp[n-1] + (2 * dp[n-2])

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€