<?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/%ed%94%bc%eb%b3%b4%eb%82%98%ec%b9%98/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>생각의 흐름을 타고 다니며 만드는 블로그</description>
	<lastBuildDate>Tue, 11 Mar 2025 01:35:59 +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>백준 11444번 (피보나치 수 6, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-11444%eb%b2%88-%ed%94%bc%eb%b3%b4%eb%82%98%ec%b9%98-%ec%88%98-6-c-baekjoon/39936/</link>
					<comments>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-11444%eb%b2%88-%ed%94%bc%eb%b3%b4%eb%82%98%ec%b9%98-%ec%88%98-6-c-baekjoon/39936/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Mon, 10 Feb 2025 01:13:51 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[11444]]></category>
		<category><![CDATA[11444번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[fibonacci]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 11444번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[피보나치]]></category>
		<category><![CDATA[피보나치 수]]></category>
		<category><![CDATA[피보나치 수 6]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=39936</guid>

					<description><![CDATA[<p>피보나치 수 6 https://www.acmicpc.net/problem/11444 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 1 초 256 MB 27890 12508 10367 47.603% 문제 피보나치 수는 0과 1로 시작한다. 0번째 피보나치 수는 0이고, 1번째 피보나치 수는 1이다. 그 다음 2번째 부터는 바로 앞 두 피보나치 수의 합이 된다. 이를 식으로 써보면 Fn&#160;= Fn-1&#160;+ Fn-2&#160;(n ≥ 2)가 [&#8230;]</p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-11444%eb%b2%88-%ed%94%bc%eb%b3%b4%eb%82%98%ec%b9%98-%ec%88%98-6-c-baekjoon/39936/">백준 11444번 (피보나치 수 6, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">피보나치 수 6 <img decoding="async" width="30" height="38" class="wp-image-37903" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Gold_2.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Gold_2.jpg 400w, https://lycos7560.com/wp-content/uploads/2024/03/Gold_2-234x300.jpg 234w" sizes="(max-width: 30px) 100vw, 30px" /></h2>



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



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



<figure 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">256 MB</td><td class="has-text-align-left" data-align="left">27890</td><td class="has-text-align-left" data-align="left">12508</td><td class="has-text-align-left" data-align="left">10367</td><td class="has-text-align-left" data-align="left">47.603%</td></tr></tbody></table></figure>



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



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



<p class="wp-block-paragraph">피보나치 수는 0과 1로 시작한다. 0번째 피보나치 수는 0이고, 1번째 피보나치 수는 1이다. </p>



<p class="wp-block-paragraph">그 다음 2번째 부터는 바로 앞 두 피보나치 수의 합이 된다.</p>



<p class="wp-block-paragraph">이를 식으로 써보면 F<sub>n</sub>&nbsp;= F<sub>n-1</sub>&nbsp;+ F<sub>n-2</sub>&nbsp;(n ≥ 2)가 된다.</p>



<p class="wp-block-paragraph">n=17일때 까지 피보나치 수를 써보면 다음과 같다.</p>



<p class="wp-block-paragraph">0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597</p>



<p class="wp-block-paragraph">n이 주어졌을 때, n번째 피보나치 수를 구하는 프로그램을 작성하시오.</p>



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



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



<p class="wp-block-paragraph">첫째 줄에 n이 주어진다. n은 1,000,000,000,000,000,000보다 작거나 같은 자연수이다.</p>



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



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



<p class="wp-block-paragraph">첫째 줄에 n번째 피보나치 수를 1,000,000,007으로 나눈 나머지를 출력한다.</p>



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



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



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1000</pre>



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



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



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">517691607</pre>



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



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



<ul class="wp-block-list">
<li>문제를 만든 사람: <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>



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



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/124" target="_blank" rel="noreferrer noopener">수학</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/39" target="_blank" rel="noreferrer noopener">분할 정복을 이용한 거듭제곱</a></li>
</ul>



<div style="height:40px" 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)"/>



<p class="wp-block-paragraph">문제를 그대로 구현한 코드</p>



<p class="wp-block-paragraph">답은 정확하게 나오지만 N의 크기가 증가하면 증가할 수록 연산량이 증가하여 시간초과 결과가 나오는 것은 당연하다.</p>



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

using namespace std;

long long int N, t1, t2, t3;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

	cin >> N;
    t1 = 0;
    t2 = 1;
    t3 = 1;

	if (N == 0) cout &lt;&lt; t1;
	else if (N == 1) cout &lt;&lt; t2;
	else if (N == 2) cout &lt;&lt; t3;
    else 
    {
        for (int i = 2; i &lt; N; i++)
        {
            t1 = t2 % 1000000007;
            t2 = t3 % 1000000007;
            t3 = (t1 + t2) % 1000000007;
        }
    }

	cout &lt;&lt; t3 &lt;&lt; endl;
    return 0;
}
</pre>



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



<p class="wp-block-paragraph">피보나치 수를 더 좋은 방법으로 해결하는 방법은 아래 링크에 자세하게 설명되어 있다. </p>



<p class="wp-block-paragraph"><a href="https://www.acmicpc.net/blog/view/28" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/blog/view/28</a></p>



<p class="wp-block-paragraph">위의 게시글의 내용을 살펴보면 2가지로 문제를 해결하고 있다.</p>



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



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="844" height="369" src="https://lycos7560.com/wp-content/uploads/2025/03/image-14.png" alt="" class="wp-image-39937" srcset="https://lycos7560.com/wp-content/uploads/2025/03/image-14.png 844w, https://lycos7560.com/wp-content/uploads/2025/03/image-14-300x131.png 300w, https://lycos7560.com/wp-content/uploads/2025/03/image-14-768x336.png 768w" sizes="(max-width: 844px) 100vw, 844px" /><figcaption class="wp-element-caption"><strong>행렬을 이용한 풀이법</strong></figcaption></figure>



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



<figure class="wp-block-image size-full"><img decoding="async" width="850" height="218" src="https://lycos7560.com/wp-content/uploads/2025/03/image-15.png" alt="" class="wp-image-39938" srcset="https://lycos7560.com/wp-content/uploads/2025/03/image-15.png 850w, https://lycos7560.com/wp-content/uploads/2025/03/image-15-300x77.png 300w, https://lycos7560.com/wp-content/uploads/2025/03/image-15-768x197.png 768w" sizes="(max-width: 850px) 100vw, 850px" /><figcaption class="wp-element-caption"><strong>메모이제이션, 홀수/짝수를 이용한 풀이법</strong></figcaption></figure>



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



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



<p class="wp-block-paragraph">메모이제이션, 홀수/짝수를 이용한 풀이법을 적용한 코드</p>



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

using namespace std;

map&lt;long long, long long> d; // 메모이제이션을 위한 map
const long long mod = 1000000007; 

// 피보나치 수를 재귀적으로 계산하는 함수
long long Fibonacci_recursive(long long n) {
    if (n &lt;= 0) {
        return 0;
    }
    else if (n == 1 || n == 2) {
        return 1;
    }
    else if (d.count(n) > 0) {
        return d[n];
    }
    else {
        // n이 홀수
        if (n % 2 == 1) {
            long long m = (n + 1) / 2;
            long long t1 = Fibonacci_recursive(m);
            long long t2 = Fibonacci_recursive(m - 1);
            d[n] = t1 * t1 + t2 * t2;
            d[n] %= mod;
            return d[n];
        }
        else { // n이 짝수
            long long m = n / 2;
            long long t1 = Fibonacci_recursive(m - 1);
            long long t2 = Fibonacci_recursive(m);
            d[n] = (2LL * t1 + t2) * t2;
            d[n] %= mod;
            return d[n];
        }
    }
}

int main() {
    long long n;
    cin >> n; // 입력값 받기
    cout &lt;&lt; Fibonacci_recursive(n) &lt;&lt; '\n'; // n번째 피보나치 수 출력
    return 0;
}
</pre>



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



<p class="wp-block-paragraph">개인적으로 이런 수학적인 문제는 어느 정도 고민해보다 검색해보는 것이 공부하는 데 더 도움이 되는 것 같다.</p>



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



<figure class="wp-block-image size-full"><img decoding="async" width="964" height="76" src="https://lycos7560.com/wp-content/uploads/2025/03/image-16.png" alt="" class="wp-image-39939" srcset="https://lycos7560.com/wp-content/uploads/2025/03/image-16.png 964w, https://lycos7560.com/wp-content/uploads/2025/03/image-16-300x24.png 300w, https://lycos7560.com/wp-content/uploads/2025/03/image-16-768x61.png 768w" sizes="(max-width: 964px) 100vw, 964px" /></figure>



<p class="wp-block-paragraph"></p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-11444%eb%b2%88-%ed%94%bc%eb%b3%b4%eb%82%98%ec%b9%98-%ec%88%98-6-c-baekjoon/39936/">백준 11444번 (피보나치 수 6, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-11444%eb%b2%88-%ed%94%bc%eb%b3%b4%eb%82%98%ec%b9%98-%ec%88%98-6-c-baekjoon/39936/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1003번 (피보나치 함수, C++, DP) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/1003-fibonacci-function-c-dp-baekjoon/5030/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/1003-fibonacci-function-c-dp-baekjoon/5030/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Thu, 19 Jan 2023 21:16:26 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[algorithm]]></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[백준 1003]]></category>
		<category><![CDATA[백준 1003번]]></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=5030</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 1003번 '피보나치 함수' 문제에 대한 글입니다. 'DP' 알고리즘을 이용하여 해결하였습니다. (This is an article about the 'Fibonacci function' problem in BAEKJOON No. 1003. Solved using 'DP' algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/1003-fibonacci-function-c-dp-baekjoon/5030/">백준 1003번 (피보나치 함수, 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:62px" 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/1003" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/1003</a></p>



<div style="height:38px" 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">0.25 초 </td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">178348</td><td class="has-text-align-left" data-align="left">52048</td><td class="has-text-align-left" data-align="left">40832</td><td class="has-text-align-left" data-align="left">31.975%</td></tr></tbody></table></figure>



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



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



<p class="has-medium-font-size wp-block-paragraph">다음 소스는 N번째 피보나치 수를 구하는 C++ 함수이다.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">int fibonacci(int n) {
    if (n == 0) {
        printf("0");
        return 0;
    } else if (n == 1) {
        printf("1");
        return 1;
    } else {
        return fibonacci(n‐1) + fibonacci(n‐2);
    }
}</pre>



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



<p class="has-medium-font-size wp-block-paragraph">fibonacci(3)을 호출하면 다음과 같은 일이 일어난다.</p>



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



<p class="has-medium-font-size wp-block-paragraph">fibonacci(3)은 fibonacci(2)와 fibonacci(1) (첫 번째 호출)을 호출한다.</p>



<p class="has-medium-font-size wp-block-paragraph">fibonacci(2)는 fibonacci(1) (두 번째 호출)과 fibonacci(0)을 호출한다.</p>



<p class="has-medium-font-size wp-block-paragraph">두 번째 호출한 fibonacci(1)은 1을 출력하고 1을 리턴한다.</p>



<p class="has-medium-font-size wp-block-paragraph">fibonacci(0)은 0을 출력하고, 0을 리턴한다.</p>



<p class="has-medium-font-size wp-block-paragraph">fibonacci(2)는 fibonacci(1)과 fibonacci(0)의 결과를 얻고, 1을 리턴한다.</p>



<p class="has-medium-font-size wp-block-paragraph">첫 번째 호출한 fibonacci(1)은 1을 출력하고, 1을 리턴한다.</p>



<p class="has-medium-font-size wp-block-paragraph">fibonacci(3)은 fibonacci(2)와 fibonacci(1)의 결과를 얻고, 2를 리턴한다.</p>



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



<p class="has-medium-font-size wp-block-paragraph">1은 2번 출력되고, 0은 1번 출력된다. N이 주어졌을 때, fibonacci(N)을 호출했을 때, </p>



<p class="has-medium-font-size wp-block-paragraph">0과 1이 각각 몇 번 출력되는지 구하는 프로그램을 작성하시오.</p>



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



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



<p class="has-medium-font-size wp-block-paragraph">첫째 줄에 테스트 케이스의 개수 T가 주어진다.</p>



<p class="has-medium-font-size wp-block-paragraph">각 테스트 케이스는 한 줄로 이루어져 있고, N이 주어진다. N은 40보다 작거나 같은 자연수 또는 0이다.</p>



<div style="height:40px" 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>



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



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



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3
0
1
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="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1 0
0 1
1 2</pre>



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



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



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2
6
22</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="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5 8
10946 17711</pre>



<div style="height:60px" 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/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</a></li>



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



<li>어색한 표현을 찾은 사람: <a href="https://www.acmicpc.net/user/cyj101366" target="_blank" rel="noreferrer noopener">cyj101366</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">1 &#8211; 10 까지 확인</p>



<div style="height:30px" 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 T, N, Zero, One;

int fibonacci(int n) {

	if (n == 0) {
		Zero++;
		return 0;
	} 
	else if (n == 1) {
		One++;
		return 1;
	}
	else return fibonacci(n - 1) + fibonacci(n - 2);
}

int main()
{
	cin >> T;
	for (int i = 0; i &lt; T; i++) {
		cin >> N;
		Zero = 0;
		One = 0;
		fibonacci(N);
		cout &lt;&lt; Zero &lt;&lt; " " &lt;&lt; One &lt;&lt; "\n";
	}

	return 0;
}</pre>



<div style="height:35px" 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/image-996.png" alt="" class="wp-image-5031" width="186" height="507" srcset="https://lycos7560.com/wp-content/uploads/image-996.png 138w, https://lycos7560.com/wp-content/uploads/image-996-110x300.png 110w" sizes="(max-width: 186px) 100vw, 186px" /></figure>



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



<p class="has-medium-font-size wp-block-paragraph"><strong>arrZero[0] = 1</strong>, arrZero[1] = 0, arrZero[2] = 1 / arrZero[N] = arrZero[N-1] + arrZero[N-2]</p>



<p class="has-medium-font-size wp-block-paragraph">arrOne[0] = 0, arrOne[1] = 1, arrOne[2] = 1 / arrOne[N] = arrOne[N-1] + arrOne[N-2]  점화식을 발견</p>



<p class="has-medium-font-size wp-block-paragraph"><strong>N = 0 일때 arrZero[0] 를 생각해야 한다. (생각을 못해 12%에서 틀렸습니다.)</strong></p>



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



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



<div style="height:30px" 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>
#include &lt;list>

using namespace std;

int T, N, Zero, One;

long long int arrZero[41] = {1, 0, 1}; // 초기화
long long int arrOne[41] = {0, 1, 1}; // 초기화

list&lt;int> myList;

int main()
{
	cin >> T;

	for (int i = 0; i &lt; T; i++) {
		cin >> N;
		myList.push_back(N);
	}

	for (auto it = myList.begin(); it != myList.end(); it++) {
		for (int i = 3; i &lt;= *it; i++) {
			if (arrZero[i] == 0) arrZero[i] = arrZero[i - 1] + arrZero[i - 2];
			if (arrOne[i] == 0) arrOne[i] = arrOne[i - 1] + arrOne[i - 2];
		}

		cout &lt;&lt; arrZero[*it] &lt;&lt; " " &lt;&lt; arrOne[*it] &lt;&lt; "\n";
	}

	return 0;
}</pre>



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



<figure class="wp-block-image size-full"><img decoding="async" width="1040" height="146" src="https://lycos7560.com/wp-content/uploads/image-997.png" alt="" class="wp-image-5037" srcset="https://lycos7560.com/wp-content/uploads/image-997.png 1040w, https://lycos7560.com/wp-content/uploads/image-997-300x42.png 300w, https://lycos7560.com/wp-content/uploads/image-997-768x108.png 768w" sizes="(max-width: 1040px) 100vw, 1040px" /></figure>



<div style="height:100px" 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/1003-fibonacci-function-c-dp-baekjoon/5030/">백준 1003번 (피보나치 함수, 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/1003-fibonacci-function-c-dp-baekjoon/5030/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>백준 9471번 (피사노 주기, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-9471-pisano-cycle-c-baekjoon/3812/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-9471-pisano-cycle-c-baekjoon/3812/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Tue, 03 Jan 2023 05:42:24 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[fibonacci]]></category>
		<category><![CDATA[Period of Pisano]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[모듈러 산술]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 9471]]></category>
		<category><![CDATA[백준 9471번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[피보나치]]></category>
		<category><![CDATA[피사노의 주기]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=3812</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 9471번 피사노 주기 문제를 풀이한 내용의 글입니다. (This is an article that solves the Pisano cycle problem of BAEKJOON No. 9471.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-9471-pisano-cycle-c-baekjoon/3812/">백준 9471번 (피사노 주기, C++) [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/9471" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/9471</a></p>



<div style="height:50px" 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">1367</td><td class="has-text-align-left" data-align="left">963</td><td class="has-text-align-left" data-align="left">826</td><td class="has-text-align-left" data-align="left">73.422%</td></tr></tbody></table></figure>



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



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



<p class="has-medium-font-size wp-block-paragraph">1960년, IBM의 직원 Donald Wall은 피보나치 수열을 m으로 나눈 나머지가 주기를 이룬다는 것을 증명했다.</p>



<p class="has-medium-font-size wp-block-paragraph">예를 들어, 피보나치 수열의 처음 10개를 11로 나눈 예는 다음과 같다.</p>



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



<figure class="wp-block-table has-medium-font-size"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">n</th><th class="has-text-align-left" data-align="left">1</th><th class="has-text-align-left" data-align="left">2</th><th class="has-text-align-left" data-align="left">3</th><th class="has-text-align-left" data-align="left">4</th><th class="has-text-align-left" data-align="left">5</th><th class="has-text-align-left" data-align="left">6</th><th class="has-text-align-left" data-align="left">7</th><th class="has-text-align-left" data-align="left">8</th><th class="has-text-align-left" data-align="left">9</th><th class="has-text-align-left" data-align="left">10</th></tr></thead><tbody><tr><th class="has-text-align-left" data-align="left">F(n)</th><td class="has-text-align-left" data-align="left">1</td><td class="has-text-align-left" data-align="left">1</td><td class="has-text-align-left" data-align="left">2</td><td class="has-text-align-left" data-align="left">3</td><td class="has-text-align-left" data-align="left">5</td><td class="has-text-align-left" data-align="left">8</td><td class="has-text-align-left" data-align="left">13</td><td class="has-text-align-left" data-align="left">21</td><td class="has-text-align-left" data-align="left">34</td><td class="has-text-align-left" data-align="left">55</td></tr><tr><th class="has-text-align-left" data-align="left">F(n) mod 11</th><td class="has-text-align-left" data-align="left">1</td><td class="has-text-align-left" data-align="left">1</td><td class="has-text-align-left" data-align="left">2</td><td class="has-text-align-left" data-align="left">3</td><td class="has-text-align-left" data-align="left">5</td><td class="has-text-align-left" data-align="left">8</td><td class="has-text-align-left" data-align="left">2</td><td class="has-text-align-left" data-align="left">10</td><td class="has-text-align-left" data-align="left">1</td><td class="has-text-align-left" data-align="left">0</td></tr></tbody></table></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">k(m)을 반복하는 부분 수열의 길이라고 했을 때, k(11) = 10임을 알 수 있다.</p>



<p class="has-medium-font-size wp-block-paragraph">Wall은 아래와 같은 여러 가지 성질도 증명했다.</p>



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



<figure class="wp-block-image size-full"><img decoding="async" width="947" height="404" src="https://lycos7560.com/wp-content/uploads/image-784.png" alt="" class="wp-image-3818" srcset="https://lycos7560.com/wp-content/uploads/image-784.png 947w, https://lycos7560.com/wp-content/uploads/image-784-300x128.png 300w, https://lycos7560.com/wp-content/uploads/image-784-768x328.png 768w" sizes="(max-width: 947px) 100vw, 947px" /></figure>



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



<p class="has-medium-font-size wp-block-paragraph">m이 주어졌을 때, k(m)을 구하는 프로그램을 작성하시오.</p>



<div style="height:50px" 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>



<p class="has-medium-font-size wp-block-paragraph">각 테스트 케이스는 N과 M으로 이루어져 있다. </p>



<p class="has-medium-font-size wp-block-paragraph">N은 테스트 케이스의 번호이고, M은 문제에서 설명한 m이다.</p>



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



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



<p class="has-medium-font-size wp-block-paragraph">각 테스트 케이스마다 테스트 케이스 번호를 출력하고 k(M)을 출력한다.</p>



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



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



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



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



<div style="height:50px" 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="">5
1 4
2 5
3 11
4 123456
5 987654</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="">1 6
2 20
3 10
4 15456
5 332808</pre>



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



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



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



<p class="wp-block-paragraph"><a href="https://www.acmicpc.net/category/1">ICPC</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/7" target="_blank" rel="noreferrer noopener">Regionals</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/8" target="_blank" rel="noreferrer noopener">North America</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/9" target="_blank" rel="noreferrer noopener">Greater New York Region</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/1170" target="_blank" rel="noreferrer noopener">2013 Greater New York Programming Contest</a>&nbsp;D번</p>



<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:50px" 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/124" target="_blank" rel="noreferrer noopener">수학</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/95" target="_blank" rel="noreferrer noopener">정수론</a></li>
</ul>



<div style="height:20px" 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)"/>



<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>



<p class="has-medium-font-size wp-block-paragraph">포인트는 피사노 주기는 피보나치수열을 M으로 나눴을 때 이 나머지가 규칙성을 가지고 반복된다는 것</p>



<p class="has-medium-font-size wp-block-paragraph">피보나치수열은 F(0) = 0, F(1) = 1 &#8230;  나눌 숫자 M &gt;= 2 조건이므로 </p>



<p class="has-medium-font-size wp-block-paragraph">이후의 연산에서 나머지가 0, 1 순서로 나온다면 주기가 반복된다는 뜻이다.</p>



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



<p class="has-medium-font-size wp-block-paragraph">피보나치수열을 M으로 나누어주고 나머지가 0 과 1이 순서대로 나올 때 까지 반복해주면서 </p>



<p class="has-medium-font-size wp-block-paragraph">주기를 카운트 하면 문제를 해결 할 수 있다.</p>



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



<p class="has-medium-font-size wp-block-paragraph"><a href="https://en.wikipedia.org/wiki/Pisano_period" target="_blank" rel="noreferrer noopener">참고 : https://en.wikipedia.org/wiki/Pisano_period</a> &lt;- 피사노의 주기 위키</p>



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



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



<div style="height:25px" 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>
#include &lt;list>

using namespace std;

list&lt;pair&lt;int, int>> tcList;
list&lt;pair&lt;int, int>> resultList;

// 문제의 입력을 처리하는 함수
void GetInput()
{
	int N;
	cin >> N;
	pair&lt;int, int> tempPair;
	while (N-- > 0) {
		cin >> tempPair.first >> tempPair.second;
		tcList.push_back(tempPair);
	}
}

// 피사노의 주기를 확인하는 함수
int PeriodOfPisano(int tc)
{
	int a = 0, b = 1, cnt = 0, temp =0;

	// 주기가 나올때까지 반복해준다.
	while (true)
	{
		// 모듈러 산술 분배법칙
		temp = ((a % tc) + (b % tc)) % tc;
		a = b;
		b = temp;
		cnt++; // 주기가 얼마인지 체크

		// 나머지로 나눈 값이 처음과 같은 0과 1의 순서로 나온다면, 다시 주기가 반복된다는 뜻
		// (F2 = F1 + F0 , 나머지도 계속 반복을 하게 된다.)
		if (a == 0 &amp;&amp; b == 1) break; 
	}

	return cnt; // 주기 리턴
}

int main()
{
	GetInput(); // 입력 받기
	
	// 테스트 케이스만큼 순회 
	for (auto iter = tcList.begin(); iter != tcList.end(); iter++) { // 결과값 출력
		cout &lt;&lt; iter->first &lt;&lt; " " &lt;&lt; PeriodOfPisano(iter->second) &lt;&lt; "\n"; 
	}

	return 0;
}</pre>



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



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



<div style="height:54px" 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/%eb%b0%b1%ec%a4%80-9471-pisano-cycle-c-baekjoon/3812/">백준 9471번 (피사노 주기, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-9471-pisano-cycle-c-baekjoon/3812/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 2749번 (피보나치 수 3, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2749-fibonacci-3-c-baekjoon/3738/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2749-fibonacci-3-c-baekjoon/3738/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Mon, 02 Jan 2023 09:41:43 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2749]]></category>
		<category><![CDATA[2749번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[fibonacci]]></category>
		<category><![CDATA[Period of Pisano]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[sum]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[모듈러 산술]]></category>
		<category><![CDATA[문자열]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 2749]]></category>
		<category><![CDATA[백준 2749번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[피보나치]]></category>
		<category><![CDATA[피사노의 주기]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=3738</guid>

					<description><![CDATA[<p>백준(BAEKJOON)  2749번 피보나치 수 3를 풀이한 내용의 글입니다. 해당 문제를 해결하기 위해 모듈러 산술 및 피사노의 주기 개념에 대해서도 간단하게 설명합니다. (This is an article that solves the Fibonacci number 3 of BAEKJOON 2749. To address this problem, we also briefly discuss the concepts of modular arithmetic and Pisano's cycle.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2749-fibonacci-3-c-baekjoon/3738/">백준 2749번 (피보나치 수 3, C++) [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">피보나치 수 3</h1>



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



<div style="height:50px" 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">128 MB</td><td class="has-text-align-left" data-align="left">25212</td><td class="has-text-align-left" data-align="left">8423</td><td class="has-text-align-left" data-align="left">6821</td><td class="has-text-align-left" data-align="left">39.244%</td></tr></tbody></table></figure>



<div style="height:50px" 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">0번째 피보나치 수는 0이고, 1번째 피보나치 수는 1이다. </p>



<p class="has-medium-font-size wp-block-paragraph">그 다음 2번째 부터는 바로 앞 두 피보나치 수의 합이 된다.</p>



<p class="has-medium-font-size wp-block-paragraph">이를 식으로 써보면 F<sub>n</sub>&nbsp;= F<sub>n-1</sub>&nbsp;+ F<sub>n-2</sub>&nbsp;(n ≥ 2)가 된다.</p>



<p class="has-medium-font-size wp-block-paragraph">n=17일때 까지 피보나치 수를 써보면 다음과 같다.</p>



<p class="has-medium-font-size wp-block-paragraph">0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597</p>



<p class="has-medium-font-size wp-block-paragraph">n이 주어졌을 때, n번째 피보나치 수를 구하는 프로그램을 작성하시오.</p>



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



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



<p class="has-medium-font-size wp-block-paragraph">첫째 줄에 n이 주어진다. n은 1,000,000,000,000,000,000보다 작거나 같은 자연수이다.</p>



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



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



<p class="has-medium-font-size wp-block-paragraph">첫째 줄에 n번째 피보나치 수를 1,000,000으로 나눈 나머지를 출력한다.</p>



<div style="height:50px" 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="">1000</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="">228875</pre>



<div style="height:50px" 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:50px" 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/2747" target="_blank" rel="noreferrer noopener">2747번. 피보나치 수</a></li>



<li><a href="https://www.acmicpc.net/problem/2748" target="_blank" rel="noreferrer noopener">2748번. 피보나치 수 2</a></li>



<li><a href="https://www.acmicpc.net/problem/10826" target="_blank" rel="noreferrer noopener">10826번. 피보나치 수 4</a></li>



<li><a href="https://www.acmicpc.net/problem/10870" target="_blank" rel="noreferrer noopener">10870번. 피보나치 수 5</a></li>
</ul>



<div style="height:50px" 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/124" target="_blank" rel="noreferrer noopener">수학</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/39">분할 </a><a href="https://www.acmicpc.net/problem/tag/39" target="_blank" rel="noreferrer noopener">정복을 이용한 거듭제곱</a></li>
</ul>



<div style="height:50px" 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>



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



<p class="has-medium-font-size wp-block-paragraph"><strong>1. 모듈러 산술 : <a href="https://sskl660.tistory.com/75" target="_blank" rel="noreferrer noopener">https://sskl660.tistory.com/75</a></strong></p>



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



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



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



<p class="has-medium-font-size wp-block-paragraph">모듈러 연산의 분배 법칙을 사용 (덧셈)</p>



<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)"/>



<p class="has-medium-font-size wp-block-paragraph"><strong>2. </strong><a href="https://comyoung.tistory.com/236" target="_blank" rel="noreferrer noopener"><strong>피사노의 주기 : https://comyoung.tistory.com/236</strong></a></p>



<p class="has-medium-font-size wp-block-paragraph"><strong>피사노 주기(Pisano period)</strong></p>



<p class="has-medium-font-size wp-block-paragraph">&#8211; 개념 : 피보나치 수를 어떤 수 K로 나눌 때, 나머지는 항상 주기를 가지게 되는데 이를 피사노 주기라고 한다.</p>



<p class="has-medium-font-size wp-block-paragraph">&nbsp;ex) 피보나치 수를&nbsp;<strong>3</strong>으로 나누었을때, 주기의 길이는<strong>&nbsp;8</strong></p>



<figure class="wp-block-table has-small-font-size"><table class="has-fixed-layout"><tbody><tr><td>N</td><td>0</td><td>1&nbsp;</td><td>2&nbsp;</td><td>3&nbsp;</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td><td>9</td><td>10</td><td>11</td><td>12</td><td>13</td><td>14</td><td>15</td></tr><tr><td>&nbsp;Fn</td><td>&nbsp;0</td><td>1&nbsp;</td><td>1&nbsp;</td><td>2&nbsp;</td><td>3&nbsp;</td><td>5&nbsp;</td><td>8&nbsp;</td><td>13&nbsp;</td><td>21&nbsp;</td><td>34&nbsp;</td><td>55&nbsp;</td><td>89&nbsp;</td><td>144&nbsp;</td><td>&nbsp;233</td><td>377&nbsp;</td><td>&nbsp;610</td></tr><tr><td>&nbsp;PP</td><td>&nbsp;0</td><td>1&nbsp;</td><td>1&nbsp;</td><td>2&nbsp;</td><td>0&nbsp;</td><td>2&nbsp;</td><td>2&nbsp;</td><td>1&nbsp;</td><td>0&nbsp;</td><td>1&nbsp;</td><td>1&nbsp;</td><td>2&nbsp;</td><td>0&nbsp;</td><td>2&nbsp;</td><td>2&nbsp;</td><td>1&nbsp;</td></tr></tbody></table></figure>



<p class="has-medium-font-size wp-block-paragraph">&#8211; 성질 :&nbsp;<strong>주기의 길이를&nbsp;P라고 하면 N번째 피보나치 수를&nbsp;M으로 나눈 나머지는</strong></p>



<p class="has-medium-font-size wp-block-paragraph"><strong>&nbsp; &nbsp; &nbsp;N%P번째 피보나치 수를 M으로 나눈 나머지와 같다.</strong></p>



<p class="has-medium-font-size wp-block-paragraph"><strong>&nbsp; &nbsp; &nbsp;주기는&nbsp;M = 10^k (k&gt;2)일때, 항상&nbsp;15 * 10^(k-1)이다.</strong></p>



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



<p class="has-medium-font-size wp-block-paragraph">=&gt; 해당 문제에서 M = 1,000,000 / K = 6  =&gt; 주기는 15 * (10 ^ 5) = 1,500,000</p>



<p class="has-large-font-size wp-block-paragraph"><strong>n = 1 결과와 1,500,001의 결과가 같다.</strong></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">통과된 코드 </h2>



<div style="height:28px" 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>
#include &lt;string>

using namespace std;

constexpr auto MN = 1000000;

string N = "";

int arr[3];

int ModFibonacci(string N)
{	
	// unsigned long long의 범위 0 ~ 18,446,744,073,709,551,615
	// N의 최대 1,000,000,000,000,000,000
	unsigned long long tempN = stoull(N); // string -> unsigned long long 변환
	tempN = tempN % 1500000; // 피사노의 주기 1,500,000

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

	if (tempN == 0) { return arr[0]; }
	else if (tempN == 1) { return arr[1]; }

	for (unsigned long long i = 2; i &lt;= tempN; i++) {
		arr[2] = (arr[1] % MN + arr[0] % MN) % MN; //
		arr[0] = arr[1];
		arr[1] = arr[2];
	}

	return arr[2];
}

int main()
{
	cin >> N;
	cout &lt;&lt; ModFibonacci(N);
	return 0;
}
</pre>



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



<figure class="wp-block-image size-full"><img decoding="async" width="1037" height="99" src="https://lycos7560.com/wp-content/uploads/image-775.png" alt="" class="wp-image-3740" srcset="https://lycos7560.com/wp-content/uploads/image-775.png 1037w, https://lycos7560.com/wp-content/uploads/image-775-300x29.png 300w, https://lycos7560.com/wp-content/uploads/image-775-768x73.png 768w" sizes="(max-width: 1037px) 100vw, 1037px" /></figure>



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



<p class="has-medium-font-size wp-block-paragraph">N = 1,000,000,000,000,000,000 (최대 값) 이면 연산에 엄청난 시간이 소모되어 해결법을 찾으려 많은 고민을 했다.</p>



<p class="has-medium-font-size wp-block-paragraph">피사노의 주기가 문제 해결의 포인트</p>



<p class="has-medium-font-size wp-block-paragraph"><a href="https://www.acmicpc.net/problem/9471" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/9471</a>  &lt;- 백준 피사노의 주기 문제</p>



<p class="has-medium-font-size wp-block-paragraph"><a href="https://www.acmicpc.net/blog/view/28" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/blog/view/28</a> &lt;- 피보나치 수를 구하는 여러가지 방법</p>



<div style="height:35px" 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/%eb%b0%b1%ec%a4%80-2749-fibonacci-3-c-baekjoon/3738/">백준 2749번 (피보나치 수 3, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2749-fibonacci-3-c-baekjoon/3738/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 10826번 (피보나치 수 4, C++, DP) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-10826-fibonacci-4-c-dp-baekjoon/3729/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-10826-fibonacci-4-c-dp-baekjoon/3729/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Mon, 02 Jan 2023 07:27:00 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></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[string]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[sum]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 10826]]></category>
		<category><![CDATA[백준 10826번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[큰 수 연산]]></category>
		<category><![CDATA[피보나치]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=3729</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 10826번 피보나치 수 4 문제를 풀이한 내용의 글입니다. DP 그리고 string을 이용하여 더하는 방법을 사용하였습니다. (This is an article that solves the Fibonacci number 4 problem of BAEKJOON 10826. DP and string were used to add.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-10826-fibonacci-4-c-dp-baekjoon/3729/">백준 10826번 (피보나치 수 4, 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/10826" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/10826</a></p>



<div style="height:50px" 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">256 MB</td><td class="has-text-align-left" data-align="left">16497</td><td class="has-text-align-left" data-align="left">5646</td><td class="has-text-align-left" data-align="left">4823</td><td class="has-text-align-left" data-align="left">39.407%</td></tr></tbody></table></figure>



<div style="height:50px" 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">0번째 피보나치 수는 0이고, 1번째 피보나치 수는 1이다. </p>



<p class="has-medium-font-size wp-block-paragraph">그 다음 2번째 부터는 바로 앞 두 피보나치 수의 합이 된다.</p>



<p class="has-medium-font-size wp-block-paragraph">이를 식으로 써보면 F<sub>n</sub>&nbsp;= F<sub>n-1</sub>&nbsp;+ F<sub>n-2</sub>&nbsp;(n ≥ 2)가 된다.</p>



<p class="has-medium-font-size wp-block-paragraph">n=17일때 까지 피보나치 수를 써보면 다음과 같다.</p>



<p class="has-medium-font-size wp-block-paragraph">0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597</p>



<p class="has-medium-font-size wp-block-paragraph">n이 주어졌을 때, n번째 피보나치 수를 구하는 프로그램을 작성하시오.</p>



<div style="height:50px" 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">n은 10,000보다 작거나 같은 자연수 또는 0이다.</p>



<div style="height:50px" 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>



<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="">10</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="">55</pre>



<div style="height:100px" 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:43px" 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/2747" target="_blank" rel="noreferrer noopener">2747번. 피보나치 수</a></li>



<li><a href="https://www.acmicpc.net/problem/2748" target="_blank" rel="noreferrer noopener">2748번. 피보나치 수 2</a></li>



<li><a href="https://www.acmicpc.net/problem/2749" target="_blank" rel="noreferrer noopener">2749번. 피보나치 수 3</a></li>



<li><a href="https://www.acmicpc.net/problem/10870" target="_blank" rel="noreferrer noopener">10870번. 피보나치 수 5</a></li>
</ul>



<div style="height:45px" 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">다이나믹 </a><a href="https://www.acmicpc.net/problem/tag/25" target="_blank" rel="noreferrer noopener">프로그래밍</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/117" target="_blank" rel="noreferrer noopener">임의 정밀도 / 큰 수 연산</a></li>
</ul>



<div style="height:51px" 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)"/>



<p class="has-medium-font-size wp-block-paragraph">n은 10,000보다 작거나 같은 자연수 또는 0으로 </p>



<p class="has-medium-font-size wp-block-paragraph">만약 n이 10,000이라면 결과 값은 아래와 같다.</p>



<p class="has-small-font-size wp-block-paragraph">33644764876431783266621612005107543310302148460680063906564769974680081442166662368155595513633734025582065332680836159373734790483865268263040892463056431887354544369559827491606602099884183933864652731300088830269235673613135117579297437854413752130520504347701602264758318906527890855154366159582987279682987510631200575428783453215515103870818298969791613127856265033195487140214287532698187962046936097879900350962302291026368131493195275630227837628441540360584402572114334961180023091208287046088923962328835461505776583271252546093591128203925285393434620904245248929403901706233888991085841065183173360437470737908552631764325733993712871937587746897479926305837065742830161637408969178426378624212835258112820516370298089332099905707920064367426202389783111470054074998459250360633560933883831923386783056136435351892133279732908133732642652633989763922723407882928177953580570993691049175470808931841056146322338217465637321248226383092103297701648054726243842374862411453093812206564914032751086643394517512161526545361333111314042436854805106765843493523836959653428071768775328348234345557366719731392746273629108210679280784718035329131176778924659089938635459327894523777674406192240337638674004021330343297496902028328145933418826817683893072003634795623117103101291953169794607632737589253530772552375943788434504067715555779056450443016640119462580972216729758615026968443146952034614932291105970676243268515992834709891284706740862008587135016260312071903172086094081298321581077282076353186624611278245537208532365305775956430072517744315051539600905168603220349163222640885248852433158051534849622434848299380905070483482449327453732624567755879089187190803662058009594743150052402532709746995318770724376825907419939632265984147498193609285223945039707165443156421328157688908058783183404917434556270520223564846495196112460268313970975069382648706613264507665074611512677522748621598642530711298441182622661057163515069260029861704945425047491378115154139941550671256271197133252763631939606902895650288268608362241082050562430701794976171121233066073310059947366875</p>



<div style="height:23px" 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">따라서 string을 이용한 덧셈 연산 / DP를 사용해야 한다. </p>



<p class="has-medium-font-size wp-block-paragraph">(전에 풀었던 아래의 두 문제를 조합하여 해결했다. )</p>



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



<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="JQMfA8ZJ1b"><a href="https://lycos7560.com/?p=3715">백준 15353번 (큰 수 A+B (2), C++, 큰 수 연산) [BAEKJOON]</a></blockquote><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  title="&#8220;백준 15353번 (큰 수 A+B (2), C++, 큰 수 연산) [BAEKJOON]&#8221; &#8212; 어제와 내일의 나 그 사이의 이야기" src="https://lycos7560.com/?p=3715&#038;embed=true#?secret=cZDjBvznYA#?secret=JQMfA8ZJ1b" data-secret="JQMfA8ZJ1b" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
</div></figure>



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



<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="SYUnwnGY1B"><a href="https://lycos7560.com/?p=3698">백준 2748번 (피보나치 수 2, C++, DP) [BAEKJOON]</a></blockquote><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  title="&#8220;백준 2748번 (피보나치 수 2, C++, DP) [BAEKJOON]&#8221; &#8212; 어제와 내일의 나 그 사이의 이야기" src="https://lycos7560.com/?p=3698&#038;embed=true#?secret=NuL7HhHl3C#?secret=SYUnwnGY1B" data-secret="SYUnwnGY1B" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
</div></figure>



<div style="height:30px" 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>



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



<pre class="EnlighterJSRAW" data-enlighter-language="generic" 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;

int N;

string arr[10001];

// 두 문자열을 더해주는 함수
string SumString(string strA, string strB)
{
	string tempStr;

	// 두 문자열 중에서 가장 높은 인덱스 길이 찾음
	int maxLength = strA.length() > strB.length() ? strA.length() : strB.length();

	// 문자열을 뒤집어 준다.
	reverse(strA.begin(), strA.end()); 
	reverse(strB.begin(), strB.end());

	// 뒤집은 두 문자열의 인덱스를 맞추어 준다.
	for (int i = strA.length(); i &lt; maxLength; i++) { strA.push_back('0'); }
	for (int i = strB.length(); i &lt; maxLength; i++) { strB.push_back('0'); }

	int tempOne, tempTwo, tempThree;
	int up = 0; 

	// 같은 인덱스의 char를 int로 변환하고 서로 더 해준다.
	// 만약 up 변수로 올림 처리를 해준다.
	for (int i = 0; i &lt; maxLength; i++) {
		tempOne = 0;
		tempTwo = 0;
		if (i &lt;= strA.length()) { tempOne = strA[i] - '0'; } // char => int
		if (i &lt;= strB.length()) { tempTwo = strB[i] - '0'; } // char => int
		tempThree = tempOne + tempTwo + up;
		up = 0;

		if (tempThree >= 10) {
			tempThree -= 10;
			up += 1; // 올림처리
		}

		tempStr.push_back(tempThree + '0'); // int => char
	}

	if (up >= 1) { tempStr.push_back('1'); } // 올림이 남아있다면
	reverse(tempStr.begin(), tempStr.end()); // 더한 값을 뒤집어 준다.
	return tempStr;
}

string FibonacciDP(int N)
{
	arr[0] = "0";
	arr[1] = "1";

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

	return arr[N];
}

int main()
{
	cin >> N;
	cout &lt;&lt; FibonacciDP(N);
	return 0;
}
</pre>



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



<figure class="wp-block-image size-full"><img decoding="async" width="1033" height="148" src="https://lycos7560.com/wp-content/uploads/image-774.png" alt="" class="wp-image-3730" srcset="https://lycos7560.com/wp-content/uploads/image-774.png 1033w, https://lycos7560.com/wp-content/uploads/image-774-300x43.png 300w, https://lycos7560.com/wp-content/uploads/image-774-768x110.png 768w" sizes="(max-width: 1033px) 100vw, 1033px" /></figure>



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



<p class="has-medium-font-size wp-block-paragraph">arr의 범위를 10,000으로 설정하여 런타임 에러가 발생했음.</p>



<p class="has-medium-font-size wp-block-paragraph">전에 풀었던 문제의 코드들을 조합해서 해결해서 크게 어렵지 않았다.</p>



<p class="has-medium-font-size wp-block-paragraph"><a href="https://www.acmicpc.net/blog/view/28" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/blog/view/28</a> &lt;- 피보나치 수를 구하는 여러가지 방법</p>



<div style="height:25px" 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/%eb%b0%b1%ec%a4%80-10826-fibonacci-4-c-dp-baekjoon/3729/">백준 10826번 (피보나치 수 4, 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/%eb%b0%b1%ec%a4%80-10826-fibonacci-4-c-dp-baekjoon/3729/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 2748번 (피보나치 수 2, C++, DP) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2748-fibonacci-2-c-dp-baekjoon/3698/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2748-fibonacci-2-c-dp-baekjoon/3698/#comments</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Mon, 02 Jan 2023 03:42:33 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2747]]></category>
		<category><![CDATA[2747번]]></category>
		<category><![CDATA[2748]]></category>
		<category><![CDATA[2748번]]></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[백준 2748]]></category>
		<category><![CDATA[백준 2748번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[피보나치]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=3698</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 2748번 피보나치 수를 풀이한 내용의 글입니다.  (This is an article that solves the number of Fibonacci in BAEKJOON 2748.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2748-fibonacci-2-c-dp-baekjoon/3698/">백준 2748번 (피보나치 수 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/2748">https://www.acmicpc.net/problem/2748</a></p>



<div style="height:58px" 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">81352</td><td class="has-text-align-left" data-align="left">32945</td><td class="has-text-align-left" data-align="left">27046</td><td class="has-text-align-left" data-align="left">40.142%</td></tr></tbody></table></figure>



<div style="height:100px" 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로 시작한다. 0번째 피보나치 수는 0이고, 1번째 피보나치 수는 1이다. </p>



<p class="has-medium-font-size wp-block-paragraph">그 다음 2번째 부터는 바로 앞 두 피보나치 수의 합이 된다.</p>



<p class="has-medium-font-size wp-block-paragraph">이를 식으로 써보면 F<sub>n</sub>&nbsp;= F<sub>n-1</sub>&nbsp;+ F<sub>n-2</sub>&nbsp;(n ≥ 2)가 된다.</p>



<p class="has-medium-font-size wp-block-paragraph">n=17일때 까지 피보나치 수를 써보면 다음과 같다.</p>



<p class="has-medium-font-size wp-block-paragraph">0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597</p>



<p class="has-medium-font-size wp-block-paragraph">n이 주어졌을 때, n번째 피보나치 수를 구하는 프로그램을 작성하시오.</p>



<div style="height:44px" 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">n은 90보다 작거나 같은 자연수이다.</p>



<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">첫째 줄에 n번째 피보나치 수를 출력한다.</p>



<div style="height:71px" 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="">10</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="">55</pre>



<div style="height:100px" 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/2747" target="_blank" rel="noreferrer noopener">2747번. 피보나치 수</a></li>



<li><a href="https://www.acmicpc.net/problem/2749" target="_blank" rel="noreferrer noopener">2749번. 피보나치 수 3</a></li>



<li><a href="https://www.acmicpc.net/problem/10826" target="_blank" rel="noreferrer noopener">10826번. 피보나치 수 4</a></li>



<li><a href="https://www.acmicpc.net/problem/10870" target="_blank" rel="noreferrer noopener">10870번. 피보나치 수 5</a></li>
</ul>



<div style="height:56px" 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/124" target="_blank" rel="noreferrer noopener">수학</a></li>



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



<div style="height:100px" 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)"/>



<p class="has-medium-font-size wp-block-paragraph">전에 풀었던 문제를 참고하여 접근했습니다. (DP 방법)</p>



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



<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="lK3CUm5KbC"><a href="https://lycos7560.com/?p=3652">백준 24416번 (알고리즘 수업 &#8211; 피보나치 수 1, C++, DP) [BAEKJOON]</a></blockquote><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  title="&#8220;백준 24416번 (알고리즘 수업 &#8211; 피보나치 수 1, C++, DP) [BAEKJOON]&#8221; &#8212; 어제와 내일의 나 그 사이의 이야기" src="https://lycos7560.com/?p=3652&#038;embed=true#?secret=xfjNLQpHrG#?secret=lK3CUm5KbC" data-secret="lK3CUm5KbC" 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)"/>



<h2 class="wp-block-heading">주의 사항</h2>



<p class="has-medium-font-size wp-block-paragraph">n은 90보다 작거나 같은 자연수</p>



<p class="has-medium-font-size wp-block-paragraph">int로 값을 처리하면 오버플로우가 발생합니다.</p>



<p class="has-medium-font-size wp-block-paragraph">그래서 long long int로 처리해 주었습니다.</p>



<p class="has-medium-font-size wp-block-paragraph">전의 문제와 약간의 차이가 있는 부분은 0번째 항부터 시작한다는 것입니다.</p>



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



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



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



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



<div style="height:16px" 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;

// N이 90일 경우 int로 처리하면 
// 21억을 넘어가서 오버플로우가 발생한다. 
long long int arr[91];

long long int FibonacciDP(int N)
{
	arr[0] = 0;
	arr[1] = 1;

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

	return arr[N];
}

int main()
{
	cin >> N;
	cout &lt;&lt; FibonacciDP(N);
	return 0;
}</pre>



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



<figure class="wp-block-image size-full"><img decoding="async" width="1034" height="101" src="https://lycos7560.com/wp-content/uploads/image-767.png" alt="" class="wp-image-3699" srcset="https://lycos7560.com/wp-content/uploads/image-767.png 1034w, https://lycos7560.com/wp-content/uploads/image-767-300x29.png 300w, https://lycos7560.com/wp-content/uploads/image-767-768x75.png 768w" sizes="(max-width: 1034px) 100vw, 1034px" /></figure>



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



<p class="has-medium-font-size wp-block-paragraph">위의 코드로 백준 2747, 10870번 문제도 통과가 가능합니다. (N의 범위 차이만 있음)</p>



<p class="has-medium-font-size wp-block-paragraph"><a href="https://www.acmicpc.net/blog/view/28" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/blog/view/28</a> &lt;- 피보나치 수를 구하는 여러가지 방법</p>



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



<figure class="wp-block-image size-full"><img decoding="async" width="1031" height="102" src="https://lycos7560.com/wp-content/uploads/image-769.png" alt="" class="wp-image-3710" srcset="https://lycos7560.com/wp-content/uploads/image-769.png 1031w, https://lycos7560.com/wp-content/uploads/image-769-300x30.png 300w, https://lycos7560.com/wp-content/uploads/image-769-768x76.png 768w" sizes="(max-width: 1031px) 100vw, 1031px" /></figure>



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



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



<div style="height:71px" 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/%eb%b0%b1%ec%a4%80-2748-fibonacci-2-c-dp-baekjoon/3698/">백준 2748번 (피보나치 수 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/%eb%b0%b1%ec%a4%80-2748-fibonacci-2-c-dp-baekjoon/3698/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>백준 24416번 (알고리즘 수업 &#8211; 피보나치 수 1, C++, DP) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/baekjoon-24416-fibonacci-1-c/3652/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/baekjoon-24416-fibonacci-1-c/3652/#comments</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sun, 01 Jan 2023 14:25:05 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></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[백준 24416]]></category>
		<category><![CDATA[백준 24416번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[피보나치]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=3652</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 24416번 문제를 풀이한 내용의 글입니다. (This is an article that solves question 24416 of BAEKJOON.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/baekjoon-24416-fibonacci-1-c/3652/">백준 24416번 (알고리즘 수업 &#8211; 피보나치 수 1, 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">알고리즘 수업 &#8211; 피보나치 수 1</h1>



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



<div style="height:48px" 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">512 MB</td><td class="has-text-align-left" data-align="left">8113</td><td class="has-text-align-left" data-align="left">4750</td><td class="has-text-align-left" data-align="left">4276</td><td class="has-text-align-left" data-align="left">60.056%</td></tr></tbody></table></figure>



<div style="height:61px" 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">아빠가 수업한 내용을 학생들이 잘 이해했는지 문제를 통해서 확인해보자.</p>



<p class="has-medium-font-size wp-block-paragraph">오늘은 <em>n</em>의 피보나치 수를 재귀호출과 동적 프로그래밍으로 구하는 알고리즘을 배웠다. </p>



<p class="has-medium-font-size wp-block-paragraph">재귀호출에 비해 동적 프로그래밍이 얼마나 빠른지 확인해 보자. </p>



<p class="has-medium-font-size wp-block-paragraph">아래 의사 코드를 이용하여 <em>n</em>의 피보나치 수를 구할 경우 코드1 코드2 실행 횟수를 출력하자.</p>



<p class="has-medium-font-size wp-block-paragraph">피보나치 수 재귀호출 의사 코드는&nbsp;다음과 같다.</p>



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



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">fib(n) {
    if (n = 1 or n = 2)
    then return 1;  # 코드1
    else return (fib(n - 1) + fib(n - 2));
}</pre>



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



<p class="has-medium-font-size wp-block-paragraph">피보나치 수 동적 프로그래밍 의사 코드는 다음과 같다.</p>



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



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">fibonacci(n) {
    f[1] &lt;- f[2] &lt;- 1;
    for i &lt;- 3 to n
        f[i] &lt;- f[i - 1] + f[i - 2];  # 코드2
    return f[n];
}</pre>



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



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



<p class="has-medium-font-size wp-block-paragraph">첫째 줄에&nbsp;<em>n</em>(5&nbsp;≤&nbsp;<em>n</em>&nbsp;≤ 40)이&nbsp;주어진다.</p>



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



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



<p class="has-medium-font-size wp-block-paragraph">코드1 코드2 실행 횟수를 한 줄에&nbsp;출력한다.</p>



<div style="height:50px" 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="">5</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="">5 3</pre>



<div style="height:100px" 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="">30</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="">832040 28</pre>



<div style="height:100px" 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/chansol">chansol</a>, <a href="https://www.acmicpc.net/user/jhnah917" target="_blank" rel="noreferrer noopener">jhnah917</a>, <a href="https://www.acmicpc.net/user/jinhan814" target="_blank" rel="noreferrer noopener">jinhan814</a>, <a href="https://www.acmicpc.net/user/jthis" target="_blank" rel="noreferrer noopener">jthis</a>, <a href="https://www.acmicpc.net/user/parkky">p</a><a href="https://www.acmicpc.net/user/parkky" target="_blank" rel="noreferrer noopener">a</a><a href="https://www.acmicpc.net/user/parkky">rkky</a>, <a href="https://www.acmicpc.net/user/sait2000" target="_blank" rel="noreferrer noopener">sait2000</a>, <a href="https://www.acmicpc.net/user/tony9402" target="_blank" rel="noreferrer noopener">tony9402</a></li>



<li>문제를 만든 사람: <a href="https://www.acmicpc.net/user/MenOfPassion" target="_blank" rel="noreferrer noopener">MenOfPassion</a></li>
</ul>



<div style="height:71px" 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/124" target="_blank" rel="noreferrer noopener">수학</a></li>



<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)"/>



<p class="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" target="_blank" rel="noreferrer noopener">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:37px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1203" height="431" src="https://lycos7560.com/wp-content/uploads/image-765.png" alt="" class="wp-image-3654" srcset="https://lycos7560.com/wp-content/uploads/image-765.png 1203w, https://lycos7560.com/wp-content/uploads/image-765-300x107.png 300w, https://lycos7560.com/wp-content/uploads/image-765-768x275.png 768w" sizes="(max-width: 1203px) 100vw, 1203px" /></figure>



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



<div style="height:34px" 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">피보나치 수와 관련된 의사 코드가 2개 주어진다. (재귀 호출 / 동적 프로그래밍) &lt;- 의사 코드를 보고 구현</p>



<p class="has-medium-font-size wp-block-paragraph">n의 피보나치 수를 구할 때  재귀 호출과 동적 프로그래밍이 실행되는 횟수를 구하는 문제</p>



<p class="has-medium-font-size wp-block-paragraph">재귀 호출을 카운트할 때 위치에 주의</p>



<div style="height:55px" 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;

int N, cntOne, cntTwo;
int arr[41];

/*
fib(n) {
	if (n = 1 or n = 2)
	then return 1;  # 코드1
	else return (fib(n - 1) + fib(n - 2));
}
*/
int FibonacciRecursive(int N) // 재귀 호출 방법
{
	if (N == 1 || N == 2) { 
		cntOne++; // 카운트
		return 1; 
	}
	else { 
		return FibonacciRecursive(N - 1) + FibonacciRecursive(N - 2); 
	}
}

/*
fibonacci(n) {
	f[1] &lt;- f[2] &lt;- 1;
	for i &lt;- 3 to n
		f[i] &lt;- f[i - 1] + f[i - 2];  # 코드2
	return f[n];
}
*/
int FibonacciDP(int N) // 동적 프로그래밍 방법
{
	arr[1] = arr[2] = 1;
	for (int i = 3; i &lt;= N; i++) {
		cntTwo++; // 카운트
		arr[i] = arr[i - 1] + arr[i - 2];
	}

	return arr[N];
}


int main()
{
	cin >> N;
	cntOne = 0;
	cntTwo = 0;
	FibonacciRecursive(N); // 재귀
	FibonacciDP(N); // 동적 프로그래밍
	cout &lt;&lt; cntOne &lt;&lt; " " &lt;&lt; cntTwo;
	return 0;
}</pre>



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



<figure class="wp-block-image size-full"><img decoding="async" width="1025" height="99" src="https://lycos7560.com/wp-content/uploads/image-766.png" alt="" class="wp-image-3656" srcset="https://lycos7560.com/wp-content/uploads/image-766.png 1025w, https://lycos7560.com/wp-content/uploads/image-766-300x29.png 300w, https://lycos7560.com/wp-content/uploads/image-766-768x74.png 768w" sizes="(max-width: 1025px) 100vw, 1025px" /></figure>



<div style="height:100px" 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/baekjoon-24416-fibonacci-1-c/3652/">백준 24416번 (알고리즘 수업 &#8211; 피보나치 수 1, 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/baekjoon-24416-fibonacci-1-c/3652/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
