Buggy python submission (verdict: Runtime Error):
N,P=[int(s) for s in input().split()]
S=list(input())
if P!=2 and P!=5:
  ls=[0 for _ in range(N)]
  mod=[0 for _ in range(P)]
  d=10
  ls[0]=int(S[-1])
  mod[ls[0]]+=1
  for i in range(1,N):
    ls[i]=(ls[i-1]+d*int(S[-1-i]))%2019
    d=(d*10)%2019
    mod[ls[i]]+=1
  #print(ls)
  #print(mod)  
  ans=mod[0]
  for i in range(N):
    m=ls[i]
    mod[ls[i]]-=1
    ans+=mod[ls[i]]
else:
  ans=0
  for i in range(N):
    if int(S[i])%P==0:
      ans+=i+1
print(ans)
   


Fixed submission:
N,P=[int(s) for s in input().split()]
S=list(input())
if P!=2 and P!=5:
  ls=[0 for _ in range(N)]
  mod=[0 for _ in range(P)]
  d=10
  ls[0]=int(S[-1])%P
  mod[ls[0]]+=1
  for i in range(1,N):
    ls[i]=(ls[i-1]+d*int(S[-1-i]))%P
    d=(d*10)%P
    mod[ls[i]]+=1
  #print(ls)
  #print(mod)  
  ans=mod[0]
  for i in range(N):
    m=ls[i]
    mod[ls[i]]-=1
    ans+=mod[ls[i]]
else:
  ans=0
  for i in range(N):
    if int(S[i])%P==0:
      ans+=i+1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,P=[int(s) for s in input().split()]
num=input()


ans=0
if P!=2 and P!=5:
  ls=[int(num[i:])%P for i in range(N)]
  count=[0 for _ in range(P)]
  for i in range(N):
    count[ls[i]]+=1
  for i in range(N):
    a=ls[i]
    if a==0:
      ans+=1
    count[a]-=1
    ans+=count[a]
else :
  for i in range(N):
    if int(num[i])%P==0:
      ans+=i+1
print(ans)


Fixed submission:
N,P=[int(s) for s in input().split()]
S=list(input())
if P!=2 and P!=5:
  ls=[0 for _ in range(N)]
  mod=[0 for _ in range(P)]
  d=10
  ls[0]=int(S[-1])%P
  mod[ls[0]]+=1
  for i in range(1,N):
    ls[i]=(ls[i-1]+d*int(S[-1-i]))%P
    d=(d*10)%P
    mod[ls[i]]+=1
  #print(ls)
  #print(mod)  
  ans=mod[0]
  for i in range(N):
    m=ls[i]
    mod[ls[i]]-=1
    ans+=mod[ls[i]]
else:
  ans=0
  for i in range(N):
    if int(S[i])%P==0:
      ans+=i+1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,P=[int(s) for s in input().split()]
DP=[[0 for _ in range(i+1)] for i in range(N)]
ls=[int(s)%P for s in list(input())]

count=[[0 for _ in range(P)] for _ in range(N)]


for j in range(N):
  count[j][ls[j]]+=1

for i in range(1,N):
  a=ls[i]
  for k in range(P):
    if count[i-1][k]!=0:
      count[i][(10*k+a)%P]+=count[i-1][k]
  
ans=0
for j in range(N):
  ans+=count[j][0]
print(ans)

Fixed submission:
N,P=[int(s) for s in input().split()]
S=list(input())
if P!=2 and P!=5:
  ls=[0 for _ in range(N)]
  mod=[0 for _ in range(P)]
  d=10
  ls[0]=int(S[-1])%P
  mod[ls[0]]+=1
  for i in range(1,N):
    ls[i]=(ls[i-1]+d*int(S[-1-i]))%P
    d=(d*10)%P
    mod[ls[i]]+=1
  #print(ls)
  #print(mod)  
  ans=mod[0]
  for i in range(N):
    m=ls[i]
    mod[ls[i]]-=1
    ans+=mod[ls[i]]
else:
  ans=0
  for i in range(N):
    if int(S[i])%P==0:
      ans+=i+1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,P=[int(s) for s in input().split()]
DP=[[0 for _ in range(i+1)] for i in range(N)]
ls=[int(s)%P for s in list(input())]

count=[[0 for _ in range(P)] for _ in range(N)]


for j in range(N):
  count[j][ls[j]]+=1

for i in range(1,N):
  a=ls[i]
  for k in range(P):
    count[i][(10*k+a)%P]+=count[i-1][k]
  
ans=0
for j in range(N):
  ans+=count[j][0]
print(ans)

Fixed submission:
N,P=[int(s) for s in input().split()]
S=list(input())
if P!=2 and P!=5:
  ls=[0 for _ in range(N)]
  mod=[0 for _ in range(P)]
  d=10
  ls[0]=int(S[-1])%P
  mod[ls[0]]+=1
  for i in range(1,N):
    ls[i]=(ls[i-1]+d*int(S[-1-i]))%P
    d=(d*10)%P
    mod[ls[i]]+=1
  #print(ls)
  #print(mod)  
  ans=mod[0]
  for i in range(N):
    m=ls[i]
    mod[ls[i]]-=1
    ans+=mod[ls[i]]
else:
  ans=0
  for i in range(N):
    if int(S[i])%P==0:
      ans+=i+1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,P=[int(s) for s in input().split()]
DP=[[0 for _ in range(N-i)] for i in range(N)]
ls=[int(s)%P for s in list(input())]

count=0

for j in range(N):
  DP[0][j]=ls[j]
  if DP[0][j]==0:
    count+=1


for i in range(1,N):
  for j in range(N-i):
    DP[i][j]=(DP[i-1][j]*10+DP[0][i+j])%P
    if DP[i][j]==0:
      count+=1
    
print(count)

Fixed submission:
N,P=[int(s) for s in input().split()]
S=list(input())
if P!=2 and P!=5:
  ls=[0 for _ in range(N)]
  mod=[0 for _ in range(P)]
  d=10
  ls[0]=int(S[-1])%P
  mod[ls[0]]+=1
  for i in range(1,N):
    ls[i]=(ls[i-1]+d*int(S[-1-i]))%P
    d=(d*10)%P
    mod[ls[i]]+=1
  #print(ls)
  #print(mod)  
  ans=mod[0]
  for i in range(N):
    m=ls[i]
    mod[ls[i]]-=1
    ans+=mod[ls[i]]
else:
  ans=0
  for i in range(N):
    if int(S[i])%P==0:
      ans+=i+1
print(ans)