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


for i in range(N):
    U[N-(i+1)] = (U[N-i] + S[N-1-i] * (10 ** i)) % P
    
def solve(U,S):
    if P == 2 or P == 5:
        return solve1(S)
    else:
        return solve2(U)
    
def solve1(S):
    ans = 0
    for i in range(N):
        if S[i] % P == 0:
            ans += i+1
    return ans

def solve2(U):
    ans = 0
    cnt = [0 for i in range(P)]
    for i in range(N+1):
        cnt[U[i]] += 1
    for i in range(P):
        p = cnt[i]
        ans += (p * (p-1)) // 2
    return ans

print(solve(U,S))

Fixed submission:
N, P = map(int, input().split())
S = list(map(int,list(input())))
U = [0 for i in range(N+1)]
POW = [1 for i in range(N)]

for i in range(N-1):
    POW[i+1] = (POW[i]*10) % P

for i in range(N):
    U[N-(i+1)] = (U[N-i] + S[N-1-i] * POW[i]) % P
    
def solve(U,S):
    if P == 2 or P == 5:
        return solve1(S)
    else:
        return solve2(U)
    
def solve1(S):
    ans = 0
    for i in range(N):
        if S[i] % P == 0:
            ans += i+1
    return ans

def solve2(U):
    ans = 0
    cnt = [0 for i in range(P)]
    for i in range(N+1):
        cnt[U[i]] += 1
    for i in range(P):
        p = cnt[i]
        ans += (p * (p-1)) // 2
    return ans

print(solve(U,S))

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


for i in range(N):
    U[i+1] = (U[i] + S[N-1-i] * (10 ** i)) % P

U.reverse()
    
def solve(U,S):
    if P == 2 or P == 5:
        return solve1(S)
    else:
        return solve2(U)
    
def solve1(S):
    ans = 0
    for i in range(N):
        if S[i] % P == 0:
            ans += i+1
    return ans

def solve2(U):
    ans = 0
    cnt = [0 for i in range(P)]
    for i in range(N+1):
        cnt[U[i]] += 1
    for i in range(P):
        p = cnt[i]
        ans += (p * (p-1)) // 2
    return ans

print(solve(U,S))

Fixed submission:
N, P = map(int, input().split())
S = list(map(int,list(input())))
U = [0 for i in range(N+1)]
POW = [1 for i in range(N)]

for i in range(N-1):
    POW[i+1] = (POW[i]*10) % P

for i in range(N):
    U[N-(i+1)] = (U[N-i] + S[N-1-i] * POW[i]) % P
    
def solve(U,S):
    if P == 2 or P == 5:
        return solve1(S)
    else:
        return solve2(U)
    
def solve1(S):
    ans = 0
    for i in range(N):
        if S[i] % P == 0:
            ans += i+1
    return ans

def solve2(U):
    ans = 0
    cnt = [0 for i in range(P)]
    for i in range(N+1):
        cnt[U[i]] += 1
    for i in range(P):
        p = cnt[i]
        ans += (p * (p-1)) // 2
    return ans

print(solve(U,S))

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


for i in range(N):
    U[i+1] = (U[i] + S[N-1-i] * (10 ** i)) % P

U.reverse()
    
def solve(U,S):
    if P == 2 or P == 5:
        return solve1(S)
    else:
        return solve2(U)
    
def solve1(S):
    ans = 0
    for i in range(N):
        if S[i] % P == 0:
            ans += i+1
    return ans

def solve2(U):
    ans = 0
    cnt = [0 for i in range(P)]
    for i in range(N+1):
        cnt[U[i]] += 1
    for i in range(P):
        p = cnt[i]
        ans += (p * (p-1)) // 2
    return ans

print(solve(U,S))

Fixed submission:
N, P = map(int, input().split())
S = list(map(int,list(input())))
U = [0 for i in range(N+1)]
POW = [1 for i in range(N)]

for i in range(N-1):
    POW[i+1] = (POW[i]*10) % P

for i in range(N):
    U[N-(i+1)] = (U[N-i] + S[N-1-i] * POW[i]) % P
    
def solve(U,S):
    if P == 2 or P == 5:
        return solve1(S)
    else:
        return solve2(U)
    
def solve1(S):
    ans = 0
    for i in range(N):
        if S[i] % P == 0:
            ans += i+1
    return ans

def solve2(U):
    ans = 0
    cnt = [0 for i in range(P)]
    for i in range(N+1):
        cnt[U[i]] += 1
    for i in range(P):
        p = cnt[i]
        ans += (p * (p-1)) // 2
    return ans

print(solve(U,S))

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


for i in range(N+1):
    tmp = 0
    for j in range(i,N):
        tmp += S[j] * (10 ** (N-1-j))
    U.append(tmp % P)
    
def solve(U,S):
    if P == 2 or P == 5:
        return solve1(S)
    else:
        return solve2(U)
    
def solve1(S):
    ans = 0
    for i in range(N):
        if S[i] % P == 0:
            ans += i+1
    return ans

def solve2(U):
    ans = 0
    cnt = [0 for i in range(P)]
    for i in range(N+1):
        cnt[U[i]] += 1
    for i in range(P):
        p = cnt[i]
        ans += (p * (p-1)) // 2
    return ans

print(solve(U,S))

Fixed submission:
N, P = map(int, input().split())
S = list(map(int,list(input())))
U = [0 for i in range(N+1)]
POW = [1 for i in range(N)]

for i in range(N-1):
    POW[i+1] = (POW[i]*10) % P

for i in range(N):
    U[N-(i+1)] = (U[N-i] + S[N-1-i] * POW[i]) % P
    
def solve(U,S):
    if P == 2 or P == 5:
        return solve1(S)
    else:
        return solve2(U)
    
def solve1(S):
    ans = 0
    for i in range(N):
        if S[i] % P == 0:
            ans += i+1
    return ans

def solve2(U):
    ans = 0
    cnt = [0 for i in range(P)]
    for i in range(N+1):
        cnt[U[i]] += 1
    for i in range(P):
        p = cnt[i]
        ans += (p * (p-1)) // 2
    return ans

print(solve(U,S))

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


for i in range(N+1):
    tmp = 0
    for j in range(i,N):
        tmp += S[j] * (10 ** (N-1-j))
    U.append(tmp % P)
    
def solve(U,S):
    if P == 2 or P == 5:
        return solve1(S)
    else:
        return solve2(U)
    
def solve1(S):
    ans = 0
    for i in range(N):
        if S[i] % P == 0:
            ans += i+1
    return ans

def solve2(U):
    ans = 0
    cnt = [0 for i in range(P)]
    for i in range(N+1):
        cnt[U[i]] += 1
    for i in range(P):
        p = cnt[i]
        ans += (p * (p-1)) // 2
    return ans

print(solve(U,S))

Fixed submission:
N, P = map(int, input().split())
S = list(map(int,list(input())))
U = [0 for i in range(N+1)]
POW = [1 for i in range(N)]

for i in range(N-1):
    POW[i+1] = (POW[i]*10) % P

for i in range(N):
    U[N-(i+1)] = (U[N-i] + S[N-1-i] * POW[i]) % P
    
def solve(U,S):
    if P == 2 or P == 5:
        return solve1(S)
    else:
        return solve2(U)
    
def solve1(S):
    ans = 0
    for i in range(N):
        if S[i] % P == 0:
            ans += i+1
    return ans

def solve2(U):
    ans = 0
    cnt = [0 for i in range(P)]
    for i in range(N+1):
        cnt[U[i]] += 1
    for i in range(P):
        p = cnt[i]
        ans += (p * (p-1)) // 2
    return ans

print(solve(U,S))

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


for i in range(N+1):
    tmp = 0
    for j in range(i,N):
        tmp += S[j] * (10 ** (N-1-j))
    U.append(tmp % P)
    
