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()
a = [0]
b = [0]*2019
c = 0
d = 0
for n in range(len(S)):
    c += 10**n*int(S[-n-1])
    a.append(c%2019)
for n in range(len(S)+1):
    b[a[n]] += 1
for n in range(2019):
    d += int(b[n]*(b[n]-1)/2)
print(d)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())

r = 0
for i in range(len(S)):
    for j in range(i, len(S) + 1):
        t = S[i:j]
        if t:
            t = float(t)
            if t >= 1 and t <= 200000:
                if str(t / 2019.0) == str(int(int(S[i:j]) / 2019.0)) + ".0":
                    r += 1

print(r)

Submission status: Time Limit Exceeded

Solution code:
import collections
s = input()
p = []
for i in range(len(s)):
    p.append(int(s[i:])%2019)

c = collections.Counter(p)
ans = 0
for x , y in c.items():
    ans += y*(y-1)//2
    if x == 0:
        ans += y
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
cnt = 0
pass_ = False
for start in range(len(s)):
  end = start + 3
  if int(s[start:end+1]) == 2019:
    cnt += 1
    pass_ = True
  elif int(s[start:end+1]) < 2019:
    end += 1
  while(end+1<=len(s)):
    if int(s[start:end+1]) % 2019 == 0 and pass_ == False:
      cnt += 1
    end += 1
    pass_ = False
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = list(input())
    N = len(S)
    t = 0
    A = [0] * 2019
    A[0] += 1
    for i in range(N):
        d = 10 ** i % 2019
        t += (int(S[-1-i]) * d)
        t %= 2019
        A[t] += 1
    result = sum([a*(a-1)//2 for a in A])
    print(result)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
ans = 0
mod = [0]*2019
t = s[len(s)-1]
n = 1
for i in range(len(s)-1,-1,-1):
#     mod.append(int(s[i:])%2019)
    mod[int(t)%2019] += 1
    t = int(t) + (10**(len(s)-i))*int(s[i-1])
#     t = int(t) + n*10*int(s[i-1])
#     n = n*10%2019
for i in range(0,2019):
    ans += mod[i]*(mod[i]-1)/2
ans += mod[0]
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
S = str(input())

r = 0
for i in range(len(S)):
    for j in range(i, len(S) + 1):
        t = S[i:j]
        if t:
            if float(t) >= 1 and float(t) <= 200000:
                if str(float(S[i:j]) / 2019.0) == str(int(int(S[i:j]) / 2019.0)) + ".0":
                    r += 1

print(r)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
table = [1] + [0] * 2020

curr = 0
for i in range(N):
    curr += int(S[N-1-i])*(10**i)%2019
    curr %= 2019
    table[curr] += 1

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

Submission status: Time Limit Exceeded

Solution code:
from sys import setrecursionlimit, exit
setrecursionlimit(1000000000)

from collections import Counter

def main():
    s = input()
    l = len(s)
    m = [0] * (l + 1)
    for i in range(l - 1, -1, -1):
        m[i] = (m[i+1] + int(s[i]) * pow(10, l - i - 1)) % 2019
    ans = 0
    for i in Counter(m).values():
        ans += (i - 1) * i // 2
    print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys


def solve(S: str):

    l = [0] * 2019
    ps = ""
    for s in S[::-1]:
        ps = s + ps
        l[ int(ps) % 2019 ] += 1
    
    ans = 0
    for i in range(0, 2019):
        ans += l[i] * (l[i] - 1) // 2
    
    ans += l[0]
    
    print(ans)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S = str(next(tokens))  # type: str
    solve(S)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input() #sys.stdin.readlineは最後が改行
n =len(s)

from collections import defaultdict
amari = defaultdict(lambda:0)
amari[0]=1
num=0
tmp=0
ans =0

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

    if amari[tmp]>1:
        val = amari[tmp]
        ans -= (val-1)*(val-2)//2
        ans += (val)*(val-1)//2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = input()
a = [0] * 2050
n = 0
p = 1
for i in range(len(N)):
    n += int(N[len(N)-1-i]) * p
    p *= 10
    a[int(n) % 2019] += 1
ans = 0
for i in range(2019):
    if i == 0:
        ans += a[i] * (a[i]+1) // 2
    else:
        ans += (a[i]-1) * a[i] // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from queue import deque


y = 2019
S = deque(input())
N = len(S)
T = [0] * (N + 1)

for i in range(N-1, -1, -1):
    T[i] = T[i+1] + int(S[i]) * 10 ** (N - 1 - i)
    
T = [t % y for t in T]

cnt = 0
for c in Counter(T).values():
    cnt += c * (c - 1) // 2
    
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
table = [1] + [0] * 2020

curr = 0
for i in range(N):
    curr += int(S[N-1-i])*(10**i)
    curr %= 2019
    table[curr] += 1

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
N=len(s)
kyonen=2019
modlist=[int(s[-1])%kyonen]
t=0
for i in range(2,N+1):
    modlist.append((modlist[-1]+(int(s[(-1)*i])*10**(i-1))%kyonen)%kyonen)
modlist.sort()
now=modlist[0]
nowc=1
ans=0
if modlist[0]==0:
    ans+=1
for i in range(1,N):
    if modlist[i]==0:
        ans+=1
    if modlist[i]==now:
        nowc+=1
    else:
        if nowc>=2:
            ans+=int(nowc*(nowc-1)/2)
        nowc=1
        now=modlist[i]

if nowc>=2:
    ans+=int(nowc*(nowc-1)/2)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    L = len(S)
    amari = [0] * 2019
    tmp = 0
    for i in range(L):
        tmp += int(S[-i-1])*(10**i)%2019
        tmp %= 2019
        amari[tmp] += 1
    ans = amari[0]
    for i in range(2019):
        ans += amari[i]*(amari[i]-1)//2
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import math
    
def abs(num):
    if num < 0:
        return -num
    else:
        return num

###
# main
if(__name__ == '__main__'):
    # input
    s = input()
    len_s = len(s)

    ans = 0
    if len_s >= 4:
        dic_a = {}
        dic_b = {}
        for i in range(2019):
            dic_a[i] = 0
            dic_b[i] = 0
        dic = [dic_a, dic_b]
        current = 0
        next = 1

        seed = int(s[0:4]) % 2019
        dic[current][seed] = 1

        for s_i in range(len_s-3):
            ans += dic[current][0]

            if s_i == len_s-4:
                break

            tgt = int(s[s_i+4])

            for i in range(2019):
                new_i = (i*10 + tgt) % 2019
                dic[next][new_i] = dic[current][i]
            seed = int(s[s_i+1:s_i+5]) % 2019
            dic[next][seed] += 1

            current, next = next, current

    print(ans)
    
# else:
    # do nothing

Submission status: Time Limit Exceeded

Solution code:
import math
s=input()
n=len(s)
count=0
a=[0 for i in range(2019)]
a[0]=1
k=0
for i in reversed(range(0,n)):
    k+=(10**(n-i-1)%2019)*int(s[i])%2019
    a[k%2019]+=1        
for i in range(len(a)):
    if a[i]>=2:
        count+=(a[i])*(a[i]-1)//2
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input() #sys.stdin.readlineは最後が改行
n =len(s)

from collections import defaultdict
amari = defaultdict(lambda:0)
amari[0]=1
num=0
tmp=0
ans =0

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

    if amari[tmp]>1:
        val = amari[tmp]
        ans -= (val-1)*(val-2)//2
        ans += (val)*(val-1)//2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    from collections import Counter
    from operator import mul
    from functools import reduce

    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
    s=input()
    n=len(str(s))
    b=[]
    ans=0
    k=1
    for i in range(n):
        a = int(''.join([x.strip() for x in s[i:].split(',')]))
        b.append(a%2019)
    b.append(0)
    c = Counter(b)
    for i in list(c.values()):
        if i>=2:
            ans+=cmb(i,2)
    print(ans)
resolve()

Submission status: Time Limit Exceeded

Solution code:
s=input()
N=len(s)
kyonen=2019
modlist=[int(s[-1])%kyonen]
t=0
for i in range(2,N+1):
    modlist.append((modlist[-1]+int(s[(-1)*i])*10**(i-1))%kyonen)
modlist.sort()
now=modlist[0]
nowc=1
ans=0
if modlist[0]==0:
    ans+=1
for i in range(1,N):
    if modlist[i]==0:
        ans+=1
    if modlist[i]==now:
        nowc+=1
    else:
        if nowc>=2:
            ans+=int(nowc*(nowc-1)/2)
        nowc=1
        now=modlist[i]

if nowc>=2:
    ans+=int(nowc*(nowc-1)/2)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()[::-1]
P = [0]*(len(S)+1)
for i,j in enumerate((S)):
    P[i+1] = (P[i] + int(j)*pow(10,i))

dp = [0]*2019
for i in P:
    dp[i%2019] += 1
print(sum([i*(i-1)//2 for i in dp]))

Submission status: Time Limit Exceeded

Solution code:
s = input() #sys.stdin.readlineは最後が改行
n =len(s)

from collections import defaultdict
amari = defaultdict(lambda:0)
amari[0]=1
num=0
tmp=0
ans =0

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

    if amari[tmp]>1:
        val = amari[tmp]
        ans -= (val-1)*(val-2)//2
        ans += (val)*(val-1)//2



print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = input()
count = 0

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
S = input()
S_list = []
for i in range(len(S)):
    S_list.append(int(S[i]))
S_list = np.array(S_list)
num = 0
for i in range(len(S_list)-3):
    sum_temp = np.sum([v * np.power(10, k) for k, v in enumerate(S_list[i:i+3][::-1])])
    for j in range(i+3, len(S)):
        sum_temp *= 10
        sum_temp += S_list[j]
        if sum_temp % 2019 == 0:
            num += 1
print(num)

Submission status: Time Limit Exceeded

Solution code:
import math
s=input()
n=len(s)
count=0
a=[0 for i in range(2019)]
a[0]=1
k=0
for i in reversed(range(0,n)):
    k+=(10**(n-i-1)%2019)*int(s[i])
    a[k%2019]+=1        
for i in range(len(a)):
    if a[i]>=2:
        count+=(a[i])*(a[i]-1)//2
print(count)

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = list(input())
    N = len(S)
    t = 0
    A = [0] * 2019
    A[0] += 1
    for i in range(N):
        t = (t + int(S.pop()) * 10 ** i) % 2019
        A[t] += 1
    result = sum([a*(a-1)//2 for a in A])
    print(result)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main(): #なんか早くなるおまじない
    import sys
    from collections import defaultdict
    dic = defaultdict(int)

    input = sys.stdin.readline #早くなるおまじない

    s = str(input().rstrip())
    m = 0

    for i in range(1, len(s) + 1):
        m_1 = pow(10, i-1) % 2019
        m_add = (int(s[-i]) * m_1) % 2019
        m = (m_add + m) % 2019
        dic[m] += 1

    ans1 = 0

    for x in dic.values():
        if x > 1:
            ans1 += x * (x - 1) // 2
    ans2 = dic[0]

    print(ans1 + ans2)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
S=str(input())
l=len(S)
ans=0

mod=[0]*2019
mod[0]+=1
for i in range(l):
    mod[int(S[i:l+1])%2019]+=1
    
for j in range(len(mod)):
    m=mod[j]
    ans=ans+m*(m-1)//2
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input();n=len(s);t=1;p=0;c=[0]*2019;r=0
for i in range(n+1):p=((ord(s[n-i])-ord("0")if i>0 else 0)*t+p)%2019;r+=c[p];c[p]+=1;t*=10
print(r)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
s = input()
n = len(s)
cs = [0] * (n + 1)
for i, x in enumerate(s):
    cs[i + 1] = ( cs[i] + int(x) ) % 3
ans = 0
for i in range(n):
    csi = cs[i]
    for j in range(i + 3, n + 1):
        if cs[j] != csi:
            continue
        k = int(s[i:j])
        ans += (k%2019 == 0)
print(ans)

# from https://atcoder.jp/contests/abc164/submissions/12380181

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

S = sr()
mod_cnt = [0] * 2019
mod_cnt[0] = 1
power = 1
remain = 0
answer = 0
for i in range(len(S)-1, -1, -1):
    remain += int(S[i]) * power
    remain %= 2019
    power *= 10
    answer += mod_cnt[remain]
    mod_cnt[remain] += 1

print(answer)

Submission status: Time Limit Exceeded

Solution code:
n = input()
count = 0
li = [0 for i in range(2019)]
for i in range(len(n)):
    a = int(n[i:])%2019
    if a == 0:
        count += 1
    else:
        li[a] += 1
aa = list(map(lambda x:x*(x-1)/2,li))
print(int(sum(aa)+count))

Submission status: Time Limit Exceeded

Solution code:
import math
s=input()
n=len(s)
count=0
a=[0 for i in range(2019)]
a[0]=1
k=0
for i in reversed(range(0,n)):
    k+=10**(n-i-1)*int(s[i])
    a[k%2019]+=1
        

for i in range(len(a)):
    if a[i]>=2:
        count+=(a[i])*(a[i]-1)//2
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = [0] * 2050
for i in range(200010):
    a[N % 2019] += 1
    N //= 10**(i+1)
    N *= 10**(i+1)
    if N <= 0:
        break
ans = 0
for i in range(2019):
    if i == 0:
        ans += a[i] * (a[i]+1) // 2
    else:
        ans += (a[i]-1) * a[i] // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
input = sys.stdin.readline
 
S = input().strip()
l = len(S)
dp = np.zeros((l, 2019), dtype=int)
for i in range(l):
    s = int(S[i])
    for j in range(2019):
        k = (10 * j + s) % 2019
        dp[i][k] = dp[i-1][j]
    dp[i][s] += 1
 
print(dp[:, 0].sum())

Submission status: Time Limit Exceeded

Solution code:
S = input()
n = len(S)
T = [0]*2019
T[0]=1
t = 0
ans = 0
for i in range(n):
    t = (t + (int(S[-i-1]))*(10**i))%2019
    T[t] += 1
for j in T:
    ans +=int(j*(j-1)/2)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
p = 2019
n = len(s)
u_i = []
ans = 0
l = [0 for i in range(p)]
l[0] = 1
for i in range(n):
    l[int(s[i:])%p] += 1

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
input = sys.stdin.readline
 
S = input().strip()
l = len(S)
dp = [[0 for _ in range(2019)] for i in range(l)]
dp = np.zeros((l, 2019), dtype=int)
for i in range(l):
    s = int(S[i])
    for j in range(2019):
        k = (10 * j + s) % 2019
        dp[i][k] = dp[i-1][j]
    dp[i][s] += 1
 
print(dp[:, 0].sum())

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(300000)
from collections import Counter

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


S = input()

m = [0]
pow = 1
for i in range(len(S)):
    m.append((m[-1] + int(S[- i - 1]) * pow) % 2019)
    pow *= 10


ans = 0
for cnt in Counter(m).values():
    ans += cnt * (cnt - 1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys
from collections import defaultdict

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

S = sr()
dic = defaultdict(int)
dic[0] = 1
power = 1
remain = 0
answer = 0
for i in range(len(S)-1, -1, -1):
    remain += int(S[i]) * power
    remain %= 2019
    power *= 10
    answer += dic[remain]
    dic[remain] += 1

print(answer)

Submission status: Time Limit Exceeded

Solution code:
n = str(input())
ans,div = 0,[]
for i in range(0,len(n)):
        k = int(n[i:len(n)])
        div.append(k % 2019)
div.append(0)
for i in range(0,len(n)):
    for j in range(i+1,len(n)+1):
        if div[i] == div[j]:
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from numba import jit

@jit
def solve(s):
    n = len(s)
    ans = 0
    now = 0
    mp = np.zeros(2019, np.int32)
    mp[0] += 1
    tmp = np.zeros(2019, np.int32)

    id = np.zeros(2019, np.int32)
    for i in range(2019):
        id[i] = i * 10 % 2019

    for c in s:
        now *= 10;
        now += ord(c) - ord("0")
        now %= 2019

        ans += mp[now]
        mp[now] += 1
        for i in range(2019):
            tmp[id[i]] = mp[i]
        for i in range(2019):
            mp[i] = tmp[i]

    print(ans)

s = input()
solve(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
count = [0] * 2019
N = len(S)
mod = 0

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

answer = count.pop(0)

for k in range(2018):
    if count[k] > 1:
        answer = answer + count[k]*(count[k] - 1)/2

print(int(answer))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(300000)
from collections import Counter

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


S = input()

m = [0]
for i in range(len(S)):
    m.append((m[-1] + int(S[- i - 1]) * (10 ** i)) % 2019)


ans = 0
for cnt in Counter(m).values():
    ans += cnt * (cnt - 1) // 2
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    N = len(s)

    l = []
    sum = 0

    for i in range(N):
        if i == N-1:
            l.append(0)
        l.append(int(s[i:N])%2019)
        
    lset = set(l)
    for key in lset:
        m = l.count(key)
        sum += m*(m-1)//2
    print(sum)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


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



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
#n,p = map(int,input().split())
s = input()
p = 2019
n = len(s)
u_i = []
ans = 0
l = [0 for i in range(p)]
l[0] = 1
for i in range(n):
    l[int(s[i:])%p] += 1

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

Submission status: Time Limit Exceeded

Solution code:
# from sys import stderr
from functools import reduce

def debug(*args):
    print(*args, file=stderr)

s = input()

checked = {}

for left_i in range(len(s)):
    for right_i in range(left_i + 1, len(s) + 1):
        now_checking = int(s[left_i:right_i])

        if not now_checking in checked:
            checked[now_checking] = 0

        # debug(left_i, right_i, now_checking_str)

        checked[now_checking] += 1 if now_checking % 2019 == 0 else 0

# debug(checked)

result = reduce(lambda x, y: x+y, checked.values(), 0)
print(result)


# echo "1817181712114" | python3 d.py && echo "14282668646" | python3 d.py && echo "2119" | python3 d.py

Submission status: Time Limit Exceeded

Solution code:
S = input()
n=len(S)
T=[0]*(n+1)
T[0]=int(S[-1][0])
amari=[0]*(n)
su=0
for i in range(1,len(S)):
	T[i]=(T[i-1]+int(S[n-i-1])*(10 ** i))%2019
for i in range(2019):
	m=T.count(i)
	su+=m*(m-1)/2
print(int(su))

Submission status: Time Limit Exceeded

Solution code:
s = input()
ans = 0
mod = []
t = s[len(s)-1]
n = 1
for i in range(len(s)-1,-1,-1):
#     mod.append(int(s[i:])%2019)
    mod.append(int(t)%2019)
#     t = int(t) + (10**(len(s)-i-1))*int(s[i])
    t = int(t) + n*10*int(s[i-1])
    n = n*10%2019
for i in range(0,2019):
    ans += mod.count(i)*(mod.count(i)-1)/2
ans += mod.count(0)
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

S = input()
B = defaultdict(int)
B[0]=1
ans=0

for i in range(len(S)):
    m=int(S[-i-1:])%2019
    ans+=B[m]
    B[m]+=1

print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import collections

def main():
    S = str(input())
    n = len(S)

    T = [-1 for i in range(n+1)]
    T[-1] = 0
    for i in range(n):
        #print(S[i:])
        T[i] = int(S[i:]) % 2019
    #print(T)
    C = collections.Counter(T)
    #print(C) # dict 型のサブクラス，Counter
    ans = 0
    for value, count in C.items():
        ans += int(count*(count-1)/2)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
a = []
b = 0
c = 0
for i in range(len(s)):
    b = (int(s[- 1 - i]) * (10 ** i) % 2019 + b) % 2019
    if b == 0:
        c += 1
    else:
        a.append(b)
b = set(a)
print(len(a) - len(b) + c)

Submission status: Time Limit Exceeded

Solution code:
S = input()
mod_bucket = [0] * 2019
mod_bucket[0] += 1

_S = S[::-1]
mod = 0
p = 1
for i in range(len(S)):
    mod = (p * int(_S[i]) + mod) % 2019
    mod_bucket[mod] += 1
    p *= 10

ans = sum([mod*(mod-1)/2 for mod in mod_bucket])
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
# from sys import stderr
from functools import reduce

def debug(*args):
    print(*args, file=stderr)

s = input()

checked = {}

for left_i in range(len(s)):
    for right_i in range(left_i + 1, len(s) + 1):
        now_checking_str = s[left_i:right_i]

        if not now_checking_str in checked:
            checked[now_checking_str] = 0

        # debug(left_i, right_i, now_checking_str)

        now_checking_int = int(now_checking_str)
        checked[now_checking_str] += 1 if now_checking_int % 2019 == 0 else 0

# debug(checked)

result = reduce(lambda x, y: x+y, checked.values(), 0)
print(result)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

s = input()
N = len(s)

l = []
sum = 0

for i in range(N):
    if i == N-1:
        l.append(0)
    l.append(int(s[i:N])%2019)
    
c = Counter(l)
for key in c.keys():
    m = c[key]
    sum += m*(m-1)/2
print(int(sum))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3


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

    mod_count = [0] * 2019
    mod_count[0] = 1
    sums = [0]
    d = 1
    for c in S[::-1]:
        sums.append((sums[-1] + int(c) * d) % 2019)
        mod_count[sums[-1]] += 1
        d *= 10

    print(sum([m * (m - 1) for m in mod_count]) // 2)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from numba import jit

@jit
def solve(s):
    n = len(s)
    ans = 0
    now = 0
    mp = np.zeros(2019, np.int)
    mp[0] += 1
    tmp = np.zeros(2019, np.int)

    id = np.zeros(2019, np.int)
    for i in range(2019):
        id[i] = i * 10 % 2019

    for c in s:
        now *= 10;
        now += ord(c) - ord("0")
        now %= 2019

        ans += mp[now]
        mp[now] += 1
        for i in range(2019):
            tmp[id[i]] = mp[i]
        for i in range(2019):
            mp[i] = tmp[i]

    print(ans)

s = input()
solve(s)

Submission status: Time Limit Exceeded

Solution code:
from numba import njit
from numpy import int32,zeros,arange
@njit('i4(i4[:])')
def solve(s):
    a=0
    d=zeros(2019,int32)
    for i in s:
        p=zeros(2019,int32)
        p[(arange(2019)*10+i)%2019]+=d[arange(2019)]
        d=p
        d[i]+=1
        a+=d[0]
    return a
print(solve(int32([*input()])))

Submission status: Time Limit Exceeded

Solution code:
S = input()
n=len(S)
T=[0]*(n)
T[0]=int(S[-1][0])
amari=[0]*(n)
su=0
for i in range(1,len(S)):
	T[i]=(T[i-1]+int(S[n-i-1])*(10 ** i))
	amari[i]=T[i]%2019
for i in range(2019):
	su+=amari.count(i)*(amari.count(i)-1)/2
print(int(su))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

S = input().strip()
l = len(S)
dic = {0:[-1]}
dp = [[0 for _ in range(2019)] for i in range(l)]
ans = 0
base = 1
mod = 0
for i in range(0, l):
    base = (10 * base) % 2019
    s = base * int(S[l-1-i]) + mod
    mod = s % 2019
    if mod in dic:
        dic[mod].append(i)
    else:
        dic[mod] = [i]

for key, val in dic.items():
    v_len = len(val)
    ans += (v_len * (v_len -1))// 2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from numpy import int32,zeros,arange
def solve(s):
    a=0
    d=zeros(2019,int32)
    for i in s:
        p=zeros(2019,int32)
        p[(arange(2019)*10+i)%2019]+=d[arange(2019)]
        d=p
        d[i]+=1
        a+=d[0]
    return a
print(solve(int32([*input()])))

Submission status: Time Limit Exceeded

Solution code:
arr = [0 for i in range(2019)]
arr[0] = 1

s = input()
n = len(s)

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


ans = 0
for m in arr:
    if m==0: continue
    ans += m*(m-1)//2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
#n,p = map(int,input().split())
s = input()
p = 2019
n = len(s)
u_i = []
ans = 0
amari = [0]*p
amari[0] += 1
for i in range(n):
    amari[int(s[i:])%p] += 1

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

Submission status: Time Limit Exceeded

Solution code:
arr = [0 for i in range(2019)]
arr[0] = 1

s = input()
n = len(s)

num = 0
for i,ele in enumerate(s[::-1]):
    num += int(ele) * (10**i % 2019)
    arr[num%2019] += 1


ans = 0
for m in arr:
    if m==0: continue
    ans += m*(m-1)//2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def D():
    s = input()
    sketa = len(s)
    ans = 0
    mods = [0]*2019
    mods[0] = 1
    for i in range(1,sketa+1):
        mod = int(s[-i:])%2019
        ans += mods[mod]
        mods[mod] += 1
    print(ans)
D()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
S = input()
S_list = []
for i in range(len(S)):
    S_list.append(int(S[i]))
S_list = np.array(S_list)
num = 0
for i in range(len(S_list)-3):
    #print(S_list[i], S_list[i+3])
    for j in range(i+3, len(S)):
        #print("\t", i, j, S_list[j], np.sum(S_list[i:j+1]))
        sum_temp = np.sum([v * np.power(10, k) for k, v in enumerate(S_list[i:j+1][::-1])])
        if sum_temp % 2019 == 0:
            num += 1
print(num)

Submission status: Time Limit Exceeded

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

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

ans = temp[0]
for t in temp:
    if t > 1:
        ans += t*(t-1)/2

print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
S = input()

ans = 0
lists = [0 for i in range(2019)]
lists[0] += 1

tmp = 1
num = 0
for i in range(len(S) - 1, -1, -1):
    num += tmp * int(S[i])
    lists[num % 2019] += 1
    tmp *= 10

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=len(s)
MOD=2019
l=[1]*n
for i in range(n-1,-1,-1):
  l[i]=int(s[i:n])%MOD
from collections import Counter
c=Counter(l)
ans=0
for i in c:
  ans+=c[i]*(c[i]-1)//2
  if i==0:
    ans+=c[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


def main():
    S = input().rstrip()

    N = len(S)
    S = int(S)

    M = [0 for i in range(2019)]
    M[0] += 1

    for i in range(N):
        T = S % 10 ** (N-i)
        M[T%2019] += 1

    res = 0

    for m in M:
        res += m * (m-1) // 2


    print(res)



main()

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys
from collections import defaultdict

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

S = sr()
dic = defaultdict(int)
dic[0] = 1
remain = 0
answer = 0
for i in range(len(S)-1, -1, -1):
    remain += 10 ** (len(S)-1-i) * int(S[i])
    remain %= 2019
    answer += dic[remain]
    dic[remain] += 1

print(answer)

Submission status: Time Limit Exceeded

Solution code:
# 解説を参考に作成
def solve():
    S = input()
    lenS = len(S)
    mod_list = [0] * 2019
    Si = 0
    for i in range(lenS):
        Si += 10 ** i * int(S[lenS - 1 - i])
        x = Si % 2019
        mod_list[x] += 1
    result = 0
    for i, ml in enumerate(mod_list):
        if i == 0:
            result += ml
        result += int(ml * (ml - 1) / 2)
    print(result)


if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

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

temp = [0]*2019
num = 0
for i in range(n):
    num += int(s.pop())*pow(10,i)
    temp[num % 2019] += 1

ans = temp[0]
for t in temp:
    if t > 1:
        ans += t*(t-1)/2

print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=len(s)
t=int(s)
dict={}
sum=0
for i in range(n):
    for j in range(i+1,n):
        """string=str(i)+"_"+str(j)
        if string not in dict.keys():
            dict[string]=0"""
        num=s[i:j+1]
        num=int(num)
        if num%2019==0:
            sum+=1
        
print(sum)

Submission status: Time Limit Exceeded

Solution code:
N = input()
count = 0

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
S = input().strip()
N = len(S)
SS = S[::-1]

mod_count = [0] * 2019
mod_count[0] = 1
sums = [0]
for i in range(N):
    sums.append((sums[-1] + int(SS[i]) * (10**i)) % 2019)
    mod_count[sums[-1]] += 1

print(sum([m * (m - 1) for m in mod_count]) // 2)