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 = [i for i in input()]
mod_num = 2019
mods = []
ans = 0

S.reverse()

for n,s in enumerate(S):
    mods.append(int(s) * 10**n % mod_num)
tmp = 0

mod_sum = [0 for _ in range(2020)] 
mod_sum[0] += 1

for mod in mods:
    tmp += mod
    mod_sum[tmp % mod_num] += 1

for i in mod_sum:
    if i <= 1:
        continue
    else:
        ans += (i * (i - 1)) // 2
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()

target = 2019
max_mul = int(s) // target
cnt = 0
for i in range(max_mul+1):
    cnt += s.count(str(i*target))
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import re
s = input()
int_s = int(s)

target = 2019
max_mul = int_s // target
cnt = 0
for i in range(max_mul+1):
    cnt += len(re.findall(str(i*target), s))
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S=input()

T,A=[1]+[0]*2018,0
for i in range(1,len(S)+1):
  T[int(S[-i:])%2019]+=1
print(sum(T[i]*~-T[i]//2 for i in range(2019)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

# 17:36-18:18 give up => kaisetu
# -18:46


def main():
    S = input() + "0"
    N = len(S)

    q = 2019

    res = [0] * N
    num = 0
    p = 0
    for i in range(N-1, -1, -1):
        num += int(S[i]) * 10**p
        res[i] = num % q
        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())

Submission status: Time Limit Exceeded

Solution code:
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) * 10 ** (i) + acum[i]
    acum[i+1] %= 2019
    r[acum[i+1]] += 1
# print(acum)

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

Submission status: Time Limit Exceeded

Solution code:
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()

Submission status: Time Limit Exceeded

Solution code:
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 += int(s[i])*x
    total %= mod
    ans += cnt[total]
    x *= 10%2019
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

# 17:36-18:06


def main():
    S = input()
    N = len(S)

    start_cnt = [0] * N

    for i in range(N-5, -1, -1):
        j = i + 3
        cnt = 0
        while j < N:
            x = int(S[i:j+1])
            if x % 2019 == 0:
                cnt += 1
                if j + 1 < N:
                    cnt += start_cnt[j+1]
                start_cnt[i] = cnt
                break
            j += 1

    # print(start_cnt)
    return sum(start_cnt)


print(main())

Submission status: Time Limit Exceeded

Solution code:
s = input()

li = []
mod = 2019
tmp = 0

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

li.append(tmp)

li.sort()
#print(li)

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]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
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 = int(s[i::])%mod
    ans += cnt[total]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S =  int(input())
a = str(S)
li = list(a)
r = len(li)
c = 0
for i in range(r):
    for a in range(r): 
        v = li[i:a+1]
        if len(v)!= 0:
            d = "".join(v)
            b = int(d)
            if b%2019 == 0:
                c += 1
print(c)

Submission status: Time Limit Exceeded

Solution code:
S =  int(input())
a = str(S)
li = list(a)
r = len(li)

c = 0
b=0

for i in range(r):
    for a in range(r): 
        v = li[i:a+1]
        if len(v) != 0:
            d = "".join(v)
            b = int(d)
            if b%2019 == 0:
                c += 1
print(c)

Submission status: Time Limit Exceeded

Solution code:
S =  int(input())
a = str(S)
li = list(a)
r = len(li)
c = 0
b=0

for i in range(r):
    for a in range(r): 
        v = []
        v = li[i:a+1]
        if len(v) != 0:
            d = "".join(v)
            b = int(d)
            if b%2019 == 0:
                c += 1
print(c)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S_list = list(input())
i = len(S_list)

x = 0

for n_0 in range(i-3):
  for n in range(n_0+4, i+1):
    Si_list = S_list[n_0 : n]
    Si_str = ""
    for y in Si_list:
      Si_str = Si_str + y
    Si = int(Si_str)
    if Si % 2019 == 0:
      x = x + 1
      
print(x)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from collections import Counter

s = list(input())

l = len(s)
ans = 0

dp = np.zeros((3, l))

for i in range(l):
    dp[0][i] = int(s[i])
    dp[1][i] = (int(s[i]) * (10**(l-i-1))) % 2019

dp[2][l-1] = dp[0][l-1]

for i in range(l-2, -1, -1):
    dp[2][i] = (dp[1][i] + dp[2][i+1]) % 2019

c = Counter(dp[2])
for i, j in c.items():
    if i == 0:
        ans += int(j*(j+1)/2)
    elif j >= 2:
        ans += int(j*(j-1)/2)

print(ans)

Submission status: Time Limit Exceeded

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

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
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
    a[i+1] =  ( a[i] + (int(S[ll-1-i])*ten ) ) % 2019
    mm[ a[i+1] ] += 1
    #print(s)

ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S =  int(input())
a = str(S)
li = list(a)
#print(li)
c = 0
b=0
#v =[v = li[i:a+1] for i in range(len(li)) for a in range(len(li)) if len(li)-1 == a ]

for i in range(len(li)):
    for a in range(len(li)): 
        v = []
        v = li[i:a+1]
        if len(v) == 0:
            v = ["1"]
        else:    
           # print(v)
            d = "".join(v)
            b = int(d)
           # print(d)
            if b%2019 == 0:
                c += 1
    
print(c)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
# S = "1817181712114"

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
import re
S = input()
#print(type(S))

s = []
for i in range(int(S) // 2019 // 5):
    s.append(str(i*2019))

count = 0
for i in s:
    S_ = S
    while re.search(i, S_):
        #print(i)
        S_ = S_[:re.search(i, S_).start()] + "," + S_[re.search(i, S_).start()+1:]
        count += 1
        
print(count)

Submission status: Time Limit Exceeded

Solution code:
import re
S = input()
#print(type(S))

s = []
for i in range(int(S) // 2019):
    s.append(str(i*2019))

count = 0
for i in s:
    S_ = S
    while re.search(i, S_):
        #print(i)
        S_ = S_[:re.search(i, S_).start()] + "," + S_[re.search(i, S_).start()+1:]
        count += 1
        
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()

s0 = 0
for i in range(len(s)):
    if i <= len(s) - 3 :
        for j in range(len(s)):
            if i + 3 <= j:
                if int(s[i:j+1]) % 2019 == 0:
                    s0 += 1
print(s0)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
np.set_printoptions(suppress=True)
from collections import Counter
import math

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

s = list(input())

l = len(s)
ans = 0

dp = np.zeros((3, l))

for i in range(l):
    dp[0][i] = int(s[i])
    dp[1][i] = (int(s[i]) * (10**(l-i-1))) % 2019

dp[2][l-1] = dp[0][l-1]

for i in range(l-2, -1, -1):
    c = i
    a = dp[1][i]
    b = dp[2][i+1]
    dp[2][i] = (dp[1][i] + dp[2][i+1]) % 2019

c = Counter(dp[2])
for i, j in c.items():
    if i == 0:
        ans += combinations_count(j+1, 2)
    elif j >= 2:
        ans += combinations_count(j, 2)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
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] * 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)

Submission status: Time Limit Exceeded

Solution code:
s = input()

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

Submission status: Time Limit Exceeded

Solution code:
S = input()

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
N = len(S)

import collections

count_div = collections.defaultdict(int)
count_div[0] += 1

s = ''

for i in reversed(S):
    s = i + s
    count_div[int(s)%2019] += 1

count = 0

for v in count_div.values():
    count += v*(v-1)/2
print(int(count))

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
N = len(S)

import collections

count_div = collections.defaultdict(int)
count_div[0] += 1

for i in range(1, N+1):
    count_div[int(''.join(S[N-i:N]))%2019] += 1

count = 0

for v in count_div.values():
    count += v*(v-1)/2
print(int(count))

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)

import collections

count_div = collections.defaultdict(int)
count_div[0] += 1

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

count = 0

for v in count_div.values():
    count += v*(v-1)/2
print(int(count))

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)

div_list = [0]

import collections

count_div = collections.defaultdict(int)
count_div[0] += 1

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

import collections

count = 0

def cmb(n, r):
    r = min(n - r, r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under

for v in count_div.values():
    if v > 2:
        count += cmb(v, 2)
    elif v == 2:
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
S=input()
N=len(S)

mats=np.zeros((10, 2019, 2019))
for i in range(2019):
  for j in range(10):
    tmp=(i*10+j)%2019
    mats[j][tmp][i]+=1
    
out=0
dp=np.zeros(2019)
dp[int(S[0])]=1
for i in range(1, N):
    dp=np.dot(mats[int(S[i])], dp)
    dp[int(S[i])]+=1
    out+=dp[0]
print(int(out))

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)

