[๋ฐฑ์ค(BOJ)] 9461๋ฒ: ํ๋๋ฐ ์์ด (Dynamic Programming, C++)
P(n) = 1 (0 <= n < 3)
P(n) = P(n-2) + P(n-3) (n >= 3)
0 ์ด์ 2 ์ดํ์ ์์ ๋ํด์๋ 1์ ๊ฐ์ ๊ฐ๊ณ , 3 ์ด์๋ถํฐ๋ ์ ํ์ F(n) = F(n-2) + F(n-3) ์ด ๋๋ค.
n = 3 P(3) = P(1) + P(0) = 1 + 1 = 2
n = 4 P(4) = P(2) + P(1) = 1 + 1 = 2
n = 5 P(5) = P(3) + P(2) = 2 + 1 = 3
n = 6 P(6) = P(4) + P(3) = 2 + 2 = 4
n = 7 P(7) = P(5) + P(4) = 3 + 2 = 5
n = 8 P(8) = P(6) + P(5) = 4 + 3 = 7
n = 9 P(9) = P(7) + P(6) = 5 + 4 = 9
n = 10 P(10) = P(8) + P(7) = 7 + 5 = 12
...
์ฃผ์ํ ์ ์ด, N์ด 100๊ฐ์ด ์ปค์ง๋ฉด integer ๋ฒ์๋ฅผ ๋ฒ์ด๋๊ธฐ ๋๋ฌธ์ python ๊ฐ์ด integer ๋ฒ์๊ฐ ํฌ์ง ์์ C, C++ ๋ฑ์ ๋ฒ์๋ฅผ ์ ๊ฒฝ์จ์ค์ผ ํ๋ค. long ํ์ผ๋ก ์์ด P์ ํ์ ๋ฐ๊ฟ์ฃผ๊ณ , ์ถ๋ ฅ ์ %d๊ฐ ์๋ %ld๋ฅผ ์ฌ์ฉํ๋ฉด ํด๊ฒฐํ ์ ์๋ค.
#include <stdio.h>
#include <deque>
using namespace std;
int main(){
int T;
scanf("%d", &T);
for(int i=0; i<T; i++){
int N;
scanf("%d", &N);
deque<long long> P(N, 0);
for(int i=0; i<3; i++){
P[i] = 1;
}
for(int i=3; i<N; i++){
P[i] = P[i-2] + P[i-3];
}
printf("%ld\n", P[N-1]);
}
}