CSES - Datatähti 2017 alku - Results
Submission details
Task:Bittijono
Sender:JesseNiininen
Submission time:2016-10-05 19:43:00 +0300
Language:Java
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#10.17 s1details
#21.98 s2details
#3--3details

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;
                }
                
                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:

input
100
62
9
12
73
...

correct output
1
1
1
0
1
...

user output
1
1
1
0
1
...

Test 2

Group: 2

Verdict:

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:

input
100000
374768524402011755
937067109466254318
389256426086302899
932585725667010169
...

correct output
0
1
1
1
1
...

user output
(empty)