-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b1aa4d
commit c0923ee
Showing
3 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package test.boj.dp; | ||
|
||
import org.junit.*; | ||
|
||
import java.io.*; | ||
|
||
// https://mizzo-dev.tistory.com/entry/baekjoon2133 참고 | ||
public class q_타일_채우기 { | ||
|
||
@Test | ||
public void test() { | ||
System.out.println(solution(3)); | ||
System.out.println(solution(4)); | ||
System.out.println(solution(5)); | ||
System.out.println(solution(6)); | ||
System.out.println(solution(30)); | ||
} | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); | ||
|
||
int N = Integer.parseInt(br.readLine()); | ||
bw.write(String.valueOf(solution(N))); | ||
|
||
br.close(); | ||
bw.flush(); | ||
bw.close(); | ||
} | ||
|
||
// dp[2] = 3 | ||
// dp[2]는 고유조합이 없으므로 3을 사용 | ||
// 나머지는 고유조합이 2개씩 존재 | ||
|
||
// dp[4] = dp[2] dp[2] = dp[2] * dp[2] = 9 | ||
// dp[4]의 고유 조합 = 2 | ||
// 9 + 2 = 11 | ||
|
||
// 1. dp[4]를 치환해보자. | ||
// dp[2] dp[4] = dp[2] (dp[2] dp[2]) | ||
// dp[2] (dp[4]의 고유 조합) | ||
// dp[2] (dp[2] dp[2])에서 dp[2]를 괄호 뒤로 뗴어 붙일 수가 없다 | ||
// 반면, dp[2] (dp[4]의 고유 조합)는 (dp[4]의 고유 조합) dp[2]처럼 괄호 뒤로 뗴어 붙일 수가 있다. | ||
|
||
// dp[6]으로 돌아와서 | ||
// dp[6] = dp[2] dp[4] | ||
// dp[6]의 고유 조합 | ||
// 1.에서 (dp[4]의 고유 조합)이 뒤에 한번밖에 사용되지 않았다. 앞에도 사용하자. | ||
|
||
// dp[6] = dp[2] dp[2] dp[2] | ||
// dp[2] (dp[4]의 고유 조합) | ||
// (dp[4]의 고유 조합) dp[2] | ||
// dp[6]의 고유 조합 | ||
// | ||
// dp[2] dp[2] dp[2]과 dp[2] (dp[4]의 고유 조합)은 dp[2] dp[4]이므로 dp[6]을 다시 정리하면 다음과 같다. | ||
// dp[2]는 (dp[2]의 고유조합)과 같다 | ||
// dp[6] = dp[2] dp[4] = 3 * 11 = 33 | ||
// (dp[4]의 고유 조합) dp[2] = 2 * 3 = 6 | ||
// (dp[6]의 고유 조합) = 2 | ||
// 33 + 6 + 2 = 41 | ||
public static int solution(int N) { | ||
if (N < 2) return 0; | ||
|
||
int[] dp = new int[N + 1]; | ||
// dp[i - j]에서 dp[0]에 접근하기 때문에 1로 설정 | ||
dp[0] = 1; | ||
dp[2] = 3; | ||
|
||
for (int i = 4; i <= N; i+=2) { | ||
for (int j = 2; j <= i; j += 2) { | ||
int std = j == 2 ? 3 : 2; | ||
dp[i] += std * dp[i - j]; | ||
} | ||
} | ||
|
||
return dp[N]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package test.boj.dp; | ||
|
||
import org.junit.*; | ||
|
||
import java.io.*; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class r_파도반_수열 { | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); | ||
|
||
int tCase = Integer.parseInt(br.readLine()); | ||
|
||
for (int i = 0; i < tCase; i++) { | ||
int N = Integer.parseInt(br.readLine()); | ||
bw.write(String.valueOf(solution(N)) + "\n"); | ||
} | ||
|
||
br.close(); | ||
bw.flush(); | ||
bw.close(); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
assertThat(solution(1)) | ||
.isEqualTo(1L); | ||
assertThat(solution(2)) | ||
.isEqualTo(1L); | ||
assertThat(solution(12)) | ||
.isEqualTo(16L); | ||
assertThat(solution(100)) | ||
.isEqualTo(888855064897L); | ||
} | ||
|
||
|
||
// i 1 2 3 4 5 6 7 8 9 10 11 | ||
// dp[i] 1 1 1 2 2 3 4 5 7 9 12 | ||
// 1 1 1 2 2 2 + 1 3 + 1 4 + 1 5 + 2 7 + 2 9 + 3 | ||
// 1 1 1 2 2 dp[5] + dp[1..3] dp[6] + dp[1..3] dp[7] + dp[1..3] dp[8] + dp[4..5] dp[9] + dp[4..5] dp[10] + dp[6] | ||
// -> dp[i - 1] + dp[i - 5] (i > 5) | ||
private static long solution(int N) { | ||
long[] dp = new long[101]; | ||
dp[1] = 1; | ||
dp[2] = 1; | ||
dp[3] = 1; | ||
dp[4] = 2; | ||
dp[5] = 2; | ||
|
||
for (int i = 6; i <= N; i++) { | ||
dp[i] = dp[i - 1] + dp[i - 5]; | ||
} | ||
|
||
return dp[N]; | ||
} | ||
} |