Connection Status:
Competition Arena > PrimeStrings
2016 TCO Algo 1C · 2016-03-24 · by praveen123 · Brute Force
Class Name: PrimeStrings
Return Type: long
Method Name: getCount
Arg Types: (long long, long long, int)
Problem Statement

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:

  1. Find the string S that is the binary representation of A.
  2. Let L = max( 1, length(S)-k ).
  3. Let T be any (not necessarily contiguous) subsequence of S such that the length of T is between 1 and L, inclusive.
  4. 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.
    1. We convert A to S = "1010".
    2. Then we compute that L = 3, which means that we are interested in subsequences of length at most 3.
    3. Hence, T is one of "0", "1", "00", "01", "10", "11", "010", "100", "101", or "110".
    4. 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 int k. You are also given longs x and y. Consider the integers between 1 and x, inclusive, such that their largest binary substring is smaller than or equal to y.

Compute and return the number of such integers.

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
0)
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.

1)
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.

2)
6
1
3
Returns: 6
3)
31
6
2
Returns: 20
4)
413
34
2
Returns: 130

Submissions are judged against all 102 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class PrimeStrings with a public method long long getCount(long long x, long long y, int k) · 102 test cases · 2 s / 256 MB per case

Submitting as anonymous