def count_divisible_substrings(s, p):
n = len(s)
result = [0] * n
mod = 2 ** p # Calculate 2^p for divisibility checks
for i in range(n):
current_mod = 0
# Iterate through substrings starting at index i
for j in range(i, n):
current_mod = (current_mod * 10 + int(s[j])) % mod
# If divisible, increment the count for index i
if current_mod == 0:
result[i] += 1
# Stop early if we've examined the last `p` digits
if j - i + 1 >= p:
break
print("res :", result)
return result
# Example usage:
s = "1048"
p = 3
print(count_divisible_substrings(s, p)) # Output: [2, 2, 1, 1]
s = "743212"
p = 4
print(count_divisible_substrings(s, p))
To embed this program on your website, copy the following code and paste it into your website's HTML: