Buggy python submission (verdict: Time Limit Exceeded):
N, P = map(int, input().split())
S = input()

if P == 2 or P == 5:
    cnt = 0

    cnt_a = 0
    for s in S[::-1]:
        if int(s) % P == 0:
            cnt_a += 1

        cnt += cnt_a
else:
    r_lst = [0] * P
    r_lst[0] = 1

    cnt = 0
    num = 0

    for i, s in enumerate(S[::-1]):
        num  = (num + int(s) * pow(10, i)) % P
        cnt += r_lst[num]

        r_lst[num] += 1


print(cnt)



Fixed submission:
N, P = map(int, input().split())
S = input()

if P == 2 or P == 5:
    cnt = 0

    cnt_a = 0
    for s in S[::-1]:
        if int(s) % P == 0:
            cnt_a += 1

        cnt += cnt_a
else:
    r_lst = [0] * P
    r_lst[0] = 1

    cnt = 0
    num = 0

    for i, s in enumerate(S[::-1]):
        num  = (num + int(s) * pow(10, i, P)) % P
        cnt += r_lst[num]

        r_lst[num] += 1


print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
N, P = map(int, input().split())
S = input()

if P == 2 or P == 5:
    cnt = 0

    cnt_a = 0
    for s in S[::-1]:
        if int(s) % P == 0:
            cnt_a += 1

        cnt += cnt_a
else:
    r_lst = [0] * P
    r_lst[0] = 1

    cnt = 0
    num = ''
    for s in S[::-1]:
        num = s + num
        r = int(num) % P

        cnt += r_lst[r]

        r_lst[r] += 1


print(cnt)


Fixed submission:
N, P = map(int, input().split())
S = input()

if P == 2 or P == 5:
    cnt = 0

    cnt_a = 0
    for s in S[::-1]:
        if int(s) % P == 0:
            cnt_a += 1

        cnt += cnt_a
else:
    r_lst = [0] * P
    r_lst[0] = 1

    cnt = 0
    num = 0

    for i, s in enumerate(S[::-1]):
        num  = (num + int(s) * pow(10, i, P)) % P
        cnt += r_lst[num]

        r_lst[num] += 1


print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
N, P = map(int, input().split())
S = input()

if P == 2:
    cnt = 0

    cnt_2 = 0
    for s in S[::-1]:
        if int(s) % 2 == 0:
            cnt_2 += 1

        cnt += cnt_2
elif P == 5:
    cnt = 0

    cnt_0 = 0
    cnt_5 = 0
    for s in S[::-1]:
        if s == '0':
            cnt_0 += 1
        elif s == '5':
            cnt_5 += 1

        cnt += cnt_0
        cnt += cnt_5
else:
    dic = {}
    dic[0] = 1
    cnt = 0
    num = ''
    for s in S[::-1]:
        num = s + num
        r = int(num) % P

        cnt += dic.get(r, 0)

        if r not in dic:
            dic[r] = 1
        else:
            dic[r] += 1

print(cnt)


Fixed submission:
N, P = map(int, input().split())
S = input()

if P == 2 or P == 5:
    cnt = 0

    cnt_a = 0
    for s in S[::-1]:
        if int(s) % P == 0:
            cnt_a += 1

        cnt += cnt_a
else:
    r_lst = [0] * P
    r_lst[0] = 1

    cnt = 0
    num = 0

    for i, s in enumerate(S[::-1]):
        num  = (num + int(s) * pow(10, i, P)) % P
        cnt += r_lst[num]

        r_lst[num] += 1


print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
N, P = map(int, input().split())
S = input()

if P == 2:
    cnt = 0

    cnt_2 = 0
    for s in S[::-1]:
        if int(s) % 2 == 0:
            cnt_2 += 1

        cnt += cnt_2
elif P == 5:
    cnt = 0

    cnt_0 = 0
    cnt_5 = 0
    for s in S[::-1]:
        if s == '0':
            cnt_0 += 1
        elif s == '5':
            cnt_5 += 1

        cnt += cnt_0
        cnt += cnt_5
else:
    dic = {}
    dic[0] = 1
    cnt = 0
    num = ''
    for s in S[::-1]:
        num = s + num
        r = int(num) % P

        cnt += dic.get(r, 0)

        if r not in dic:
            dic[r] = 1
        else:
            dic[r] += 1

print(cnt)


Fixed submission:
N, P = map(int, input().split())
S = input()

if P == 2 or P == 5:
    cnt = 0

    cnt_a = 0
    for s in S[::-1]:
        if int(s) % P == 0:
            cnt_a += 1

        cnt += cnt_a
else:
    r_lst = [0] * P
    r_lst[0] = 1

    cnt = 0
    num = 0

    for i, s in enumerate(S[::-1]):
        num  = (num + int(s) * pow(10, i, P)) % P
        cnt += r_lst[num]

        r_lst[num] += 1


print(cnt)