Buggy python submission (verdict: Time Limit Exceeded):
n, p = map(int, input().split())
s = input()
count = 0
if p in {2, 5}:
    for right in range(n):
        if int(s[right]) % p == 0:
            count += (right + 1)
else:
    mod_p_count = [0 for i in range(p)]
    mod_p_count[0] = 1
    for pow in range(1, n + 1):
        sub_s = int(s) % 10**pow
        mod_p_count[sub_s % p] += 1
    for i in mod_p_count:
        count += i*(i - 1) // 2
print(count)

Fixed submission:
n, p = map(int, input().split())
s = input()
sum_count = 0
if p in {2, 5}:
    for right in range(n):
        if int(s[right]) % p == 0:
            sum_count += (right + 1)
else:
    modp_count = [0] * p
    modp_count[0] = 1
    subs_modp, pow = 0, 1
    for si in s[::-1]:
        subs_modp += ((int(si)*pow) % p)
        subs_modp %= p
        modp_count[subs_modp] += 1
        pow = (pow*10) % p
        
    for count in modp_count:
        sum_count += count*(count - 1) // 2
print(sum_count)

Buggy python submission (verdict: Time Limit Exceeded):
n, p = map(int, input().split())
s = input()
count = 0
if p in {2, 5}:
    for right in range(n):
        if int(s[right]) % p == 0:
            count += (right + 1)
else:
    mod_p_count = [0 for i in range(p)]
    mod_p_count[0] += 1
    for i in range(n):
        sub_s = int(s[i:])
        mod_p_count[sub_s % p] += 1
    for i in mod_p_count:
        count += i*(i - 1) // 2
print(count)

Fixed submission:
n, p = map(int, input().split())
s = input()
sum_count = 0
if p in {2, 5}:
    for right in range(n):
        if int(s[right]) % p == 0:
            sum_count += (right + 1)
else:
    modp_count = [0] * p
    modp_count[0] = 1
    subs_modp, pow = 0, 1
    for si in s[::-1]:
        subs_modp += ((int(si)*pow) % p)
        subs_modp %= p
        modp_count[subs_modp] += 1
        pow = (pow*10) % p
        
    for count in modp_count:
        sum_count += count*(count - 1) // 2
print(sum_count)

Buggy python submission (verdict: Time Limit Exceeded):
n, p = map(int, input().split())
s = input()
count = 0
if p in {2, 5}:
    for right in range(n):
        if int(s[right]) % p == 0:
            count += (right + 1)
else:
    mod_p_count = {0: 1}
    for i in range(n):
        sub_s = int(s[i:])
        try:
            mod_p_count[sub_s % p] += 1
        except KeyError:
            mod_p_count[sub_s % p] = 1
    for v in mod_p_count.values():
        count += v*(v - 1) // 2
print(count)

Fixed submission:
n, p = map(int, input().split())
s = input()
sum_count = 0
if p in {2, 5}:
    for right in range(n):
        if int(s[right]) % p == 0:
            sum_count += (right + 1)
else:
    modp_count = [0] * p
    modp_count[0] = 1
    subs_modp, pow = 0, 1
    for si in s[::-1]:
        subs_modp += ((int(si)*pow) % p)
        subs_modp %= p
        modp_count[subs_modp] += 1
        pow = (pow*10) % p
        
    for count in modp_count:
        sum_count += count*(count - 1) // 2
print(sum_count)

Buggy python submission (verdict: Time Limit Exceeded):
n, p = map(int, input().split())
s = input()
count = 0
for i in range(n):
    for j in range(i + 1, n + 1):
        sub_s = int(s[i:j])
        if sub_s % p == 0:
            count += 1
print(count)

Fixed submission:
n, p = map(int, input().split())
s = input()
sum_count = 0
if p in {2, 5}:
    for right in range(n):
        if int(s[right]) % p == 0:
            sum_count += (right + 1)
else:
    modp_count = [0] * p
    modp_count[0] = 1
    subs_modp, pow = 0, 1
    for si in s[::-1]:
        subs_modp += ((int(si)*pow) % p)
        subs_modp %= p
        modp_count[subs_modp] += 1
        pow = (pow*10) % p
        
    for count in modp_count:
        sum_count += count*(count - 1) // 2
print(sum_count)