๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90
๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ

 

 

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

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

www.acmicpc.net

 

๐Ÿง‘๐Ÿป‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

ํ”ผ๋ณด๋‚˜์น˜ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ–ˆ์„ ๋•Œ, \( n = 0 \) ๋˜๋Š” \( n = 1 \)์ผ ๋•Œ์˜ 0๊ณผ 1์˜ ์ถœ๋ ฅ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

(์ฆ‰, ์œ„ ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์†Œ์Šค ์ฝ”๋“œ์—์„œ if(n == 0)๊ณผ if (n==1)์— ํ•ด๋‹นํ•  ๊ฒฝ์šฐ) 

 

1. Top-down ๋ฐฉ์‹์œผ๋กœ DP ๋ฐฐ์—ด์„ ํ†ตํ•ด ์ค‘๋ณต ๊ณ„์‚ฐ ์ตœ์†Œํ™” ๋ฐฉ๋ฒ• ์‹œ๋„ (์‹คํŒจ)

ํ”ผ๋ณด๋‚˜์น˜ ํ•จ์ˆ˜๋ฅผ ์žฌ๊ท€๋กœ ๊ตฌํ˜„ํ–ˆ์„ ๋•Œ์˜ ๋ฌธ์ œ์ ์ด ๋ฐ”๋กœ ์ค‘๋ณต ๊ณ„์‚ฐ์ž…๋‹ˆ๋‹ค. ์ค‘๋ณต ๊ณ„์‚ฐ์ด ์ด๋ฃจ์–ด์ง€๊ธฐ์— ์‹œ๊ฐ„์ด ํ„ฐ๋ฌด๋‹ˆ์—†์ด ๋งŽ์ด ๊ฑธ๋ฆฌ๋Š” ๊ฒƒ์ด๊ณ , ์ด๊ฒƒ์„ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•ด DP ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ ๊ณ„์‚ฐ๋œ N์ด๋ผ๋ฉด ๊ณ„์‚ฐํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด์ฃ .

 

ํ•˜์ง€๋งŒ N == 0 ๋˜๋Š” N == 1์ด ๋  ๋•Œ๊นŒ์ง€ ๋‚ด๋ ค๊ฐ€์•ผ ํ•˜๋Š”๋ฐ, ์ด๋ฏธ ๊ณ„์‚ฐ๋œ N์ด๋ผ๊ณ  ํ•ด์„œ ๊ณ„์‚ฐ์„ ์ค‘์ง€ํ•ด๋ฒ„๋ฆฌ๋ฉด ์ œ๋Œ€๋กœ ๊ฐœ์ˆ˜๋ฅผ ์…€ ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๊ณ , ๋‹ค ๋‚ด๋ ค๊ฐ€ ๋ฒ„๋ฆฌ๋ฉด DP ๋ฐฐ์—ด์„ ์“ฐ๋Š” ์ด์œ ๊ฐ€ ์—†๊ตฌ์š”.

 

๋”ฐ๋ผ์„œ Top-down ๋ฐฉ์‹์—์„œ๋Š” N == 0 ๋˜๋Š” N == 1์ด ๋  ๋•Œ๊นŒ์ง€ ๋‹ค ๋‚ด๋ ค๊ฐ€์ง€ ์•Š๋Š” ์ด์ƒ, ๋ฐฉ๋ฒ•์ด ์—†๋‹ค๊ณ  ํŒ๋‹จํ•˜์˜€๊ณ , Bottom-up ๋ฐฉ์‹์œผ๋กœ ์ƒ๊ฐ์„ ๋Œ๋ฆฌ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

 

2. Bottom-up ๋ฐฉ์‹์œผ๋กœ 0๊ณผ 1์˜ ๊ฐœ์ˆ˜ ์ €์žฅํ•ด ๊ฐ€๊ธฐ (์„ฑ๊ณต)

ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์˜ ์ ํ™”์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

// fibonacci[0] = fibonacci[1] = 1
fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];  // if) i >= 2

 

์žฌ๊ท€๋กœ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์งฐ์„ ๋•Œ, N == 0 ๋˜๋Š” N == 1 ์ผ ๋•Œ๊ฐ€ ์ข…๋ฃŒ ์กฐ๊ฑด์ด๋ฉฐ, ๊ฐ๊ฐ 0๊ณผ 1์„ ๋ฆฌํ„ดํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  fibonacci[i]๋Š” ์ด๋Ÿฌํ•œ 0๊ณผ 1๋“ค์ด ๋‹ค ๋”ํ•ด์ ธ์„œ ์ƒ์„ฑ๋œ ์ˆซ์ž์ž…๋‹ˆ๋‹ค.

 

์ฆ‰, ์œ„์˜ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์˜ ์ ํ™”์‹์€ 0๊ณผ 1์˜ ๊ฐœ์ˆ˜์—์„œ๋„ ์ ์šฉ๋œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

fibonacci[i].zero = fibonacci[i - 1].zero + fibonacci[i - 2].zero;
fibonacci[i].one = fibonacci[i - 1].one + fibonacci[i - 2].one;

 

๋”ฐ๋ผ์„œ, DP[i] ๋ฐฐ์—ด์€ i๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์˜ 0๊ณผ 1์˜ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ œ ์กฐ๊ฑด์—์„œ ์ž…๋ ฅ์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๊ฐ€ 40๊ฐœ์ด๋ฏ€๋กœ, N = 40๊นŒ์ง€ DP ๋ฐฐ์—ด์„ ์ฑ„์šฐ๋„๋ก ํ•˜๊ณ , ๋ฌธ์ œ์—์„œ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ์ˆซ์ž์˜ ์ธ๋ฑ์Šค๋กœ ์ฐพ์•„๊ฐ€์„œ ๋‹ต์„ ์ถ”์ถœํ•ด ์˜ค๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <iostream>
#include <vector>
using namespace std;
using Count = pair<int, int>;   // (0์˜ ๊ฐœ์ˆ˜, 1์˜ ๊ฐœ์ˆ˜)

void SetFastIO()
{
	ios::sync_with_stdio(false);
	cout.tie(NULL);
	cin.tie(NULL);
}

int main()
{
	SetFastIO();

	const int MAX_INPUT_SIZE = 41;
	vector<Count> dp(MAX_INPUT_SIZE);
	dp[0] = Count(1, 0), dp[1] = Count(0, 1);

	for (int i = 2; i < MAX_INPUT_SIZE; i++)
	{
		int sumOfZeroCount = dp[i - 1].first + dp[i - 2].first;
		int sumOfOneCount = dp[i - 1].second + dp[i - 2].second;
		dp[i] = Count(sumOfZeroCount, sumOfOneCount);
	}

	int testCase;
	cin >> testCase;
	for (int i = 0; i < testCase; i++)
	{
		int N;
		cin >> N;
		cout << dp[N].first << " " << dp[N].second << "\n";
	}

	return 0;
}

 

728x90
๋ฐ˜์‘ํ˜•