Problem:
 Score : 400 points    Problem Statement Given is a string S consisting of digits from 1 through 9 .  Find the number of pairs of integers (i,j) ( 1 ≤ i ≤ j ≤ |S| ) that satisfy the following condition:  Condition: In base ten, the i -th through j -th characters of S form an integer that is a multiple of 2019 .      Constraints  1 ≤ |S| ≤ 200000  S is a string consisting of digits from 1 through 9 .         Input Input is given from Standard Input in the following format:  S       Output Print the number of pairs of integers (i,j) ( 1 ≤ i ≤ j ≤ |S| ) that satisfy the condition.        Sample Input 1 1817181712114      Sample Output 1 3  Three pairs - (1,5) , (5,9) , and (9,13) - satisfy the condition.       Sample Input 2 14282668646      Sample Output 2 2       Sample Input 3 2119      Sample Output 3 0  No pairs satisfy the condition.

Solution (Python):
S = input()

modList = [0] * 2019 

ans = 0

modList[0] = 1 














T = 0
a = 1
for s in S[::-1]:
    T = T+int(s) * a
    modList[T%2019]+=1
    a*=10
    a%=2019


for j in modList:
    if j <= 1:
        continue
    else:
        ans += j*(j-1)/2

print(int(ans))

Solution (Python):
S=input()[::-1]
l=[0]*2019
a=1
d=0



for s in S:
  d=d+int(s)*a
  l[d%2019]+=1
  a*=10
  a%=2019


ans=l[0]
for i in l:
  if i>=2:
    ans+=i*(i-1)//2
    
print(ans)

Solution (Python):
m=2019
l=[0]*m
a=b=0
k=1
for c in input():l[b%m]+=1;b-=int(c)*k;a+=l[b%m];k=k*202%m
print(a)

Solution (Python):
S = input()[::-1]

length = len(S)
dp = [0] * (length + 2)

times = 1
for x in range(1, length + 1):
    dp[x] += dp[x - 1]
    dp[x] %= 2019
    dp[x] += int(S[x - 1]) * times
    dp[x] %= 2019
    times *= 10
    times %= 2019


cnt = [0] * 2020
for x in range(length + 1):
    cnt[dp[x]] += 1

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

print(ans)

Solution (Python):
s = str(input()[::-1])
n=len(s)
mlist,m,d=[],0,1
  
for ss in s:
  m += int(ss) * d
  m %= 2019
  d *= 10
  d %= 2019
  mlist.append(m)
  
l=sorted(mlist)
l.append(2020)
pre,count,ans=0,0,0
for ll in l:
  if pre==ll:
    count+=1
  else:
    ans+=count*(count+1)//2
    pre=ll
    count=0
print(ans)

Solution (Python):
S = input()[::-1]
n = len(S)

cnt = [0] * 2019
cnt[0] = 1
m = 0
d = 1
for i in S:
  m += int(i) * d
  m %= 2019
  cnt[m] += 1
  d *= 10
  d %= 2019

ans=0
for i in cnt:
  ans += int(i*(i-1)/2)
print(ans)

Solution (Python):
s = input()[::-1] 

counts = [0] * 2019
counts[0] = 1

num, d = 0, 1

for char in s:
    num += int(char) * d
    num %= 2019
    d *= 10
    d%= 2019
    counts[num] += 1

ans = 0
for cnt in counts:
    ans += cnt * (cnt - 1) // 2

print(ans)

Solution (Python):
import time

s = input()[::-1] 

counts = [0] * 2019
counts[0] = 1

num, d = 0, 1

for char in s:
    num += int(char) * d
    num %= 2019
    d *= 10
    d%= 2019
    counts[num] += 1


ans = 0
for cnt in counts:
    ans += cnt * (cnt - 1) // 2

print(ans)

Solution (Python):

S = input()
ans = 0
P = 2019
N = len(S)
S = S[::-1]
cnt = [0 for _ in range(P)]
cnt[0] = 1
v = 0
bai = 1
for i in range(N):
    v += int(S[i]) * bai
    v %= P
    bai *= 10
    bai %= P
    cnt[v] += 1
for i in range(P):
    ans += ((cnt[i] - 1) * cnt[i]) // 2
print(ans)

Solution (Python):

S = input()
ans = 0
P = 2019
N = len(S)
S = S[::-1]
cnt = [0 for _ in range(P)]
cnt[0] = 1
v = 0
bai = 1
for i in range(N):
    v = (v + int(S[i]) * bai) % P
    bai *= 10
    bai %= P
    cnt[v] += 1
for i in range(P):
    ans += ((cnt[i] - 1) * cnt[i]) // 2
print(ans)

Solution (Python):
s = input()[::-1]
mod_cnt = [0] * 2019
mod_cnt[0] = 1
t = 0
d = 1
for c in s:
    t += int(c) * d
    t %= 2019
    mod_cnt[t] += 1
    d *= 10
    d %= 2019
print(sum([m * (m - 1) // 2 for m in mod_cnt]))

Solution (Python):
import sys
input = sys.stdin.readline
s = input().strip()
c = 0
chk = [0]*2019
for i,si in enumerate(s[::-1]):
  c += int(si)*pow(10,i,2019)
  chk[c%2019] += 1
ans = chk[0]
for i in chk:
  ans += i*(i-1)//2
print(ans)

Solution (Python):

S = input()
ans = 0
P = 2019
N = len(S)
S = S[::-1]
dp = [0 for _ in range(N)]
d = {}
for i in range(N):
    dp[i] = int(S[i]) * pow(10, i, P) % P
for i in range(N-1):
    dp[i+1] += dp[i]
    dp[i+1] %= P
dp[-1] %= P
for i in range(N):
    if dp[i] not in d:
        if dp[i] == 0:
            ans += 1
        d[dp[i]] = 1
    else:
        if dp[i] == 0:
            ans += 1
        ans += d[dp[i]]
        d[dp[i]] += 1
print(ans)

Solution (Python):
def main():
    S = input()[::-1]
    ans = 0
    mods = [0] * 2019
    mods[0] = 1
    current = 0
    x = 1
    for s in S:
        current = (current + x * int(s)) % 2019
        ans += mods[current % 2019]
        mods[current % 2019] += 1
        x = x * 10 % 2019
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
s = input()
sr = s[::-1]
mod = [0]*2019
mod[0] += 1
T = 0
d = 1 
for s in sr:
  T = T + int(s) * d
  T %= 2019
  mod[T] += 1
  d = (d * 10) % 2019
  
res = [m*(m-1)//2 for m in mod]
print(sum(res))

Solution (Python):
S = list(map(int,input()))[::-1] 

counter = 0
amari = [0]*2019
amari[0] = 1

s,d = 0,1

for i in S:
    s += i *d
    amari[s%2019] += 1
    d *= 10
    d %= 2019


for i in range(2019):
    counter += amari[i] * (amari[i]-1) / 2


print(int(counter))

Solution (Python):

import sys
from collections import defaultdict

S=sys.stdin.readline().strip()
S=S[::-1]


L=[0] 
val=0
for i,x in enumerate(S):
    val+=pow(10,i,2019)*int(x)
    L.append(val%2019)

C=defaultdict(lambda: 0)    
ans=0
for x in L:
    ans+=C[x]   
    C[x]+=1     
print ans

Solution (Python):

import sys
from collections import defaultdict

N=sys.stdin.readline().strip()
N=N[::-1]


L=[0] 
val=0
for i,x in enumerate(N):
    val+=pow(10,i,2019)*int(x)
    L.append(val%2019)

C=defaultdict(lambda: 0)    
ans=0
for x in L:
    ans+=C[x]   
    C[x]+=1     
print ans

Solution (Python):

import sys
from collections import defaultdict

N=sys.stdin.readline().strip()
N=N[::-1]


L=[0] 
val=0
for i,x in enumerate(N):
    val+=pow(10,i,2019)*int(x)
    L.append(val%2019)

C=defaultdict(lambda: 0)    
ans=0
for x in L:
    ans+=C[x]   
    C[x]+=1     
print ans

Solution (Python):
s = input()[::-1]

sum_of_digits = 0

cnts = [0] * 2019

cnts[0] = 1

d = 1

for c in s:
    sum_of_digits += int(c) * d
    sum_of_digits %= 2019
    d *= 10
    d %= 2019
    cnts[sum_of_digits] += 1

ans = 0
    
for cnt in cnts:
    ans += cnt * (cnt - 1) // 2

print(ans)

Solution (Python):
def main():
    s = input()[::-1]

    sum_of_digits = 0

    cnts = [0] * 2019

    cnts[0] = 1

    d = 1

    for i in range(len(s)):
        sum_of_digits += int(s[i]) * d
        d *= 10
        d %= 2019
        sum_of_digits %= 2019
        cnts[sum_of_digits] += 1


    ans = 0

    for cnt in cnts:
        ans += cnt * (cnt - 1) // 2

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
def main():
    mod=2019
    S=input().strip()
    l=len(S)
    M=[0]*(l+1)
    p=1
    for i in range(1,l+1):
        x=int(S[l-i])
        M[i]=(M[i-1]+x*p)%mod
        p*=10
        p%=mod
    print(sum(m*(m-1)//2 for m in Counter(M).values()))
if __name__=='__main__':
    main()

Solution (Python):
from collections import defaultdict
s=str(input())
n=len(s)
m=2019
d=[0]
for i in range(n):
    d.append((d[-1]+int(s[n-i-1])*pow(10,i,m))%m)
p=defaultdict(int)
ans=0
for i in d:
    ans+=p[i]
    p[i]+=1
print(ans)

Solution (Python):
from collections import Counter
def main():
    mod=2019
    S=input().strip()
    l=len(S)
    M=[0]*(l+1)
    p=1
    for i in range(1,l+1):
        x=int(S[l-i])
        M[i]=(M[i-1]+x*p)%mod
        p*=10
        p%=mod
    C=Counter(M)
    ans=0
    for m in C.values():
        ans+=m*(m-1)//2
    print(ans)
    
if __name__=='__main__':
    main()

Solution (Python):
S = input()
dp = [int(s) for s in S[::-1]]
dp.insert(0, 0)
for i in range(len(S)):
    dp[i+1] = (dp[i+1]*pow(10, i, 2019) + dp[i])%2019

from collections import Counter

c = Counter(dp)

print(sum([x*(x-1)//2 for x in c.values()]))

Solution (Python):
n = input()
mod = [0]*2019
mod[0] += 1
T = 0
d = 1 
for i, s in enumerate(n[::-1]):
  T += int(s) * d
  T %= 2019
  d = (d * 10) % 2019
  mod[T] += 1
  
res = 0  
for m in mod:
  res += m*(m-1)/2
print(int(res))

Solution (Python):
s = input()[::-1] 

counts = [0] * 2019
counts[0] = 1

num, d = 0, 1

for char in s:
    num += int(char) * d
    num %= 2019
    d *= 10
    d%= 2019
    counts[num] += 1

ans = 0
for cnt in counts:
    ans += cnt * (cnt - 1) // 2

print(ans)

Solution (Python):


S = list(map(int,input()))

counter = 0
amari = [0]*2019

s = 0
for i in range(len(S)):
    if S[len(S)-i-1] == 0:
        continue
    else:
        s += S[len(S)-i-1] * pow(10, i, 2019)
        s = s % 2019
        amari[s] += 1

for i in range(2019):
    counter += amari[i] * (amari[i]-1) / 2

counter += amari[0]

print(int(counter))

Solution (Python):
s=input()
b=0
m=len(s)
c=[0 for i in range(2019)]
c[0]=c[0]+1
a=""
e=0
f=1
s= ''.join(list(reversed(s)))
for i in s:
    e+=int(i)*f
    g=e%2019
    c[g]+=1
    f*=10
    f%=2019
e = sum(i * (i - 1) // 2 for i in c)
print(e)

Solution (Python):
import sys
input = sys.stdin.readline
s = input().strip()
if len(s) <= 3:
  print(0)
  exit()
c = 0
chk = [0]*2019
for i,si in enumerate(s[::-1]):
  c += int(si)*pow(10,i,2019)
  chk[c%2019] += 1
ans = chk[0]
for i in chk:
  ans += i*(i-1)//2
print(ans)

Solution (Python):
def POW(x, n, mod):
    ans = 1
    
    while n > 0:
        if n % 2 == 1:
            ans *= x%mod
        x = (x*x)%mod						
        n >>= 1						
    return ans

if __name__ == '__main__':
	S = input()
	L = len(S)
	lst = [0] * 2019
	lst[0] = 1
	amari = 0
	ans = 0
	for i in range(1,L+1):
		amari = ( int(S[L-i]) * POW(10,i-1,2019) + amari ) % 2019
		lst[amari] += 1

	for i in range(2019):
		m = lst[i]
		ans += m * (m-1) / 2

	print(int(ans))

Solution (Python):
s = input()
t = 0
number = 0

m = [0]*2019
m[0] = 1
a = 1
for i in range(len(s)):
    t = t  +  int(s[len(s) - 1 - i])* a 
    
    m[t % 2019] += 1
    a = a * 10
    a = a % 2019
    

for j in range(2019):
    
    number += (m[j]*(m[j] - 1))//2

print(number)

Solution (Python):


import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

S = input()
N = len(S)
mod = 2019

acc = [0] * (N+1)
mul = 1
for i in range(N-1, -1, -1):
    x = int(S[i]) * mul
    acc[i] = (acc[i+1] + x) % mod
    mul *= 10
    mul %= mod

C = Counter(acc)
ans = 0
for v in C.values():
    ans += v*(v-1) // 2
print(ans)

Solution (Python):

s = input()[::-1]
from collections import defaultdict
amari = defaultdict(int)
keta_ = defaultdict(int)

cur = 0
keta = 1

for i in range(len(s)):
    cur += int(s[i]) * keta
    cur %= 2019

    keta *= 10
    keta %= 2019
    
    amari[cur] += 1
    keta_[keta] += 1












ans = 0
if 0 in amari:
    ans += amari[0]

for n in amari.values():
    ans += (n * (n-1)) // 2
  
print(ans)

Solution (Python):

import sys


def solve(S: int):
    from itertools import takewhile, count
    SS = tuple(map(int, str(S)))
    ans = 0
    t = [0] * 2020
    t[0] = 1
    u, cnt = 0, 0
    for i, n in enumerate(reversed(SS)):
        u = (n * pow(10, i, 2019) + u) % 2019
        cnt += t[u]
        t[u] += 1
    return cnt




def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S = int(next(tokens))  
    print(solve(S))

if __name__ == '__main__':
    main()

Solution (Python):
import sys,os
from collections import defaultdict, deque
from math import ceil, floor
if sys.version_info[1] >= 5:
    from math import gcd
else:
    from fractions import gcd
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    MOD = 2019

    S = input()[::-1]
    N = len(S)

    if N < 4:
        print(0)
        return

    R = [0] * 2019
    R[0] = 1 
    t = 0
    b = 1
    for c in S:
        t += int(c) * b
        t %= 2019
        R[t] += 1
        b *= 10
        b %= 2019
    ans = 0
    for r in R:
        ans += r*(r-1)//2
    print(ans)
    




    

if __name__ == '__main__':
    main()

Solution (Python):
s = str(input())[::-1]
sum_of_digits = 0
cnt = [0] * 2019

cnt[0] = 1
d = 1
for c in s:
    d *= 10
    d %= 2019
    sum_of_digits += d * int(c)
    sum_of_digits %= 2019
    cnt[sum_of_digits] += 1

print(sum([i*(i-1)//2 for i in cnt]))

Solution (Python):
def main():
    S = input()
    work, temp = [0] * 2019, 0
    work[0] = 1
    a = 1
    for s in S[::-1]:
        temp += int(s) * a
        work[temp%2019] += 1
        a *= 10
        a %= 2019
    print(sum([w*(w-1)//2 for w in work]))
main()

Solution (Python):
from collections import Counter
S = input()[::-1]
MOD = 2019

X = [0]
for i, s in enumerate(S):
    X.append((X[-1] + int(s) * pow(10, i, MOD)) % MOD)


C = Counter(X)
ans = 0
for v in C.values():
    ans += v * (v - 1) // 2
print(ans)

Solution (Python):
S = input()
Sr = S[::-1] 

T = 0 
mod = [0] * 2019 
mod[0] += 1 
d = 1 
for i, s in enumerate(Sr):
  T += int(s) * d
  T %= 2019
  d = (d * 10) % 2019
  
  mod[T] += 1
  


mod = [n * (n-1) // 2 for n in mod]
print(sum(mod))

Solution (Python):
s = input()[::-1]
current = 0
a = [0] * 2019
a[0] = 1
digit = 1
for i in s:
  current += int(i) * digit
  digit *= 10
  digit %= 2019
  current %= 2019
  a[current] += 1
ans = 0
for i in a:
  ans += i * (i - 1) // 2
print(ans)

Solution (Python):
S=input()[::-1]
l=[0]*2019
a=1
d=0

for s in S:
  d=d+int(s)*a
  l[d%2019]+=1
  a*=10
  a%=2019
  
ans=l[0]
for i in l:
  if i>=2:
    ans+=i*(i-1)//2
    
print(ans)

Solution (Python):
s=input()
s_len=len(s)
n=2019
sp=[0]*n
sp[0]=1
p=1
d=0
for i in reversed(s):
    d+=int(i)*p
    m=d%n
    sp[m]+=1
    p*=10
    p%=2019
result=sum(i*(i-1)//2 for i in sp)
print(result)

Solution (Python):
M=2019
a=i=0
d=[0]*M
p=1
for j in input()[::-1]:d[i%M]+=1;i-=int(j)*p;a+=d[i%M];p=p*10%M
print(a)

Solution (Python):
s = input()
s_len = len(s)
n = 2019
mod_list = [0] * n
mod_list[0] = 1

p = 1
d = 0
for t in reversed(s):
    d += int(t) * p
    m = d % n
    mod_list[m] += 1
    p *= 10
    p %= 2019

result = sum(i * (i - 1) // 2 for i in mod_list)
print(result)

Solution (Python):
num = [0 for i in range(2019)]
num[0] = 1
count = 0
s = input()
now = 0
p = 1
for i in range(len(s) - 1, -1, -1):
    now = (now + int(s[i]) * p) % 2019
    count += num[now]
    num[now] += 1
    p = p * 10 % 2019
print(count)

Solution (Python):
S = input()
n = len(S)
k = 2019



A = [0] * (n + 1) 
d = {0:1}

w = 1
for i in range(n - 1, -1, -1):
    A[i] = (A[i + 1] + w * int(S[i])) % k
    w = w * 10 % k
    if A[i] in d:
        d[A[i]] += 1
    else:
        d[A[i]] = 1

ans = 0
for j in d:
    ans += d[j] * (d[j] - 1) // 2

print(ans)

Solution (Python):

S = input()[::-1]

mod = [0]*2019
mod[0] = 1
ten = 1
index = 0
for s in S:
    index += ten*int(s)
    index %= 2019
    mod[index] += 1
    ten *= 10
    ten %= 2019

ans = 0
for m in mod:
    ans += m * (m-1) // 2

print(ans)

Solution (Python):
S=input()[::-1]
List=[0]*2019
ans=0
a=1
d=0

for s in S:
  d=d+int(s)*a
  List[d%2019]+=1
  a=a*10
  a=a%2019
  
  

ans=List[0]
for i in List:
  if (i>=2):
    ans+=(i*(i-1))//2
  
print(ans)

Solution (Python):
def main():
    import sys
    from collections import Counter
    sys.setrecursionlimit(10 ** 6)
    input = sys.stdin.readline
    S = [int(x) for x in input().strip()]
    N = len(S)
    ans = 0
    l = [0] * (N + 1)
    p = 1
    cumsum = [0] * (N + 1)
    for i in range(N-1, -1, -1):
        l[i] = (S[i] * p) % 2019
        p *= 10
        p %= 2019
    for i in range(N-1, -1, -1):
        cumsum[i] = (l[i] + cumsum[i+1]) % 2019
    cumsum = Counter(cumsum)
    for i in range(0, 2019):
        ans += (cumsum[i] * (cumsum[i] - 1)) // 2

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
S = input()[::-1]

counts = [0] * 2019
counts[0] = 1

a = 1
d = 0

for s in S:
    d = d + int(s)*a
    num = d%2019
    counts[num] += 1
    a *= 10
    a %= 2019

ans = 0

for i in counts:
    if i >=2:
        x = (i*(i-1))//2
        ans += x

print(ans)

Solution (Python):


s = input()[::-1] 

counts = [0] * 2019
counts[0] = 1

num, d = 0, 1

for char in s:
    num += int(char) * d
    num %= 2019
    d *= 10
    d%= 2019
    counts[num] += 1

ans = 0
for cnt in counts:
    ans += cnt * (cnt - 1) // 2

print(ans)

Solution (Python):
s = list(map(int, list(input())))
n = len(s)

countdic = {}
mod = 2019

num = 0
d = 1
for i in range(1,n+1):
    num += s[n-i]*d
    num %= mod
    if num in countdic:
        countdic[num] += 1
    if num not in countdic:
        countdic[num] = 1
    d = (10*d)%mod

ans = 0
for key in countdic:
    ans += countdic[key]*(countdic[key]-1)//2

if 0 in countdic:
    ans += countdic[0]

print(ans)

Solution (Python):
S = input()
r = 1
arr = [0]
for c in S[::-1]:
    arr.append((arr[-1] + int(c) * r) % 2019)
    r *= 10
    r %= 2019
from collections import Counter
ctr = Counter(arr)
ans = 0
for v in ctr.values():
    ans += v*(v-1)//2
print(ans)

Solution (Python):
from collections import Counter,deque
from scipy import special
S = input()
l = [int(n) for n in S]
a = [0]
t = 0
m = 1
d = deque(l)
mod = 2019
while d:
    temp = d.pop()
    t = (t + (temp * m)) % mod
    m = (m * 10) % mod
    a.append(t%mod)
b = [special.comb(kv,2,exact=True) for kv in Counter(a).values() if kv > 1]
print(sum(b))

Solution (Python):
s = input()

n = len(s)
c = [0]*2019
c[0]+=1
t = 0
digit = 1
for ss in s[::-1]:
    t = (t+int(ss)*digit) % 2019
    digit = (digit * 10) % 2019
    c[t]+=1
ans = 0
ans = sum(cc*(cc-1)//2 for cc in c)
print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    S = str(input())
    N=len(S)
    P=2019

    def main():
        if P == 2:
            res = 0
            for i in range(N):
                if int(S[N - 1 - i]) % 2 == 0:
                    res += N - i
        elif P == 5:
            res = 0
            for i in range(N):
                if int(S[N - 1 - i]) == 0 or int(S[N - 1 - i]) == 5:
                    res += N - i

        else:
            mods = [0] * P
            
            tenfactor = 1
            mod = 0
            mods[mod] += 1

            for i in range(N):
                mod = (mod + int(S[N - i - 1]) * tenfactor) % P
                tenfactor = (tenfactor * 10) % P
                mods[mod] += 1

            res = 0
            for p in range(P):
                res += mods[p] * (mods[p] - 1) // 2

        return res

    print(main())


resolve()

Solution (Python):
m=2019
l=[0]*m
a=b=0
k=1
for c in input()[::-1]:l[b]+=1;b=(b-int(c)*k)%m;a+=l[b];k=k*10%m
print(a)

Solution (Python):
S=input()
mc=[0]*2019

tg=0
digit=1
for s in S[::-1]:
    tg+=int(s)*digit
    mc[tg%2019]+=1
    digit=digit*10%2019

ans=sum(m*(m-1)//2 for m in mc)
print(ans+mc[0])

Solution (Python):


surplus = [0]*2019
surplus[0] += 1

d = 1
n = 0
for i in reversed(input()):
    n = (n + int(i)*d) %2019
    d = d*10 % 2019
    surplus[n] += 1

print(sum([i*(i-1)//2 for i in surplus]))

Solution (Python):



S = list(input())
N = len(S)


ans = 0


nums_2019 = [0]*2019
nums_2019[0] = 1
s = 0
for i in range(N-1, -1, -1):
    s += int(S[i]) * pow(10, N - i - 1, 2019)
    nums_2019[s%2019] += 1

for i in range(2019):
    if nums_2019[i]>=2:
        ans += nums_2019[i] * (nums_2019[i]-1) // 2


print(ans)

Solution (Python):
m=2019
l=[0]*m
a=b=0
k=1
for c in input()[::-1]:l[b]+=1;b=(b-int(c)*k)%m;a+=l[b];k=k*10%m
print(a)

Solution (Python):
S = input()
mod = [0] * 2019
mod[0] = 1 
d = 1
n = 0
for i in reversed(S):
  n += int(i) * d
  n %= 2019
  d *= 10
  d %= 2019
  mod[n % 2019] += 1
ans = 0
for j in range(2019):
  ans += mod[j] * (mod[j] - 1) // 2
print(ans)

Solution (Python):
s = input()[::-1]
p = 0
dp = [0] * 2019
dp[0] = 1
d = 1
for i in s:    
    p = (int(i) * d + (p)) % 2019
    dp[p] += 1
    d *= 10
    d = d % 2019
ans = 0
for i in dp:
    ans += i * (i - 1) // 2
print(ans)

Solution (Python):
m=2019
l=[0]*m
a=b=0
k=1
for c in input()[::-1]:l[b]+=1;b=(b-int(c)*k)%m;a+=l[b];k=k*10%m
print(a)

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



S = list(input())
N = len(S)
d = defaultdict(int)

beki = [0]*N
now = 1
for i in range(N):
    beki[i] = now
    now *= 10
    now %= 2019

now = 0
d[0] = 1
ans = 0
for i in range(N):
    now += int(S[N-1-i])*beki[i]
    now %= 2019
    ans += d[now]
    d[now] += 1

print(ans)

Solution (Python):
def main():
    s = input()
    rem = [0] * 2019  
    rem[0] = 1  

    temp = 0
    for i, c in enumerate(s[::-1]):
        temp += pow(10, i, 2019) * int(c)
        rem[temp % 2019] += 1

    return sum(c * (c - 1) // 2 for c in rem)


if __name__ == '__main__':
    print(main())

Solution (Python):
def main():
    s = list(input())[::-1]
    ary_mod = [0] * 2019
    ary_mod[0] = 1
    now = 0

    for i, ss in enumerate(s):
        now += int(ss) *pow( 10 , i, 2019)
        remind = now % 2019
        ary_mod[remind] += 1
    ans = 0
    for i, n in enumerate(ary_mod):
        ans += (n*(n-1)) // 2
    print(ans)
if __name__=='__main__':
    main()

Solution (Python):
def main():
    s = input()
    rem = [0] * 2019  
    rem[0] = 1  

    temp = 0
    digit = 1
    for c in s[::-1]:
        
        temp += int(c) * digit
        rem[temp % 2019] += 1
        digit = digit * 10 % 2019

    return sum(c * (c - 1) // 2 for c in rem)


if __name__ == '__main__':
    print(main())

Solution (Python):
def main():
    s = input()
    rem = [0] * 2019  
    rem[0] = 1  

    temp = 0
    digit = 1
    for c in s[::-1]:
        
        temp += int(c) * digit
        rem[temp % 2019] += 1
        digit *= 10
        digit %= 2019

    return sum(c * (c - 1) // 2 for c in rem)


if __name__ == '__main__':
    print(main())

Solution (Python):
def main():
    s = input()
    rem = [0] * 2019  
    rem[0] = 1  

    temp = 0
    digit = 1
    for i, c in enumerate(s[::-1]):
        
        temp += int(c) * digit
        rem[temp % 2019] += 1
        digit *= 10
        digit %= 2019

    return sum(c * (c - 1) // 2 for c in rem)


if __name__ == '__main__':
    print(main())

Solution (Python):

def main():
    s = list(input())[::-1]
    ary_mod = [0] * 2019
    ary_mod[0] = 1
    now = 0
    digit = 1
    for i, ss in enumerate(s):
        now += int(ss) * digit % 2019
        digit *= 10
        digit %= 2019
        remind = now % 2019
        ary_mod[remind] += 1
    ans = 0
    for i, n in enumerate(ary_mod):
        ans += (n*(n-1)) // 2
    print(ans)
if __name__=='__main__':
    main()

Solution (Python):

def main():
    s = list(input())[::-1]
    ary_mod = [0] * 2019
    ary_mod[0] = 1
    now = 0
    digit = 1
    for i, ss in enumerate(s):
        now += int(ss) * digit % 2019
        digit *= 10
        digit %= 2019
        remind = now % 2019
        ary_mod[remind] += 1
    ans = 0
    for i, n in enumerate(ary_mod):
        ans += (n*(n-1)) // 2
    print(ans)
if __name__=='__main__':
    main()

Solution (Python):

s = input()[::-1] 

counts = [0] * 2019
counts[0] = 1

num, d = 0, 1

for char in s:
    num += int(char) * d
    num %= 2019
    d *= 10
    d%= 2019
    counts[num] += 1

ans = 0
for cnt in counts:
    ans += cnt * (cnt - 1) // 2

print(ans)

Solution (Python):
def main():
  n, d, mods = 0, 1, [1]+[0]*2019
  for i in reversed(input()):
    n = (n+int(i)*d)%2019
    d = d*10%2019
    mods[n] += 1
  print(sum([i*(i-1)//2 for i in mods]))
main()

Solution (Python):
import time

p0 = time.time()
S = input()
n = len(S)
Y = {0:1}

Y[0] = 1
A = [0]
pk = 0
for i in range(1,n+1):
    
    k = (int(S[-i])*pow(10,i-1,2019)+pk)%2019
    A.append(k)
    if k not in Y:
        Y[k] = 1
    else:
        Y[k] += 1
    pk = k




c = 0
for y in Y:
    d = Y[y]
    c += d*(d-1)//2
print(c)

Solution (Python):
S = input()

num = 0

r_lst = [0] * 2019
r_lst[0] = 1
cnt = 0
for i, s in enumerate(S[::-1]):
    num = (num + pow(10, i, 2019) * int(s)) % 2019
    cnt += r_lst[num]

    r_lst[num] += 1


print(cnt)

Solution (Python):
s = input()[::-1]

counts = [0] * 2019
counts[0] = 1

num, d = 0, 1

for char in s:
    num += int(char) * d
    num %= 2019
    counts[num] += 1
    d *= 10
    d %= 2019

ans = 0
for cnt in counts:
    ans += cnt * (cnt - 1) // 2
    
print(ans)

Solution (Python):
s = input()[::-1] 

counts = [0] * 2019
counts[0] = 1

num, d = 0, 1

for char in s:
    num += int(char) * d
    num %= 2019
    d *= 10
    d%= 2019
    counts[num] += 1

ans = 0
for cnt in counts:
    ans += cnt * (cnt - 1) // 2

print(ans)

Solution (Python):


import sys

stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip() 

def comb(a,b):
    ans = 1
    for i in range(b):
        ans *= a - i
    for j in range(b):
        ans //= (j + 1)
    return ans

def main():
    s = ns()
    mod = 2019
    tmp1  = 1
    tmp2 = 0
    mod2019 = [1] + [0 for i in range(2018)]
    for i in range(len(s)-1, -1, -1):
        tmp2 = (tmp2 + tmp1*int(s[i]))%mod
        tmp1 = (tmp1*10)%mod
        mod2019[tmp2] += 1
    ans = 0
    for i in range(2019):
        ans += comb(mod2019[i],2)
    print(ans) 
    return

if __name__ == '__main__':
    main()

Solution (Python):
s = input()[::-1]
n = len(s)
amari = [0]*2019
amari[0] = 1

temp = 0
d = 1
for char in s:
    temp += int(char)*d
    temp %= 2019
    d *= 10
    d %= 2019  
    amari[temp] += 1

ans = 0
for i in amari:
    ans += i*(i-1)/2

print(int(ans))

Solution (Python):
if __name__ == '__main__':

    s = list(input())
    s.reverse()
    n = len(s)
    now = 0
    mods = [0] * 2019
    mods[0] += 1
    for i in range(n):
        now += pow(10, i, 2019) * int(s[i])
        now %= 2019
        mods[now] += 1
    ans = 0
    for i in mods:
        ans += (i * (i - 1)) // 2
    print(ans)

Solution (Python):
if __name__ == '__main__':

    s = list(input())
    s.reverse()
    n = len(s)
    now = 0
    mods = [0] * 2019
    mods[0] += 1
    ten = 1
    for i in range(n):
        now += ten * int(s[i])
        now %= 2019
        ten *= 10
        ten %= 2019
        mods[now] += 1
    ans = 0
    for i in mods:
        ans += (i * (i - 1)) // 2
    print(ans)

Solution (Python):

s=input()
s=s[::-1]
md=[0]
ex10=1
tmp=0
mod=2019
for i in range(len(s)):
    tmp+=ex10*int(s[i])
    tmp%=mod
    md.append(tmp)
    ex10*=10
    ex10%=mod
from collections import Counter

sc= Counter(md)
ans=0
for k in sc:
    sck=sc[k]
    ans+= sck*(sck-1)//2
print(ans)

Solution (Python):
from collections import defaultdict
s = input()
n = len(s)

d = defaultdict(int)
d[0] = 1
x = 0
b = 1
for i in range(n-1,-1,-1):
    x += b*int(s[i])
    x %= 2019
    d[x] += 1

    b *= 10
    b %= 2019

res = 0
for k in d:
    c = d[k]
    res += c*(c-1)//2
print(res)

Solution (Python):
s=list(input())
s=s[::-1]
l=len(s)
mod=2019
count=[0]*2019
count[0]=1
num=0
seki=1
for i in range(l):
    num+=int(s[i])*seki
    num=num%mod
    count[num]+=1
    seki=(seki*10)%mod
ans=0
for i in range(2019):
    N=count[i]
    ans+=N*(N-1)//2
print(ans)

Solution (Python):
s=input()
n=len(s)
from collections import Counter

cc=Counter()
cc[0]+=1
num=0
tenpow=1
ans=0
for i in range(n-1,-1,-1):
    num=tenpow*(int(s[i]))+num
    num%=2019
    tenpow=10*tenpow
    tenpow%=2019
    ans+=cc[num]
    cc[num]+=1
print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=2019
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    A=list(map(int,list(input())[::-1]))
    N=len(A)
    S=[0]*(N+1)
    for i in range(N):
        S[i+1]=(S[i]+A[i]*pow(10,i,MOD))%MOD
    l=[0]*2019
    ans=0
    for i in range(N+1):
        ans+=l[S[i]]
        l[S[i]]+=1
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
S = input()
digits = list(map(int,S))




ls = [0] * 2019


ls[0] += 1

x = 0
pow10 = 1
count = 0
for i in reversed(digits):
    x += i*pow10
    x %= 2019
    count += ls[x]
    ls[x] += 1
    pow10 *= 10
    pow10 %= 2019




print(int(count))