For a number X, let its "Coolness" be defined as the number of "101"s occurring in its…
- User
For a number X, let its "Coolness" be defined as the number of "101"s occurring in its binary representation. For example, the number 21 has Coolness 2, since its binary representation is 101012, and the string "101" occurs twice in this representation.
A number is defined as Very Cool if its Coolness is greater than or equal to K. Please, output the number of Very Cool integers between 1 and R. Input: The first line contains an integer T, the number of test cases. The next T lines contains two space-separated integers, R and K.
Output: Output T lines, the answer for each test case.
Constraints: 1 ≤ T ≤ 100
1 ≤ R ≤ 10^5 1 ≤ K ≤ 100
SAMPLE INPUT 1 5 1
SAMPLE OUTPUT 1
Assistant
pythondef precompute_coolness_and_prefix(): max_num = 10**5 max_k = 100 # Compute coolness for each number coolness = [0] * (max_num + 1) for n in range(1, max_num + 1): binary = bin(n)[2:] count = 0 for i in range(len(binary) - 2): if binary[i:i+3] == '101': count += 1 coolness[n] = count # Precompute prefix sums for each K prefix = [[0] * (max_num + 1) for _ in range(max_k + 1)] for k in range(1, max_k + 1): current_count = 0 for r in range(1, max_num + 1): if coolness[r] >= k: current_count += 1 prefix[k][r] = current_count return prefix # Precompute data once prefix = precompute_coolness_and_prefix() # Read input and process test cases import sys input = sys.stdin.read().split() T = int(input[0]) index = 1 for _ in range(T): R = int(input[index]) K = int(input[index + 1]) index += 2 if K > 100: print(0) else: print(prefix[K][R])
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.