Buggy python submission (verdict: Time Limit Exceeded):
N, P=map(int,input().split())
S = input()
def ma25():
  ret = 0
  for i in range(N):
    if int(S[i])%P == 0:
      ret+=(i+1)
  print(ret)

def ma():
  dp = [0]*P
  dp[0] = 1
  for i in range(N-1,-1,-1):
    dp[int(S[i:])%P] += 1
  ret = 0
  for m in dp:
    ret += m*(m-1)//2
  print(ret)

if P == 2 or P == 5:
  ma25()
else:
  ma()

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

def ma25():
  ret = 0
  for i in range(N):
    if S[i]%P == 0:
      ret+=(i+1)
  print(ret)

def ma():
  dp = [0]*P
  dp[0] = 1
  r = 0
  for i in range(N-1,-1,-1):
    r = ((S[i]*pow(10,N-1-i,P))+r)%P
    dp[r] += 1
  ret = 0
  for m in dp:
    ret += m*(m-1)//2
  print(ret)

if P == 2 or P == 5:
  ma25()
else:
  ma()

Buggy python submission (verdict: Time Limit Exceeded):
N, P=map(int,input().split())
S = list(map(int,list(input())))
def ma25():
  ret = 0
  for i in range(N):
    if S[i]%P == 0:
      ret+=(i+1)
  print(ret)

def ma():
  dp = [0]*P
  ret = 0
  for i in range(N):
    dp_ = [0]*P
    b = S[i]%P
    for j in range(P):
      dp_[(j*10+b)%P] += dp[j]
    dp_[b] += 1
    ret += dp_[0]
    dp = dp_[:]
  print(ret)    
if P == 2 or P == 5:
  ma25()
else:
  ma()

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

def ma25():
  ret = 0
  for i in range(N):
    if S[i]%P == 0:
      ret+=(i+1)
  print(ret)

def ma():
  dp = [0]*P
  dp[0] = 1
  r = 0
  for i in range(N-1,-1,-1):
    r = ((S[i]*pow(10,N-1-i,P))+r)%P
    dp[r] += 1
  ret = 0
  for m in dp:
    ret += m*(m-1)//2
  print(ret)

if P == 2 or P == 5:
  ma25()
else:
  ma()

Buggy python submission (verdict: Time Limit Exceeded):
N, P=map(int,input().split())
S = list(map(int,list(input())))
def ma():
  dp = [0]*P
  ret = 0
  for i in range(N):
    dp_ = [0]*P
    b = S[i]%P
    for j in range(P):
      dp_[(j*10+b)%P] += dp[j]
    dp_[b] += 1
    ret += dp_[0]
    dp = dp_[:]
  print(ret)    
ma()

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

def ma25():
  ret = 0
  for i in range(N):
    if S[i]%P == 0:
      ret+=(i+1)
  print(ret)

def ma():
  dp = [0]*P
  dp[0] = 1
  r = 0
  for i in range(N-1,-1,-1):
    r = ((S[i]*pow(10,N-1-i,P))+r)%P
    dp[r] += 1
  ret = 0
  for m in dp:
    ret += m*(m-1)//2
  print(ret)

if P == 2 or P == 5:
  ma25()
else:
  ma()

Buggy python submission (verdict: Time Limit Exceeded):
N, P=map(int,input().split())
S = list(map(int,list(input())))
def ma():
  dp = [0]*P
  ret = 0
  for i in range(N):
    dp_ = [0]*P
    b = S[i]%P
    for j in range(P):
      dp_[(j*10+b)%P] += dp[j]
    dp_[b] += 1
    ret += dp_[0]
    dp = dp_[:]
  print(ret)    
ma()

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

def ma25():
  ret = 0
  for i in range(N):
    if S[i]%P == 0:
      ret+=(i+1)
  print(ret)

def ma():
  dp = [0]*P
  dp[0] = 1
  r = 0
  for i in range(N-1,-1,-1):
    r = ((S[i]*pow(10,N-1-i,P))+r)%P
    dp[r] += 1
  ret = 0
  for m in dp:
    ret += m*(m-1)//2
  print(ret)

if P == 2 or P == 5:
  ma25()
else:
  ma()

Buggy python submission (verdict: Time Limit Exceeded):
N, P=map(int,input().split())
S = list(map(int,list(input())))
dp = [0]*P
ret = 0
for i in range(N):
  dp_ = [0]*P
  b = S[i]%P
  for j in range(P):
    dp_[(j*10+b)%P] += dp[j]
  dp_[b] += 1
  ret += dp_[0]
  dp = dp_[:]
print(ret)    

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

def ma25():
  ret = 0
  for i in range(N):
    if S[i]%P == 0:
      ret+=(i+1)
  print(ret)

def ma():
  dp = [0]*P
  dp[0] = 1
  r = 0
  for i in range(N-1,-1,-1):
    r = ((S[i]*pow(10,N-1-i,P))+r)%P
    dp[r] += 1
  ret = 0
  for m in dp:
    ret += m*(m-1)//2
  print(ret)

if P == 2 or P == 5:
  ma25()
else:
  ma()