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()[::-1]) # 入力文字列を逆順でsに格納
n = len(s)
counts = [0]*2019
counts[0]=1
b = 1
num = 0
for i in s:
    num += int(i)*b
    num %=2019
    b *=10
    b %2019
    counts[num]+=1

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


print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
s = (input()[::-1]) # 入力文字列を逆順でsに格納
n = len(s)
counts = [0]*2019
counts[0]=1
b = 1
num = 0
for i in range(n):
    num += int(s[i])*b
    num %=2019
    b *=10
    b %2019
    counts[num]+=1

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
# print(counts)
ans = 0
for j in counts:
    ans +=j*(j-1)/2


print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import math
s = (input()[::-1]) # 入力文字列を逆順でsに格納
n = len(s)
counts = [0]*2019
counts[0]=1
b = 1
num = 0
for i in range(n):
    num += int(s[i])*b
    num %=2019
    b *=10
    b %2019
    counts[num]+=1

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
# print(counts)
ans = 0
for j in counts:
    if j>=2:
        ans +=combinations_count(j, 2)


print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
s = input()[::-1] # 入力文字列を逆順でsに格納
n = len(s)
# taro = [0]*n
s_ = [0]*n
counts = [0]*2019
counts[0]=1
b = 1
num = 0
for i in range(n):
    s_[i] = int(s[i])*b +num
    num += int(s[i])*b
    num %=2019
    b *=10
    b %2019
    counts[num]+=1

# for i in range(n):
#     a = s_[i]%2019
#     taro[i]=a
# print(taro)
# counts = [0]*2019
# for i in range(n):
#     counts[taro[i]]+=1
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
# print(counts)
ans = 0
for j in counts:
    if j>=2:
        ans +=combinations_count(j, 2)


print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
s = input()[::-1] # 入力文字列を逆順でsに格納
n = len(s)
taro = [0]*n
s_ = [0]*n
b = 1
num = 0
for i in range(n):
    s_[i] = int(s[i])*b +num
    num += int(s[i])*b
    num %=2019
    b *=10
    b %2019

for i in range(n):
    a = s_[i]%2019
    taro[i]=a
# print(taro)
counts = [0]*2019
for i in range(n):
    counts[taro[i]]+=1
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
# print(counts)
ans = 0
for j in counts:
    if j>=2:
        ans +=combinations_count(j, 2)
ans += counts[0]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()

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

    tmp = ""

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
# a = '1'
# b = '5'
# print(a+b)
ans = 0
for i in range(len(S)):
    tmp = S[i]
    for j in range(i+1,len(S)):
        tmp += S[j]
        # print(tmp)
        if int(tmp)%2019 == 0 :
            ans += 1

    tmp = ""

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()[::-1] # 入力文字列を逆順でsに格納

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

num, d = 0, 1

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

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

print(ans) # 答えの出力

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
n = len(s)
count = 0
for i in range(1,n-2):
    for k in range(3,n+1):
        j = i+k
        if (j<=n):
            st = int(s[i-1:j])
            if int(st)%2019==0:
                count +=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
s = input()
n = len(s)
target = 2019
res = 0
dic = defaultdict(int)
for i in range(n):
    dic[int(s[i:]) % target] += 1
for i in range(2018):
    res += dic[i] * (dic[i] -1) //2
res += dic[0]
print(res)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
target = 2019
res = 0
for i in range(n):
    j = i + 4
    while j < n:
        if int(s[i:j+1]) % target == 0:
            res += 1
            j += 4
        j += 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
target = 2019
res = 0
for i in range(n):
    j = i + 4
    while j < n:
        if int(s[i:j+1]) % target == 0:
            res += 1
        j += 1
print(res)

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)
        ans += cnt * (cnt-1)


    print(ans//2)

if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

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

from collections import Counter
def main():
    S = input()
    l = len(S)
    amari =[int(S[-(i + 1) :]) % 2019 for i in range(l)]
    C = Counter(amari)
    ans = 0
    for v in C.values():
        ans += v * (v - 1) // 2
    print(ans + amari.count(0))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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


def main():
    S = input()
    l = len(S)
    amari =[int(S[-(i + 1) :]) % 2019 for i in range(l)]
    print(l - len(set(amari)) + amari.count(0))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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


def main():
    S = input()
    amari = []
    l = len(S)
    amari =[int(S[-(i + 1) :]) % 2019 for i in range(l)]
    print(l - len(set(amari)) + amari.count(0))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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


def main():
    S = input()
    amari = []
    for i in range(len(S)):
        a = int(S[-(i + 1) :]) % 2019
        amari.append(a)
    print(len(amari) - len(set(amari)) +amari.count(0) )


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

s = str(input())
count = 0
c = -1
for i in range(0,len(s)-5):
    if c-1 > i:
        continue
    
    for j in range(i+5,len(s)):
        if int(s[i:j] )% 2019 == 0:
            c = j
            count += 1

# k = 0
# for i in range(0,len(s)-1):
#     k += int(s[i])
# print(k)

print(count)

# for i in range(101):
#     k = str(2019*i)
#     s = 0
#     if not '0' in k:
#         for j in k:
#             s += int(j)
#         print(s)

Submission status: Time Limit Exceeded

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

s = str(input())
count = 0
c = -1
for i in range(0,len(s)-5):
    if c-1 > i:
        continue
    
    for j in range(i+5,len(s)):
        if int(s[i:j] )% 2019 == 0:
            c = j
            count += 1


print(count)

Submission status: Time Limit Exceeded

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

s = str(input())
count = 0

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
num = int(s)
counter = 0
rems = [0]*2019
rem = (num%10)%2019
for i in range(1, len(s)+1):
    rems[rem]+=1
    num=num//10
    rem = ((num%10)*round(10**i)+rem)%2019
rems[0]+=1
sum = 0
for i in rems:
    sum+=i*(i-1)/2
print(round(sum))

Submission status: Time Limit Exceeded

Solution code:
s = input()
num = int(s)
counter = 0
dict={}
rem = (num%10)%2019
for i in range(1, len(s)+1):
    if rem not in dict:
        dict[rem] = 1
    else:
        dict[rem]+=1
    num=num//10
    rem = ((num%10)*round(10**i)+rem)%2019
if 0 in dict:
    dict[0]+=1
sum = 0
for key in dict:
    sum+=dict[key]*(dict[key]-1)/2
print(round(sum))

Submission status: Time Limit Exceeded

Solution code:
s = input()
counter = 0
dict={}
for i in range(0, len(s)):
    rem = int(s[i:])%2019
    if rem not in dict:
        dict[rem] = 1
    else:
        dict[rem]+=1
if 0 in dict:
    dict[0]+=1
sum = 0
for key in dict:
    sum+=dict[key]*(dict[key]-1)/2
print(round(sum))

Submission status: Time Limit Exceeded

Solution code:
s = input()
counter = 0
length = 4
while length <=len(s):
    for i in range(0, len(s)-length+1):
        if int(s[i:i+length])%2019 == 0:
            counter+=1
    length+=1
print(counter)

Submission status: Time Limit Exceeded

Solution code:
s = input()
counter = 0
length = 1
while length <=len(s):
    for i in range(0, len(s)-length+1):
        if int(s[i:i+length])%2019 == 0:
            counter+=1
    length+=1
print(counter)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())

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

mod = [1] + [0] * 2018
ans = 0
for i in num:
    m = i % 2019
    ans += mod[m]
    mod[m] += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())

num = [int(s[i:]) for i in range(len(s))]
mod = [1] + [0] * 2018

ans = 0
for i in num:
    m = i % 2019
    ans += mod[m]
    mod[m] += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
ls_rem = []
for i in range(len(S)):
  p=int(S[i:len(S)])%2019
  ls_rem.append(p)
ls_rem.append(0)
from collections import Counter
C_rem = Counter(ls_rem)
ans=0
for i in C_rem.keys():
  n=C_rem[i]
  ans+=n*(n-1)/2
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
S = input()
ls_rem = [int(S[i:])%2019 for i in range(len(S))]
ls_rem.append(0)
from collections import Counter
C_rem = Counter(ls_rem)
ans=0
for i in C_rem.keys():
  n=C_rem[i]
  ans+=n*(n-1)/2
print(int(ans))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
s=input()
n=len(s)
M=[0]
mod=2019
for i in range(n):
	m=(M[-1]+int(s[n-1-i])*10**i%mod)%mod
	M.append(m)
MC=Counter(M)
r=0
for v in MC.values():
	if v>1:
		r+=v*(v-1)//2
print(r)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
s=input()
n=len(s)
M=[0]*(n+1)
mod=2019
for i in range(n):
	M[n-1-i]=(M[n-i]+int(s[n-1-i])*10**i%mod)%mod
MC=Counter(M)
r=0
for v in MC.values():
	if v>1:
		r+=v*(v-1)//2
print(r)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
s=input()
n=len(s)
M=[0]*(n+1)
mod=2019
for i in range(n):
	M[n-1-i]=int(s[n-1-i:])%mod
MC=Counter(M)
r=0
for v in MC.values():
	if v>1:
		r+=v*(v-1)//2
print(r)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
s=input()
n=len(s)
M=[0]*(n+1)
mod=2019
for i in range(n):
	M[n-1-i]=int(s[n-1-i:])%mod
MC=Counter(M)
r=0
for v in MC.values():
	r+=v*(v-1)//2
print(r)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
#print(s)
cnt=0
s_num_1=[]

for i in range(0,len(s)-2):
    for j in range(i,len(s)):
        #s_num_1.append(s[i])
        #print('s_num_1',s_num_1)
        #s_num_2=int(''.join(s_num_1))
        #print('s_num_2',s_num_2)
        #if s_num_2 % 2019 ==0:
            #cnt+=1
        #print(s[i:j+1])
        #print(''.join(s[i:j+1]))
        s_num_3=int(''.join(s[i:j+1]))
        if s_num_3 % 2019 ==0:
            cnt +=1

print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
cnt  = 0

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

print(cnt)

Submission status: Time Limit Exceeded

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

if n < 4:
  print(0)
  exit()
else:
  cnt = 0
  for i in range(n):
    for j in range(i+3, n):
      S_ij = int(S[i:j+1])
      if S_ij % 2019 == 0:
        cnt += 1
  print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()
int_s = 10**9

ans = 0
mul = 2019
while mul < int_s:
    sub_s = s
    f = sub_s.find(str(mul))
    while f != -1:
        ans += 1
        sub_s = sub_s[f+1:]
        f = sub_s.find(str(mul))
    mul += 2019

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()

muls = [str(2019 * i) for i in range(1000)]

ans = 0
for mul in muls:
    i = 0
    j = len(mul)
    current = s[i:j]
    ans += current == mul
    while j < len(s):
        current = current[1:]
        current += s[j]
        if current == mul:
            ans += 1
        i += 1
        j += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()

ans = 0
dif = 4
while dif < len(s):
    i = 0
    j = dif
    current = s[i:j]
    ans += int(current)%2019 == 0
    while j < len(s):
        current = current[1:]
        current += s[j]
        if int(current) % 2019 == 0:
            ans += 1
        i += 1
        j += 1
    dif += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()

ans = 0
dif = 4
while dif < len(s):
    i = 0
    j = i + dif
    while j < len(s):
        if int(s[i:j+1]) % 2019 == 0:
            ans += 1
        i += 1
        j += 1
    dif += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = int(input())
Smod = [0]*2019
for i in range(len(str(S))+1):
  Smod[(S%(10**i))%2019] += 1

