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

# process
ans = 0
if P == 2 or P == 5:
    # 1桁目がPで割り切れる数をカウント
    for i in range(N):
        if S[i] % P == 0:
            ans += i+1

else:
    cnt = [0]*P # S[i:N]%Pの結果をカウント
    num = 0
    tenPow = 1
    for i in range(N):
        num = (num + S[N-i-1]*tenPow)%P
        tenPow *= 10%P
        cnt[num] += 1

    ans += cnt[0]
    for c in cnt:
        ans += c*(c-1)//2 # c=0,1のときは0加算

# output
print(ans)


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

# process
ans = 0
if P == 2 or P == 5:
    # 1桁目がPで割り切れる数をカウント
    for i in range(N):
        if int(S[i]) % P == 0:
            ans += i+1

else:
    cnt = [0]*P # S[i:N]%Pの結果をカウント
    num = 0
    tenPow = 1
    for i in reversed(range(N)):
        num = (num + int(S[i])*tenPow)%P
        tenPow = tenPow*10%P
        cnt[num] += 1

    ans += cnt[0]
    for c in cnt:
        ans += c*(c-1)//2 # c=0,1のときは0加算

# output
print(ans)

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

# process
ans = 0
if P == 2 or P == 5:
    # 1桁目がPで割り切れる数をカウント
    for i in range(N):
        if S[i] % P == 0:
            ans += i+1

else:
    cnt = [0]*P # S[i:N]%Pの結果をカウント
    num = 0
    for i in range(N):
        num += S[N-i-1]*10**i
        cnt[num%P] += 1

    ans += cnt[0]
    for c in cnt:
        ans += c*(c-1)//2 # c=0,1のときは0加算

# output
print(ans)


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

# process
ans = 0
if P == 2 or P == 5:
    # 1桁目がPで割り切れる数をカウント
    for i in range(N):
        if int(S[i]) % P == 0:
            ans += i+1

else:
    cnt = [0]*P # S[i:N]%Pの結果をカウント
    num = 0
    tenPow = 1
    for i in reversed(range(N)):
        num = (num + int(S[i])*tenPow)%P
        tenPow = tenPow*10%P
        cnt[num] += 1

    ans += cnt[0]
    for c in cnt:
        ans += c*(c-1)//2 # c=0,1のときは0加算

# output
print(ans)

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

# process
ans = 0
if P == 2 or P == 5:
    # 1桁目がPで割り切れる数をカウント
    S = list(map(int, S))
    for i in range(N):
        if S[i] % P == 0:
            ans += i+1

else:
    cnt = [0]*P # S[i:N]%Pの結果をカウント
    for i in range(N):
        cnt[int(S[i:N])%P] += 1

    ans += cnt[0]
    for c in cnt:
        if c > 1:
            ans += c*(c-1)//2

# output
print(ans)


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

# process
ans = 0
if P == 2 or P == 5:
    # 1桁目がPで割り切れる数をカウント
    for i in range(N):
        if int(S[i]) % P == 0:
            ans += i+1

else:
    cnt = [0]*P # S[i:N]%Pの結果をカウント
    num = 0
    tenPow = 1
    for i in reversed(range(N)):
        num = (num + int(S[i])*tenPow)%P
        tenPow = tenPow*10%P
        cnt[num] += 1

    ans += cnt[0]
    for c in cnt:
        ans += c*(c-1)//2 # c=0,1のときは0加算

# output
print(ans)

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

# process
sl = [] # S[i:N]のリスト
for i in range(N):
    sl.append(int(S[i:N]))
sl.append(0)

ans = 0
for i in range(N):
    for j in range(i+1, N+1):
        if (sl[i]-sl[j])//10**(N-j)%P == 0:
            ans += 1

# output
print(ans)

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

# process
ans = 0
if P == 2 or P == 5:
    # 1桁目がPで割り切れる数をカウント
    for i in range(N):
        if int(S[i]) % P == 0:
            ans += i+1

else:
    cnt = [0]*P # S[i:N]%Pの結果をカウント
    num = 0
    tenPow = 1
    for i in reversed(range(N)):
        num = (num + int(S[i])*tenPow)%P
        tenPow = tenPow*10%P
        cnt[num] += 1

    ans += cnt[0]
    for c in cnt:
        ans += c*(c-1)//2 # c=0,1のときは0加算

# output
print(ans)

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

# process
ans = 0
for i in range(N-1):
    mod = S[i]%P
    if mod == 0:
        ans += 1
    pmod = mod

    for j in range(i+1, N):
        mod = (S[j]+pmod*10)%P
        if mod == 0:
            ans += 1
        pmod = mod

if S[-1]%P == 0:
    ans += 1

# output
print(ans)

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

# process
ans = 0
if P == 2 or P == 5:
    # 1桁目がPで割り切れる数をカウント
    for i in range(N):
        if int(S[i]) % P == 0:
            ans += i+1

else:
    cnt = [0]*P # S[i:N]%Pの結果をカウント
    num = 0
    tenPow = 1
    for i in reversed(range(N)):
        num = (num + int(S[i])*tenPow)%P
        tenPow = tenPow*10%P
        cnt[num] += 1

    ans += cnt[0]
    for c in cnt:
        ans += c*(c-1)//2 # c=0,1のときは0加算

# output
print(ans)

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

# process
ans = 0
pmod = 0
for i in range(N-1):
    mod = int(S[i])%P
    if mod == 0:
        ans += 1
    pmod = mod

    for j in range(i+1, N):
        mod = (int(S[j])+pmod*10)%P
        if mod == 0:
            ans += 1
        pmod = mod

if int(S[-1])%P == 0:
    ans += 1

# output
print(ans)


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

# process
ans = 0
if P == 2 or P == 5:
    # 1桁目がPで割り切れる数をカウント
    for i in range(N):
        if int(S[i]) % P == 0:
            ans += i+1

else:
    cnt = [0]*P # S[i:N]%Pの結果をカウント
    num = 0
    tenPow = 1
    for i in reversed(range(N)):
        num = (num + int(S[i])*tenPow)%P
        tenPow = tenPow*10%P
        cnt[num] += 1

    ans += cnt[0]
    for c in cnt:
        ans += c*(c-1)//2 # c=0,1のときは0加算

# output
print(ans)

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

# process
def toNum(l):
    n = 0
    for i in range(len(l[::-1])):
        n += l[i]*10**i
    return n

ans = 0
for i in range(N):
    for j in range(i+1, N+1):
#        print("{} {} {}".format(i, j, S[i:j]))
        if toNum(S[i:j])%P == 0:
            ans += 1 

# output
print(ans)

# print("ans: {}".format(ans))


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

# process
ans = 0
if P == 2 or P == 5:
    # 1桁目がPで割り切れる数をカウント
    for i in range(N):
        if int(S[i]) % P == 0:
            ans += i+1

else:
    cnt = [0]*P # S[i:N]%Pの結果をカウント
    num = 0
    tenPow = 1
    for i in reversed(range(N)):
        num = (num + int(S[i])*tenPow)%P
        tenPow = tenPow*10%P
        cnt[num] += 1

    ans += cnt[0]
    for c in cnt:
        ans += c*(c-1)//2 # c=0,1のときは0加算

# output
print(ans)