Harryxun/stf_run huggingface dataset:
w = map(int, input().split())
n = int(input())
l = list(map(int, input().split()))
 
ans = []
for i in range(n):
  ans += [i+1]*l[i]
 
for i in range(h):
  temp = ans[i*w:(i+1)*w]
  if i%2 == 0:
    print(*temp)
  else:
    temp = reversed(temp)
    print(*temp)

### FIXED CODE ###

h, w = map(int, input().split())
n = int(input())
l = list(map(int, input().split()))

ans = []
for i in range(n):
  ans += [i+1]*l[i]

for i in range(h):
  temp = ans[i*w:(i+1)*w]
  if i%2 == 0:
    print(*temp)
  else:
    temp = reversed(temp)
    print(*temp)
def solve():
    N = int(input())
    S = input()

    AW = [0]*(N+1)
    AE = [0]*(N)

    wcount = 0
    ecount = 0
    for i in range(N):
        if S[i] == "W":
            wcount += 1
        else:
            ecount += 1
        AW[i] = wcount
        AE[i] = ecount

    MIN = 1000000

    for i in range(N):
        tmp = 0
        tmp += AW[i-1]
        tmp += AE[-1] - AE[i]
        MIN = min(MIN, tmp)
    print(MIN)

if __name__ == "__main__":
    local_test()
    solve()

### FIXED CODE ###

def solve():
    N = int(input())
    S = input()

    AW = [0]*(N+1)
    AE = [0]*(N)

    wcount = 0
    ecount = 0
    for i in range(N):
        if S[i] == "W":
            wcount += 1
        else:
            ecount += 1
        AW[i] = wcount
        AE[i] = ecount

    MIN = 1000000

    for i in range(N):
        tmp = 0
        tmp += AW[i-1]
        tmp += AE[-1] - AE[i]
        MIN = min(MIN, tmp)
    print(MIN)

if __name__ == "__main__":
    solve()
def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx as nx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
a,b=n1()
# from math import gcd
from fractions import gcd
m=gcd(a,b)

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors

def primes(n):
    num = [True]*n
    num[0] = num[1] = False
    for i in range(2,int(n**0.5)+1):
        if num[i]:
            for j in range(i**2, n, i):
                num[j] = False
    return [i for i in range(2,n) if num[i]]

gcd_list=make_divisors(m)
if len(gcd_list)==1:
    print(1)
else:
    prime_list=primes(gcd_list[-1])
    ans=1
    for i in gcd_list[1:]:
        if i in prime_list:
            ans+=1
    print(ans)

### FIXED CODE ###


def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx as nx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
a,b=n1()
# from math import gcd
from fractions import gcd
m=gcd(a,b)

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors

gcd_list=make_divisors(m)
if len(gcd_list)==1:
    print(1)
else:
    prime=[]
    for i in gcd_list[1:]:
        for j in prime:
            if i%j==0:
                break
        else:
            prime.append(i)
    print(len(prime)+1)
words = list(input())
if words[3] == words[4] and words[5]== words[6]:
    print("Yes")
else:
    print("No")

### FIXED CODE ###

words = list(input())
if words[2] == words[3] and words[4]== words[5]:
    print("Yes")
else:
    print("No")
n, q = map(int, input().split())
stx = [list(map(int, input().split())) for _ in range(n)]
d = [int(input()) for _ in range(q)]

l = d.copy()

N = len(l)
LV = (N-1).bit_length()
N0 = 2**LV
data = [float('inf')]*(2*N0)
lazy = [None]*(2*N0)

# 伝搬対象の区間を求める
def gindex(l, r):
    L = (l + N0) >> 1; R = (r + N0) >> 1
    lc = 0 if l & 1 else (L & -L).bit_length()
    rc = 0 if r & 1 else (R & -R).bit_length()
    for i in range(LV):
        if rc <= i:
            yield R
        if L < R and lc <= i:
            yield L
        L >>= 1; R >>= 1