div_list = [0]

for i in range(1, N+1):
    div_list.append(int(S[N-i:N])%2019)

import collections

count_div = collections.Counter(div_list)
count = 0

def cmb(n, r):
    r = min(n - r, r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under

for v in count_div.values():
    if v > 2:
        count += cmb(v, 2)
    elif v == 2:
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)

div_list = [0]

for i in range(1, N+1):
    div_list.append(int(S[N-i:N])%2019)

import collections

count_div = collections.Counter(div_list)
count = 0

def cmb(n, r):
    r = min(n - r, r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under

for v in count_div.values():
    if v >= 2:
        count += cmb(v, 2)
print(count)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)

div_list = [0]

for i in range(1, N+1):
    # print(S[N-i:N])
    div_list.append(int(S[N-i:N])%2019)

import itertools

# print(div_list)

count = 0
for i in itertools.combinations(div_list, 2):
    # print(i)

    if i[0] % 2019 == i[1] % 2019:
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from collections import deque
s = input()

s = [int(s[-(i+1):]) for i in range(len(s))]
s = deque(s)
s.appendleft(0)
s = list(s)
s = list(np.array(s) % 2019)

mod_list = [0]*2019
for i in s:
    mod_list[i] += 1

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

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)

def main():
    S = [int(s) for s in input()]
    N = len(S)
    K = 2019

    V = [0] * (N+1)
    count = [0] * 2020
    count[0] = 1
    ans = 0
    ten = 1
    for i in reversed(range(N)):
        rev_i = (N-1)-i
        ten = ten * 10
        v = V[rev_i] + S[i] * ten
        V[rev_i+1] = v
        u = v % K

        count[u] += 1
        cnt = count[u]
        ans -= (cnt-1) * (cnt-2) // 2
        ans += cnt * (cnt-1) // 2


    print(ans)

if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)

def main():
    S = [int(s) for s in input()]
    N = len(S)
    K = 2019

    V = [0] * (N+1)
    count = [0] * 2020
    count[0] = 1
    ans = 0
    for i in reversed(range(N)):
        rev_i = (N-1)-i
        v = V[rev_i] + S[i] * pow(10,rev_i+1)
        V[rev_i+1] = v
        u = v % K

        count[u] += 1
        cnt = count[u]
        ans -= (cnt-1) * (cnt-2) // 2
        ans += cnt * (cnt-1) // 2


    print(ans)

if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
S=input()
N=len(S)
dp=[[0]*2019 for _ in range(N)]
dp[0][int(S[0])]=1
for i in range(1, N):
    for j in range(2019):
        dp[i][(j*10+int(S[i]))%2019]+=dp[i-1][j]
    dp[i][int(S[i])]=1
print(sum([dp[i][0] for i in range(N)]))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)

def main():
    S = [int(s) for s in input()]
    N = len(S)
    K = 2019

    V = [0] * (N+1)
    count = [0] * 2020
    count[0] = 1
    ans = 0
    for i in reversed(range(N)):
        rev_i = (N-1)-i
        v = V[rev_i] + S[i] * (10**(rev_i+1))
        V[rev_i+1] = v
        u = v % K

        count[u] += 1
        cnt = count[u]
        ans -= (cnt-1) * (cnt-2) // 2
        ans += cnt * (cnt-1) // 2


    print(ans)

if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
import re
import sys

s = sys.stdin.buffer.readline().rstrip().decode()
ans = 0

for i in range(1, 200001):
    tmp = str(2019*i)
    if tmp in s:
        index = -1
        while True:
            index = s.find(tmp, index + 1)
            if index == -1:
                break
            ans += 1
    if int(tmp) > int(s):
        break
print(ans)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

