Buggy python submission (verdict: Time Limit Exceeded):
n,p=map(int,input().split())
s=list(map(int,list(input())))
dp=[[-1,-1] for i in range(n)]
for i in range(n):
    d=0
    for j in range(i,n):
        d=10*d+s[j]
        if d%p==0:
            dp[i]=[1,j+1]
            break

ans=0
for i in range(n):
    if dp[i]!=[-1,-1]:
        ans+=dp[i][0]
        if dp[i][1]!=n:
            if dp[dp[i][1]]!=[-1,-1]:
                dp[dp[i][1]][0]+=dp[i][0]

print(ans)

Fixed submission:
from itertools import groupby
n,p=map(int,input().split())
s=[int(i) for i in input()]
if p==2 or p==5:
    ans=0
    for i in range(n-1,-1,-1):
        #Rとなるもの
        if s[i]%p==0:
            ans+=(i+1)
    exit(print(ans))
S=[0]*(n+1)
po=[1]*(n+1)
for i in range(n):
    po[i+1]=po[i]*10
    po[i+1]%=p
for i in range(n-1,-1,-1):
    S[i]=S[i+1]+s[i]*po[n-1-i]
    S[i]%=p
t=[[S[i],i] for i in range(n+1)]
t.sort()
#[L,R)となるのの候補
u=[[i[1] for i in sorted(list(group))] for key,group in groupby(t,key=lambda x: x[0])]
ans=0
for v in u:
    ans+=(len(v))*(len(v)-1)//2
print(ans)

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

    ans=0
    for i in range(n):
        if dp[i]!=[-1,-1]:
            ans+=dp[i][0]
            if dp[i][1]!=n:
                if dp[dp[i][1]]!=[-1,-1]:
                    dp[dp[i][1]][0]+=dp[i][0]
    print(ans)

main()

Fixed submission:
from itertools import groupby
n,p=map(int,input().split())
s=[int(i) for i in input()]
if p==2 or p==5:
    ans=0
    for i in range(n-1,-1,-1):
        #Rとなるもの
        if s[i]%p==0:
            ans+=(i+1)
    exit(print(ans))
S=[0]*(n+1)
po=[1]*(n+1)
for i in range(n):
    po[i+1]=po[i]*10
    po[i+1]%=p
for i in range(n-1,-1,-1):
    S[i]=S[i+1]+s[i]*po[n-1-i]
    S[i]%=p
t=[[S[i],i] for i in range(n+1)]
t.sort()
#[L,R)となるのの候補
u=[[i[1] for i in sorted(list(group))] for key,group in groupby(t,key=lambda x: x[0])]
ans=0
for v in u:
    ans+=(len(v))*(len(v)-1)//2
print(ans)

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

ans=0
for i in range(n):
    if dp[i]!=[-1,-1]:
        ans+=dp[i][0]
        if dp[i][1]!=n:
            if dp[dp[i][1]]!=[-1,-1]:
                dp[dp[i][1]][0]+=dp[i][0]

print(ans)

Fixed submission:
from itertools import groupby
n,p=map(int,input().split())
s=[int(i) for i in input()]
if p==2 or p==5:
    ans=0
    for i in range(n-1,-1,-1):
        #Rとなるもの
        if s[i]%p==0:
            ans+=(i+1)
    exit(print(ans))
S=[0]*(n+1)
po=[1]*(n+1)
for i in range(n):
    po[i+1]=po[i]*10
    po[i+1]%=p
for i in range(n-1,-1,-1):
    S[i]=S[i+1]+s[i]*po[n-1-i]
    S[i]%=p
t=[[S[i],i] for i in range(n+1)]
t.sort()
#[L,R)となるのの候補
u=[[i[1] for i in sorted(list(group))] for key,group in groupby(t,key=lambda x: x[0])]
ans=0
for v in u:
    ans+=(len(v))*(len(v)-1)//2
print(ans)

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

ans=0
for i in range(n):
    if dp[i]!=[-1,-1]:
        ans+=dp[i][0]
        if dp[i][1]!=n:
            if dp[dp[i][1]]!=[-1,-1]:
                dp[dp[i][1]][0]+=dp[i][0]

print(ans)

Fixed submission:
from itertools import groupby
n,p=map(int,input().split())
s=[int(i) for i in input()]
if p==2 or p==5:
    ans=0
    for i in range(n-1,-1,-1):
        #Rとなるもの
        if s[i]%p==0:
            ans+=(i+1)
    exit(print(ans))
S=[0]*(n+1)
po=[1]*(n+1)
for i in range(n):
    po[i+1]=po[i]*10
    po[i+1]%=p
for i in range(n-1,-1,-1):
    S[i]=S[i+1]+s[i]*po[n-1-i]
    S[i]%=p
t=[[S[i],i] for i in range(n+1)]
t.sort()
#[L,R)となるのの候補
u=[[i[1] for i in sorted(list(group))] for key,group in groupby(t,key=lambda x: x[0])]
ans=0
for v in u:
    ans+=(len(v))*(len(v)-1)//2
print(ans)