Problem Statement
In this problem we deal with binary representations of integers. For clarity, decimal numbers will be given as numbers and their binary representations as strings. For example, the binary representation of 47 is "101111".
Additionally, in the entire problem statement, k is a positive integer constant. (The value of k will be given to you as one of the inputs.)
Let A be a positive integer. Consider the following process:
- Find the string S that is the binary representation of A.
- Let L = max( 1, length(S)-k ).
- Let T be any (not necessarily contiguous) subsequence of S such that the length of T is between 1 and L, inclusive.
- Convert T to decimal and output the result.
Each integer B that can be the result of the above process is called a binary substring of A. Note that S never starts with a leading zero, but T might. Some examples are shown below.
-
Let A = 10 and k = 1.
- We convert A to S = "1010".
- Then we compute that L = 3, which means that we are interested in subsequences of length at most 3.
- Hence, T is one of "0", "1", "00", "01", "10", "11", "010", "100", "101", or "110".
- This means that the binary substrings of A are 0, 1, 2, 3, 4, 5, and 6.
- Let A = 10 and k = 2. Now we have L = 2, which means that the possible values of T are "0", "1", "00", "01", "10", and "11". Thus, the binary substrings of A are 0, 1, 2, and 3.
- For A = 10 and k >= 3 the only binary substrings of A are the integers 0 and 1.
- For A = 15 and k = 1 the binary substrings of A are the integers 1, 3, and 7.
You are given the
Compute and return the number of such integers.
Definition
- Class:
- PrimeStrings
- Method:
- getCount
- Parameters:
- long, long, int
- Returns:
- long
- Method signature:
- long getCount(long x, long y, int k)
- (be sure your method is public)
Constraints
- x will be between 1 and 1012, both inclusive.
- y will be between 1 and 1012, both inclusive.
- k will be between 1 and 40, both inclusive.
Examples
2
1
1
Returns: 2
Given that k=1, the only binary substring of 1 is 1, and the binary substrings of 2 are 0 and 1. In both cases, the largest binary substring is less than or equal to y=1. Thus, the answer is 2.
6
2
1
Returns: 4
As x=6, we are interested in the numbers between 1 and 6, inclusive. For k=1, the largest binary substrings of 1, 2, 3, 4, 5, and 6 are 1, 1, 1, 2, 3, and 3, respectively. As we only count those for which the largest binary substring does not exceed 2, the answer is 4.
6
1
3
Returns: 6
31
6
2
Returns: 20
413
34
2
Returns: 130
1000000000
1000000000
5
Returns: 1000000000
549755813602
8369864093
5
Returns: 178429547459
1
1
1
Returns: 1
1
1
2
Returns: 1
2
1
1
Returns: 2
2
1
2
Returns: 2
2
2
1
Returns: 2
2
2
2
Returns: 2
1000000000000
8589934591
5
Returns: 274877906943
1000000000000
8589934591
6
Returns: 549755813887
1000000000000
8589934591
7
Returns: 1000000000000
1000000000000
8589934591
8
Returns: 1000000000000
999999999999
8589934591
5
Returns: 274877906943
999999999999
8589934591
6
Returns: 549755813887
999999999999
8589934591
7
Returns: 999999999999
999999999999
8589934591
8
Returns: 999999999999
999999999998
8589934591
5
Returns: 274877906943
999999999998
8589934591
6
Returns: 549755813887
999999999998
8589934591
7
Returns: 999999999998
999999999998
8589934591
8
Returns: 999999999998
461676937380
654316600
8
Returns: 137556399160
908109542027
117401560
10
Returns: 68937152082
608255817586
51239042
9
Returns: 17205720349
129585098534
202002738
2
Returns: 606008216
98423914896
1167807506
2
Returns: 4389032978
676759240994
1997549437
5
Returns: 38995748181
159114673004
1744932506
2
Returns: 5234797520
915664659517
1873660725
3
Returns: 10178997463
125133074187
443378842
1
Returns: 752539957
34359738199
8149542948
1
Returns: 14881431699
34359738199
4095361993
2
Returns: 13573203427
34359738199
2127828527
3
Returns: 14837668479
34359738199
509506477
4
Returns: 5484695753
34359738199
225991385
5
Returns: 4510064923
34359738199
8116
20
Returns: 4299738513
34359738199
4039
21
Returns: 4296639263
34359738199
2001
22
Returns: 4295425323
34359738199
958
23
Returns: 4294989523
34359738199
436
24
Returns: 4294968748
34359738199
202
25
Returns: 4294967645
68719476128
16247854512
1
Returns: 29557937859
68719476128
7666944982
2
Returns: 23453091593
68719476128
3605736099
3
Returns: 19791653519
68719476128
1499836572
4
Returns: 17605963932
68719476128
232745066
5
Returns: 4550587009
68719476128
16376
20
Returns: 8803692302
68719476128
8142
21
Returns: 8600876117
68719476128
4023
22
Returns: 8591149647
68719476128
2030
23
Returns: 8592181847
68719476128
1015
24
Returns: 8591330087
68719476128
431
25
Returns: 8589935967
137438953079
33996879255
1
Returns: 66005451151
137438953079
16905404183
2
Returns: 60844405351
137438953079
7838853003
3
Returns: 44028791671
137438953079
3809615883
4
Returns: 38890627251
137438953079
1907209944
5
Returns: 37098618828
137438953079
32704
20
Returns: 17290703240
137438953079
16336
21
Returns: 17231945255
137438953079
8191
22
Returns: 34359738367
137438953079
4018
23
Returns: 17181187145
137438953079
1951
24
Returns: 17180011295
137438953079
949
25
Returns: 17179891721
274877905969
68635488867
1
Returns: 136548844931
274877905969
34083070889
2
Returns: 127333969137
274877905969
17040301928
3
Returns: 119691075319
274877905969
8375102454
4
Returns: 97436206473
274877905969
3785967068
5
Returns: 73599728416
274877905969
65469
20
Returns: 34767892781
274877905969
32730
21
Returns: 34615299626
274877905969
16298
22
Returns: 34391610251
274877905969
8106
23
Returns: 34367056631
274877905969
4023
24
Returns: 34361356391
274877905969
1980
25
Returns: 34359990299
549755813602
137102493885
1
Returns: 271649359343
549755813602
68297629430
2
Returns: 258234344195
549755813602
34281391361
3
Returns: 257960073455
549755813602
16861002788
4
Returns: 202628264477
549755813602
8369864093
5
Returns: 178429547459
549755813602
131015
20
Returns: 70429538423
549755813602
65477
21
Returns: 69359363603
549755813602
32741
22
Returns: 69207187553
549755813602
16309
23
Returns: 68763892103
549755813602
8157
24
Returns: 68748453953
549755813602
4045
25
Returns: 68723515403
4
1
1
Returns: 3
8
5
1
Returns: 8
641381782850
2863311530
5
Returns: 69435304618
641381782850
2863311530
6
Returns: 138154781354
641381782850
2863311530
7
Returns: 275593734826
641381782850
2863311530
8
Returns: 550471641770
641381782850
2863311530
9
Returns: 641381782850
641381782850
2863311530
38
Returns: 641381782850
641381782850
2863311530
39
Returns: 641381782850
641381782850
2863311530
40
Returns: 641381782850
999999999963
9999999927
4
Returns: 138849018807
100000000000
100000000000
4
Returns: 100000000000
981248851958
809203166
10
Returns: 550067114132