Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import collections
    N, P = map(int, input().split())
    S = input()[::-1]
    ans = 0
    if P == 2 or P == 5:
        for i, s in enumerate(S):
            if int(s) % P == 0:
                ans += N - i
    else:
        mod = [0] * P
        mod[0] = 1
        current = 0
        X = 0
        for s in S:
            current = (current + int(s) * 10 ** X) % P
            ans += mod[current]
            mod[current] += 1
            X += 1
    
    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import collections
    N, P = map(int, input().split())
    S = input()[::-1]
    ans = 0
    if P == 2 or P == 5:
        for i, s in enumerate(S):
            if int(s) % P == 0:
                ans += N - i
    else:
        mod = [0] * P
        mod[0] = 1
        current = 0
        X = 1
        for s in S:
            current = (current + int(s) * X) % P
            ans += mod[current]
            mod[current] += 1
            X = X * 10 % P
    
    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import collections
    N, P = map(int, input().split())
    S = input()[::-1]
    ans = 0
    if P == 2 or P == 5:
        for i, s in enumerate(S):
            if int(s) % P == 0:
                ans += N - i
    else:
        D = [0] * N
        A = [0] * (N + 1)
        for i, s in enumerate(S):
            D[i] = int(s) * 10 ** i % P
            if i == 0:
                A[i] = D[i]
            else:
                A[i] = (D[i] + A[i - 1]) % P
        c = collections.Counter(A)
        ans = sum([i * (i - 1) // 2 for i in c.values()])
    
    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import collections
    N, P = map(int, input().split())
    S = input()[::-1]
    ans = 0
    if P == 2 or P == 5:
        for i, s in enumerate(S):
            if int(s) % P == 0:
                ans += N - i
    else:
        mod = [0] * P
        mod[0] = 1
        current = 0
        X = 1
        for s in S:
            current = (current + int(s) * X) % P
            ans += mod[current]
            mod[current] += 1
            X = X * 10 % P
    
    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import collections
    N, P = map(int, input().split())
    S = input()[::-1]
    ans = 0
    if P == 2 or P == 5:
        for i, s in enumerate(S):
            if int(s) % P == 0:
                ans += N - i
    else:
        D = [0] * N
        A = [0] * (N + 1)
        for i, s in enumerate(S):
            D[i] = int(s) * 10 ** i % P
            if i == 0:
                A[i] = D[i]
            else:
                A[i] = (D[i] + A[i - 1]) % P
        c = collections.Counter(A)
        ans = sum([i * (i - 1) // 2 for i in c.values()])
    
    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import collections
    N, P = map(int, input().split())
    S = input()[::-1]
    ans = 0
    if P == 2 or P == 5:
        for i, s in enumerate(S):
            if int(s) % P == 0:
                ans += N - i
    else:
        mod = [0] * P
        mod[0] = 1
        current = 0
        X = 1
        for s in S:
            current = (current + int(s) * X) % P
            ans += mod[current]
            mod[current] += 1
            X = X * 10 % P
    
    print(ans)

if __name__ == '__main__':
    main()