From bdcaacd8563d4478a055f8117751eaf874aa69b7 Mon Sep 17 00:00:00 2001 From: Donew <47556347+03do-new30@users.noreply.github.com> Date: Fri, 14 Feb 2025 11:27:25 +0900 Subject: [PATCH] =?UTF-8?q?[20250214]=20BOJ=20/=20G3=20/=20=ED=96=89?= =?UTF-8?q?=EC=84=B1=20X3=20/=20=EC=8B=A0=EB=8F=99=EC=9C=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../14 BOJ G3 \355\226\211\354\204\261 X3.md" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 "03do-new30/202502/14 BOJ G3 \355\226\211\354\204\261 X3.md" diff --git "a/03do-new30/202502/14 BOJ G3 \355\226\211\354\204\261 X3.md" "b/03do-new30/202502/14 BOJ G3 \355\226\211\354\204\261 X3.md" new file mode 100644 index 00000000..243e285d --- /dev/null +++ "b/03do-new30/202502/14 BOJ G3 \355\226\211\354\204\261 X3.md" @@ -0,0 +1,52 @@ +```java +import java.io.*; +import java.math.BigInteger; + +public class Main { + + static int MAX_IDX = 20; // 2의 20승이 1,000,000과 가장 가까움 + + 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()); + int[][] arr = new int[N][MAX_IDX]; // N번째 숫자 이진수로 변환한 결과를 담는다. arr[i][j] = i번째 숫자의 j번째 비트 + + for (int i = 0; i < N; i++) { + String binary = Integer.toBinaryString(Integer.parseInt(br.readLine())); + // binary 문자열의 인덱스 역순으로 arr[i]에 담아준다. + int binaryLength = binary.length(); + for (int j = 0; j < binaryLength; j++) { + arr[i][j] = binary.charAt(binaryLength - j - 1) - '0'; + } + } + + BigInteger answer = BigInteger.ZERO; + // 2^j + (j열에 있는 모든 1의 개수 * j열에 있는 모든 0의 개수) + for (int j = 0; j < MAX_IDX; j++) { + // j열에 있는 모든 1의 개수 + int cntOne = 0; + // j열에 있는 모든 0의 개수 + int cntZero = 0; + for (int i = 0; i < N; i++) { + if (arr[i][j] == 1) { + cntOne++; + } else { + cntZero++; + } + } + BigInteger tmp1 = BigInteger.valueOf((long) Math.pow(2, j)); + BigInteger tmp2 = BigInteger.valueOf((long) cntOne * cntZero); + BigInteger result = tmp1.multiply(tmp2); + answer = answer.add(result); + } + + bw.write(answer + "\n"); + + br.close(); + bw.flush(); + bw.close(); + } +} +```