Skip to content

Commit

Permalink
day3: (2) dp/11726, dp/11727
Browse files Browse the repository at this point in the history
  • Loading branch information
CheolHoJung committed Mar 26, 2019
1 parent 107a58b commit 7498c01
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
Binary file added dp/images/타일링_2곱하기n_2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions dp/타일링_2곱하기n.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package test.boj.dp;

import org.junit.*;

import java.util.Scanner;

public class 타일링_2곱하기n {

@Test
public void test() {
System.out.println(solution(1000));
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(solution(sc.nextInt()));
}

// n: 0 1 2 3 4 5 6 7 8 9 ....
// F(n): 1 1 2 3 5 8 13 21 34 55 .....
private static int solution(int n) {
return f(n);
}

private static int f(int n) {
int[] fibo = new int[n + 1];
fibo[0] = fibo[1] = 1;

for (int i = 2; i <= n; i++) {
// !!! overflow -> % 10007
fibo[i] = (fibo[i - 1] + fibo[i - 2]) % 10007;
}
return fibo[n];
}
}
43 changes: 43 additions & 0 deletions dp/타일링_2곱하기n_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package test.boj.dp;

import org.junit.*;

import java.util.Scanner;

import static org.assertj.core.api.Assertions.assertThat;

// refer images/타일링_2곱하기n_2.png

This comment has been minimized.

Copy link
@CheolHoJung

CheolHoJung Mar 26, 2019

Author Owner

refer

public class 타일링_2곱하기n_2 {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(solution(sc.nextInt()));
}

@Test
public void test() {
System.out.println(solution(1));
assertThat(solution(2))
.isEqualTo(3);

assertThat(solution(3))
.isEqualTo(5);

assertThat(solution(4))
.isEqualTo(11);

assertThat(solution(1000))
.isEqualTo(7326);
}

private static int solution(int n) {
int[] dp = new int[n + 2];
dp[1] = 1;
dp[2] = 3;

for (int i = 3; i <= n; i++) {
dp[i] = (dp[i - 1] + (dp[i - 2] * 2)) % 10_007;
}
return dp[n];
}
}

0 comments on commit 7498c01

Please sign in to comment.