-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathSolution.java
55 lines (43 loc) · 1.04 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
/**
5+0=5x0=5
5+2=5x2=7
101 = 2^2 + 2^0
000 = 0
101 = 5
101 010 = 111 = 7
101 001 = 4
101 011 = 110 = 6
*/
public class Solution {
// Complete the sumXor function below.
static long sumXor(long n) {
if (n == 0) {
return 1;
}
int c = 0;
String num = Long.toBinaryString(n);
for (int i = 0; i < num.length(); i++) {
if (num.charAt(i) == '0') {
c++;
}
}
return (long) Math.pow(2.0, c);
}
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
long n = Long.parseLong(bufferedReader.readLine().trim());
long result = sumXor(n);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}