Count K-Reducible Numbers Less Than N

You are given a binary string s representing a number n in its binary form.

You are also given an integer k.

An integer x is called k-reducible if performing the following operation at most k times reduces it to 1:

  • Replace x with the count of set bits in its binary representation.

Return an integer denoting the number of positive integers less than n that are k-reducible.

Since the answer may be too large, return it modulo 10^9 + 7.

Example 1
Inputs = "111", k = 1
Output3
n = 7, and the 1-reducible integers less than 7 are 1, 2, and 4.
Example 2
Inputs = "1000", k = 2
Output6
n = 8, and the 2-reducible integers less than 8 are 1, 2, 3, 4, 5, and 6.

Constraints

  • 1 <= s.length <= 800
  • s has no leading zeros.
  • s consists only of the characters '0' and '1'.
  • 1 <= k <= 5

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate