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):
import sys
import numba
import numpy as np
S = sys.stdin.buffer.readline().decode('utf-8').rstrip()
S = np.array([int(d) for d in S[::-1]], dtype=np.int32)

@numba.njit(numba.i8(numba.typeof(S)), cache=True)
def solve(S):
	M = 2019
	t = 1
	ans = 0
	c = np.zeros(M, dtype=np.int64)
	c[0] = 1
	r = 0
	for d in S:
		r = (d * t + r) % M
		ans += c[r]
		c[r] += 1
		t = t * 10 % M
	return ans

print(solve(S))

Solution (Python):
s=input()
r=set()
last=0
power=1
d={0:0}
total=0
rem=0
for i in range(len(s)-1,-1,-1):
    n=int(s[i])*power+rem
    power*=10
    power=power%2019
    rem=n%2019
    if rem in d.keys():
        d[rem]+=1
    else:
        d[rem]=1
    total+=d[rem]-1
print(total+d[0])

Solution (Python):
S = input()[::-1]
rem = [0]*2019

n = 0
d = 1

for s in S:
    n = n%2019 + (int(s)*d)%2019
    tmp = n%2019
    rem[tmp] += 1
    d *= 10
    d %= 2019
    
ans = 0
for i in rem:
    ans += ((i*(i-1))//2)

print(ans+rem[0])

Solution (Python):



S = input()


def main():
    n = len(S)
    p = [None] * n
    p[0] = 1
    for i in range(1, n):
        p[i] = (p[i - 1] * 10) % 2019
    
    d = {i: 0 for i in range(2019)}
    d[0] = 1
    ans = 0
    x = 0
    for i in range(n):
        x = (x + int(S[n - i - 1]) * p[i]) % 2019
        ans += d[x]
        d[x] += 1
    
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
s=input()
b=[0]*2019
b[0]=1
now=0
m=2019
c=1
ans=0
for i in range(len(s)-1,-1,-1):
    now+=int(s[i])*c
    now=now%m
    c=c*10
    ans=ans+b[now]
    b[now]=b[now]+1
    c=c%m

print(ans)

Solution (Python):
s = input()[::-1]
v = 0
mods = [ 0 for i in range(2019) ]
mods[0] = 1
d = 1

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

ans = 0
for n in mods:
    ans += (n * (n - 1)) // 2

print(ans)

Solution (Python):
S = input()[::-1]
N = len(S)
amari = [0 for i in range(2019)]
before = int(S[0])
amari[before] += 1
S = S[1::]
keta = 1
for i in S:
    i = int(i)
    keta = (keta * 10) % 2019
    x = (keta * i + before) % 2019
    amari[x] += 1
    before = x
ans = amari[0]
for i in amari:
    ans += i * (i - 1) // 2
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):
s, count, m, pow10, ans = input(), [0 if i else 1 for i in range(2019)], 0, 1, 0
for c in reversed(s):
    m = (m + (ord(c) - ord('0')) * pow10) % 2019
    pow10 = pow10 * 10 % 2019
    ans += count[m]
    count[m] += 1
print(ans)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()

import collections
s = [int(c) for c in input()]
tmp = 1
mod = 0
count = collections.defaultdict(int)
for i,num in enumerate(s[::-1]):
    mod = (tmp * num + mod) % 2019
    count[mod] += 1
    tmp *= 10
    tmp %= 2019
count[0] += 1
print(sum((item*(item-1)//2 for item in count.values())))

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

m = 0
d = 1
cnt = [0] * 2019

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

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

print(ans)

Solution (Python):
from collections import Counter


def read():
    S = list(input())
    return S,


class ModInt(int):
    
    MOD = 10**9+7

    def __new__(cls, x, *args, **kwargs):
        return super().__new__(cls, x % ModInt.MOD)

    def __add__(self, other):
        return ModInt(super().__add__(other) % ModInt.MOD)

    def __radd__(self, other):
        return ModInt(super().__radd__(other) % ModInt.MOD)

    def __mul__(self, other):
        return ModInt(super().__mul__(other) % ModInt.MOD)

    def __rmul__(self, other):
        return ModInt(super().__rmul__(other) % ModInt.MOD)

    def __pow__(self, other, *args):
        return ModInt(super().__pow__(other, ModInt.MOD))

    def __rpow__(self, other, *args):
        raise NotImplementedError()

    def __truediv__(self, other):
        raise NotImplementedError()

    def __floordiv__(self, other):
        raise NotImplementedError()

    def __rtruediv__(self, other):
        raise NotImplementedError()

    def __rfloordiv__(self, other):
        raise NotImplementedError()


def solve(S, MOD=2019):
    N = len(S)
    S = list(reversed(S))
    T = [ModInt(0) for i in range(N+1)]
    ModInt.MOD = 2019
    
    mul = ModInt(1)
    for i in range(N):
        T[i+1] = T[i] + int(S[i]) * mul
        mul *= 10
    
    
    
    
    ans = 0
    for k, v in Counter(T).most_common():
        iv = int(v)
        if iv >= 2:
            ans += v * (v-1) // 2
    return ans

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]

S = list(map(int, list(input())))
num = 0
dict = [0 for i in range(2019)]
dict[0] = 1
ans = 0

for i , n in enumerate(S[::-1]):
    num += n * pow(10,i,2019)
    dict[num % 2019] += 1

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

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]

S = list(map(int, list(input())))
num = 0
dict = {}
dict[0] = 1
ans = 0

for i , n in enumerate(S[::-1]):
    num += n * pow(10,i,2019)
    cand = num % 2019
    if cand in dict.keys():
        dict[cand] += 1
    else:
        dict[cand] = 1

for key in dict.keys():
    ans += dict[key] * (dict[key] - 1) // 2
print(ans)

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]

S = list(map(int, list(input())))
num = 0
dict = {}
dict[0] = 1
ans = 0

for i , n in enumerate(S[::-1]):
    num += n * pow(10,i,2019)
    cand = num % 2019
    if cand in dict.keys():
        dict[cand] += 1
    else:
        dict[cand] = 1

for key in dict.keys():
    ans += dict[key] * (dict[key] - 1) // 2
print(ans)

Solution (Python):
n = int(input())
nums = [int(c) for c in str(n)]
nums.reverse()
a = [0]*2019
a[0] = 1
now = 0
pow10 = [1]*len(nums)
for i, num in enumerate(nums):
    if i < len(nums)-1:
        pow10[i+1] = pow10[i]*10 % 2019
for i, num in enumerate(nums):
    now = (now+num*pow10[i]) % 2019
    a[now] += 1
sum = 0
for i in range(2019):
    sum += a[i]*(a[i]-1)//2
print(sum)

Solution (Python):

from collections import deque



S = deque(map(int, list(input())))
num = 0
dict = {}
dict[0] = 1
ans = 0

for i in range(len(S)):
    num += S.pop() * pow(10,i,2019)
    cand = num % 2019
    if cand in dict.keys():
        dict[cand] += 1
    else:
        dict[cand] = 1

for key in dict.keys():
    ans += dict[key] * (dict[key] - 1) // 2
print(ans)

Solution (Python):
import sys
from collections import deque
def input():
    return sys.stdin.readline()[:-1]

S = deque(map(int, list(input())))
num = 0
dict = {}
dict[0] = 1
ans = 0

for i in range(len(S)):
    num += S.pop() * pow(10,i,2019)
    cand = num % 2019
    if cand in dict.keys():
        dict[cand] += 1
    else:
        dict[cand] = 1

for key in dict.keys():
    ans += dict[key] * (dict[key] - 1) // 2
print(ans)

Solution (Python):
import sys
from collections import deque
def input():
    return sys.stdin.readline()[:-1]

S = deque(map(int, list(input())))
num = 0
dict = {}
dict[0] = 1
ans = 0

for i in range(len(S)):
    num += S.pop() * pow(10,i,2019)
    cand = num % 2019
    if cand in dict.keys():
        dict[cand] += 1
    else:
        dict[cand] = 1

for key in dict.keys():
    ans += dict[key] * (dict[key] - 1) // 2
print(ans)

Solution (Python):
s = str(input())
N = len(s)
array = [0]*2019
array[0] += 1
count = 0
d = 1
r = 0
for i in reversed(range(0, N)):
    r = ( r + int( s[i] ) * d ) % 2019
    d = d * 10 % 2019
    array[r] += 1
for i in array:
    count += i*(i-1)/2
print(int(count))

Solution (Python):
import collections

mod = 2019

def main():
    S = list(input())
    lenS = len(S)
    Smod2019 = [0] * lenS
    tmp = 0
    for i in range(lenS):
        tmp += (pow(10, i, mod)*int(S[-(i+1)]))%mod
        Smod2019[i] = tmp%mod
    
    cnt = collections.Counter(Smod2019)
    cntitems = cnt.items()
    ans = 0
    
    for c in cntitems:
        if c[0] == 0:
            ans += c[1]
        ans += (c[1]*(c[1]-1))//2
    
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
'''
[point 1]
1817181712114 の部分 "18171"について考える...

    181710000 / 10000 ≡ 0 (MOD 2019) ならば、2019の倍数
    181710000 ≡ 0 (MOD 2019) でよい

[point 2]
    181710000
    =
    181712114
    -
         2114

    なので、特定区間の値 は 累積和の差 から瞬時に求められる。
    さらに、181710000 ≡ 0 (MOD 2019) を考えると、

    0 ≡ 181712114 - 2114
    → 181712114 ≡ 2114

    となる。
    結局の所、MOD2019において合同な累積和のペアの数の数え上げ。
'''

from collections import defaultdict

def gen_10exp_mod(n, mod):
    a = 1
    yield a
    for _ in range(1, n):
        a = (a*10) % mod
        yield a

def main():
    MOD = 2019
    S = input()[::-1]
    
    A = [int(s)*n % MOD for s, n in zip(S, gen_10exp_mod(len(S), MOD))]
    
    ACC = [0]
    for a in A:
        ACC.append((ACC[-1] + a) % MOD)
    
    d = defaultdict(int)
    for acc in ACC:
        d[acc] += 1
    ans = sum(v*(v-1)//2 for v in d.values())
    print(ans)


main()

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

dp = [0]*2019
now_sum = 0
ten_times = 1

for ch in s_reverse:
    now_sum = (now_sum + int(ch)*ten_times) % 2019
    ten_times = (ten_times * 10) % 2019
    dp[now_sum] += 1

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

print(ans)

Solution (Python):
def main():
  s = input()[::-1]
  l = len(s)
  n = 2019
  dp = [0]*n
  wk = 0
  r = 1
  for i in range(l):
    wk += int(s[i])*r
    wk %= n
    dp[wk] += 1
    r*= 10
    r %= n
  print(dp[0]+sum([i*(i-1)//2 for i in dp]))
main()

Solution (Python):
s=str(input())
n=len(s)
ans=0
l=[0 for i in range(2019)]
now=l[int(s[n-1])]
r=1
for i in range(1,n+1):
  now=(now+int(s[-i])*r)%2019
  l[now]+=1
  r=r*10
  r=r%2019
ans+=l[0]
for i in l:
  ans+=int(i*(i-1)//2)
print(ans)

Solution (Python):
M = 2019

S = list(input())
s_size = len(S)

S.reverse()
x = 1
tot = 0
ans = 0

cnt_list = [0 for i in range(M)]

for i in range(s_size):
	cnt_list[tot] += 1
	tot += (ord(S[i])-ord('0'))*x
	tot %= M
	ans += cnt_list[tot]
	x = x*10%M

print(ans)

Solution (Python):



def main():
    S = input()[::-1]
    L = len(S)
    dp = [0] * 2019
    wk = 0
    r = 1
    for i in range(L):
        wk += int(S[i])*r
        wk %= 2019
        r *= 10
        r %= 2019
        dp[wk] += 1
    ret = dp[0]
    for i in range(2019):
        ret += dp[i]*(dp[i]-1)//2
    print(ret)


if __name__ == '__main__':
    main()

Solution (Python):
s=input()
tmp,mod,n,ans=0,2019,len(s),0
dic={0:1}
for i in range(len(s)):
  tmp=(tmp+int(s[i])*pow(10,n-i-1,mod))%mod
  if tmp in dic.keys():
    dic[tmp]+=1
  else:
    dic[tmp]=1
for i in dic.items():
  ans+=(i[1]*(i[1]-1))//2
print(ans)

Solution (Python):
s=input()
n=len(s)
l=[0]*2019
l[0]=1
k=0
mod=2019
r=1
for i in range(n-1,-1,-1):
  k+=int(s[i])*r
  k%=mod
  r*=10
  r%=mod
  l[k]+=1
ans=0
for i in l:
  ans+=i*(i-1)//2
print(ans)

Solution (Python):
S = list(reversed(input()))

length = len(S)

a = [0]*length
for i in range(length):
    pickup = int(S[i])
    a[i] = pickup*pow(10, i, 2019)

b = [0]*(length+1)
for i in range(1, length+1):
    b[i] = (b[i-1] + a[i-1]) % 2019
cnt, ans = [0] * 2019, 0
for x in b:
    ans += cnt[x]
    cnt[x] += 1

print(ans)

Solution (Python):
s = str(input())
n = len(s)
s = s[::-1]
x, tot = 1, 0
m = 2019
ans = 0
cnt = [int() for _ in range(m)]
for i in range(n):
    cnt[tot] += 1
    tot += (int(s[i])) * x
    tot %= m
    ans += cnt[tot]
    x = x * 10 % m
print(ans)

Solution (Python):
'''冷めたコーヒー
入力文字列として、S = 31415926535を考えます。
このとき、例えば4番目から8番目までの部分文字列15926が
2019の倍数であるか判定したいというのが本問の主題です。
特定の部分文字列が2019の倍数であるかどうかは、
int(s[[i:j+1]) % 2019 == 0とすればよいのですが、
これを逐一やっていてはO(N^2)の計算量がかかってしまいます。
そこで、この部分文字列を次のようにとらえてみます。
15926=(15926535−535)/10^3
このように考えることの利点は、10^3 が 2019 と
互いに素（最大公約数が 1）であるため、
15926 が 2019 の倍数であるかどうかは、
上式右辺の分子に着目すればよいということです。
したがって、アルゴリズムを設計する際は、
与えられた文字列の右からk番目（0～n)から最右端までの
部分文字列を保持しておけばよいことになります。
分子が 2019 の倍数かどうかは、分子を A−B としたときに、
A mod2019 と B mod2019 の値がそれぞれ等しければ、
元の整数（上式左辺）は 2019 の倍数であると言えます。
'''
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(input())
s=[int(i) for i in s]
now=0
c=[0]*2019
c[0]=1
for n,i in enumerate(s[::-1]):
    now+=i*pow(10,n,2019)
    c[now%2019]+=1
ans=0
for k in range(2019):
    ans+=c[k]*(c[k]-1)//2
print(ans)

Solution (Python):







def main():
    S = input() + "0"
    
    N = len(S)
    S = list(map(int, S))

    q = 2019

    res = [0] * N
    num = 0
    p = 1
    pow_memo = {0: (10**0) % q}
    for i in range(N-2, -1, -1):
        pow_memo[p] = (pow_memo[p-1] * 10) % q
        num = (num + (S[i] % q) * pow_memo[p]) % q
        res[i] = num
        p += 1

    cnt = [0] * q
    for r in res:
        cnt[r] += 1

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

    return ans


print(main())

Solution (Python):
S=input()
C,m,T=2019,0,[1]+[0]*2018
for i,s in enumerate(S[::-1]):
  m=(m+int(s)*pow(10,i,C))%C
  T[m]+=1
print(sum(T[i]*~-T[i]//2 for i in range(2019)))

Solution (Python):
from math import floor,ceil,sqrt,factorial,log
from collections import Counter, deque
from functools import reduce
import numpy as np
def S(): return input()
def I(): return int(input())
def MS(): return map(str,input().split())
def MI(): return map(int,input().split())
def FLI(): return [int(i) for i in input().split()]
def LS(): return list(MS())
def LI(): return list(MI())
def LLS(): return [list(map(str, l.split() )) for l in input()]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def LLSN(n: int): return [LS() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]

T = S()

revT = reversed(T)

n, d, mods = 0, 1, [1]+[0]*2019

for i in revT:
    n = (n + int(i)*d)%2019
    d = d*10%2019
    mods[n] += 1

sum = 0
for j in mods:
    sum += j*(j-1)//2

print(sum)

Solution (Python):
s = input()

li = []
mod = 2019
tmp = 0

for i in range(len(s)):
    li.append(tmp)
    tmp += int(s[i])*pow(10,len(s)-i-1,mod)
    
    tmp %= mod
    

li.append(tmp)
li.sort()

tmp = li[0]
t_num = 1
ans = 0

for i in range(1,len(li)):
    if tmp == li[i]:
        t_num += 1
    else:
        ans += t_num*(t_num-1)//2
        t_num = 1
        tmp = li[i]

ans += t_num*(t_num-1)//2

print(ans)

Solution (Python):
s = input()

li = []
mod = 2019
tmp = 0

for i in range(len(s)):
    li.append(tmp)
    tmp += int(s[i])*pow(10,len(s)-i-1,mod)
    
    tmp %= mod
    

li.append(tmp)
li.sort()

tmp = li[0]
t_num = 1
ans = 0

for i in range(1,len(li)):
    if tmp == li[i]:
        t_num += 1
    else:
        ans += t_num*(t_num-1)//2
        t_num = 1
        tmp = li[i]

ans += t_num*(t_num-1)//2

print(ans)

Solution (Python):
import sys
from pprint import pprint

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

n = len(S)
r = [0] * 2019
acum = [0] * (len(S)+1)
r[0] += 1
for i, s_i in enumerate(S):
    acum[i+1] = int(s_i) * pow(10, i, 2019) + acum[i]
    acum[i+1] %= 2019
    r[acum[i+1]] += 1


ans = 0
for r_i in r:
    ans += r_i * (r_i - 1) // 2
print(ans)

Solution (Python):
S = str(input())


A = []
p =1
for i in range(len(S)):
  A.append(p)
  p = p*10%2019


X = []
a = 0
for i in range(len(S)):
  a = (a + int(S[len(S)-i-1])*A[i]) %2019
  X.append(a)
X.sort()
flag =1
n = 0


for i in range(len(X)-1):
  if X[i]==0:
    n +=1
  if X[i]==X[i+1]:
    flag += 1
  else:
    n += int(flag*(flag-1)/2)
    flag = 1
n += int(flag*(flag-1)/2)
if X[len(X)-1]==0:
  n +=1
print(n)

Solution (Python):
def main():
    S=input()
    a=0 
    b=[0]*2019
    l=[0]*2019
    for i in S:
        k=int(i)
        for j in range(2019):
            l[(j*10+k)%2019]=b[j]
        l[k]+=1
        a+=l[0]
        b=l.copy()
    print(a)
main()

Solution (Python):
S = input()[::-1]
dp = [0]*2019
dp[0] = 1

tmp = 0
for i in range(len(S)):
    tmp += int(S[i])*pow(10, i, 2019)
    tmp %= 2019
    dp[tmp] += 1
ans = 0
for i in dp:
    if i >= 2:
        ans += i*(i-1)//2
print(ans)

Solution (Python):
s = input()
from collections import Counter
x = 1
mod = 2019
total = 0
ans = 0
cnt = Counter()
for i in range(len(s)-1,-1,-1):
    cnt[total] += 1
    total = total + int(s[i])*x
    total %= mod
    ans += cnt[total]
    x = x*10%2019
print(ans)

Solution (Python):
S = input()
N = len(S)
P = 2019


d = [0] * (N+1)
ten = 1
for i in range(N-1, -1, -1):
    a = int(S[i]) * ten % P
    d[i] = (d[i+1] + a) % P 
    ten *= 10
    ten %= P

ans = 0
count = [0] * P
for i in range(N, -1, -1):
    ans += count[d[i]]
    count[d[i]] += 1

print(ans)

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

s = s[::-1]

x = 1
mod = 2019
total = 0
ans = 0
cnt = Counter()

for i in range(len(s)):
    cnt[total] += 1
    total = total + int(s[i])*x
    total %= mod
    ans += cnt[total]
    x = x*10%mod

print(ans)

Solution (Python):


import sys



from collections import defaultdict          















def main():
    mod = 1000000007                  
    inf = float('inf')
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    
    s = input()
    n = len(s)
    """
    s = d0d1...dn-1
    Tk = dk...dn-1 とする
    Tn = 0
    Tk = T(k+1) + dk * 10^(n-k-1) で計算可能
    s[i:j] = di...d(j-1)
           = (Ti - Tj) * 10^(n-j)
    s[i:j] ≡ 0 (mod 2019) <=> Ti ≡ Tj (mod 2019) で判定可能
    """

    dp = [0] * (n + 1)
    for i in range(n-1, -1, -1):
        dp[i] = (dp[i+1] + int(s[i]) * pow(10, n - i - 1, 2019)) % 2019
    
    prev_dict = defaultdict(int)
    cnt = 0
    for i in range(n, -1, -1):
        cnt += prev_dict[dp[i]]
        prev_dict[dp[i]] += 1
    print(cnt)


if __name__ == "__main__":
    main()

Solution (Python):
s = input()[::-1]
l = len(s)
MOD = 2019
ans = 0
mods = [0] * 2019
mods[0] = 1
num = 0
cnt = 0
d = 1
for i in s:
    num += int(i) * d
    num %= MOD
    mods[num] += 1
    d *= 10
    d %= MOD

for j in mods:
    ans += int(j*(j-1)/2)

print(ans)

Solution (Python):
def main():
    s = input()
    n = len(s)
    modlis = [0 for i in range(2019)]
    modlis[0]=1
    t = 0
    m = 1
    for i in range(n):
        t = (t+int(s[n-1-i])*m)%2019
        m = m*10%2019
        modlis[t]+=1

    ans = 0
    for i in range(2019):
        ans += (modlis[i]*(modlis[i]-1))//2
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import numpy as np
import sys
import math

S = input()

mm = [0] * (2019)
mm[0] = 1
a = [0] * (len(S) + 1 ) 
T = 0
ten = 1
ll = len(S)
for i in range(ll):
    if i > 0:
        ten = ( ten * 10 ) % 2019
    a[i+1] =  ( a[i] + (int(S[ll-1-i])*ten ) ) % 2019
    mm[ a[i+1] ] += 1
    

ans = 0

for m in mm:
    ans += int( (m * (m-1) ) / 2 )

print(ans)

Solution (Python):
'''
1~9の文字列で構成されたSの中で
i文字目からj文字目までが2019の倍数になっているものの個数を求めよ

terms:
    1<=|S|<=2*10**5

input:
    1817181712114

output:
    3
'''
from collections import defaultdict
import sys
input = sys.stdin.readline
s = input()[:-1]
n=len(s)
arr = [int(s[i]) for i in range(n)]
ans = 0
dic = defaultdict(int)
dic[0] = 1
now = 0
for i in range(n - 1, -1, -1):
    now += arr[i] * pow(10, n - i - 1, 2019)
    now %= 2019
    ans += dic[now]
    dic[now] += 1
print(ans)

Solution (Python):
s=input()
mod=2019
dp=[0]*mod
k=0
t=1
for x in map(int,s[::-1]):
    k+=t*x
    k%=mod
    dp[k]+=1
    t*=10
    t%=mod
print(sum(i*(i-1)//2 for i in dp)+dp[0])

Solution (Python):
from collections import Counter

S = [int(i) for i in input()]
S.reverse()
lst = []
_sum = 0

for i in range(len(S)):
    _sum = (_sum + S[i] * pow(10,i,2019)) % 2019
    lst.append(_sum)
    
C = Counter(lst)    
ans = C[0]

for i in C:
    ans += C[i]*(C[i]-1)//2
print(ans)

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=list(input())
s.reverse()
s_len = len(s)

list = [0 for _ in range(2019)]
ans = 0
mod = 2019
li = [0]
digit = 0
for i in range(s_len):
    li.append((li[i] + int(s[i])*pow(10,digit,mod))%mod)
    digit+=1
for i in range(s_len+1):
    list[li[i]]+=1
for i in range(2019):
    x = list[i]
    ans += x*(x-1)/2
print(int(ans))

Solution (Python):



def main():
    
    mod_count = [1] + [0] * 2019
    tmp_mod_val = 0
    cnt = 0
    i = 0
    for s in reversed(input()):
        tmp_mod_val += int(s) * pow(10, i, 2019)
        tmp_mod_val %= 2019
        cnt += mod_count[tmp_mod_val]
        mod_count[tmp_mod_val] += 1
        i += 1

    print(cnt)


main()

Solution (Python):



def main():
    
    

    
    mod_res_dic = {0: 1}
    tmp_mod_val = 0
    
    cnt = 0
    i = 0
    for s in reversed(input()):
        tmp_mod_val += int(s) * pow(10, i, 2019)
        tmp_mod_val %= 2019
        ls = mod_res_dic.setdefault(tmp_mod_val, 0)
        mod_res_dic[tmp_mod_val] = ls + 1
        i += 1
        if ls > 0:
            cnt += ls

    print(cnt)


main()

Solution (Python):
from collections import Counter

MOD = 2019
S = input()[::-1]
n = 0
x = 1
c = Counter()
ans = 0

for s in S:
    c[n] += 1
    n += int(s) * x
    n %= MOD
    ans += c[n]
    x = x * 10 % MOD
    
print(ans)

Solution (Python):



def main():
    
    st = input()

    
    mod_res_dic = {0: 1}
    tmp_mod_val = 0
    r_st = ''.join(list(reversed(st)))
    cnt = 0
    for i in range(len(r_st)):
        target_st = r_st[i:i+1]
        tmp_mod_val += int(target_st)*pow(10, i, 2019)
        tmp_mod_val %= 2019
        ls = mod_res_dic.setdefault(tmp_mod_val, 0)
        mod_res_dic[tmp_mod_val] = ls+1
        if ls > 0:
            cnt += ls

    print(cnt)


main()

Solution (Python):



def main():
    
    st = input()

    
    mod_res_dic = {0: [0]}
    tmp_mod_val = 0
    r_st = ''.join(list(reversed(st)))
    cnt = 0
    for i in range(len(r_st)):
        target_st = r_st[i:i+1]
        tmp_mod_val += int(target_st)*pow(10, i, 2019)
        tmp_mod_val %= 2019
        ls = mod_res_dic.setdefault(tmp_mod_val, [])
        ls.append(i-1)
        if len(ls) > 1:
            cnt += len(ls)-1

    print(int(cnt))


main()

Solution (Python):


def main():
    
    st = input()

    
    
    

    
    mod_res_dic = {0: [0]}
    tmp_mod_val = 0
    r_st = ''.join(list(reversed(st)))
    cnt = 0
    for i in range(len(r_st)):
        target_st = r_st[i:i+1]
        
        tmp_mod_val += int(target_st)*pow(10, i, 2019)
        tmp_mod_val %= 2019
        ls = mod_res_dic.setdefault(tmp_mod_val, [])
        ls.append(i-1)
        if len(ls) > 1:
            cnt += len(ls)-1
        

    
    
    
    
    
    
    
    

    print(int(cnt))


main()

Solution (Python):
import collections
s = input()
n = len(s)
mod_list = [0]

T = 0
for i in range(n):
    T += int(s[-1-i])*pow(10, i, 2019)%2019
    T = T%2019
    mod_list.append(T)

res = collections.Counter(mod_list)
cnt = 0
for d in res.values():
    cnt += int(d*(d-1)/2)
print(cnt)

Solution (Python):
import collections
S = input()
N = len(S)
mod = 2019
div = [0]*(N+1)
for i in range(N):
    div[i+1] = (div[i] + int(S[N-1-i])*pow(10,i,mod))%mod
d = collections.Counter(div[1::])
ans = d[0]
for i in d.values():
    ans += i*(i-1)//2
print(ans)

Solution (Python):
import collections
S = input()
N = len(S)
mod = 2019
div = [0]*(N+1)
for i in range(N):
    div[i+1] = (div[i] + int(S[N-1-i])*pow(10,i,mod))%mod
d = collections.Counter(div[1::])
ans = d[0]
for i in d.values():
    ans += i*(i-1)//2
print(ans)

Solution (Python):
s = input()

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

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

Solution (Python):
s = input()

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

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

Solution (Python):
S=input()

N=len(S)
count=0
tot=[0]*2019
S=S[::-1]
base=0

for i,s in enumerate(S):
    base+=int(s)*pow(10,i,2019)
    base%=2019
    
    
    if base==0:
        count+=1
    count+=tot[base]
    
    tot[base]+=1


print(count)

Solution (Python):
s = input()

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

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

Solution (Python):
S=input()
T=len(S)
a=0
d=1
mod=[0]*2019
mod[0]+=1
for i in range(T):
 a+=(int(S[T-i-1])*(d))%2019
 b=a%2019
 mod[b]+=1
 d*=10
 d%=2019
c=0
for i in range(2019):
 if mod[i]>=2:
  c+= mod[i]*(mod[i]-1)/2
print(int(c))

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

count = [0] * 2019
count[0] = 1

num = 0
d = 1

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


res = 0
for cnt in count:
    res += cnt * (cnt - 1) // 2

print(res)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline

MOD = 2019

"""
gcd(10, 2019) = 1 互いに素(最大公約数が1)
-> 10のmod2019での逆元(1/10)が存在
"""
S = input().rstrip()
s_list = list(map(int, list(S)))[::-1]

counts = [0] * MOD
counts[0] = 1

num = 0
for i, s_element in enumerate(s_list):
    num += s_element * pow(10, i, MOD)
    num %= MOD
    counts[num] += 1

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

print(ans)

Solution (Python):
mod=2019
a=input()

N=len(a)
t=[0]*mod
t[0]=1

w=1
u=0

for i in range(N):
  u=int(a[N-1-i]) * w + u
  v=u % mod
  t[v] += 1
  w *=10
  w %= mod

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

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
from collections import *

S = input()[:-1]
cnt = defaultdict(int)
cnt[0] = 1
now = 0
ans = 0

for i in range(len(S)-1, -1, -1):
    now = (int(S[i])*pow(10, len(S)-1-i, 2019)+now)%2019
    ans += cnt[now]
    cnt[now] += 1

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
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
from itertools import permutations,  combinations, accumulate
from functools import *
from collections import deque, defaultdict, Counter
from heapq import heapify, heappop, heappush, heappushpop

INF = float('inf')
NIL = - 1
MOD = 2019

"""
gcd(10, 2019) = 1 互いに素
-> 10のmod2019での逆元(1/10)が存在
"""
S = input().rstrip()
s_list = list(map(int, list(S)))[::-1]

counts = [0] * MOD
counts[0] = 1

num, d = 0, 1

for s_element in s_list:
    num += s_element * d
    num %= MOD
    d *= 10
    d %= MOD
    counts[num] += 1

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

print(ans)

Solution (Python):
S = input()[::-1]
digits = 1
Sum, ans = 0, 0
l = [0] * 2019
l[0] = 1

for c in S:
  Sum += int(c) * digits % 2019
  Sum %= 2019
  l[Sum] += 1
  digits = digits * 10 % 2019

for i in range(len(l)):
  ans += l[i] * (l[i] - 1) // 2
  
print(ans)

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; ansli = []; tmpli = []; candili = []
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]
def mi(n): return [wi() for _ in range(n)]
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]

s = s()

mod = 2019
num_int = 0
mod_dict = defaultdict(int)
mod_dict[num_int % mod] += 1
s = s[::-1]
t = 1
for i in range(len(s)):
    num_int += t * int(s[i])
    num_int %= mod
    t *= 10
    t %= mod
    mod_dict[num_int] += 1

for i in mod_dict:
    ans += ((mod_dict[i] - 1) * mod_dict[i]) // 2

print(ans)

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; ansli = []; tmpli = []; candili = []
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]
def mi(n): return [wi() for _ in range(n)]
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]

s = s()

mod = 2019
num_int = 0
mod_dict = defaultdict(int)
mod_dict[num_int % mod] += 1
s = s[::-1]
t = 1
for i in range(len(s)):
    num_int += t * int(s[i])
    num_int %= mod
    t *= 10
    t %= mod
    mod_dict[num_int] += 1

for i in mod_dict:
    ans += ((mod_dict[i] - 1) * mod_dict[i]) // 2

print(ans)