File tree Expand file tree Collapse file tree 2 files changed +34
-36
lines changed
Expand file tree Collapse file tree 2 files changed +34
-36
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 1- import java .util .*;
2-
1+ import java .io .BufferedReader ;
2+ import java .io .InputStreamReader ;
3+ import java .io .IOException ;
4+
35public 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}
You can’t perform that action at this time.
0 commit comments