# 遅延伝搬処理
def propagates(*ids):
    for i in reversed(ids):
        v = lazy[i-1]
        if v is None:
            continue
        lazy[2*i-1] = data[2*i-1] = lazy[2*i] = data[2*i] = v
        lazy[i-1] = None

# 区間[l, r)をxで更新
def update(l, r, x):
    *ids, = gindex(l, r)
    propagates(*ids)

    L = N0 + l; R = N0 + r
    while L < R:
        if R & 1:
            R -= 1
            lazy[R-1] = data[R-1] = x
        if L & 1:
            lazy[L-1] = data[L-1] = x
            L += 1
        L >>= 1; R >>= 1
    for i in ids:
        data[i-1] = min(data[2*i-1], data[2*i])

# 区間[l, r)内の最小値を求める
def query(l, r):
    propagates(*gindex(l, r))
    L = N0 + l; R = N0 + r

    s = float('inf')
    while L < R:
        if R & 1:
            R -= 1
            s = min(s, data[R-1])
        if L & 1:
            s = min(s, data[L-1])
            L += 1
        L >>= 1; R >>= 1
    return s

from bisect import bisect_left

stx.sort(key=lambda x:x[2], reverse=True)
for s, t, x in stx:
    i1 = bisect_left(l, s - 0.5 - x)
    i2 = bisect_left(l, t - 0.5 - x)
    update(i1, i2, x)

for i in range(q):
    tmp = query(i, i+1)
    if tmp == float('inf'):
        print(-1)
    else:
        print(tmp)


### FIXED CODE ###

def update(l, r, v):
    L = l + N0; R = r + N0
    while L < R:
        if R & 1:
            R -= 1
            data[R-1] = v

        if L & 1:
            data[L-1] = v
            L += 1
        L >>= 1; R >>= 1
# a_iの現在の値を取得
def _query(k):
    k += N0-1
    s = INF
    while k >= 0:
        if data[k]:
            s = max(s, data[k])
        k = (k - 1) // 2
    return s
# これを呼び出す
def query(k):
    return _query(k)[1]

from bisect import bisect_left
from sys import stdin

if __name__ == '__main__':
    n, q = map(int, stdin.readline().rstrip().split())
    stx = [list(map(int, stdin.readline().rstrip().split())) for _ in range(n)]
    l = [int(stdin.readline().rstrip()) for _ in range(q)]

    INF = (-1, 2**31-1)
    N = len(l)
    N0 = 2**(N-1).bit_length()
    data = [None]*(2*N0)

    stx.sort(key=lambda x:x[2], reverse=True)
    for i, (s, t, x) in enumerate(stx):
        i1 = bisect_left(l, s - 0.5 - x)
        i2 = bisect_left(l, t - 0.5 - x)
        update(i1, i2, (i, x))

    for i in range(q):
        tmp = query(i)
        if tmp == INF[1]:
            print(-1)
        else:
            print(tmp)
def common_divisor(a, b):
    # a <= b
    res = []
    for i in range(2, int(a**0.5)+1):
        if a % i == 0 and b % i == 0:
            res.append(i)
            t = a // i
            if i != t and b % t == 0:
                res.append(t)
    return sorted(res)

def primes_num(arr):
    res = 0
    TESU = int(arr[-1]**0.5)+1
    for i in arr:
        flg = True
        rt_i = i**0.5
        for j in range(2, TESU):
            if rt_i < j:
                break
            elif i % j == 0:
                flg = False
                break
        if flg: res += 1
    return res

A, B = sorted([int(i) for i in input().split()])
# A <= B

cds = common_divisor(A, B)
print(primes_num(cds)+1)


### FIXED CODE ###

A, B = sorted([int(i) for i in input().split()])

res = 1
for i in range(2, int(B**0.5)+1):
    if B % i == 0:
        if A % i == 0:
            for j in range(2, int(i**0.5)+1):
                if i % j == 0:
                    break
            else:
                res += 1
        t = B // i
        if t != i and A % t == 0:
            for j in range(2, int(t**0.5)+1):
                if t % j == 0:
                    break
            else:
                res += 1

print(res if res > 1 else (1 if (A == B == 1) or A != B else 2))
S = input()

if (s=='AAA') or (S=='BBB'):
  print('No')
  exit()
else:
  print('Yes')

### FIXED CODE ###

s = input()

if (s == 'AAA' ) or (s == 'BBB') :
  print('No')
else:
  print('Yes')
A,B,C,D = map(int,input().split())
while True:
  C = C - B
  if C <= 0:
    break
  A = A - D
  if A <= 0:
    break
print('Yes' if A >= C else 'No')

### FIXED CODE ###

S, W = map(int,input().split())
print('unsafe' if S <= W else 'safe')
N = int(input())
S = list(input())
count_E = S.count("E")
count_W = 0
count = count_E + count_W
ans = count + 1
for e in range S:
    if e == "E":
        count_E -= 1
    elif e == "w":
        count_W += 1
    if count < ans :
        ans = count
print(ans)

### FIXED CODE ###

N = int(input())
S = list(input())
count_E = S.count("E")
count_W = 0
ans = count_E
for i in range(N):
    if S[i] == "E":
        count_E -= 1
    if S[i-1] == "W" and i != 0:
        count_W += 1
    if count_E + count_W < ans :
        ans = count_E + count_W
print(ans)
#ABC145 E
N,T = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(N)]
ab.sort()

a = []
b = []
for ai, bi in ab:
    a.append(ai)
    b.append(bi)
    
dp = [[0 for j in range(T)] for _ in range(N+1)]
# i番目より前まで注文してT分以内(max T-1)で完食する
    
for i in range(N):
    for t in range(T): 
        if t+a[i] <= T-1:
        	dp[i+1][t+a[i]] =max(dp[i+1][t+a[i]],dp[i][t]+b[i])
        
        dp[i+1][t] = max(dp[i+1][t],dp[i][t])

#一番遅いものをラスト(T-1分)に注文
#i番目をラストに注文 = dp[i][t-1] + b[i]
ans = 0
for i in range(N):
    ans = max(ans,dp[i][t-1]+b[i])
    
print(ans)


### FIXED CODE ###

n = int(input())

print(n**2)
n, x = map(int, input().split())
l = list(map(int, input().split()))

count = 0
sum = 0

for i in range(len(l)):
  sum += l[i]
  if sum < x
    count+=1
print(count)

### FIXED CODE ###

n, x = map(int, input().split())
l = list(map(int, input().split()))

count = 0
sum = 0

for i in range(len(l)):
  sum += l[i]
  if sum <= x:
    count+=1
print(count + 1)
S = input() 
K = int(input())

count = 0
S = S + "0"

i = 0
while i < len(S)-2:
    if S[i] == S[i+1]:
        count += 1
        i += 2
        """
        if S[i] == S[i+2]:
            i += 2
        else:
            i += 2
        """
    else:
        i += 1

S = S[:-1] #最後の0を削除

if len(S) == 1:
    result = K
elif len(S) == 2:
    result = count * K
else: #S[0]からの連続文字列数とS[-1]からの連続文字列数がどちらも奇数ならば「+ 1 × (K-1)」が必要
    if S[0] == S[-1]:
        len_first = 1
        i = 0
        while S[i] == S[i+1]:
            len_first += 1
            i += 1
        
        len_last = 1
        i = -1
        while S[i] == S[i-1]:
            len_last += 1
            i -= 1
        
        if len_first % 2 == 1 and len_last % 2 == 1:
            result = count * K + (K-1)
        else:
            result = count * K

              
        """
        if S[0] != S[1] and S[-1] != S[-2]:
            result = count * K + (K-1)
            #print("if1")
        elif S[0] == S[1] == S[2] and S[-1] != S[-2]: 
            result = count * K + (K-1)
            #print("if2")
        elif S[-1] == S[-2] == S[-3] and S[0] != S[1]: 
            result = count * K + (K-1)
            #print("if3")    
        else:
            result = count * K
            #print("else1")
        """

    else:
        result = count * K
        #print("else2")


