Skip to content

Commit 370afe1

Browse files
committed
[Silver III] Title: 파도반 수열, Time: 100 ms, Memory: 14088 KB -BaekjoonHub
1 parent cb3d6c5 commit 370afe1

File tree

2 files changed

+34
-36
lines changed

2 files changed

+34
-36
lines changed

백준/Silver/9461. 파도반 수열/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 18372 KB, 시간: 196 ms
7+
메모리: 14088 KB, 시간: 100 ms
88

99
### 분류
1010

1111
다이나믹 프로그래밍, 수학
1212

1313
### 제출 일자
1414

15-
2025년 5월 2일 02:49:58
15+
2025년 5월 2일 02:56:12
1616

1717
### 문제 설명
1818

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
1-
import java.util.*;
2-
1+
import java.io.BufferedReader;
2+
import java.io.InputStreamReader;
3+
import java.io.IOException;
4+
35
public class Main {
4-
static long[] DP = new long[101];
5-
6-
public static void main(String[] args) {
7-
Scanner sc = new Scanner(System.in);
8-
StringBuilder sb = new StringBuilder();
9-
10-
DP[1] = 1;
11-
DP[2] = 1;
12-
DP[3] = 1;
13-
DP[4] = 2;
14-
DP[5] = 2;
15-
16-
int T = sc.nextInt();
17-
18-
// DP 배열을 한 번만 계산
19-
for(int i=6; i<=100; i++){
20-
DP[i] = DP[i-1] + DP[i-5]; // long 타입으로 계산
21-
}
22-
23-
for(int i=0; i<T; i++){
24-
int N = sc.nextInt();
25-
26-
// for(int j=1; j<=N; j++){
27-
// if(DP[j] == 0){
28-
// DP[j] = DP[j-1] + DP[j-5];
29-
// }
30-
// }
31-
sb.append(DP[N] + "\n");
32-
}
33-
34-
System.out.println(sb);
35-
}
6+
7+
public static Long[] seq = new Long[101];
8+
9+
public static void main(String[] args) throws IOException {
10+
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
StringBuilder sb = new StringBuilder();
13+
14+
seq[0] = 0L;
15+
seq[1] = 1L;
16+
seq[2] = 1L;
17+
seq[3] = 1L;
18+
19+
int T = Integer.parseInt(br.readLine());
20+
21+
while(T-->0) {
22+
sb.append(padovan(Integer.parseInt(br.readLine()))).append('\n');
23+
}
24+
System.out.println(sb);
25+
}
26+
27+
public static long padovan(int N) {
28+
if(seq[N] == null) {
29+
seq[N] = padovan(N - 2) + padovan(N - 3);
30+
}
31+
return seq[N];
32+
}
33+
3634
}

0 commit comments

Comments
 (0)