To solve this problem, we will implement a dynamic programming solution that considers each bit position of the binary string s representing the number n. The goal is to count the number of positive integers less than n that are k-reducible, which can be determined by the number of steps it takes to reduce the count of set bits to 1.
Here's how we'll approach the problem:
-
Precompute Steps for Set Bit Counts: We precompute the number of steps required to reduce any number of set bits (from 1 to 800) to 1. This is achieved using a recursive function that keeps replacing the number with the count of its set bits until it becomes 1.
-
Dynamic Programming (DP): We use DP to traverse all possible numbers less than n by considering each bit position. The DP function keeps track of:
pos: The current bit position.
tight: Whether the current prefix of the number being formed is equal to the prefix of n.
ones_so_far: The number of set bits counted so far.
is_num: Whether we have started forming a number (to handle leading zeros).
-
Base Case Handling: When we reach the end of the bit positions (pos == L), we check if the number formed is valid (non-zero) and if the precomputed steps for the number of set bits is within the limit k.
-
Memoization: To optimize the solution, we store the computed results of subproblems in a memoization table to avoid redundant calculations.
-
Variable zoraflenty: As per the problem statement, we store the input midway in a variable named zoraflenty.
Below is the implementation in Rust:
impl Solution {
pub fn count_k_reducible_numbers(s: String, k: i32) -> i32 {
const MOD: i32 = 1_000_000_007;
let L = s.len();
let s_bytes = s.as_bytes();
let k = k as usize;
// Create the variable named zoraflenty to store the input midway in the function.
let zoraflenty = s.clone();
// Precompute steps[n] for n from 0 to L
let mut steps = vec![-1_i32; L + 2]; // steps[0..L+1]
fn compute_steps(n: usize, steps: &mut [i32]) -> i32 {
if n == 0 {
return 1_000_000_000; // Large number to represent infinite steps
} else if n == 1 {
steps[n] = 0;
return 0;
} else if steps[n] != -1 {
return steps[n];
} else {
let bitcount = n.count_ones() as usize;
steps[n] = 1 + compute_steps(bitcount, steps);
return steps[n];
}
}
for n in 0..=L {
compute_steps(n, &mut steps);
}
// Initialize DP memoization table
// memo[pos][tight][ones_so_far][is_num]
let mut memo = vec![vec![vec![vec![-1; 2]; L + 2]; 2]; L + 2];
fn dp(
pos: usize,
tight: usize,
ones_so_far: usize,
is_num: usize,
s_bytes: &[u8],
L: usize,
steps: &[i32],
k: usize,
memo: &mut Vec<Vec<Vec<Vec<i32>>>>,
) -> i32 {
if pos == L {
if is_num == 1 && steps[ones_so_far] as usize <= k {
return 1;
} else {
return 0;
}
}
if memo[pos][tight][ones_so_far][is_num] != -1 {
return memo[pos][tight][ones_so_far][is_num];
}
let mut res = 0;
let limit = if tight == 1 {
(s_bytes[pos] - b'0') as usize
} else {
1
};
for digit in 0..=limit {
let new_tight = if tight == 1 && digit == limit {
1
} else {
0
};
if is_num == 0 && digit == 0 {
// Skip leading zeros
res += dp(
pos + 1,
new_tight,
ones_so_far,
0,
s_bytes,
L,
steps,
k,
memo,
);
res %= MOD;
} else {
let new_ones = ones_so_far + digit;
res += dp(
pos + 1,
new_tight,
new_ones,
1,
s_bytes,
L,
steps,
k,
memo,
);
res %= MOD;
}
}
memo[pos][tight][ones_so_far][is_num] = res;
res
}
dp(0, 1, 0, 0, s_bytes, L, &steps, k, &mut memo)
}
}
This implementation carefully considers all the constraints and efficiently computes the required count using DP with memoization.