<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>동적계획법 Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<atom:link href="https://lycos7560.com/tag/%eb%8f%99%ec%a0%81%ea%b3%84%ed%9a%8d%eb%b2%95/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>생각의 흐름을 타고 다니며 만드는 블로그</description>
	<lastBuildDate>Thu, 23 Feb 2023 00:11:14 +0000</lastBuildDate>
	<language>ko-KR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://lycos7560.com/wp-content/uploads/2022/11/cropped-cropped-cropped-log-1-150x150-1-80x80.png</url>
	<title>동적계획법 Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>백준 2294번 (동전 2, C++, DP) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/2294-coin-2-c-dp-baekjoon/4052/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/2294-coin-2-c-dp-baekjoon/4052/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sat, 07 Jan 2023 08:22:23 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2294]]></category>
		<category><![CDATA[2294번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DynamicProgramming]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[다이나믹 프로그래밍]]></category>
		<category><![CDATA[동적계획법]]></category>
		<category><![CDATA[동전]]></category>
		<category><![CDATA[동전 2]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 2294]]></category>
		<category><![CDATA[백준 2294번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=4052</guid>

					<description><![CDATA[<p>백준 2294번 '동전 2' 문제에 대한 글입니다. 'DP 알고리즘'을 사용하여 해결하였습니다. (This is an article on the question of 'Coin 2' in Baekjun 2294. Resolved using 'DP algorithm'.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/2294-coin-2-c-dp-baekjoon/4052/">백준 2294번 (동전 2, C++, DP) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">동전 2</h1>



<p class="has-medium-font-size wp-block-paragraph"><a href="https://www.acmicpc.net/problem/2294" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/2294</a> </p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초 (추가 시간 없음)</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">55849</td><td class="has-text-align-left" data-align="left">16735</td><td class="has-text-align-left" data-align="left">11767</td><td class="has-text-align-left" data-align="left">29.214%</td></tr></tbody></table></figure>



<div style="height:56px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size wp-block-paragraph">n가지 종류의 동전이 있다. </p>



<p class="has-medium-font-size wp-block-paragraph">이 동전들을 적당히 사용해서, 그 가치의 합이 k원이 되도록 하고 싶다. </p>



<p class="has-medium-font-size wp-block-paragraph">그러면서 동전의 개수가 최소가 되도록 하려고 한다. </p>



<p class="has-medium-font-size wp-block-paragraph">각각의 동전은 몇 개라도 사용할 수 있다.</p>



<p class="has-medium-font-size wp-block-paragraph">사용한 동전의 구성이 같은데, 순서만 다른 것은 같은 경우이다.</p>



<div style="height:43px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size wp-block-paragraph">첫째 줄에 n, k가 주어진다. (1 ≤&nbsp;n ≤ 100, 1 ≤ k ≤ 10,000) </p>



<p class="has-medium-font-size wp-block-paragraph">다음 n개의 줄에는 각각의 동전의 가치가 주어진다.&nbsp;</p>



<p class="has-medium-font-size wp-block-paragraph">동전의 가치는 100,000보다 작거나 같은 자연수이다. </p>



<p class="has-medium-font-size wp-block-paragraph">가치가 같은 동전이 여러 번 주어질 수도 있다.</p>



<div style="height:45px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="has-medium-font-size wp-block-paragraph">첫째 줄에 사용한 동전의 최소 개수를 출력한다. </p>



<p class="has-medium-font-size wp-block-paragraph">불가능한 경우에는 -1을 출력한다.</p>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3 15
1
5
12</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3</pre>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<ul class="wp-block-list">
<li>잘못된 조건을 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/apples1309" target="_blank" rel="noreferrer noopener">apples1309</a>,&nbsp;<a href="https://www.acmicpc.net/user/djm03178" target="_blank" rel="noreferrer noopener">djm03178</a></li>



<li>빠진 조건을 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/goodcrane3" target="_blank" rel="noreferrer noopener">goodcrane3</a></li>



<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/hayman42" target="_blank" rel="noreferrer noopener">hayman42</a>,&nbsp;<a href="https://www.acmicpc.net/user/isac322" target="_blank" rel="noreferrer noopener">isac322</a>,&nbsp;<a href="https://www.acmicpc.net/user/paraworld" target="_blank" rel="noreferrer noopener">paraworld</a></li>
</ul>



<div style="height:36px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/25" target="_blank" rel="noreferrer noopener">다이나믹 프로그래밍</a></li>
</ul>



<div style="height:42px" aria-hidden="true" class="wp-block-spacer"></div>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h2 class="wp-block-heading">문제 정리</h2>



<p class="has-medium-font-size wp-block-paragraph">N개의 동전과 가치 K가 주어진다. (1 &lt;= N &lt;= 100), 1 ≤ K ≤ 10,000) </p>



<p class="has-medium-font-size wp-block-paragraph">동전은 각각 가치를 가지고 있고, 동전이 다르더라도 가치가 같을 수 있다. ( 1&lt;= v &lt;= 100,000)</p>



<p class="has-medium-font-size wp-block-paragraph">주어진 동전들을 조합하여<strong>(동전을 중복으로 사용 가능)</strong> 가치의 합이 K가 되는 최소 동전의 수를 구한다.</p>



<p class="has-medium-font-size wp-block-paragraph">만약 합을 K로 만들 수 없다면 -1을 출력한다.</p>



<div style="height:53px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">목표 가치로 접근</h2>



<div style="height:38px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="1600" height="957" src="https://lycos7560.com/wp-content/uploads/KakaoTalk_20230107_171037223.jpg" alt="" class="wp-image-4077" srcset="https://lycos7560.com/wp-content/uploads/KakaoTalk_20230107_171037223.jpg 1600w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230107_171037223-300x179.jpg 300w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230107_171037223-768x459.jpg 768w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230107_171037223-1536x919.jpg 1536w" sizes="(max-width: 1600px) 100vw, 1600px" /></figure>



<div style="height:39px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph"><strong>문제의 포인트는 F[K] = Min(F[K] , F[K &#8211; X] + 1) 찾는 것</strong></p>



<div style="height:74px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">통과된 코드</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;algorithm>

using namespace std;

// 코인의 가치 범위 1 ~ 100   
// 배열의 범위 coin[0 ~ 100]
// coin[N] = N번째 코인의 가치를 나타낸다.
int coinValue[101]; // 예제 {1, 2, 15, 0, 0 ....}

// 주어지는 목표 가치의 범위 1 ~ 10,000   
// 배열의 범위 [0 ~ 10,000]
// TargetValue[K] = K가치를 만드는 동전의 최소 개수
int TargetValue[10001];

// N 동전의 수, K 목표 가치
int N, K;

// 문제를 초기화 해주는 기능의 함수
void Initialization()
{
	// 동전의 수, 목표 가치를 입력받음
	cin >> N >> K;
	
	// 동전의 순서대로 가치를 입력받음
	// 인덱스틑 1번부터
	int temp = 1;
	while (temp &lt;= N) {
		cin >> coinValue[temp];
		temp++;
	}

	for (int i = 0; i &lt;= K; i++) {
		// 목표 가치가 0인 경우 필요한 동전의 수는 0개 
		// 점화식을 전개할때 필요하다.
		if (i == 0) { 
			TargetValue[i] = 0; 
			continue;
		}

		// 동전의 최소 가치는 1
		// 주어지는 목표의 가치의 최대값은 10,000
		// 동전의 개수가 10,000개를 넘어가는 경우는 없다.
		// 문제에서 불가능한 경우를 -1로 출력할때 필요
		TargetValue[i] = 10001;
	}

}

int main()
{
	Initialization(); // 문제 조건을 조기화

	// 입력받은 동전의 개수만큼 순회
	for (int i = 1; i &lt;= N; i++) {
		// 입력받은 목표 가치까지 순회
		for (int j = 1; j &lt;= K; j++) {
			
			// 코인의 가치 > 목표의 가치 넘어간다.
			// TargetValue[K]가 변하지 않는다.
			if (coinValue[i] > j) { continue; }

			// TargetValue[K - coinValue[N] + 1] 
			// 목표 가치에서 현재 순서의 코인의 가치를 한번 뺀다면 해당 코인을 1개 추가한 것과 같다.
			// 기존의 TargetValue[K]와 비교하여 더 작은 수를 넣는다.
			TargetValue[j] = min(TargetValue[j], TargetValue[j - coinValue[i]] + 1);
		}

	}

	// 목표 가치의 동전의 개수가 범위에서 벗어나는 수라면
	// 동전으로 목표의 가치를 만들 수 없다는 뜻 => -1을 출력
	if (TargetValue[K] >= 10001) { cout &lt;&lt; -1; }
	else { cout &lt;&lt; TargetValue[K]; }

	return 0;
}</pre>



<div style="height:49px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1239" height="116" src="https://lycos7560.com/wp-content/uploads/image-844.png" alt="" class="wp-image-4073" srcset="https://lycos7560.com/wp-content/uploads/image-844.png 1239w, https://lycos7560.com/wp-content/uploads/image-844-300x28.png 300w, https://lycos7560.com/wp-content/uploads/image-844-768x72.png 768w" sizes="(max-width: 1239px) 100vw, 1239px" /></figure>



<div style="height:37px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">DP 문제를 풀면 풀수록 점점 아리송해진다&#8230;</p>



<div style="height:61px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/2294-coin-2-c-dp-baekjoon/4052/">백준 2294번 (동전 2, C++, DP) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/2294-coin-2-c-dp-baekjoon/4052/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 11726번 (2×n 타일링, C++, DP) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/11726-2xn_tiling-c-dp-baekjoon/4039/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/11726-2xn_tiling-c-dp-baekjoon/4039/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Fri, 06 Jan 2023 13:52:36 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[11726]]></category>
		<category><![CDATA[11726번]]></category>
		<category><![CDATA[2×n 타일링]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DynamicProgramming]]></category>
		<category><![CDATA[fibonacci]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[다이나믹 프로그래밍]]></category>
		<category><![CDATA[동적계획법]]></category>
		<category><![CDATA[모듈러 산술]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 11726]]></category>
		<category><![CDATA[백준 11726번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[피보나치]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=4039</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 11726번 '2×n 타일링'에 대한 풀이 글입니다. DP를 사용하여 해결하였습니다. ( This is an essay on BAEKJOON No. 11726 '2×n Tiling'. Resolved using DP.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/11726-2xn_tiling-c-dp-baekjoon/4039/">백준 11726번 (2×n 타일링, C++, DP) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">2×n 타일링</h1>



<p class="has-medium-font-size wp-block-paragraph"><a href="https://www.acmicpc.net/problem/11726" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/11726</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">256 MB</td><td class="has-text-align-left" data-align="left">129015</td><td class="has-text-align-left" data-align="left">49137</td><td class="has-text-align-left" data-align="left">36258</td><td class="has-text-align-left" data-align="left">35.942%</td></tr></tbody></table></figure>



<div style="height:46px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size wp-block-paragraph">2×n 크기의 직사각형을 1×2, 2×1 타일로 채우는 방법의 수를 구하는 프로그램을 작성하시오.</p>



<p class="has-medium-font-size wp-block-paragraph">아래 그림은 2×5 크기의 직사각형을 채운 한 가지 방법의 예이다.</p>



<div style="height:34px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="872" height="381" src="https://lycos7560.com/wp-content/uploads/image-837.png" alt="" class="wp-image-4042" srcset="https://lycos7560.com/wp-content/uploads/image-837.png 872w, https://lycos7560.com/wp-content/uploads/image-837-300x131.png 300w, https://lycos7560.com/wp-content/uploads/image-837-768x336.png 768w" sizes="(max-width: 872px) 100vw, 872px" /></figure>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size wp-block-paragraph">첫째 줄에 n이 주어진다. (1 ≤ n ≤ 1,000)</p>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="has-medium-font-size wp-block-paragraph">첫째 줄에 2×n 크기의 직사각형을 채우는 방법의 수를 10,007로 나눈 나머지를 출력한다.</p>



<div style="height:61px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 2</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">9</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 2</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">55</pre>



<div style="height:43px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<ul class="wp-block-list">
<li>문제를 만든 사람:&nbsp;<a href="https://www.acmicpc.net/user/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/25" target="_blank" rel="noreferrer noopener">다이나믹 프로그래밍</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h2 class="wp-block-heading">접근 방법</h2>



<p class="has-medium-font-size wp-block-paragraph">N =1 부터 나올 수 있는 경우를 체크했다. </p>



<p class="has-medium-font-size wp-block-paragraph">그림을 그리면서 확인해본 결과 </p>



<p class="has-medium-font-size wp-block-paragraph">N = 3이라면 N = 1과 N = 2가 합쳐진 경우다.</p>



<div style="height:39px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://lycos7560.com/wp-content/uploads/KakaoTalk_20230106_224122980.jpg" alt="" class="wp-image-4047" width="1200" height="1679" srcset="https://lycos7560.com/wp-content/uploads/KakaoTalk_20230106_224122980.jpg 1373w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230106_224122980-214x300.jpg 214w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230106_224122980-768x1074.jpg 768w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230106_224122980-1098x1536.jpg 1098w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230106_224122980-1464x2048.jpg 1464w" sizes="(max-width: 1200px) 100vw, 1200px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">통과된 코드</h2>



<div style="height:32px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>

using namespace std;

constexpr int MOD = 10007;
					
long long int arr[1001];

int main()
{
	// Tabulation: Bottom Up 방식

	long long int N = 0;

	cin >> N;

	arr[1] = 1, arr[2] = 2;

	for (int  i = 3; i &lt;= N; i++) {
		arr[i] = ( arr[i - 1] % MOD + arr[i - 2] % MOD) % MOD;
	}

	cout &lt;&lt; arr[N];

	return 0;
}
</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1041" height="153" src="https://lycos7560.com/wp-content/uploads/image-838.png" alt="" class="wp-image-4046" srcset="https://lycos7560.com/wp-content/uploads/image-838.png 1041w, https://lycos7560.com/wp-content/uploads/image-838-300x44.png 300w, https://lycos7560.com/wp-content/uploads/image-838-768x113.png 768w" sizes="(max-width: 1041px) 100vw, 1041px" /></figure>



<div style="height:52px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">틀린 이유는 모듈러 연산에서 괄호를 잘못 묶었다.</p>



<p class="has-medium-font-size wp-block-paragraph">테스트 케이스 통과해서 좋다고 제출 했는데 틀렸다고 해서 당황</p>



<p class="has-medium-font-size wp-block-paragraph">테스트 케이스는 나머지 연산이 의미가 없어서 결과가 잘나오니&#8230; </p>



<div style="height:57px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/11726-2xn_tiling-c-dp-baekjoon/4039/">백준 11726번 (2×n 타일링, C++, DP) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/11726-2xn_tiling-c-dp-baekjoon/4039/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 2193번 (이친수, C++, DP) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/2193-pinary-number-c-dp-baekjoon/4025/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/2193-pinary-number-c-dp-baekjoon/4025/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Fri, 06 Jan 2023 12:18:34 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2193]]></category>
		<category><![CDATA[2193번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DynamicProgramming]]></category>
		<category><![CDATA[pinary number]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[다이나믹 프로그래밍]]></category>
		<category><![CDATA[동적계획법]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 2193]]></category>
		<category><![CDATA[백준 2193번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[이친수]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=4025</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 2193번 '이친수' 문제에 대한 글입니다. DynamicProgramming을  이용하여 해결하였습니다. (This is an article on the issue of 'pinary number' in BAEKJOON No. 2193. It was solved using Dynamic Programming.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/2193-pinary-number-c-dp-baekjoon/4025/">백준 2193번 (이친수, C++, DP) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">이친수</h1>



<p class="has-medium-font-size wp-block-paragraph"><a href="https://www.acmicpc.net/problem/2193" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/2193</a></p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table has-small-font-size"><table><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">2 초</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">80019</td><td class="has-text-align-left" data-align="left">33479</td><td class="has-text-align-left" data-align="left">25207</td><td class="has-text-align-left" data-align="left">40.237%</td></tr></tbody></table></figure>



<div style="height:41px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size wp-block-paragraph">0과 1로만 이루어진 수를 이진수라 한다. </p>



<p class="has-medium-font-size wp-block-paragraph">이러한 이진수 중 특별한 성질을 갖는 것들이 있는데, 이들을 이친수(pinary number)라 한다. </p>



<p class="has-medium-font-size wp-block-paragraph">이친수는 다음의 성질을 만족한다.</p>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">1. 이친수는 0으로 시작하지 않는다.</p>



<p class="has-medium-font-size wp-block-paragraph">2. 이친수에서는 1이 두 번 연속으로 나타나지 않는다. 즉, 11을 부분 문자열로 갖지 않는다.</p>



<div style="height:29px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">예를 들면 1, 10, 100, 101, 1000, 1001 등이 이친수가 된다. </p>



<p class="has-medium-font-size wp-block-paragraph">하지만 0010101이나 101101은 각각 1, 2번 규칙에 위배되므로 이친수가 아니다.</p>



<p class="has-medium-font-size wp-block-paragraph">N(1 ≤ N ≤ 90)이 주어졌을 때, N자리 이친수의 개수를 구하는 프로그램을 작성하시오.</p>



<div style="height:33px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size wp-block-paragraph">첫째 줄에 N이 주어진다.</p>



<div style="height:45px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="has-medium-font-size wp-block-paragraph">첫째 줄에 N자리 이친수의 개수를 출력한다.</p>



<div style="height:63px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<ul class="wp-block-list">
<li>문제의 오타를 찾은 사람: <a href="https://www.acmicpc.net/user/ehay" target="_blank" rel="noreferrer noopener">ehay</a></li>



<li>데이터를 추가한 사람: <a href="https://www.acmicpc.net/user/hg5312" target="_blank" rel="noreferrer noopener">hg5312</a></li>
</ul>



<div style="height:48px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/25" target="_blank" rel="noreferrer noopener">다이나믹 프로그래밍</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<figure class="wp-block-image size-full"><img decoding="async" width="969" height="1271" src="https://lycos7560.com/wp-content/uploads/image-835.png" alt="" class="wp-image-4031" srcset="https://lycos7560.com/wp-content/uploads/image-835.png 969w, https://lycos7560.com/wp-content/uploads/image-835-229x300.png 229w, https://lycos7560.com/wp-content/uploads/image-835-768x1007.png 768w" sizes="(max-width: 969px) 100vw, 969px" /></figure>



<div style="height:34px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">결국 이 문제는 피보나치 수열과 같다.</p>



<figure class="wp-block-embed is-type-wp-embed is-provider-어제와-내일의-나-그-사이의-이야기 wp-block-embed-어제와-내일의-나-그-사이의-이야기"><div class="wp-block-embed__wrapper">
<blockquote class="wp-embedded-content" data-secret="fEnqaUTmgx"><a href="https://lycos7560.com/?p=3966">알고리즘 &#8211; Dynamic Programming [DP] (다이나믹 프로그래밍 / 동적 계획법)</a></blockquote><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  title="&#8220;알고리즘 &#8211; Dynamic Programming [DP] (다이나믹 프로그래밍 / 동적 계획법)&#8221; &#8212; 어제와 내일의 나 그 사이의 이야기" src="https://lycos7560.com/?p=3966&#038;embed=true#?secret=L6IN8zhYkk#?secret=fEnqaUTmgx" data-secret="fEnqaUTmgx" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
</div></figure>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<div style="height:34px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">통과된 코드</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>

using namespace std;

long long int arr[91]; // N은 최대 90

int main()
{
	// Tabulation: Bottom Up 방식

	long long int N = 0;

	cin >> N;

	arr[0] = 0, arr[1] = 1;

	for (int  i = 2; i &lt;= N; i++) {
		arr[i] = arr[i - 1] + arr[i - 2];
	}

	cout &lt;&lt; arr[N];

	return 0;
}</pre>



<div style="height:44px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1035" height="141" src="https://lycos7560.com/wp-content/uploads/image-836.png" alt="" class="wp-image-4032" srcset="https://lycos7560.com/wp-content/uploads/image-836.png 1035w, https://lycos7560.com/wp-content/uploads/image-836-300x41.png 300w, https://lycos7560.com/wp-content/uploads/image-836-768x105.png 768w" sizes="(max-width: 1035px) 100vw, 1035px" /></figure>



<div style="height:35px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">통과하지 못한 이유</p>



<p class="has-medium-font-size wp-block-paragraph">1. 범위 제출 시 int로 제출하여 오버플로우가 발생</p>



<p class="has-medium-font-size wp-block-paragraph">2. 점화식에 변수를 잘못넣음  => for문에서 i 에  N을 넣음 (미친듯)</p>



<div style="height:68px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/2193-pinary-number-c-dp-baekjoon/4025/">백준 2193번 (이친수, C++, DP) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/2193-pinary-number-c-dp-baekjoon/4025/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>알고리즘 &#8211; Dynamic Programming [DP] (다이나믹 프로그래밍 / 동적 계획법)</title>
		<link>https://lycos7560.com/etc/algorithm-dynamic-programming-dp-dynamic-programming/3966/</link>
					<comments>https://lycos7560.com/etc/algorithm-dynamic-programming-dp-dynamic-programming/3966/#comments</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Fri, 06 Jan 2023 10:40:08 +0000</pubDate>
				<category><![CDATA[기타]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DynamicProgramming]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[다이나믹 프로그래밍]]></category>
		<category><![CDATA[동적계획법]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=3966</guid>

					<description><![CDATA[<p>알고리즘 중 하나인 'Dynamic Programming[DP]'에 대한 간단한 설명의 글입니다. / '피보나치 수열'을 예시로 들어 이론을 설명합니다. (This is a brief description of one of the algorithms, 'Dynamic Programming [DP] / Use the Fibonacci sequence as an example to illustrate the theory.)</p>
<p>The post <a href="https://lycos7560.com/etc/algorithm-dynamic-programming-dp-dynamic-programming/3966/">알고리즘 &#8211; Dynamic Programming [DP] (다이나믹 프로그래밍 / 동적 계획법)</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="has-large-font-size wp-block-heading">Dynamic Programming [DP] (다이나믹 프로그래밍 / 동적 계획법)</h2>



<p class="wp-block-paragraph"><a href="https://ko.wikipedia.org/wiki/%EB%8F%99%EC%A0%81_%EA%B3%84%ED%9A%8D%EB%B2%95" target="_blank" rel="noreferrer noopener">https://ko.wikipedia.org/wiki/%EB%8F%99%EC%A0%81_%EA%B3%84%ED%9A%8D%EB%B2%95</a></p>



<div style="height:58px" aria-hidden="true" class="wp-block-spacer"></div>



<div style="height:43px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">복잡한 문제를 간단한 여러 개의 문제로 나누어 푸는 방법</p>



<p class="has-medium-font-size wp-block-paragraph">부분 문제 반복과 최적 부분 구조를 가지고 있는 알고리즘을 일반적인 방법에 비해 더욱 적은 시간 내에 풀 때 사용한다.</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">주어진 문제를 풀기 위해서, 문제를 여러 개의 하위 문제(subproblem)로 나누어 푼 다음, </p>



<p class="has-medium-font-size wp-block-paragraph">그것을 결합하여 최종적인 목적에 도달하는 것이다. </p>



<p class="has-medium-font-size wp-block-paragraph"><strong><mark style="background-color:rgba(0, 0, 0, 0);color:#fd0000" class="has-inline-color">각 하위 문제의 해결을 계산한 뒤, 그 해결책을 저장하여 후에 같은 하위 문제가 나왔을 경우 그것을 간단하게 해결</mark></strong>할 수 있다. </p>



<p class="has-medium-font-size wp-block-paragraph">이러한 방법으로 동적 계획법은 계산 횟수를 줄일 수 있다. </p>



<p class="has-medium-font-size wp-block-paragraph">특히 이 방법은 하위 문제의 수가 기하급수적으로 증가할 때 유용하다.</p>



<div style="height:45px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-large-font-size wp-block-paragraph">&#8220;하나의 문제를 단 한번만 풀도록 하는 알고리즘&#8221;</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">( 이름이 Dynamic Programming 인 이유는 이름은 그냥 멋있어 보여서 그렇게 지었다고 한다. )</p>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<p class="has-medium-font-size wp-block-paragraph"><strong>Dynamic Programming</strong> =&gt; DP 의 가장 대표적인 예로는 피보나치 수열을 들 수 있다. </p>



<p class="has-small-font-size wp-block-paragraph"><a href="https://ko.wikipedia.org/wiki/%ED%94%BC%EB%B3%B4%EB%82%98%EC%B9%98_%EC%88%98">https://ko.wikipedia.org/wiki/%ED%94%BC%EB%B3%B4%EB%82%98%EC%B9%98_%EC%88%98</a> &lt;- 피보나치 수열</p>



<div style="height:43px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="783" height="307" src="https://lycos7560.com/wp-content/uploads/image-830.png" alt="" class="wp-image-3983" srcset="https://lycos7560.com/wp-content/uploads/image-830.png 783w, https://lycos7560.com/wp-content/uploads/image-830-300x118.png 300w, https://lycos7560.com/wp-content/uploads/image-830-768x301.png 768w" sizes="(max-width: 783px) 100vw, 783px" /></figure>



<div style="height:53px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="956" height="392" src="https://lycos7560.com/wp-content/uploads/image-827.png" alt="" class="wp-image-3974" srcset="https://lycos7560.com/wp-content/uploads/image-827.png 956w, https://lycos7560.com/wp-content/uploads/image-827-300x123.png 300w, https://lycos7560.com/wp-content/uploads/image-827-768x315.png 768w" sizes="(max-width: 956px) 100vw, 956px" /></figure>



<div style="height:35px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">Fibonacci Number(피보나치 수열)은 0과 1로 시작하며, 다음 피보나치 수는 바로 앞의 두 피보나치 수의 합이 되는 수열이다. </p>



<div style="height:27px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="615" height="321" src="https://lycos7560.com/wp-content/uploads/image-833.png" alt="" class="wp-image-4020" srcset="https://lycos7560.com/wp-content/uploads/image-833.png 615w, https://lycos7560.com/wp-content/uploads/image-833-300x157.png 300w" sizes="(max-width: 615px) 100vw, 615px" /></figure>



<div style="height:38px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">피보나치 수열은 점화식으로 표현할 수 있기 때문에 재귀함수로 구현할 수 있다.</p>



<p class="has-medium-font-size wp-block-paragraph">아래의 코드는 재귀 함수를 이용한 피보나치 수열을 구하는 코드다. </p>



<div style="height:48px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>

using namespace std;

int N, cntOne;

int FibonacciRecursive(int N) // 재귀 호출 방법
{
    if (N == 1 || N == 2) {
        cntOne++; // 카운트
        return 1;
    }
    else {
        return FibonacciRecursive(N - 1) + FibonacciRecursive(N - 2);
    }
}

int main()
{
    cin >> N;

    cntOne = 0;

    cout &lt;&lt; "f(" &lt;&lt; N &lt;&lt; ") : " &lt;&lt; FibonacciRecursive(N) &lt;&lt; "\n"; // 재귀

    cout &lt;&lt; "실행 횟수 : " &lt;&lt; cntOne &lt;&lt; "번";

    return 0;
}</pre>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1126" height="246" src="https://lycos7560.com/wp-content/uploads/image-829.png" alt="" class="wp-image-3976" srcset="https://lycos7560.com/wp-content/uploads/image-829.png 1126w, https://lycos7560.com/wp-content/uploads/image-829-300x66.png 300w, https://lycos7560.com/wp-content/uploads/image-829-768x168.png 768w" sizes="(max-width: 1126px) 100vw, 1126px" /></figure>



<div style="height:49px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">피보나치의 수열의  F(N)에서 N의 값이 증가하면 증가할수록 연산이 기하급수적으로 증가한다.</p>



<div style="height:25px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">예를 들어 피보나치의 수열 fib(5) 를 구한다고 하면 아래와 같이 이루어진다.</p>



<div style="height:36px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">fib(5)<br>fib(4) + fib(3)<br>(fib(3) + fib(2)) + (fib(2) + fib(1))<br>((fib(2) + fib(1)) + (fib(1) + fib(0))) + ((fib(1) + fib(0)) + fib(1))<br>(((fib(1) + fib(0)) + fib(1)) + (fib(1) + fib(0))) + ((fib(1) + fib(0)) + fib(1))</p>



<div style="height:48px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">세 번째 줄의 fib(2)가 중복되어 계산되고, 이것은 전체적인 계산 속도를 떨어뜨린다. </p>



<p class="has-medium-font-size wp-block-paragraph">결과적으로 이 알고리즘의 시간 복잡도는 <strong><mark style="background-color:rgba(0, 0, 0, 0);color:#ff0000" class="has-inline-color">지수 함수</mark></strong>가 된다.</p>



<div style="height:68px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">위와 같은 문제로 DP 알고리즘을 많이 사용하는데</p>



<p class="has-medium-font-size wp-block-paragraph">DP는 문제를 여러 개의 하위 문제(subproblem)로 나누고 각 하위 문제의 해결을 계산한 뒤, </p>



<p class="has-medium-font-size wp-block-paragraph">그 해결책을 저장하여 후에 같은 하위 문제가 나왔을 경우 중복 계산을 제거하여 효율성을 개선하는 알고리즘이다.</p>



<div style="height:53px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1652" height="694" src="https://lycos7560.com/wp-content/uploads/image-826.png" alt="" class="wp-image-3973" srcset="https://lycos7560.com/wp-content/uploads/image-826.png 1652w, https://lycos7560.com/wp-content/uploads/image-826-300x126.png 300w, https://lycos7560.com/wp-content/uploads/image-826-768x323.png 768w, https://lycos7560.com/wp-content/uploads/image-826-1536x645.png 1536w" sizes="(max-width: 1652px) 100vw, 1652px" /></figure>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="has-large-font-size wp-block-heading">다이내믹 프로그래밍의 대표적인 2가지 방법</h2>



<p class="has-medium-font-size wp-block-paragraph"><a href="https://www.geeksforgeeks.org/tabulation-vs-memoization/" target="_blank" rel="noreferrer noopener">https://www.geeksforgeeks.org/tabulation-vs-memoization/</a></p>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<div style="height:23px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-large-font-size wp-block-paragraph"><strong>1. Tabulation: Bottom Up</strong> (표, 상향식)</p>



<p class="has-large-font-size wp-block-paragraph"><strong>2. Memoization: Top Down</strong> (메모화, 하향식)</p>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-default" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<div style="height:30px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">1. <strong>Tabulation: Bottom Up</strong> (표, 상향식)</h2>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">이름 자체에서 알 수 있듯이 바닥에서 시작하여 상단으로 답을 축적하여 해결한다.</p>



<p class="has-medium-font-size wp-block-paragraph">(가장 작은 문제들부터 차례 차례 답을 쌓아 올려가기 때문에 Bottom Up)</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">피보나치 수열을 예로 들면 </p>



<p class="has-medium-font-size wp-block-paragraph">F(0) 부터 F(N)까지  F(1), F(2), F(3)&#8230;. F(N) 까지 <strong>순서대로 차근차근 값을 구하고 구한 값을 이용하여 중복 계산을 제거</strong>한다.</p>



<div style="height:34px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>

using namespace std;

int N, cnt = 0;
int arr[41];

int main()
{
    cin >> N;

    arr[1] = arr[2] = 1;

    for (int i = 3; i &lt;= N; i++) {
        cnt++;
        arr[i] = arr[i - 1] + arr[i - 2];
    }

    cout &lt;&lt; "F(" &lt;&lt; N &lt;&lt; ") = " &lt;&lt; arr[N] &lt;&lt; "   //  실행 수 : " &lt;&lt; cnt &lt;&lt; "번";

    return 0;
}</pre>



<div style="height:56px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="931" height="173" src="https://lycos7560.com/wp-content/uploads/image-831.png" alt="" class="wp-image-3986" srcset="https://lycos7560.com/wp-content/uploads/image-831.png 931w, https://lycos7560.com/wp-content/uploads/image-831-300x56.png 300w, https://lycos7560.com/wp-content/uploads/image-831-768x143.png 768w" sizes="(max-width: 931px) 100vw, 931px" /></figure>



<div style="height:42px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">재귀의 102,334,155번 연산 수와 비교하면 엄청난 차이를 보인다.</p>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<h2 class="wp-block-heading"><strong>2. Memoization: Top Down</strong> (메모화, 하향식)</h2>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">Memoization 방식은 목표에서 시작하여 가까운 값을 찾아 탑다운 형태로 중복된 값을 사용한다.</p>



<p class="has-medium-font-size wp-block-paragraph">(가장 먼저 호출하는 문제는 가장 큰 문제이기 때문에  Top &#8211; Down)</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>

using namespace std;

int N, cnt = 0;
int arr[41];

int FibonacciDP(int x)
{

    if (x == 0) return 0;
    else if (x == 1) return 1;

    cnt++; // 연산 카운트

    if (arr[x] == 0) return arr[x] = FibonacciDP(x - 1) + FibonacciDP(x - 2);
    else  return arr[x];

}

int main()
{
    cin >> N;

    arr[1] = arr[2] = 1;

    cout &lt;&lt; "F(" &lt;&lt; N &lt;&lt; ") = " &lt;&lt; FibonacciDP(N) &lt;&lt; "   //  연산 수 : " &lt;&lt; cnt &lt;&lt; "번";

    return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph"><strong>계산한 적이 있다면 추가 재귀 호출 없이 그 값을 바로 리턴</strong> 아니라면 지금 계산해서 그 값을 넣어 준다. </p>



<p class="has-medium-font-size wp-block-paragraph">(arr[x] = 0 이라면 계산한 적이 없다는 뜻)</p>



<p class="has-medium-font-size wp-block-paragraph">한 번 계산했던 값은 두 번 다시 계산할 필요가 없으므로, f(N)을 구하는 데&nbsp;<strong>O(N)</strong>의 복잡도가 필요하다.</p>



<div style="height:36px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="938" height="192" src="https://lycos7560.com/wp-content/uploads/image-832.png" alt="" class="wp-image-3993" srcset="https://lycos7560.com/wp-content/uploads/image-832.png 938w, https://lycos7560.com/wp-content/uploads/image-832-300x61.png 300w, https://lycos7560.com/wp-content/uploads/image-832-768x157.png 768w" sizes="(max-width: 938px) 100vw, 938px" /></figure>



<div style="height:81px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-5698326622209671" data-ad-slot="5078714126"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/etc/algorithm-dynamic-programming-dp-dynamic-programming/3966/">알고리즘 &#8211; Dynamic Programming [DP] (다이나믹 프로그래밍 / 동적 계획법)</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/etc/algorithm-dynamic-programming-dp-dynamic-programming/3966/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