def main():
    # 入力
    st = input()

    # if len(st) < 4:
    #     print(0)
    #     return

    # 右からi文字目から最後の数字までを数値化したものについてmodを計算していく
    mod_res_dic = {0: [0]}
    tmp_mod_val = 0
    r_st = ''.join(list(reversed(st)))
    for i in range(1, len(r_st) + 1):
        target_st = r_st[i-1:i]
        mod_res = ((10 ** (i-1)) * int(target_st) + tmp_mod_val) % 2019
        ls = mod_res_dic.setdefault(mod_res, [])
        ls.append(i)
        tmp_mod_val = mod_res

    # print(mod_res_dic)
    # modが同じペアのものについて、iからj文字目までを2019で割り切れるので、その組み合わせを計算
    cnt = 0
    for v in mod_res_dic.values():
        comb = len(v)
        if comb < 2:
            continue
        cnt += (comb * (comb - 1)) / 2

    print(int(cnt))


main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
res = dict()
res[0] = 1

T = 0
for i in range(n):
    T += int(s[-1-i])*(10**i)%2019
    T = T%2019
    if res.get(T) == None:
        res[T] = 1
    else:
        res[T] += 1
cnt = 0
for d in res:
    if not res[d] == 1:
        cnt += int(res[d]*(res[d]-1)/2)
print(cnt)

Submission status: Time Limit Exceeded

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

def main():
    # 入力
    st = input()

    if len(st) < 4:
        print(0)
        return

    # 右からi文字目から最後の数字までを数値化したものについてmodを計算していく
    mod_res_dic = {0: [0]}
    tmp_mod_val = 0
    r_st = ''.join(list(reversed(st)))
    for i in range(1, len(r_st) + 1):
        target_st = r_st[i-1:i]
        mod_res = ((((10 ** (i-1)) * int(target_st)) % 2019) + tmp_mod_val) % 2019
        ls = mod_res_dic.setdefault(mod_res, [])
        ls.append(i)
        tmp_mod_val = mod_res

    # print(mod_res_dic)
    # modが同じペアのものについて、iからj文字目までを2019で割り切れるので、その組み合わせを計算
    cnt = 0
    for v in mod_res_dic.values():
        comb = len(v)
        if comb < 2:
            continue
        cnt += (comb * (comb - 1)) / 2

    print(int(cnt))


main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
res = dict()
res[0] = 1

for i in range(n):
    T = int(s[-i-1:])%2019
    if res.get(T) == None:
        res[T] = 1
    else:
        res[T] += 1
cnt = 0
for d in res:
    if not res[d] == 1:
        cnt += int(res[d]*(res[d]-1)/2)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
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 *= 10
    mod[t] += 1
ans = 0

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

Submission status: Time Limit Exceeded

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

def main():
    # 入力
    st = input()

    if len(st) < 4:
        print(0)
        return

    # 右からi文字目から最後の数字までを数値化したものについてmodを計算していく
    mod_res_dic = {0: [0]}
    for i in range(1, len(st) + 1):
        target_st = st[-i:]
        mod_res = int(target_st) % 2019
        ls = mod_res_dic.setdefault(mod_res, [])
        ls.append(i)
    # print(mod_res_dic)

    # modが同じペアのものについて、iからj文字目までを2019で割り切れるので、その組み合わせを計算
    cnt = 0
    for v in mod_res_dic.values():
        comb = len(v)
        if comb < 2:
            continue
        cnt += (comb * (comb - 1)) / 2

    print(int(cnt))


main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
ans = 0
x = 1
# 累積和を格納
tot = 0
# 累積和のmod 2019をカウントする
cnt = [0] * 2019

for i in range(N)[::-1]:
  cnt[tot] += 1

  tot += int(S[i]) * x
  tot %= 2019
  ans += cnt[tot]
  x *= 10

print(ans)

Submission status: Time Limit Exceeded

Solution code:
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


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

print(res)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys

s = input()

count = 0

for i in range(min(len(s) - 3, 200000)):
  for j in range(i + 4, len(s) + 1):
    # print('i = ', i, 'j = ', j , 's = ' , s[i:j])
    if((int(s[i:j]) % 2019) == 0):
      count += 1
    
print(count)

Submission status: Time Limit Exceeded

Solution code:
S = input()
leng = len(S)

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

num = 0
d = 1

for i in range(leng):
    num += int(S[leng-1-i]) * d
    num %= 2019
    count[num] += 1
    d *= 10


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

print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys

s = input()

count = 0

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

Submission status: Time Limit Exceeded

Solution code:
import sys

s = input()

