Skip to content

Commit bdcaacd

Browse files
authored
[20250214] BOJ / G3 / 행성 X3 / 신동윤
1 parent b8460c1 commit bdcaacd

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
```java
2+
import java.io.*;
3+
import java.math.BigInteger;
4+
5+
public class Main {
6+
7+
static int MAX_IDX = 20; // 2의 20승이 1,000,000과 가장 가까움
8+
9+
public static void main(String[] args) throws IOException {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
12+
13+
int N = Integer.parseInt(br.readLine());
14+
int[][] arr = new int[N][MAX_IDX]; // N번째 숫자 이진수로 변환한 결과를 담는다. arr[i][j] = i번째 숫자의 j번째 비트
15+
16+
for (int i = 0; i < N; i++) {
17+
String binary = Integer.toBinaryString(Integer.parseInt(br.readLine()));
18+
// binary 문자열의 인덱스 역순으로 arr[i]에 담아준다.
19+
int binaryLength = binary.length();
20+
for (int j = 0; j < binaryLength; j++) {
21+
arr[i][j] = binary.charAt(binaryLength - j - 1) - '0';
22+
}
23+
}
24+
25+
BigInteger answer = BigInteger.ZERO;
26+
// 2^j + (j열에 있는 모든 1의 개수 * j열에 있는 모든 0의 개수)
27+
for (int j = 0; j < MAX_IDX; j++) {
28+
// j열에 있는 모든 1의 개수
29+
int cntOne = 0;
30+
// j열에 있는 모든 0의 개수
31+
int cntZero = 0;
32+
for (int i = 0; i < N; i++) {
33+
if (arr[i][j] == 1) {
34+
cntOne++;
35+
} else {
36+
cntZero++;
37+
}
38+
}
39+
BigInteger tmp1 = BigInteger.valueOf((long) Math.pow(2, j));
40+
BigInteger tmp2 = BigInteger.valueOf((long) cntOne * cntZero);
41+
BigInteger result = tmp1.multiply(tmp2);
42+
answer = answer.add(result);
43+
}
44+
45+
bw.write(answer + "\n");
46+
47+
br.close();
48+
bw.flush();
49+
bw.close();
50+
}
51+
}
52+
```

0 commit comments

Comments
 (0)