def check1(i,j,P,S):
    return S[j] % P == 0

def check2(i,j,P,U):
    num = U[i]-U[j+1]
    return num == 0

def check(i,j,P,U,S):
    if P == 2 or P == 5:
        return check1(i,j,P,S)
    else:
        return check2(i,j,P,U)

ans = 0
for i in range(N):
    for j in range(i,N):
        if check(i,j,P,U,S):
            ans += 1

print(ans)

Fixed submission:
N, P = map(int, input().split())
S = list(map(int,list(input())))
U = [0 for i in range(N+1)]
POW = [1 for i in range(N)]

for i in range(N-1):
    POW[i+1] = (POW[i]*10) % P

for i in range(N):
    U[N-(i+1)] = (U[N-i] + S[N-1-i] * POW[i]) % P
    
def solve(U,S):
    if P == 2 or P == 5:
        return solve1(S)
    else:
        return solve2(U)
    
def solve1(S):
    ans = 0
    for i in range(N):
        if S[i] % P == 0:
            ans += i+1
    return ans

def solve2(U):
    ans = 0
    cnt = [0 for i in range(P)]
    for i in range(N+1):
        cnt[U[i]] += 1
    for i in range(P):
        p = cnt[i]
        ans += (p * (p-1)) // 2
    return ans

print(solve(U,S))

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

for i in range(N+1):
    tmp = 0
    for j in range(i,N):
        tmp += S[j] * (10 ** (N-1-j))
    U.append(tmp)
    
def check(i,j,P,U):
    num = (U[i]-U[j])//10 ** (N - j)
    return num % P == 0


ans = 0
for i in range(N):
    for j in range(i+1,N+1):
        if check(i,j,P,U):
            ans += 1

print(ans)

Fixed submission:
N, P = map(int, input().split())
S = list(map(int,list(input())))
U = [0 for i in range(N+1)]
POW = [1 for i in range(N)]

for i in range(N-1):
    POW[i+1] = (POW[i]*10) % P

for i in range(N):
    U[N-(i+1)] = (U[N-i] + S[N-1-i] * POW[i]) % P
    
def solve(U,S):
    if P == 2 or P == 5:
        return solve1(S)
    else:
        return solve2(U)
    
def solve1(S):
    ans = 0
    for i in range(N):
        if S[i] % P == 0:
            ans += i+1
    return ans

def solve2(U):
    ans = 0
    cnt = [0 for i in range(P)]
    for i in range(N+1):
        cnt[U[i]] += 1
    for i in range(P):
        p = cnt[i]
        ans += (p * (p-1)) // 2
    return ans

print(solve(U,S))

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

for i in range(N+1):
    tmp = 0
    for j in range(i,N):
        tmp += S[j] * (10 ** (N-1-j))
    U.append(tmp)
    
def check(i,j,P,U):
    num = (U[i]-U[j])//10 ** (N - j)
    return num % P == 0


ans = 0
for i in range(N):
    for j in range(i+1,N+1):
        if check(i,j,P,U):
            ans += 1

print(ans)

Fixed submission:
N, P = map(int, input().split())
S = list(map(int,list(input())))
U = [0 for i in range(N+1)]
POW = [1 for i in range(N)]

for i in range(N-1):
    POW[i+1] = (POW[i]*10) % P

for i in range(N):
    U[N-(i+1)] = (U[N-i] + S[N-1-i] * POW[i]) % P
    
def solve(U,S):
    if P == 2 or P == 5:
        return solve1(S)
    else:
        return solve2(U)
    
def solve1(S):
    ans = 0
    for i in range(N):
        if S[i] % P == 0:
            ans += i+1
    return ans

def solve2(U):
    ans = 0
    cnt = [0 for i in range(P)]
    for i in range(N+1):
        cnt[U[i]] += 1
    for i in range(P):
        p = cnt[i]
        ans += (p * (p-1)) // 2
    return ans

print(solve(U,S))