Buggy python submission (verdict: Time Limit Exceeded):
n, p = map(int,input().split())
s = str(input())
ans = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i])%p == 0:
            ans += i+1
    print(ans)
    exit()

tmp = [0] * p
tmp[0] = 1
mod = 0
for i in range(1,n+1):
    mod += (int(s[-i]) * 10**i) % p
    mod %= p
    tmp[mod] += 1

for i in tmp:
    ans += i*(i-1)//2

print(ans)

Fixed submission:
n, p = map(int,input().split())
s = str(input())
ans = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i])%p == 0:
            ans += i+1
    print(ans)
    exit()

tmp = [0] * p
tmp[0] = 1
mod = 0
r = 1
for i in range(1,n+1):
    mod += (int(s[-i]) * r) % p
    mod %= p
    tmp[mod] += 1
    r *= 10
    r %= p

for i in tmp:
    ans += i*(i-1)//2

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, p = map(int,input().split())
s = str(input())
ans = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i])%p == 0:
            ans += i+1
    print(ans)
    exit()

tmp = [0] * p
tmp[0] = 1
mod = 0
for i in range(1,n+1):
    mod += (int(s[-i]) * 10**i) % p
    mod %= p
    ans += tmp[mod]
    tmp[mod] += 1

print(ans)

Fixed submission:
n, p = map(int,input().split())
s = str(input())
ans = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i])%p == 0:
            ans += i+1
    print(ans)
    exit()

tmp = [0] * p
tmp[0] = 1
mod = 0
r = 1
for i in range(1,n+1):
    mod += (int(s[-i]) * r) % p
    mod %= p
    tmp[mod] += 1
    r *= 10
    r %= p

for i in tmp:
    ans += i*(i-1)//2

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, p = map(int,input().split())
s = str(input())
ans = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i])%p == 0:
            ans += i+1
    print(ans)
    exit()

tmp = [0] * p
tmp[0] = 1
mod = 0
for i in range(1,n+1):
    mod += (int(s[-i]) * 10**i) % p
    mod %= p
    ans += tmp[mod]
    tmp[mod] += 1

print(ans)

Fixed submission:
n, p = map(int,input().split())
s = str(input())
ans = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i])%p == 0:
            ans += i+1
    print(ans)
    exit()

tmp = [0] * p
tmp[0] = 1
mod = 0
r = 1
for i in range(1,n+1):
    mod += (int(s[-i]) * r) % p
    mod %= p
    tmp[mod] += 1
    r *= 10
    r %= p

for i in tmp:
    ans += i*(i-1)//2

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, p = map(int,input().split())
s = str(input())
mods = [(i*10)%p for i in range(p)]
sums = [0] * p

tmp = 0
dp = [[0]*p for i in range(n+1)]

if p == 2 or p == 5:
    ans = 0
    for i in range(n):
        if int(s[i])%p == 0:
            ans += i+1
    print(ans)
    exit()

for i in range(1,n+1):
    now = int(s[i-1])%p
    dp[i][now] += 1
    for j in range(p):
        dp[i][(mods[j]+now)%p] += dp[i-1][j]
#         tmp[(mods[j]+now)%p] += sums[j]
print(sum([dp[i][0] for i in range(n+1)]))
# print(*dp,sep="\n")

Fixed submission:
n, p = map(int,input().split())
s = str(input())
ans = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i])%p == 0:
            ans += i+1
    print(ans)
    exit()

tmp = [0] * p
tmp[0] = 1
mod = 0
r = 1
for i in range(1,n+1):
    mod += (int(s[-i]) * r) % p
    mod %= p
    tmp[mod] += 1
    r *= 10
    r %= p

for i in tmp:
    ans += i*(i-1)//2

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, p = map(int,input().split())
s = str(input())
mods = [(i*10)%p for i in range(p)]
sums = [0] * p

tmp = 0
dp = [[0]*p for i in range(n+1)]
for i in range(1,n+1):
    now = int(s[i-1])%p
    dp[i][now] += 1
    for j in range(p):
        dp[i][(mods[j]+now)%p] += dp[i-1][j]
#         tmp[(mods[j]+now)%p] += sums[j]
print(sum([dp[i][0] for i in range(n+1)]))
# print(*dp,sep="\n")

Fixed submission:
n, p = map(int,input().split())
s = str(input())
ans = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i])%p == 0:
            ans += i+1
    print(ans)
    exit()

tmp = [0] * p
tmp[0] = 1
mod = 0
r = 1
for i in range(1,n+1):
    mod += (int(s[-i]) * r) % p
    mod %= p
    tmp[mod] += 1
    r *= 10
    r %= p

for i in tmp:
    ans += i*(i-1)//2

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, p = map(int,input().split())
s = str(input())
mods = [(i*10)%p for i in range(p)]
sums = [0] * p

tmp = 0
dp = [[0]*p for i in range(n+1)]
for i in range(1,n+1):
    now = int(s[i-1])%p
    dp[i][now] += 1
    for j in range(p):
        dp[i][(mods[j]+now)%p] += dp[i-1][j]
#         tmp[(mods[j]+now)%p] += sums[j]
print(sum([dp[i][0] for i in range(n+1)]))
# print(*dp,sep="\n")

Fixed submission:
n, p = map(int,input().split())
s = str(input())
ans = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i])%p == 0:
            ans += i+1
    print(ans)
    exit()

tmp = [0] * p
tmp[0] = 1
mod = 0
r = 1
for i in range(1,n+1):
    mod += (int(s[-i]) * r) % p
    mod %= p
    tmp[mod] += 1
    r *= 10
    r %= p

for i in tmp:
    ans += i*(i-1)//2

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, p = map(int,input().split())
s = str(input())
mods = [(i*10)%p for i in range(p)]
ans = 0
tmp = 0
for i in range(n):
    now = int(s[i])%p
    if not now:
        ans += 1
    for j in range(i+1,n):
        # print(i,j)
        now = int(s[j])%p + mods[now]
        now %= p
        if not now:
            ans += 1

print(ans)

Fixed submission:
n, p = map(int,input().split())
s = str(input())
ans = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i])%p == 0:
            ans += i+1
    print(ans)
    exit()

tmp = [0] * p
tmp[0] = 1
mod = 0
r = 1
for i in range(1,n+1):
    mod += (int(s[-i]) * r) % p
    mod %= p
    tmp[mod] += 1
    r *= 10
    r %= p

for i in tmp:
    ans += i*(i-1)//2

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, p = map(int,input().split())
s = str(input())
mods = [(i*10)%p for i in range(p)]
ans = 0
tmp = 0
for i in range(n):
    now = int(s[i])%p
    if not now:
        ans += 1
    for j in range(i+1,n):
        # print(i,j)
        now = int(s[j])%p + mods[now]
        now %= p
        if not now:
            ans += 1

print(ans)

Fixed submission:
n, p = map(int,input().split())
s = str(input())
ans = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i])%p == 0:
            ans += i+1
    print(ans)
    exit()

tmp = [0] * p
tmp[0] = 1
mod = 0
r = 1
for i in range(1,n+1):
    mod += (int(s[-i]) * r) % p
    mod %= p
    tmp[mod] += 1
    r *= 10
    r %= p

for i in tmp:
    ans += i*(i-1)//2

print(ans)