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.



Sample input:
1817181712114


Sample output:
3

Submission status: Time Limit Exceeded

Solution code:
S = input()
n = len(S)
Y = {0:1}
# print(Y)
Y[0] = 1
for i in range(1,n+1):
    k = int(S[-i:])%2019
    if k not in Y:
        Y[k] = 1
    else:
        Y[k] += 1
# print(Y)

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

Submission status: Time Limit Exceeded

Solution code:
S = [i for i in input()]
mods = [0 for _ in range(2020)]
ans = 0
mods[0] += 1

s = ""
for i in S[::-1]:
    s = i + s

    mods[int(s) % 2019] += 1

for i in mods:
    if i >= 2:
        ans += (i * (i-1))//2
    else:
        continue

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    rem = [0] * 2019  # the count of the each remainder: 0 ~ 2018
    rem[0] = 1  # why? can't understand.

    for i in range(len(s)):
        r = int(s[-(i + 1):]) % 2019
        rem[r] += 1

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

    return ans


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

Submission status: Time Limit Exceeded

Solution code:
S = input()
mods = [0 for _ in range(2020)]
ans = 0
mods[0] += 1

S = [s for s in S]
S.reverse()

s = []
for i in range(len(S)):
    s_tmp = [str(S[i])]
    s_tmp.extend(s)

    mods[int(''.join(s_tmp)) % 2019] += 1
    s = s_tmp

for i in mods:
    if i >= 2:
        ans += (i * (i-1))//2
    else:
        continue

print(ans)

Submission status: Time Limit Exceeded

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

counts = [0] * 2019

num, d = 0, 0

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

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

ans += counts[0]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
n = len(S)
# print(n)
M = 2019
# A = [0]
Y = [0]*M
Y[0] = 1
for i in range(1,n+1):
    Y[int(S[-i:])%2019] += 1
    # A.append(int(S[-i:])%2019)
# print(A)
# print(Y)
c = 0
for y in Y:
    if y >= 2:
        c += y*(y-1)//2
print(c)

Submission status: Time Limit Exceeded

Solution code:
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) * int(s[i])
    now %= 2019
    mods[now] += 1
ans = 0
for i in mods:
    ans += (i * (i - 1)) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()

ans = 0
for j in range(len(S) - 1, 2, -1):
        for i in range(0, j - 2):            
            if (int(S[i:j + 1]) % 2019) == 0:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
l = len(S)
ans = 0
dp = []

for n in range(l):
    m = int(S[n:])%2019
    if m == 0 or m in dp:
        ans += 1
    #print(int(S[n:]))
    #print(int(S[n:])%2019)
    dp.append(m)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
l = len(S)
ans = 0
dp = []

for n in range(l):
    #print(int(S[n:]))
    #print(int(S[n:])%2019)
    dp.append(int(S[n:])%2019)
#print(dp)
#print(sorted(dp))
dp = sorted(dp)
for n in range(l-1):
    if dp[n] == 0 or dp[n] == dp[n+1]:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

if __name__ == '__main__':
    s = list(input())
    s.reverse()
    n = len(s)
    now = 0
    mods = [0] * n
    for i in range(n):
        now += (10 ** i) * int(s[i])
        mods[i] = now % 2019
    mods.sort()
    m = Counter(mods)
    ans = 0
    for k, v in m.items():
        if k == 0:
            ans += v
        ans += (v * (v - 1)) // 2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
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
from collections import Counter

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

Submission status: Time Limit Exceeded

Solution code:


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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

if __name__ == '__main__':
    s = list(input())
    n = len(s)
    mods = [0] * n
    for i in range(n - 1, -1, -1):
        mods[i] = int(''.join(s[i:])) % 2019
    mods.sort()
    m = Counter(mods)
    ans = 0
    for k, v in m.items():
        if k == 0:
            ans += v
        ans += (v * (v - 1)) // 2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

if __name__ == '__main__':
    s = list(input())
    s.reverse()
    n = len(s)
    mods = [0] * n
    for i in range(n):
        mods[i] = int(''.join(list(reversed(s[:i + 1])))) % 2019
    mods.sort()
    m = Counter(mods)
    ans = 0
    for k, v in m.items():
        if k == 0:
            ans += v
        ans += (v * (v - 1)) // 2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
amari = [0 for i in range(2019)]
for i in range(N):
    amari[int(S[i : N]) % 2019] += 1
ans = amari[0]
for i in amari:
    if i > 1:
       ans += i * (i - 1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S=input()
cnt=0
for i in range(len(S)-3):
	for j in range(i+4,len(S)+1):
		if int(S[i:j])%2019==0:
			cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
s=input()
l=len(s)
mod=np.zeros(2019)
sum=0

for i in range(l):
  tmp=int(s[i:])%2019
  sum+=mod[tmp]
  mod[tmp]+=1

print(int(sum+mod[0]))

Submission status: Time Limit Exceeded

Solution code:
S = input()
digits = list(map(int,S))
# S = S[::-1]→これが時間かかる

# i~nまでの値をmod2019したときの数
ls = [0for i in range(2019)]

# 0文字の時
ls[0] += 1

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

# for x in ls:
#     count += x*(x-1)/2

print(int(count))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
s=input()
l=len(s)
mod=np.zeros(2019)


for i in range(l):
  mod[int(s[i:])%2019]+=1

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

print(int(sum))

Submission status: Time Limit Exceeded

Solution code:
s = input()

d = [0]*2019 
d[0] = 1
for i in range(len(s)):
  r = int(s[i:]) % 2019
  d [r] += 1

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

print(int(ans))

Submission status: Time Limit Exceeded

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

# i~nまでの値をmod2019したときの数
ls = [0for i in range(2019)]

# 0文字の時
ls[0] += 1

x = 0
pow10 = 1
count = 0
for i in range(len(S)):
    x += int(S[i])*pow10
    count += ls[x%2019]
    ls[x%2019] += 1
    pow10 *= 10

# for x in ls:
#     count += x*(x-1)/2

print(int(count))

Submission status: Time Limit Exceeded

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

# i~nまでの値をmod2019したときの数
ls = [0for i in range(2019)]

# 0文字の時
ls[0] += 1

x = 0
for i in range(len(S)):
    x += int(S[i])*10**i
    ls[x%2019] += 1

count = 0
for x in ls:
    count += x*(x-1)/2

print(int(count))

Submission status: Time Limit Exceeded

Solution code:
S = input()

# i~nまでの値をmod2019したときの数
ls = [0for i in range(2019)]

# 0文字の時
ls[0] += 1

for i in range(len(S)):
    x = int(S[i:])%2019
    ls[x] += 1

count = 0
for i in range(2019):
    x = ls[i]
    count += x*(x-1)/2

print(int(count))

Submission status: Time Limit Exceeded

Solution code:
def getAns(s):
	n = len(s)
	cnt = 0
	for i in range(n):
		for j in range(i+1, n+1):
			curr = int(s[i: j])
			#print(curr)
			if curr % 2019 == 0 and curr != 0:
				cnt += 1
	return cnt


s = input()
print(getAns(s))

Submission status: Time Limit Exceeded

Solution code:
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
    amari[temp] += 1
    d *= 10

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

print(int(ans))

Submission status: Time Limit Exceeded

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

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

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

print(int(ans))

Submission status: Time Limit Exceeded

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

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

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

print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

s=str(input())
l=len(s)
T=np.zeros(l)

sum=0

for i in range(l):
  T[i]=int(s[i:])%2019
  if(T[i]==0):
    sum=sum+1

for i in range(l-1):
  for j in range(i+1,l):
    if(T[i]==T[j]):
      sum=sum+1
      
print(sum)

Submission status: Time Limit Exceeded

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

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

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

print(int(ans))

Submission status: Time Limit Exceeded

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

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

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

print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
l=len(s)
T=[]

sum=0

for i in range(l):
  T.append(int(s[i:])%2019)
#print(T)
for i in range(l-1):
  if(T[i]==0):
    sum=sum+1
  for j in range(i+1,l):
    if(T[i]==T[j]):
      sum=sum+1
      
print(sum)

Submission status: Time Limit Exceeded

Solution code:
S = input()
digits = list(map(int, S))
divisor = 2019

rem_count = [0] * divisor
rem_count[0] = 1
num = 0
pow10 = 1
total = 0

for digit in reversed(digits):
    num = digit * pow10 + num
    total += rem_count[num % divisor]
    pow10 *= 10
    rem_count[num % divisor] += 1

print(total)

Submission status: Time Limit Exceeded

Solution code:
#--Multiple of 2019
#import time
S = (input())
#start_time = time.time()
#print(S[0:1]) #-18
number = len(S)
k = 0
#integer = int(S[0]) + int(S[2])
for i in range(number):
  for j in range(i+1,number+1):
    q, judge = divmod(int(S[i:j]),2019)
    if(judge == 0):
      k = k + 1
    else:
      pass

print(k)
#end_time = time.time()
#print(end_time - start_time)
#print(S)

Submission status: Time Limit Exceeded

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

temp = 0
for i in range(n):
    temp += int(s[i])*10**i
    amari[temp%2019] += 1

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

print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
n = input()
nn = len(n)
count = 0
if nn<5:
    pass
else:
    for i in range(nn-4):
        for j in range(i+4,nn):
            if int(n[i:j+1])%2019 == 0:
                count +=1
            else:
                pass
print(count)

Submission status: Time Limit Exceeded

Solution code:
#--Multiple of 2019
#import time
S = (input())
#start_time = time.time()
#print(S[0:1]) #-18
number = len(S)
k = 0
#integer = int(S[0]) + int(S[2])
for i in range(number):
  for j in range(number+1):
    if(i < j):
      judge = int(S[i:j]) % 2019
      if(judge == 0):
        k = k + 1
      else:
        pass

print(k)
#end_time = time.time()
#print(end_time - start_time)
#print(S)

Submission status: Time Limit Exceeded

Solution code:
#--Multiple of 2019
#import time
S = (input())
#start_time = time.time()
#print(S[0:1]) #-18
number = len(S)
k = 0
#integer = int(S[0]) + int(S[2])
for i in range(number):
  for j in range(number+1):
    if(i < j):
      q, judge = divmod(int(S[i:j]), 2019)
      if(judge == 0):
        k = k + 1
      else:
        pass

print(k)
#end_time = time.time()
#print(end_time - start_time)
#print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()

count = 0
mod_check = {}
mod_check[0] = 1
check_S = 0
for x in range(len(S) - 1, -1, -1):
    check_S += int(S[x]) * (10 ** (len(S) - x - 1))
    mod = check_S % 2019
    mod_check.setdefault(mod, 0)
    mod_check[mod] += 1
mod_match = 0
for x in mod_check.values():
    mod_match += x * (x - 1) // 2
print(mod_match)

Submission status: Time Limit Exceeded

Solution code:
s_l = list(input())
n = len(s_l)

# 公式解説参照
dp_dict = {0:1}
t = 0
for i in range(n):
    a = int(s_l[-(i+1)])
    t = (t + a*(10**i))%2019
    dp_dict[t] = dp_dict.get(t,0) + 1

ans = 0
for val in dp_dict.values():
    ans += val*(val-1)//2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
s = input()
n = len(s)
cnt = 0
lista = []
for i in range(n):
    inta = int(s[-i - 1:n])
    lista.append(inta)
listb = []
for i in range(n):
    intb = lista[i] % 2019
    listb.append(intb)
c = collections.Counter(listb)
for i in listb:
    if i == 0:
        cnt += 1
for i in c.most_common():
    match = i[1]
    if match >= 2:
        cnt += match * (match - 1) // 2
    else:
       break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
s = input()
n = len(s)
cnt = 0
lista = []
for i in range(n):
    inta = int(s[-i - 1:n])
    lista.append(inta)
listb = []
for i in range(n):
    intb = lista[i] % 2019
    listb.append(intb)
c = collections.Counter(listb)
for i in listb:
    if i == 0:
        cnt += 1
for i in c.most_common():
    match = i[1]
    if match >= 2:
        cnt += match * (match - 1) // 2
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
#dt = {} for i in x: dt[i] = dt.get(i,0)+1
#dt = {k:v for k,v in sorted(x.items(), key=lambda i: i[1])}
ipnl = lambda n: [input() for _ in range(n)]
inp = lambda :int(input())
ip = lambda :[int(w) for w in input().split()]
mp = lambda :map(int,input().split())

s = input()
n = len(s)
ctr =0
for i in range(n-1):
    sm = int(s[i])
    for j in range(i+1,n):
        sm += int(s[j])
        if sm%3 == 0 and int(s[i:j+1])%673 == 0:
            ctr += 1
print(ctr)

Submission status: Time Limit Exceeded

Solution code:
n = input()
l = len(n)
ans = 0
for i in range(l - 4):
    t = n[i]
    for j in range(i + 1, l):
        t += n[j]
        if int(t) % 2019 == 0:
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
cnt = 0
lista = []
for i in range(n):
    inta = int(s[-i - 1:n])
    lista.append(inta)
listb = []
for i in range(n):
    intb = lista[i] % 2019
    listb.append(intb)
for i in listb:
    if i == 0:
        cnt += 1
for i in range(len(listb)):
    for j in range(i + 1, len(listb)):
        if listb[i] == listb[j]:
            cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = input()
l = len(n)
ans = 0
for i in range(l - 4):
    for j in range(i+4,l + 1):
        if int(n[i:j]) % 2019 == 0:
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#dt = {} for i in x: dt[i] = dt.get(i,0)+1
#dt = {k:v for k,v in sorted(x.items(), key=lambda i: i[1])}
ipnl = lambda n: [input() for _ in range(n)]
inp = lambda :int(input())
ip = lambda :[int(w) for w in input().split()]
mp = lambda :map(int,input().split())

s = input()
n = len(s)
ctr =0
for i in range(n-1):
    for j in range(i+1,n):
        t = int(s[i:j+1])
        if t%2019 == 0:
            ctr += 1
print(ctr)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

counts = [0] * 2019

num, d = 0, 1

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

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

ans += counts[0]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
counts = [0] * 2019

for i in range(len(s)):
    counts[int(s[i: len(s)]) % 2019] += 1

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

ans += counts[0]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
count=0
mod_list=[0]*2019

for i in range(len(s)):
    mod_list[int(s[i:])%2019]+=1

for i in range(2019):
    count+=int(mod_list[i]*(mod_list[i]-1)/2)

count+=mod_list[0]
print(count)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
count = 0
for i in range(5,N):
    for j in range(0,N-i+1):
        if int(S[j:j+i])%2019 == 0:
            count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:

def main():
  s = tuple(map(int, input()[::-1]))

  mod = 2019

  counts = [0] * mod
  counts[0] = 1
  t = 0
  x = 1
  for i in range(len(s)):
    t = t + s[i] * x
    counts[t % mod] += 1
    x = x * 10

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

  print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:
s_l = list(input())
n = len(s_l)

# 公式解説参照
dp = [0]
t = 0
for i in range(n):
    a = int(s_l[-(i+1)])
    t = (t + a*(pow(10,i,2019)))%2019
    dp.append(t)

ans = 0
for i in range(2019):
    m = dp.count(i)
    ans += m*(m-1)//2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
str_list = str(input())
length = len(str_list)
 
cnt = 4
result = 0
 
for i in range(length-3):
    for j in range(length-cnt+1):
        if int(str_list[j:j+cnt])%2019 == 0:
            result += 1
    cnt += 1
 
print(result)

Submission status: Time Limit Exceeded

Solution code:

def main():
  s = input()
  n = len(s)

  mod = 2019

  counts = {}
  counts[0] = 1

  prev = int(s[-1])
  counts[prev % mod] = 1
  for i in range(2, n + 1):
    t = (prev + int(s[-i]) * 10 ** (i - 1)) % mod
    if t in counts:
      counts[t] += 1
    else:
      counts[t] = 1

    prev = t

  ans = 0
  for count in counts.values():
    if count > 1:
      ans += count * (count - 1) // 2

  print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
k = len(s)

t = [i for i, x in enumerate(s) if int(x) == 1 or int(x) % 2 == 0]

ans = 0
for i in t:
  for j in range(4,k-i+1):
    if int(s[i:i+j]) % 2019 == 0:
      ans += 1
      
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=list(map(int,input()))
d={}
d[0]=1
a=0
b=len(n)
for i in reversed(range(b)):
    a+=(n[i]*pow(10,(b-i-1)))
    a%=2019
    if not a in d:
        d[a]=1
    else:
        d[a]+=1

ans=0

for s,v in d.items():
    if v>=2:
        ans+=v*(v-1)//2
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
mod = [0]
for d, c in enumerate(S[::-1]):
    mod.append((int(c) * 10**d + mod[-1]) % 2019)
from collections import *
print(sum(v * (v - 1) // 2 for v in Counter(mod).values()))

Submission status: Time Limit Exceeded

Solution code:
S = input()

cs = {0: 1}

result = 0

n = 0
for i in range(len(S)):
    c = S[-i-1]
    n += int(c) * (10**i)
    m = n % 2019
    if m in cs:
        result += cs[m]
        cs[m] += 1
    else:
        cs[m] = 1

print(result)

Submission status: Time Limit Exceeded

Solution code:

def main():
  s = input()
  n = len(s)

  mod = 2019

  counts = [0] * mod
  counts[0] += 1

  prev = int(s[-1])
  counts[prev % mod] += 1
  for i in range(2, n + 1):
    t = prev + int(s[-i]) * 10 ** (i - 1)
    counts[t % mod] += 1
    prev = t

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

  print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:
n=list(map(int,input()))
d={}
d[0]=1
a=0
for i in reversed(range(len(n))):
    a+=(n[i]*10**(len(n)-i-1))
    a%=2019
    if not a in d:
        d[a]=1
    else:
        d[a]+=1

ans=0

for s,v in d.items():
    if v>=2:
        ans+=v*(v-1)//2
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
s = input()
u = collections.deque([0])
for i in range(len(s)):
    u.append((u[i]+int(s[::-1][i])*10**(i%224))%2019)
u2 = collections.Counter(u)
sum = 0
for i in u2.values():
    sum += i*(i-1)/2
print(int(sum))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
MOD=2019
S=input();N=len(S)
Cum=[0]*(N+1)
for i in range(N):
    Cum[i+1]=(Cum[i]+int(S[N-i-1])*(10**i))%MOD
res=0
c=Counter(Cum).most_common()
for a,b in c:
    res+=b*(b-1)//2
    if(b==1):
        break
print(res)

Submission status: Time Limit Exceeded

Solution code:
s=input()
cnt=0
l=len(s)
for i in range(l-1):
  for j in range(i+1,l):
    n=int(s[i:j+1])
    if n%2019==0:
      cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = input()
mod = [0]
for i in range(len(S)):
    mod.append(int(S[i:]) % 2019)
from collections import *
print(sum(v * (v - 1) // 2 for v in Counter(mod).values()))

Submission status: Time Limit Exceeded

Solution code:
s=input()
cnt=0
for i in range(len(s)-1):
  for j in range(i+1,len(s)):
    n=int(s[i:j+1])
    if n%2019==0:
      cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
count=0

for i in range(len(s)):
    if s[i]=="0":
        count+=1

for i in range(len(s)):
    for j in range(i+3,len(s)):
        if int(s[i:j+1])%2019==0:
            count+=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

def main():
  s = input()
  n = len(s)

  mod = 2019

  t_vals = [0] * n
  t_vals[-1] = int(s[-1])
  for i in range(2, n + 1):
    t_vals[-i] = t_vals[-i + 1] + int(s[-i]) * 10 ** (i - 1)

  counts = defaultdict(int)
  counts[0] = 1
  for t in t_vals:
    counts[t % mod] += 1

  ans = 0
  for count in counts.values():
    ans += count * (count - 1) // 2

  print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:
s_l = list(input())
n = len(s_l)

# 公式解説参照
dp = [0]
t = 0
for i in range(n):
    a = int(s_l[-(i+1)])
    t = (t + a*(10**i))%2019
    dp.append(t)

ans = 0
for i in range(2019):
    m = dp.count(i)
    ans += m*(m-1)//2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
count=0

for i in range(len(s)):
    for j in range(i,len(s)):
        if int(s[i:j+1])%2019==0:
            count+=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
S = int(S)
T = []
while S > 0:
    T.append(S)
    S = S % (10 ** (N - 1))
    N -= 1
amari = [0 for i in range(2019)]
for i in T:
    amari[i % 2019] += 1
ans = amari[0]
for i in amari:
    ans += i * (i - 1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
cnt = 0
for j in range(3, len(S)+1):
    for i in range(j-3):
        if int(S[i:j]) % 2019 == 0:
            cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
    return sys.stdin.readline().rstrip('\n')

def calc(S):
    N = len(S)
    r = 0
    d = [0] * 2019
    d[0] = 1
    for i in range(N):
        r = (r + int(S[N - 1 - i]) * 10 ** i) % 2019
        d[r] += 1
    ans = 0
    for c in d:
        ans += c * (c - 1) // 2
    return ans


S = input()
print(calc(S))

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict

lines = 1
inp = list()

for _ in range(lines):
    inp.append(sys.stdin.readline().strip())


s = inp[0]
n = len(s)

tmp = defaultdict(int)
tmp[0] = 1

for k in range(1, n + 1):
    t = int(s[n-k:n])
    tmp[t % 2019] += 1


result = 0

for v in tmp.values():
    result += int(v * (v - 1) / 2)


print(n - len(tmp) + 1)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

s = list(map(int, list(input())))[::-1]
N = len(s)
cusum = [0]*(N+1)
cusum[0] = s[0] % 2019
power = 1
for i in range(1, N):
    power = 10**i % 2019
    cusum[i] = (cusum[i-1] + power * s[i]) % 2019
counter = Counter(cusum)
print(sum(x*(x-1)//2 for x in counter.values()))

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
forcus = []
count = 0
for i in range(len(S)):
    if i+4 > len(S):
        break
    forcus = S[i] + S[i+1] + S[i+2]
    for j in range(i+3,len(S)):
        forcus = forcus + S[j]
        if int(forcus) % 2019 == 0:
            count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
amari = [0 for i in range(2019)]
for i in range(N):
    amari[int(S[i : N]) % 2019] += 1
ans = amari[0]
for i in amari:
    ans += i * (i - 1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
data = [1] + [0]*2019
num = 0
dig = 1

for i in reversed(S):
    num += int(i)*dig
    num %= 2019
    dig *= 10
    data[num] += 1

print(sum(i*(i-1)//2 for i in data))  #print(sum(i*(i-1)//2) for i in data)ではない

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
def solve():
    S = input()
    data = [1] + [0]*2019
    num = 0
    dig = 1

    for i in reversed(S):
        num += int(i)*dig
        num %= 2019
        dig *= 10
        data[num] += 1

    print(sum(i*(i-1)//2 for i in data))  #print(sum(i*(i-1)//2) for i in data)ではない

if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

S = input()
N = len(S)

s = [0 for _ in range(N)]

num = ""
for i in range(N-1, -1, -1):
    num = S[i] + num
    s[i] += int(num)%2019

ans = 0
# print(Counter(a))
for key, val in Counter(s).items():
    if key == 0:
        ans += val
    if val >= 2:
        ans += val*(val-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# D

import sys
# 再起回数上限変更
# sys.setrecursionlimit(1000000)

from collections import Counter

S = input()

# res = []

ans = 0
res = [0] * 2019
idxs = {}

for i in range(len(S)):
    val = int(S[i:]) % 2019
    if val == 0:
        ans += 1
    else:
        res[val] += 1
        if res[val] > 1:
            idxs[val] = 1

for k, v in idxs.items():
    m = res[k]
    ans += int(m * (m-1) / 2)

print(ans)

'''
ans = 0
i = 0
cnt = 0
prev_j = -1

while True:
    for j in range(i+5, len(S)+1):
        print(i, j, S[i:j])
        if int(S[i:j]) % 2019 == 0:
            ans += 1
            if prev_j == i:
                cnt += 1
                ans += cnt
            else:
                cnt = 0
            prev_j = j
            i = j - 2
            break
    i += 1
    if i+4 >= len(S):
        break

print(ans)
'''

Submission status: Time Limit Exceeded

Solution code:
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
    cnts[sum_of_digits] += 1
 
ans = 0
for cnt in cnts:
    ans += cnt * (cnt - 1) // 2
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
ans = 0
T = []
for i in range(N):
    T.append(int(S[i : N]) % 2019)
#T.sort()
ans += T.count(0)
t = set(T)
counted = []
for i in t:
    a = T.count(i)
    if a > 1:
        counted.append(a)
for i in counted:
    ans += i * (i - 1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
def solve():
    S = input()
    data = [1] + [0]*2019
    num = 0
    dig = 1

    for i in reversed(S):
        num += int(i)*dig
        num %= 2019
        dig *= 10
        data[num] += 1

    npdata = np.array(data, dtype=np.int32)
    print(np.sum(npdata*(npdata-1)//2))

if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
a=list(map(int,input()))
n=len(a)
ans=0
d=[0]*2019
d2=[0]*2019
d2[a[0]]+=1
for i in range(1,n):
    d[a[i]]+=1
    for j in range(2019):
        d[(10*j+a[i])%2019]+=d2[j]
    ans+=d[0]
    for j in range(2019):
        d2[j]=d[j]
        d[j]=0
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import math

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

S = input()
N = len(S)

s = [0 for _ in range(N)]

num = ""
for i in range(N-1, -1, -1):
    num = S[i] + num
    s[i] += int(num)%2019

ans = 0
# print(Counter(a))
for key, val in Counter(s).items():
    if key == 0:
        ans += val
    if val >= 2:
        ans += combinations_count(val, 2)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

lines = 1
inp = list()

for _ in range(lines):
    inp.append(sys.stdin.readline().strip())


s = inp[0]
n = len(s)

tmp = set()
tmp.add(0)

for k in range(1, len(s) + 1):
    t = int(s[n-k:len(s)])
    tmp.add(t % 2019)

print(n - len(tmp) + 1)

Submission status: Time Limit Exceeded

Solution code:
import collections
s = input()
u = collections.deque([0])
for i in range(len(s)):
    u.append((u[i]+int(s[::-1][i])*10**i)%2019)
u2 = collections.Counter(u)
print(sum([int(i*(i-1)/2) for i in u2.values()]))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# D

import sys
# 再起回数上限変更
# sys.setrecursionlimit(1000000)

from collections import Counter

S = input()

res = []

ans = 0
for i in range(len(S)):
    # print(int(S[i:]))
    val = int(S[i:]) % 2019
    if val == 0:
        ans += 1
    res.append(val)

# print(res)
dict = Counter(res)

keys = [int(v*(v-1)/2) for k, v in dict.items() if (v > 1)]

print(ans + sum(keys))

'''
ans = 0
i = 0
cnt = 0
prev_j = -1

while True:
    for j in range(i+5, len(S)+1):
        print(i, j, S[i:j])
        if int(S[i:j]) % 2019 == 0:
            ans += 1
            if prev_j == i:
                cnt += 1
                ans += cnt
            else:
                cnt = 0
            prev_j = j
            i = j - 2
            break
    i += 1
    if i+4 >= len(S):
        break

print(ans)
'''