Task: | Bittijono |
Sender: | JesseNiininen |
Submission time: | 2016-10-05 19:46:42 +0300 |
Language: | Java |
Status: | READY |
Result: | 0 |
group | verdict | score |
---|---|---|
#1 | WRONG ANSWER | 0 |
#2 | WRONG ANSWER | 0 |
#3 | TIME LIMIT EXCEEDED | 0 |
test | verdict | time | group | |
---|---|---|---|---|
#1 | WRONG ANSWER | 0.15 s | 1 | details |
#2 | WRONG ANSWER | 1.80 s | 2 | details |
#3 | TIME LIMIT EXCEEDED | -- | 3 | details |
Code
import java.math.BigInteger; import java.util.HashMap; import java.util.Scanner; public class Bittijono { private static BigInteger[] powerOf2 = new BigInteger[61]; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = Integer.parseInt(scanner.nextLine()); powerOf2[0] = BigInteger.valueOf(1); powerOf2[1] = BigInteger.valueOf(2); for (int i = 2; i < powerOf2.length; i++) { powerOf2[i] = powerOf2[i - 1].multiply(powerOf2[1]); } HashMap<BigInteger, Boolean> answers = new HashMap<>(); while (n-- > 0) { BigInteger k = new BigInteger(scanner.nextLine()); BigInteger originalK = k; int power = upperPowerOf2(k); BigInteger maxIndex = powerOf2[power]; boolean guess = true; while (!k.equals(BigInteger.ONE)) { Boolean answer = answers.get(k); if(answer != null){ guess = !answer; break; } BigInteger halfMaxIndex = maxIndex.divide(powerOf2[1]); if (k.compareTo(halfMaxIndex) > 0) { guess = !guess; k = k.subtract(maxIndex.divide(powerOf2[1])); } maxIndex = halfMaxIndex; } if (guess) { answers.put(originalK, false); System.out.println(0); } else { answers.put(originalK, true); System.out.println(1); } } } private static int upperPowerOf2(BigInteger num) { int low = 0; int high = powerOf2.length - 1; while (true) { int middle = (low + high) / 2; if (powerOf2[low].compareTo(num) >= 0) { return low; } if (powerOf2[middle].compareTo(num) == 0) { return middle; } if (powerOf2[middle].compareTo(num) < 0) { if (middle + 1 <= high && powerOf2[middle + 1].compareTo(num) >= 0) { return middle + 1; } else { low = middle + 1; } } else if (middle - 1 >= low && powerOf2[middle - 1].compareTo(num) < 0) { return middle; } else { high = middle - 1; } } } }
Test details
Test 1
Group: 1
Verdict: WRONG ANSWER
input |
---|
100 62 9 12 73 ... |
correct output |
---|
1 1 1 0 1 ... |
user output |
---|
1 1 1 1 1 ... |
Test 2
Group: 2
Verdict: WRONG ANSWER
input |
---|
100000 565433 141881 120108 825392 ... |
correct output |
---|
1 1 0 0 1 ... |
user output |
---|
1 1 0 0 1 ... |
Test 3
Group: 3
Verdict: TIME LIMIT EXCEEDED
input |
---|
100000 374768524402011755 937067109466254318 389256426086302899 932585725667010169 ... |
correct output |
---|
0 1 1 1 1 ... |
user output |
---|
(empty) |