From 51a4f1152862778b410f0c62cb7cc16be19f1437 Mon Sep 17 00:00:00 2001 From: LiiNi-coder <97495437+LiiNi-coder@users.noreply.github.com> Date: Sat, 2 Aug 2025 23:43:07 +0900 Subject: [PATCH] =?UTF-8?q?[20250802]=20BOJ=20/=20G5=20/=201=ED=95=99?= =?UTF-8?q?=EB=85=84=20/=20=EC=9D=B4=EC=9D=B8=ED=9D=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../02 BOJ 1\355\225\231\353\205\204.md" | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 "LiiNi-coder/202508/02 BOJ 1\355\225\231\353\205\204.md" diff --git "a/LiiNi-coder/202508/02 BOJ 1\355\225\231\353\205\204.md" "b/LiiNi-coder/202508/02 BOJ 1\355\225\231\353\205\204.md" new file mode 100644 index 00000000..ad0870d9 --- /dev/null +++ "b/LiiNi-coder/202508/02 BOJ 1\355\225\231\353\205\204.md" @@ -0,0 +1,45 @@ +```java +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class Main { + + static int N; + static int[] A; + static long[][] dp; // [index][sum], sum은 0이상 20미만 + static long dfs(int idx, int result) { + if (result < 0 || result > 20) return 0; + if (idx == 0) { + // idx==0이면 a = a 인것 1개의 경우의수 아니면 a = b형태라 0의 경우의수 + return (result == A[0])? 1 : 0; + } + if (dp[idx][result] != -1) return dp[idx][result]; + + long count = 0; // 경우의수 + count += dfs(idx - 1, result + A[idx]); + count += dfs(idx - 1, result - A[idx]); + dp[idx][result] = count; + return count; + } + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + A = new int[N]; + StringTokenizer st = new StringTokenizer(br.readLine()); + for (int i = 0; i < N; i++) { + A[i] = Integer.parseInt(st.nextToken()); + } + + dp = new long[N][21]; // 인덱스: 0~N-1, sum: 0~20 + for (int i = 0; i < N; i++) { + for (int j = 0; j <= 20; j++) { + dp[i][j] = -1; + } + } + + System.out.println(dfs(N - 2, A[N - 1])); + } +} +```