count = 0

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
leng = len(S)

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

num = 0
d = 1

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


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

Submission status: Time Limit Exceeded

Solution code:
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 % mod

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
mod=2019
a=input()

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

v=0
w=1

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#A,B,C,D=map(int,input().split(" "))

import collections

mod=2019
a=input()

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

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#A,B,C,D=map(int,input().split(" "))

import collections

mod=2019
a=input()

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

for i in range(N):
  u=int(a[i::])
  v=u % mod
  t[v] += 1
  
#tt = collections.Counter(t)

for i in range(len(t)):
  if i==0:
    ans += t[i]
  else:
    ans += (t[i]-1)*t[i]//2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#A,B,C,D=map(int,input().split(" "))

import collections

mod=2019
a=input()

N=len(a)
t=[]
ans=0

for i in range(N):
  u=int(a[i::])
  v=u % mod
  t.append(v)
  
tt = collections.Counter(t)


for i in tt.values():
  ans += (i-1)*i//2

z=t.count(0)
if z>0:
  ans += (z-1)*z//2 + z

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()[::-1] # 右からn桁の数についてループを回していきたいので、文字列を反転させる

sum_of_digits = 0 # 余りの値
cnts = [0] * 2019 # 余りの値ごとに余りが同じ値の数を格納する変数。2019で割る場合、余りは0~2018までの2019通りあるので、リストの長さは2019
cnts[0] = 1 # for文はn回しか回らないが、余りを計算したい桁数は0~n桁のn+1個ある。そこで0桁の余はfor文の前に計算しておく。0桁の値を2019で割った余りは0なので、cnts[0]に1を代入しておく
d = 1 # sum_of_digitsの計算に使う10の累乗値

# まず余りの値ごとに余りが同じ値の数を数える
for c in s:
    sum_of_digits += int(c) * d
    sum_of_digits %= 2019
    d *= 10
#    d %= 2019 # オーバーフローを避けるために2019の剰余を取る。後述
    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:
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()]#WideIntPoint
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)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
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
    mod_dict[num_int] += 1

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
count = 0
for i in range(0, len(S) - 4):
  for j in range(i + 3, len(S)):
    if j >= len(S):
      break
    sub = S[i:j+1]
    if int(sub) % 2019 == 0:
      count += 1
      while j < len(S) - 1 and S[j + 1] == "0":
        count += 1
        j += 1
      else:
        j += 4
print(count)

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = list(input())
    s.reverse()
    MOD = 2019
    tot = 0
    ans = 0
    d = [0 for _ in range(MOD)]
    for i in range(len(s)):
        d[tot] += 1
        x = (10 ** i) % MOD
        p = int(s[i]) * x
        p %= MOD
        tot = (tot + p) % MOD
        ans += d[tot]
    return ans


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

Submission status: Time Limit Exceeded

Solution code:
S = input()
count = 0
for i in range(0, len(S) - 4):
  for j in range(i + 3, len(S)):
    if j >= len(S):
      break
    sub = S[i:j+1]
    if int(sub) % 2019 == 0:
      count += 1
      if j < len(S) - 1 and S[j + 1] == "0":
        count += 1
        j += 1
      else:
        j += 4
print(count)

Submission status: Time Limit Exceeded

Solution code:
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()]#WideIntPoint
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)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
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
for i in range(len(s)):
    num_int = pow(10, i) * int(s[-i-1]) + num_int
    res = num_int % mod
    mod_dict[res] += 1

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def m():
    s = list(input())
    s.reverse()
    MOD = 2019
    tot = 0
    ans = 0
    d = [0 for _ in range(MOD)]
    for i in range(len(s)):
        d[tot] += 1
        x = (10 ** i) % MOD
        p = int(s[i]) * x
        p %= MOD
        tot = (tot + p) % MOD
        ans += d[tot]
    return ans


print(m())

Submission status: Time Limit Exceeded

Solution code:
def m():
    s = list(str(input()))
    s.reverse()
    MOD = 2019
    tot = 0
    ans = 0
    d = [0] * MOD
    for i in range(len(s)):
        d[tot] += 1
        x = (10 ** i) % MOD
        p = int(s[i]) * x
        p %= MOD
        tot = (tot + p) % MOD
        ans += d[tot]
    return ans


print(m())