ans = 0
for j in range(2019):
  ansj = Smod[j]
  ans += ansj * (ansj-1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = int(input())
Smod = [0]*2019
for i in range(len(str(S))+1):
  Smod[(S%(10**i))%2019] += 1

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

Submission status: Time Limit Exceeded

Solution code:
import math

S = int(input())
Smod = [0]*2019
for i in range(int(math.log10(S)+2)):
  Smod[(S%(10**i))%2019] += 1

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
n = len(S)
#S = int(str(S))
P = 0
for i in range(n):
    for j in range(i, n):
        B = S[i : j+1]
        #print(B)
        if int(B) % 2019 == 0:
            #print(B)
            P += 1
            #print(P)
        else:
            continue
#print(P)
print(P)

Submission status: Time Limit Exceeded

Solution code:
S = input()
n = len(S)
#S = int(str(S))
P = list()
for i in range(n):
    for j in range(i, n):
        B = S[i : j+1]
        #print(B)
        if int(B) % 2019 == 0:
            #print(B)
            P.append(1)
            #print(P)
        else:
            continue
#print(P)
print(len(P))

Submission status: Time Limit Exceeded

Solution code:
str_S = input()
#S = int(str(S))
P = list()
for i in range(len(str_S)):
    for j in range(i, len(str_S)):
        B = str_S[i : j+1]
        #print(B)
        if int(B) % 2019 == 0:
            #print(B)
            P.append(1)
            #print(P)
        else:
            continue
#print(P)
print(len(P))

Submission status: Time Limit Exceeded

Solution code:
str_S = input()
#S = int(str(S))
P = list()
for i in range(len(str_S)):
    for j in range(len(str_S)):
        if j<i:
            continue
        else:
            B = str_S[i : j+1]
            #print(B)
            if int(B) % 2019 == 0:
                #print(B)
                P.append(1)
                #print(P)
            else:
                continue
#print(P)
print(len(P))

Submission status: Time Limit Exceeded

Solution code:
str_S = input()
#S = int(str(S))
P = list()
for i in range(len(str_S)):
    for j in range(len(str_S)):
        if j<i:
            continue
        else:
            B = str_S[i : j+1]
            #print(B)
            C = int(B)
            if C % 2019 == 0:
                #print(B)
                P.append(1)
                #print(P)
            else:
                continue
#print(P)
print(len(P))

Submission status: Time Limit Exceeded

Solution code:
str_S = input()
#S = int(str(S))
P = list()
for i in range(len(str_S)):
    for j in range(len(str_S)):
        if j<i:
            continue
        else:
            B = str_S[i : j+1]
            #print(B)
            C = int(B)
            if C % 2019 == 0:
                #print(B)
                P.append(B)
                #print(P)
            else:
                continue
#print(P)
print(len(P))

Submission status: Time Limit Exceeded

Solution code:
S = input()

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


print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
s = input()
n = len(s)
dp = [0]
mod = 2019
a = 0
for i in range(n):
    a += int(s[n-i-1]) * 10 ** i
    dp.append(a%mod)
ans = 0
c = Counter(dp)
for value in c.values():
    ans += value * (value-1) / 2
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
S = input()
count = 0
for i in range(len(S)-3):
  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:
S=input()
L=len(S)
M=2019

k=0
for i in range(L):
    for j in range(i,L):
        if int(S[i:j+1])%M==0:
            k+=1

print(k)

Submission status: Time Limit Exceeded

Solution code:
S=str(input())
cnt=0
if len(S)>=4:
    for i in range(0,len(S)-4):
        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 sys
input = sys.stdin.readline

def main():
    s = input().strip()
    mod = [0] * 2019

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import defaultdict 
def main():
    s = readline().rstrip().decode()
    n = len(s)
    T = [0]
    P = 2019
    dd = defaultdict(int)
    dd[0] += 1
    for i in range(1, n+1):
        v = ((ord(s[-i])-ord('0'))*10**(i-1) + T[i-1])%P
        dd[v] += 1
        T.append(v)
    ans = 0
    for v in dd.values():
        ans += v*(v-1)//2
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
S = input()[::-1]
mod = 2019
d = defaultdict(int)
s = ""
for i in range(len(S)):
    s = S[i] + s
    d[int(s) % mod] += 1
ans = 0
for k, v in d.items():
    ans += v * (v - 1) // 2
    if k == 0:
        ans += v
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
S = input()[::-1]
mod = 2019
d = defaultdict(int)
s = ""
for i in range(len(S)):
    s += S[i]
    d[int(s[::-1]) % mod] += 1
ans = 0
for k, v in d.items():
    ans += v * (v - 1) // 2
    if k == 0:
        ans += v
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()

start = 0
end = 0
num = 4

count = 0

while(num <= len(s)):
    end = start + num
    if len(s) < end:
        start = 0
        num += 1
        continue

    t = s[start:end]

    if int(t) % 2019 == 0:
        count += 1

    start += 1

print(count)

Submission status: Time Limit Exceeded

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

dataset = [0]*2019
dataset[0] = 1

n = 0
d = 0

for i in s:
    n += int(i) * (10**d)
    a = n % 2019
    dataset[a] += 1
    d += 1

count = 0
for i in dataset:
    count += i * (i-1) // 2

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = int(input())
ss = len(str(s))

count = 0
for i in range(ss):
    for j in range(i,ss):
        sa = s // (10**i)
        sb = sa % (10**(j+1-i))
        if (sb % 2019 == 0):
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import collections
S=input()[::-1]
l=len(S)
T=0
A=[0]*2019
A[0]=1
for i in range(l):
    T+=int(S[i])*(10**i)
    T%=2019
    A[T]+=1
ans=0
for cnt in A:
    ans+=cnt*(cnt-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
S=input()[::-1]
l=len(S)
T,d=0,1
A=[0]*2019
A[0]=1
for i in range(l):
    T+=int(S[i])*d
    d*=10
    A[T%2019]+=1
ans=0
for cnt in A:
    ans+=cnt*(cnt-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
S=input()[::-1]
l=len(S)
T=0
A=[0]*2019
A[0]=1
for i in range(l):
    T+=int(S[i])*(10**i)
    A[T%2019]+=1
ans=0
for cnt in A:
    ans+=cnt*(cnt-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
S=input()
l=len(S)
T=0
A=[0]*2019
A[0]=1
for i in range(l):
    T+=int(S[l-i-1])*(10**i)
    A[T%2019]+=1
ans=0
for cnt in A:
    ans+=cnt*(cnt-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
S=input()
l=len(S)
T=0
A=[0]*2019
A[0]=1
for i in range(l):
    T+=int(S[l-i-1])*(10**i)
    A[T%2019]+=1
B=map(lambda x: x*(x-1)//2,A)
print(sum(B))

Submission status: Time Limit Exceeded

Solution code:
import collections
S=input()
l=len(S)
T=0
A=[0]
for i in range(l):
    T+=int(S[l-i-1])*(10**i)
    A.append(T%2019)
c=collections.Counter(A)
C=c.values()
ccc=map(lambda x:x*(x-1)//2,C)
print(sum(ccc))

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
mod_list = []

for i in range(len(S)-3):
    for j in range(i+4, len(S)+1):
        num = int(''.join(S[i:j]))
        mod_list.append(num % 2019)

print(len([i for i in mod_list if i == 0]))

Submission status: Time Limit Exceeded

Solution code:
import sys


readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prl = lambda x: print(*x ,sep='\n')

s = ns()
slis = [int(s[-1*i:]) if i != 0 else 0  for i in range(len(s)+1)]
amari = [i % 2019 for i in slis]
sset = set(amari)
ans = 0
for i in sset:
    m = amari.count(i)
    ans += m*(m-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
count = 0

for i in range(len(S)-3):
    for j in range(i+4, len(S)+1):
        num = int(''.join(S[i:j]))
        if num % 2019 == 0:
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys


readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prl = lambda x: print(*x ,sep='\n')

s = ns()
slis = [int(s[-1*i:]) if i != 0 else 0  for i in range(len(s)+1)]
amari = [i % 2019 for i in slis]
ans = 0
for j in range(len(amari)-1):
    for k in range(j+1, len(amari)):
        if amari[j] == amari[k]:
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
count = 0

for i in range(len(S)-3):
    for j in range(i+4, len(S)+1):
        num = int(''.join(S[i:j]))
        if num % 2019 == 0:
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

s = input()

rests = []
n = 0
a = 0
m = 1
for i in list(s)[::-1]:
    n = n + int(i)*m

    rests.append(n % 2019)
    m *= 10

c = Counter(rests)

for v in c.values():
    a += v * (v-1) // 2
 
a += c[0]
 
print(a)

Submission status: Time Limit Exceeded

Solution code:
import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prl = lambda x: print(*x ,sep='\n')

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

print(ans)

Submission status: Time Limit Exceeded

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

print(res)

Submission status: Time Limit Exceeded

Solution code:
import itertools as iters

S = input()
res = [0] + [int(S[-i:])%2019 for i in range(1,len(S)+1)]
#print(res)
R = 0
for a,b in iters.combinations(res,2):
    #print(a,b)
    if a-b == 0:
        R += 1
print(R)

Submission status: Time Limit Exceeded

Solution code:
_input = input
'''
llinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''

#ここに書きたいコードを書く
import math
s=input()
n=len(s)

amari=[0]*2020
amari[0]=1

for i in range(n):
	amari[int(s[n-i-1:])%2019]+=1
	#print(i,int(s[n-i-1:n+1]),int(s[n-i-1:n+1])%2019)

#print(amari)
sum=0
for i in amari:
	sum += i*(i-1)//2
	#print(i)
		
print(sum)

input = _input

Submission status: Time Limit Exceeded

Solution code:
_input = input
'''
llinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''

#ここに書きたいコードを書く
import math
s=input()
n=len(s)

amari=[0]*2020
amari[0]=1

for i in range(n):
	amari[int(s[n-i-1:n+1])%2019]+=1
	#print(i,int(s[n-i-1:n+1]),int(s[n-i-1:n+1])%2019)

#print(amari)
sum=0
for i in amari:
	sum += i*(i-1)//2
	#print(i)
		
print(sum)

input = _input

Submission status: Time Limit Exceeded

Solution code:
_input = input
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''

#ここに書きたいコードを書く
import math
s=input()
n=len(s)

amari=[0]*2020
amari[0]=1

for i in range(n):
	amari[int(s[n-i-1:n+1])%2019]+=1
	#print(i,int(s[n-i-1:n+1]),int(s[n-i-1:n+1])%2019)

#print(amari)
sum=0
for i in amari:
	if(i>=2):
		sum += math.factorial(i)//(math.factorial(i-2)*2)
		#print(i)
		
print(sum)

input = _input

Submission status: Time Limit Exceeded

Solution code:
_input = input
#'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''

#ここに書きたいコードを書く
import math
s=input()
n=len(s)

amari=[0]*2020
amari[0]=1

for i in range(n):
	amari[int(s[n-i-1:n+1])%2019]+=1
	#print(i,int(s[n-i-1:n+1]),int(s[n-i-1:n+1])%2019)

#print(amari)
sum=0
for i in amari:
	if(i>=2):
		sum += math.factorial(i)//(math.factorial(i-2)*2)
		#print(i)
		
print(sum)

input = _input

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

s = input()
l = len(s)
s = int(s)

rests = []

d = 10**(l-1)

while s > 0:
    r = s % 2019
    rests.append(r)

    s = s % d
    d = d // 10

a = 0

c = Counter(rests)

for v in c.values():
    if v >= 2:
        a += v * (v-1) // 2

a += c[0]

print(a)

Submission status: Time Limit Exceeded

Solution code:
s = input()
l = len(s)
s = int(s)

rests = [0]*2019

d = 10**(l-1)

while s > 0:


    r = s % 2019
    rests[r] +=  1

    s = s % d
    d = d // 10

a = 0

for v in rests:
    if v >= 2:
        a += v * (v-1) // 2

a += rests[0]

print(a)

Submission status: Time Limit Exceeded

Solution code:
s = input()
l = len(s)
s = int(s)

rests = {}

d = 10**(l-1)

while s > 0:


    r = s % 2019
    rests[r] = rests.get(r, 0) + 1

    s = s % d
    d = d // 10

a = 0

for v in rests.values():
    if v >= 2:
        a += v * (v-1) // 2

a += rests.get(0,0)

print(a)

Submission status: Time Limit Exceeded

Solution code:
S=input()
ls=len(S)
import numpy as np
pslist=[]
for i in range(ls-3):
    for j in range(i+3,ls):
        ps=int(S[i:j+1])
        pslist.append(ps)
a=np.array(pslist)
b=a%2019
c=b[b==0]

print(len(c))

Submission status: Time Limit Exceeded

Solution code:
s = int(input())
ss = str(s)
n = len(ss)

i = 0
m = list()
while i<=n:
    m.append((s%(10**i))%2019)
    i+=1

def merge(x):
    if len(x)==1:
        return
    
    p, q = divmod(len(x), 2)
    l = [x.pop() for t in range(p)]
    r = [x.pop() for t in range(p+q)]
    merge(l)
    merge(r)
    l.reverse()
    r.reverse()
    while l and r:
        if l[-1]<=r[-1]:
            x.append(l.pop())
        else:
            x.append(r.pop())
    while l:
        x.append(l.pop())
    while r:
        x.append(r.pop())

i = 0
ans = 0
merge(m)
x = -1
while i <= n:
    if x == m[i]:
        ans+=1
    else:
        x = m[i]
    i+=1

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
tmp=sys.stdin.readline().rstrip()
def main():
   ans=0
   cnt=[0]*2019
   cnt[0]+=1
   d=0
   for i,j in enumerate(reversed(tmp)):
      d+=int(j)%2019*10**(i)%2019
      cnt[d%2019]+=1
   for i in cnt:
      ans+=i*(i-1)//2
   print(ans)
if __name__ == "__main__":
    main()