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

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

    l = {0:1}
    s1 = s[::-1]

    mod = 0
    for i in range(n):
        mod += int(s1[i])*10**i%p
        mod %= p
        if mod in l:
            l[mod] += 1
        else:
            l[mod] = 1
    ans = 0

    for i in l.values():
        ans += (i-1)*i//2
    print(ans)
if __name__ == "__main__":
    main()

Fixed submission:
def main():
    n,p = map(int,input().split())
    s = input()

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

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

    for i in l:
        ans += (i-1)*i//2
    print(ans)
if __name__ == "__main__":
    main()

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

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

    l = {0:1}
    s1 = s[::-1]

    mod = 0
    for i in range(n):
        mod += int(s1[i])*10**i%p
        mod %= p
        if mod in l:
            l[mod] += 1
        else:
            l[mod] = 1
    ans = 0

    for i in l.values():
        ans += (i-1)*i//2
    print(ans)
if __name__ == "__main__":
    main()

Fixed submission:
def main():
    n,p = map(int,input().split())
    s = input()

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

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

    for i in l:
        ans += (i-1)*i//2
    print(ans)
if __name__ == "__main__":
    main()

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

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

l = [0]

s1 = s[::-1]
l.append(int(s1[0])%p)
for i in range(1,n):
    check = int(s1[i])*10**i%p
    l.append((l[-1]+check)%p)
ans = 0

for i in range(p):
    k = l.count(i)*(l.count(i)-1)//2
    ans += k
print(ans)



Fixed submission:
def main():
    n,p = map(int,input().split())
    s = input()

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

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

    for i in l:
        ans += (i-1)*i//2
    print(ans)
if __name__ == "__main__":
    main()

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

Fixed submission:
def main():
    n,p = map(int,input().split())
    s = input()

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

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

    for i in l:
        ans += (i-1)*i//2
    print(ans)
if __name__ == "__main__":
    main()