print(result)




### FIXED CODE ###

S = input()
K = int(input())
li = []
num = 1

#数えるセクション
for i in range(len(S)-1): #もしlen(S)==1の場合、ループは一回も回らない。（エラーもでない？）
    if S[i] == S[i+1]:
        num += 1
    else:
        li.append(num)
        num = 1
li.append(num)
#print(li)

#合計セクション
g = 0
for l in li:
    g += (l // 2)
#print(g)

#解答セクション
tmp = 0
result = 0
if len(li) == 1:
    result = (li[0] * K) // 2
else:
    tmp = sum(j // 2 for j in li)
    if S[0] != S[-1]:
        result = tmp * K
    else:
        if li[0] % 2 == li[-1] % 2 == 1: #最初と最後がどちらも奇数個の連続ならば、プラス1する必要がある。
            result = tmp * K + (1 * (K-1))
        else:
            result = tmp * K

print(result)
n = int(input())
nums = list(map(int,input().split()))
nums.sort()
ans = 0
cnt = {}
flag = [0]*1000005
for i in range(1,len(nums)):
  cnt[i] = cnt.get(i,0) + 1

    
for i in nums:
  if flag[i] == 0 and cnt[i] == 1:
    ans += 1
  for j in range(i*2,1000000,i):
    flag[j] = 1

print(ans)

### FIXED CODE ###

n = int(input())
nums = list(map(int,input().split()))
nums.sort()
ans = 0
cnt = {}
flag = [0]*1000005
for i in nums:
  cnt[i] = cnt.get(i,0) + 1

for i in nums:
  if flag[i] == 0 and cnt[i] == 1:
    ans += 1
  if flag[i] == 1:
    continue
  for j in range(i,1000001,i):
    flag[j] = 1

print(ans)
in, k = map(int, input().split())
h = [int(i) for i in input().split()]
def chmin(a, b):
    if a > b:
        return b
    else:
        return a
f_inf = float('inf')
dp = [f_inf] * (10**5+10)
dp[0] = 0
for i in range(1, n):
    for j in range(1, k+1):
        if i - j >= 0:
            dp[i] = chmin(dp[i], dp[i-j]+ abs(h[i] - h[i - j]))
print(dp[n-1])


### FIXED CODE ###

import numpy as np
import sys
input = sys.stdin.readline

n, k = map(int, input().split())
h = [int(i) for i in input().split()]

f_inf = float('inf')
dp = np.zeros(n, dtype=int)
h = np.array(h)
for i in range(1, n):
    start = max(0, i - k)
    dp[i] = min(dp[start: i] + np.abs(h[i] - h[start: i]))

print(dp[n-1])
n=int(input())
v=list(map(int, input().split()))
import collections
v1=v[0::2]
v11=collections.Counter(v1)
v2=v[1::2]
v22=collections.Counter(v2)
ans=0
o=v11.most_common()[0][0]
e=v22.most_common()[0][0]
if e!=o:
    for x in v1:
        if x!=o:
            ans+=1
    for y in v2:
        if y!=e:
            ans+=1
else:
    if v1.count(o)>v2.count(e):
        for x in v1:
            if x!=o:
                ans+=1
        for y in v2:
            if y!=v22.most_common()[1][0]:
                ans+=1
    else:
        for x in v1:
            if x!=v11.most_common()[1][0]:
                ans+=1
        for y in v2:
            if y!=v2:
                ans+=1
print(ans)

### FIXED CODE ###

n=int(input())
v=list(map(int, input().split()))
import collections
v1=v[0::2]
v11=collections.Counter(v1)
v2=v[1::2]
v22=collections.Counter(v2)
o=v11.most_common()+([(0,0)])
e=v22.most_common()+([(0,0)])
if o[0][0]!=e[0][0]:
    print(n-o[0][1]-e[0][1])
else:
    print(min(n-o[0][1]-e[1][1],n-o[1][1]-e[0][1]))
n = int(input())
a = list(map(int, input().split()))
ai = max(a)
aj = min(filiter(lambad x: x!=ai, a), key=lambda x: abs(x-ai//2))
print("{} {}".format(ai, aj))


### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
ai = max(a)
aj = min((item for item in a if item != ai), key=lambda x: abs(x-ai//2))
print("{} {}".format(ai, aj))
#数値型を分割して取り出し
A, V = map(int, input().split())
B, W = map(int, input().split())
#数値型の取り出し
T = int(input())
AA = A
BB = B
ans = "NO"
for i in range(1,T+1):
  AA = AA + V
  BB = BB + W
  if AA == BB:
    ans = "YES"
    break
print(ans)

### FIXED CODE ###

#文字型の取り出し
s = input()

print(s[:3])
n = int(input())
k = int(input())
x = int(input())
y = int(input())

for i in range(n):
  if i == k+1:
    ans += y;
  else:
    ans += x
    
print(ans)

### FIXED CODE ###

n = int(input())
k = int(input())
x = int(input())
y = int(input())

ans = 0
for i in range(1,n+1):
  if i >= k+1:
    ans += y
  else:
    ans += x
    
print(ans)
# B - Balance
N = int(input())
W = list(map(int, input().split()))
sum1 = 0

i = 0
while sum1 <= sum(W)/2:
    sum1 += W[i]
    i += 1
    sum2 = sum1 + W[i+1]

ans = min(abs(sum(W)-sum1-sum1),abs(sum(W)-sum2-sum2))

#ans = abs(sum(W) - sum1-sum1)
print(ans)

### FIXED CODE ###

# B - Balance
N = int(input())
W = list(map(int, input().split()))
sum_l = 0
sum_r = sum(W)
abs_W =[]
for i in range(N):
    sum_l += W[i]
    sum_r -= W[i]
    abs_W.append(abs(sum_r - sum_l))

print(min(abs_W))
import heapq

n=int(input())
d=list(map(int,input().split()))
m=int(input())
t=list(map(int,input().split()))

heapq.heapify(d)
heapq.heapify(t)
cnt=0

if n>1 and m>1:
    for i in range(m):
        a=heapq.heappop(t)
        b=heapq.heappop(d)
        #print(a,b,cnt)
        while b<a:
            b=heapq.heappop(d)
        if b==a:
            cnt+=1
    if cnt==m:
        print("YES")
    else:
        print("NO")

else:
    if n==1:
        print("NO")
    else:
        if t[0] in d:
            print("YES")
        else:
            print("NO")
        

        
        

### FIXED CODE ###

import heapq

n=int(input())
d=list(map(int,input().split()))
m=int(input())
t=list(map(int,input().split()))

heapq.heapify(d)
heapq.heapify(t)
cnt=0


if n>1 and m>1:
    for i in range(m):
        a=heapq.heappop(t)
        if len(d)>0:
            b=heapq.heappop(d)
        #print(a,b,cnt)
        while b<a and len(d)>0:
            b=heapq.heappop(d)
        if b==a:
            cnt+=1
    if cnt==m:
        print("YES")
    else:
        print("NO")

else:
    if n==1:
        if m==1 and t[0]==d[0]:
            print("YES")
        else:
            print("NO")
    else:
        if t[0] in d:
            print("YES")
        else:
            print("NO")
N,M = map(int,input().split())
X = list(map(int,input().split()))

if M == 1 and N > 0:
    print(0)
    exit()

X = sorted(X)
s = []
for i in range(len(X) - 1):
    s.append(abs(X[i+1]- X[i]))
ans = sum(s)
s = sorted(s)
for i in range(1,N):
    ans -= s[-i]
print(ans)


### FIXED CODE ###

N,M = map(int,input().split())
X = list(map(int,input().split()))

if M == 1 and N > 0:
    print(0)
    exit()
if M <= N:
    print(0)
    exit()


X = sorted(X)
s = []
for i in range(len(X) - 1):
    s.append(abs(X[i+1]- X[i]))
ans = sum(s)
s = sorted(s)
for i in range(1,N):
    ans -= s[-i]
print(ans)
k=int(input())
 
 
init=7
for i in range(k+1):
  if init%k ==0:
    print(i+1)
    break
  t = t*10+7
print(-1)

### FIXED CODE ###

k=int(input())
 
ans=-1
init=7
for i in range(k+1):
  if init%k ==0:
    ans=i+1
    break
  init = (init*10+7)%k
print(ans)
n = int(input())
a = list(map(int,input().split()))
a.sort()
k = 1
twos = []
while k<2*10**5+1:
    twos += [k]
    k = k<<1
l = []
key = a[-1]
a.pop()
ans = 0
while True:
    for k in range(len(twos)-1):
        if twos[k]<key and key<twos[k+1]:
            two = twos[k+1]
            break
    for j in range(len(a)):
        if a[j]==two-key:
            a.remove(a[j])
            ans +=1
            break
    if not a==[]:
        key = a[-1]
        a.pop()
    else:
        break
print(ans)

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
a.sort()
from collections import Counter
dic = Counter(a)
ans = 0
for k in range(len(a)-1,-1,-1):
    if dic[a[k]]==0:
        continue
    dic[a[k]] -= 1
    t = 2**a[k].bit_length()-a[k]
    if dic[t]:
        dic[t] -= 1
        ans += 1
print(ans)
import numpy as np# your code goes here
def eucrid(a, b):
	if (a > b):
		buf = a
		a = b
		b = buf
	if (b % a == 0):
		return a
	else:
		return eucrid(b%a, a)
		
def main():
	buf = input().split()
	s = input()
	t = input()
	n = len(s)
	m = len(t)
	
	big = n*m//eucrid(n, m)
	flag = big
	for i in range(big):
		if (i%(big//n) == 0 and i%(big//m) == 0):
			#print(i, n, m)
			#print((i+1)//(big//n), (i+1)//(big//m))
			if(s[(i+1)//(big//n)] != t[(i+1)//(big//m)]):
				flag = -1
				
	print(flag)
	#print(s, t, n, m)
	
	
main()

### FIXED CODE ###

import numpy as np# your code goes here
def eucrid(a, b):
	if (a > b):
		buf = a
		a = b
		b = buf
	if (b % a == 0):
		return a
	else:
		return eucrid(b%a, a)
		
def main():
	buf = input().split()
	s = input()
	t = input()
	n = len(s)
	m = len(t)
	
	big = n*m//eucrid(n, m)
	flag = big
	"""
	for i in range(big):
		if (i%(big//n) == 0 and i%(big//m) == 0):
			#print(i, n, m)
			#print((i+1)//(big//n), (i+1)//(big//m))
			if(s[(i+1)//(big//n)] != t[(i+1)//(big//m)]):
				flag = -1
	"""			
	for i in range(n):
		if((i*(big//n))%(big//m) == 0):
			if(s[i] != t[(i*(big//n))//(big//m)]):
				flag = -1

	print(flag)
	#print(s, t, n, m)
	
	
main()
from collections import defaultdict as dict

n, p = map(int, input().split())
s = input()
a = []
for x in s:
    a.append(int(x))

def solve():
    l = [0]*p
    ans = 0
    for x in a:
        l_ = [0] * p
        for i in range(p):
            l_[(i * 10 + x) % p] += l[i]
        l, l_ = l_, l
        l[x % p] += 1
        ans += l[0]
    print(ans)
if p <= 5:
    solve()
    exit()
exit(1)
x = 0
mem = dict(int)
mem[0] = 1
ans = 0
for y in a:
    x = (x * 10 + y) % p
    ans += mem[x]
    mem[x] += 1
    # print(mem)
print(ans)

### FIXED CODE ###

from collections import defaultdict as dict

n, p = map(int, input().split())
s = input()
a = []
for x in s:
    a.append(int(x))

def solve():
    l = [0]*p
    ans = 0
    for x in a:
        l_ = [0] * p
        for i in range(p):
            l_[(i * 10 + x) % p] += l[i]
        l, l_ = l_, l
        l[x % p] += 1
        ans += l[0]

    print(ans)
if p <= 5:
    solve()
    exit()
x = 0
mem = dict(int)
mem[0] = 1
ans = 0
a.reverse()
d = 1
for y in a:
    x += d*y
    x %= p
    d = (d * 10) % p
    ans += mem[x]
    mem[x] += 1
    # print(mem)

print(ans)
N, W = map(int, input().split())
 wv = [list(map(int, input().split())) for _ in range(N)]

 dp = []
 for _ in range(N+1):
     dp.append([0] * (W+1))

for i in range(N):
    for j in range(W+1):
        if j >= wv[i][0]:
            dp[i+1][j] = max(dp[i][j], dp[i][j - wv[i][0]] + wv[i][1])
        else:
            dp[i+1][j] = dp[i][j]
print(dp[N][W])

### FIXED CODE ###

# Educational DP Contest D-knapsack

def knapsack(N, W, wv):
    # initialize
    dp = [list(0 for _ in range(W+1)) for j in range(N+1)]
    
    # DP
    for i in range(N):
        for j in range(W+1):
            if j >= wv[i][0]:
                dp[i+1][j] = max(dp[i][j], dp[i][j - wv[i][0]] + wv[i][1])
            else:
                dp[i+1][j] = dp[i][j]
    return dp[N][W]

if __name__ == "__main__":
    N, W = map(int, input().split())
    wv = []
    for _ in range(N):
        wv.append(list(map(int, input().split())))
    print(knapsack(N, W, wv))
A,B = map(int, input(),split())
if A!=2 and B!=2:
  print('Yes')
else:
  print('No')

### FIXED CODE ###

A,B = map(int, input().split())
if A == 2 or B == 2:
    print("No")
else:
    print("Yes")
N = int(input())
A = list(map(int,input().split()))

X1by2 = 0
for i in range(N):
  if i%2 ==0:
    X1by2 += A[i]
  else:
    X1by2 -= A[i]

X = []

X.append(X1by2)
for i in range(len(A)-1):
  X.append((A[i]-X[i]/2)*2)

print(" ".join(repr(int(e)) for e in X))


### FIXED CODE ###

N, A, B = map(int,input().split())

train = N*A
taxi = B

if train>=taxi:
  print(taxi)
else:
  print(train)
def gcd(a,b):
  if b==0:
    return a
  else:
    return gcd(b,a%b)

def lcm(a,b):
  return (a*b)//gcd(a,b)

n,m=map(int,input().split())
arr=list(map(int,input().split()))
l=lcm(arr[0],arr[1])
for i in range(2,n):
  l=lcm(l,arr[i])
  if l//2>m:
    print(0)
    exit()
ans=(m-l//2)//l+1
print(ans)

### FIXED CODE ###

def gcd(a,b):
  if b==0:
    return a
  else:
    return gcd(b,a%b)

def lcm(a,b):
  return (a*b)//gcd(a,b)

n,m=map(int,input().split())
arr=list(map(int,input().split()))
if n==1:
  l=arr[0]
else:
  l=lcm(arr[0],arr[1])
for i in range(2,n):
  l=lcm(l,arr[i])
  if l//2>m:
    print(0)
    exit()
for i in range(n):
  if (l//arr[i])%2==0:
    print(0)
    exit()
ans=(m-l//2)//l+1
print(ans)
a, b = map(int, input().split())
print('Even' if num % 2 == 0 else 'Odd')

### FIXED CODE ###

a, b = map(int, input().split())
print('Even' if a * b % 2 == 0 else 'Odd')