CSES - Datatähti 2020 alku - Results
Submission details
Task:Lukuvälit
Sender:onnari11
Submission time:2019-10-03 20:03:02 +0300
Language:Java
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#10.17 s1, 2, 3details
#2ACCEPTED0.12 s1, 2, 3details
#3ACCEPTED0.12 s1, 2, 3details
#40.18 s1, 2, 3details
#5ACCEPTED0.12 s1, 2, 3details
#6ACCEPTED0.23 s1, 2, 3details
#7ACCEPTED0.23 s1, 2, 3details
#80.30 s1, 2, 3details
#90.30 s1, 2, 3details
#10ACCEPTED0.23 s1, 2, 3details
#110.33 s1, 2, 3details
#12ACCEPTED0.13 s1, 2, 3details
#13--2, 3details
#14--2, 3details
#15--2, 3details
#16--2, 3details
#17--2, 3details
#18--2, 3details
#190.17 s2, 3details
#20ACCEPTED0.12 s2, 3details
#21--3details
#22--3details
#23--3details
#24--3details
#25--3details
#26--3details
#270.17 s3details
#28ACCEPTED0.12 s3details

Code

import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
import java.lang.Math;

class lukuvalit {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = Integer.parseInt(input.nextLine());

        List<String[]> numPairs = new ArrayList<String[]>();

        for (int i = 0; i < n; i++) {
            String numbers = input.nextLine();
            String[] nums = numbers.split(" ");
            numPairs.add(nums);
        }
        input.close();

        for (String[] ab : numPairs) {
            long a = Long.parseLong(ab[0]);
            long b = Long.parseLong(ab[1]);

            if (a == b) {
                if (isValid(Long.toString(a))) {
                    System.out.println(1);
                } else {
                    System.out.println(0);
                }
            } else {
                int output = 0;
                if (a == 0) {
                    a += 1;
                    output += 1;
                }
                long first = getStartIndex(a, b);
                long last = getEndIndex(a, b);

                if (first != -1L && last != -1L) {
                    int startBinaryVal = getBinary(Long.toString(first));
                    int endBinaryVal = getBinary(Long.toString(last));

                    int val = (endBinaryVal - startBinaryVal) + 1;
                    output += val;
                }

                System.out.println(output);
            }
        }

    }

    public static long getStartIndex(long a, long b) {
        if(isValid(Long.toString(a))){
            return a;
        }
        int exp = (int) Math.floor(Math.log10((double) a));
        long index = 0L;
        for (int i = exp; i >= 0; i--) {
            index += (long)Math.pow(10, i);
        }
        for (int i = 0; i <= exp; i++) {
            long substraction = (long) Math.pow(10, i);
            if (index - substraction >= a) {
                index = index - substraction;
            } else {
                break;
            }
        }
        if(index < a){
            index = (long)Math.pow(10, (exp+1));
        }
        System.out.println("Start index: " + index);
        if (index > b) {
            return -1L;
        }

        return index;
    }

    public static long getEndIndex(long a, long b) {
        if(isValid(Long.toString(b))){
            return b;
        }
        int exp = (int) Math.floor(Math.log10((double) b));
        long index = (long)Math.pow(10, exp);
        for (int i = 0; i < exp; i++) {
            long addition = (long) Math.pow(10, i);
            if (index + addition <= b) {
                index += addition;
            } else {
                break;
            }
        }
        System.out.println("End index: "+index);
        if (index > b || index < a) {
            return -1L;
        }
        return index;
    }

   

    public static boolean isValid(String num) {
        boolean output = true;
        for (int i = 0; i < num.length(); i++) {
            char chr = num.charAt(i);
            if (chr != '1' && chr != '0') {
                output = false;
                break;
            }
        }
        return output;
    }

    public static int getBinary(String input) {
        int val = Integer.parseInt(input, 2);
        return val;

    }
}

Test details

Test 1

Group: 1, 2, 3

Verdict:

input
4
0 10
1 5
3 4
11 11

correct output
3
1
0
1

user output
3
End index: 1
1
Start index: 10
End index: 1
...

Test 2

Group: 1, 2, 3

Verdict: ACCEPTED

input
1
0 0

correct output
1

user output
1

Test 3

Group: 1, 2, 3

Verdict: ACCEPTED

input
1
1000 1000

correct output
1

user output
1

Test 4

Group: 1, 2, 3

Verdict:

input
16
0 0
0 1
0 2
1 1
...

correct output
1
2
2
1
1
...

user output
1
2
End index: 1
2
1
...
Truncated

