Problem: Takahashi has a string S of length N consisting of digits from `0` through `9`.  He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S \- there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten.  Here substrings starting with a `0` also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers.  Compute this count to help Takahashi.

Submission attempts (in order):
p = int(input().split()[1])
s = input()
if 10 % p:
  s = reversed(s)
a = enumerate(s, 1)
if 10 % p == 0:
  r = sum(i for i, x in a if x % p == 0)
else:
  d = [1] + [0] * (p - 1)
  t, y = 0, 1
  for i, x in a:
    t = (t + x * y) % p
    d[t] += 1
    y = y * 10 % p;
  r = sum(i * (i - 1) // 2 for i in d)
print(r)


p = int(input().split()[1])
a = list(map(int, input())
if 10 % p == 0:
  r = sum(i for i, x in enumerate(a, 1) if x % p == 0)
else:
  d = [1] + [0] * (p - 1)
  t, y = 0, 1
  for i, x in enumerate(reversed(a), 1):
    t = (t + x * y) % p
    d[t] += 1
    y = y * 10 % p;
  r = sum(i * (i - 1) // 2 for i in d)
print(r)


p = int(input().split()[1])
s = input()
#if 10 % p:

a = enumerate(s, 1)
if 10 % p == 0:
  r = sum(i for i, x in a if x % p == 0)
else:
  d = [1] + [0] * (p - 1)
  t, y = 0, 1
  for i, x in a:
    t = (t + x * y) % p
    d[t] += 1
    y = y * 10 % p;
  r = sum(i * (i - 1) // 2 for i in d)
print(r)


p = int(input().split()[1])
s = input()
if 10 % p == 0:
  r = sum(i for i, x in enumerate(s, 1) if int(x) % p == 0)
else:
  d = [1] + [0] * (p - 1)
  t = 0
  for x in reversed(s):
    t = (t + int(x) * y) % p
    d[t] += 1
    y = y * 10 % p;
  r = sum(i * (i - 1) for i in d) // 2
print(r)

p = int(input().split()[1])
s = input()
if 10 % p == 0:
  r = sum(i for i, x in enumerate(s, 1) if int(x) % p == 0)
else:
  d = [1] + [0] * (p - 1)
  s, y = 0, 1
  for x in reversed(s):
    s = (s + int(x) * y) % p
    d[s] += 1
    y = y * 10 % p;
  r = sum(i * (i - 1) // 2 for i in d)
print(r)

from itertools import accumulate
from collections import Counter

I = lambda s:map(int, s)

p = int(input().split()[1])
s = input()
if 10 % p == 0:
  r = sum(i for i, x in enumerate(I(s), 1) if x % p == 0)
else:
  c = Counter()
  c[0] = 1
  y = 1
  for i, x in enumerate(I(reversed(s)), 1):
    c[x * y % p] += 1
  r = sum(i * (i - 1) // 2 for i in c.values())
print(r)


from itertools import accumulate
from collections import Counter

p = int(input().split()[1])
a = map(int, input())
if 10 % p == 0:
  r = sum(i for i, x in enumerate(a, 1) if x % p == 0)
else:
  a = accumulate(x * pow(10, i, p) for i, x in enumerate(reversed(a), 1))
  c = Counter(x % p for x in a) 
  c[0] += 1
  r = sum(i * (i - 1) // 2 for i in c.values())
print(r)


p = int(input().split()[1])
s = input()
if 10 % p == 0:
  r = sum(i for i, x in enumerate(s, 1) if int(x) % p == 0)
else:
  d = [1] + [0] * (p - 1)
  s, y = 0, 1
  for x in reversed(s):
    t = (t + int(x) * y) % p
    d[t] += 1
    y = y * 10 % p;
  r = sum(i * (i - 1) // 2 for i in d)
print(r)

from itertools import accumulate
from collections import Counter

p = int(input().split()[1])
a = list(map(int, input())
if 10 % p == 0:
  r = sum(i for i, x in enumerate(a, 1) if x % p == 0)
else:
  a = accumulate(x * pow(10, i, p) for i, x in enumerate(reversed(a), 1))
  c = Counter(x % p for x in a) 
  c[0] += 1
  r = sum(i * (i - 1) // 2 for i in c.values())
print(r)


p = int(input().split()[1])
s = input()
if 10 % p == 0:
  r = sum(i for i, x in enumerate(s, 1) if int(x) % p == 0)
else:
  d = [1] + [0] * (p - 1)
  t, y = 0, 1
  for x in reversed(s):
    t = (t + int(x) * y) % p
    d[t] += 1
    y = y * 10 % p;
  r = sum(i * (i - 1) for i in d) // 2
print(r)

Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Wrong Answer

Runtime Error

Runtime Error

Runtime Error

Accepted