Buggy python submission (verdict: Time Limit Exceeded):
n, p = map(int, input().split())
s = list(map(int, list(input())))[::-1]

if 10%p==0:
    result = 0
    for i in range(n):
        if s[i]%p==0:
            result += n-i
else:
    r = [0 for _ in range(p)]
    r[0] = 1
    t = 0
    ten = 1
    for i in range(n):
        t += (s[i]%p)*ten
        t %= p
        ten *= 10%p
        r[t] += 1
    result = 0
    for i in r:
        result += i*(i-1)//2
print(result)

Fixed submission:
n, p = map(int, input().split())
s = list(map(int, list(input())))[::-1]

if 10%p==0:
    result = 0
    for i in range(n):
        if s[i]%p==0:
            result += n-i
else:
    r = [0 for _ in range(p)]
    r[0] = 1
    t = 0
    ten = 1
    for i in s:
        t = (t+i*ten)%p
        ten = (ten*10)%p
        r[t] += 1
    result = 0
    for i in r:
        result += i*(i-1)//2
print(result)

Buggy python submission (verdict: Time Limit Exceeded):
n, p = map(int, input().split())
s = list(map(int, list(input())))[::-1]

if 10%p==0:
    result = 0
    for i in range(n):
        if s[i]%p==0:
            result += n-i
else:
    r = [0 for _ in range(p)]
    r[0] = 1
    t = 0
    ten = 1
    for i in range(n):
        t += (s[i]%p)*ten
        t %= p
        ten *= 10%p
        r[t] += 1
    result = 0
    for i in r:
        result += i*(i-1)//2
print(result)

Fixed submission:
n, p = map(int, input().split())
s = list(map(int, list(input())))[::-1]

if 10%p==0:
    result = 0
    for i in range(n):
        if s[i]%p==0:
            result += n-i
else:
    r = [0 for _ in range(p)]
    r[0] = 1
    t = 0
    ten = 1
    for i in s:
        t = (t+i*ten)%p
        ten = (ten*10)%p
        r[t] += 1
    result = 0
    for i in r:
        result += i*(i-1)//2
print(result)

Buggy python submission (verdict: Time Limit Exceeded):
n, p = map(int, input().split())
s = list(map(int, list(input())))[::-1]

if 10%p==0:
    result = 0
    for i in range(n):
        if s[i]%p==0:
            result += n-i
else:
    r = [0 for _ in range(p)]
    r[0] = 1
    t = 0
    ten = 1
    for i in range(n):
        t += (s[i]*ten)%p
        t %= p
        ten *= 10
        r[t] += 1
    result = 0
    for i in r:
        result += i*(i-1)//2
print(result)

Fixed submission:
n, p = map(int, input().split())
s = list(map(int, list(input())))[::-1]

if 10%p==0:
    result = 0
    for i in range(n):
        if s[i]%p==0:
            result += n-i
else:
    r = [0 for _ in range(p)]
    r[0] = 1
    t = 0
    ten = 1
    for i in s:
        t = (t+i*ten)%p
        ten = (ten*10)%p
        r[t] += 1
    result = 0
    for i in r:
        result += i*(i-1)//2
print(result)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
n, p = map(int, input().split())
s = list(map(int, list(input())))
si = [s[-1]]

if 10%p==0:
    r = 0
    for i in range(1,n+1):
        if s[-i]%p==0:
            r += n-i+1
    print(r)
else:
    for i in range(2, n+1):
        si.append(si[-1]+s[-i]*(10**(i-1)))
    si = [i%p for i in si]
    si_counter = Counter(si).items()
    r = 0
    for num, count in si_counter:
        if count >= 2:
            r += (count*(count-1))//2
        if num == 0:
            r += count
    print(r)

Fixed submission:
n, p = map(int, input().split())
s = list(map(int, list(input())))[::-1]

if 10%p==0:
    result = 0
    for i in range(n):
        if s[i]%p==0:
            result += n-i
else:
    r = [0 for _ in range(p)]
    r[0] = 1
    t = 0
    ten = 1
    for i in s:
        t = (t+i*ten)%p
        ten = (ten*10)%p
        r[t] += 1
    result = 0
    for i in r:
        result += i*(i-1)//2
print(result)