Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
from copy import deepcopy 

n,p = map(int, input().split())
s = list(map(int, input().strip()))

zero = np.zeros(p,dtype = int) #動的計画法
dp = deepcopy(zero)

count = 0
d = 1
for i in range(n): #1の位から順に数を追加していく
    ne_ar = deepcopy(zero)
    if(p == 2 or p == 5): #10と互いに素ではない場合
        ne = int(s[n-i-1]) % p
        if(ne == 0):
            dp[0] += 1
        count += dp[0] #
    else:
        ne = (int(s[n-i-1]) * d) % p #10とは互いに素なので、sと同じ位で考えて良い
        ne_ar[ne:] += dp[:(p-ne)] #新しく追加する数の余りをずらす
        ne_ar[:ne] += dp[(p-ne):]
        ne_ar[ne] += 1 #新しい数のみの時
        dp = ne_ar
        d *= 10
        d %= p
        count += ne_ar[0] #neを一番上の位とする部分集合の数を足す

print(count)


Fixed submission:
n,p = map(int, input().split())
s = list(map(int, input().strip()))

count = 0

if(p == 2 or p == 5): #10と互いに素ではない場合
    co = 0
    for i in range(n): #1の位から順に数を追加していく
        ne = int(s[n-i-1]) % p
        if(ne == 0):
            co += 1
        count += co
    print(count)
else:
    sta = [0]*p
    sta[0] += 1
    d = 1
    co = 0
    for i in range(n):
        ne = int(s[n-i-1]) * d % p
        co += ne
        co %= p
        sta[co] += 1
        d *= 10
        d %= p
    for i in range(p):
        count += sta[i] * (sta[i]-1) // 2
    print(count)

Buggy python submission (verdict: Runtime Error):
import numpy as np

n,p = map(int, input().split())
s = list(map(int, input().strip()))

dp = np.zeros(p,dtype = int)

count = 0
d = 1
for i in range(n):
    ne_ar = np.zeros(p,dtype = int)
    if(p == 2 or p == 5):
        ne = int(s[n-i-1]) % p
        if(ne == 0):
            dp[0] += 1
        count += dp[0]
    else:
        ne = (int(s[n-i-1]) * d) % p
        ne_ar[ne:] += dp[:(p-ne)]
        ne_ar[:ne] += dp[(p-ne):]
        ne_ar[ne] += 1
        dp = ne_ar
        d *= 10
        d %= p
        count += ne_ar[0]

print(count)

Fixed submission:
n,p = map(int, input().split())
s = list(map(int, input().strip()))

count = 0

if(p == 2 or p == 5): #10と互いに素ではない場合
    co = 0
    for i in range(n): #1の位から順に数を追加していく
        ne = int(s[n-i-1]) % p
        if(ne == 0):
            co += 1
        count += co
    print(count)
else:
    sta = [0]*p
    sta[0] += 1
    d = 1
    co = 0
    for i in range(n):
        ne = int(s[n-i-1]) * d % p
        co += ne
        co %= p
        sta[co] += 1
        d *= 10
        d %= p
    for i in range(p):
        count += sta[i] * (sta[i]-1) // 2
    print(count)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

n,p = map(int, input().split())
s = list(map(int, input().strip()))

dp = np.zeros(p,dtype = int)

count = 0
d = 1
for i in range(n):
    ne_ar = np.zeros(p,dtype = int)
    if(p == 2 or p == 5):
        ne = int(s[n-i-1]) % p
        if(ne == 0):
            dp[0] += 1
        count += dp[0]
    else:
        ne = (int(s[n-i-1]) * d) % p
        ne_ar[ne:] += dp[:(p-ne)]
        ne_ar[:ne] += dp[(p-ne):]
        ne_ar[ne] += 1
        dp = ne_ar
        d *= 10
        d %= p
        count += ne_ar[0]

print(count)

Fixed submission:
n,p = map(int, input().split())
s = list(map(int, input().strip()))

count = 0

if(p == 2 or p == 5): #10と互いに素ではない場合
    co = 0
    for i in range(n): #1の位から順に数を追加していく
        ne = int(s[n-i-1]) % p
        if(ne == 0):
            co += 1
        count += co
    print(count)
else:
    sta = [0]*p
    sta[0] += 1
    d = 1
    co = 0
    for i in range(n):
        ne = int(s[n-i-1]) * d % p
        co += ne
        co %= p
        sta[co] += 1
        d *= 10
        d %= p
    for i in range(p):
        count += sta[i] * (sta[i]-1) // 2
    print(count)