Test 5

Group: 1, 2, 3

Verdict: ACCEPTED

input
9
0 0
0 1
0 10
0 11
...

correct output
1
2
3
4
5
...

user output
1
2
3
4
5
...

Test 6

Group: 1, 2, 3

Verdict: ACCEPTED

input
1000
0 1000
0 1000
0 1000
0 1000
...

correct output
9
9
9
9
9
...

user output
9
9
9
9
9
...
Truncated

Test 7

Group: 1, 2, 3

Verdict: ACCEPTED

input
1000
100 111
0 101
1 10
10 110
...

correct output
4
6
2
5
8
...

user output
4
6
2
5
8
...
Truncated

Test 8

Group: 1, 2, 3

Verdict:

input
1000
480 564
753 988
479 909
32 973
...

correct output
0
0
0
4
0
...

user output
Start index: 1000
End index: 111
0
Start index: 1000
End index: 111
...
Truncated

Test 9

Group: 1, 2, 3

Verdict:

input
1000
98 111
8 111
98 111
111 113
...

correct output
4
6
4
1
7
...

user output
Start index: 100
4
Start index: 10
6
Start index: 100
...
Truncated

Test 10

Group: 1, 2, 3

Verdict: ACCEPTED

input
1000
0 0
1 1
2 2
3 3
...

correct output
1
1
0
0
0
...

user output
1
1
0
0
0
...
Truncated

Test 11

Group: 1, 2, 3

Verdict:

input
1000
0 1000
0 999
1 1000
0 998
...

correct output
9
8
8
8
7
...

user output
9
End index: 111
8
8
End index: 111
...
Truncated

Test 12

Group: 1, 2, 3

Verdict: ACCEPTED

input
1
0 1000

correct output
9

user output
9

Test 13

Group: 2, 3

Verdict:

input
100000
0 100000
0 100000
0 100000
0 100000
...

correct output
33
33
33
33
33
...

user output
(empty)

Test 14

Group: 2, 3

Verdict:

input
100000
86042 98295
30077 80920
45856 67174
3890 60412
...

correct output
0
0
0
16
0
...

user output
(empty)

Test 15

Group: 2, 3

Verdict:

input
100000
11 10000
1111 11000
1011 1100
1110 1111
...

correct output
14
10
2
2
8
...

user output
(empty)

Test 16

Group: 2, 3

Verdict:

input
100000
9 9999
1109 1110
112 1012
11098 11101
...

correct output
14
1
4
2
6
...

user output
(empty)

Test 17

Group: 2, 3

Verdict:

input
100000
0 0
1 1
2 2
3 3
...

correct output
1
1
0
0
0
...

user output
(empty)

Test 18

Group: 2, 3

Verdict:

input
100000
0 100000
0 99999
1 100000
0 99998
...

correct output
33
32
32
32
31
...

user output
(empty)

Test 19

Group: 2, 3

Verdict:

input
3
99999 99999
99999 100000
100000 100000

correct output
0
1
1

user output
0
Start index: 100000
1
1

Test 20

Group: 2, 3

Verdict: ACCEPTED

input
1
0 100000

correct output
33

user output
33

Test 21

Group: 3

Verdict:

input
100000
0 1000000000000000000
0 1000000000000000000
0 1000000000000000000
0 1000000000000000000
...

correct output
262145
262145
262145
262145
262145
...

user output
(empty)

Test 22

Group: 3

Verdict:

input
100000
179926689319432205 25677963778...

correct output
0
0
0
0
0
...

user output
(empty)

Test 23

Group: 3

Verdict:

input
100000
100110100011010101 11100011010...

correct output
74822
54944
140968
252594
23521
...

user output
(empty)

Test 24

Group: 3

Verdict:

input
100000
100110100011010102 11100011010...

correct output
74822
252594
94086
10836
11352
...

user output
(empty)

Test 25

Group: 3

Verdict:

input
100000
256779637786129463 25677963778...

correct output
0
0
0
0
0
...

user output
(empty)

Test 26

Group: 3

Verdict:

input
100000
0 1000000000000000000
0 999999999999999999
1 1000000000000000000
0 999999999999999998
...

correct output
262145
262144
262144
262144
262143
...

user output
(empty)

Test 27

Group: 3

Verdict:

input
3
999999999999999999 99999999999...

correct output
0
1
1

user output
0
Start index: 10000000000000000...

Test 28

Group: 3

Verdict: ACCEPTED

input
1
0 1000000000000000000

correct output
262145

user output
262145