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:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

##### 割り算の切り上げ #####
# tmp = -(-4 // 3)

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	S = input()
	S = S[:-1]
	S = S[::-1]
	I = [0]

	for i,val in enumerate(S):
		I.append((I[-1] + (int(val) * 10**i)) % 2019)

	dol = {}
	ans = 0
	for i in range(len(I)):
		tmp = I[i] % 2019
		if tmp in dol:
			ans += 1
		else:
			dol[tmp] = 1

	print(ans)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

##### 割り算の切り上げ #####
# tmp = -(-4 // 3)

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	S = input()
	S = S[:-1]
	S = S[::-1]
	I = [0]

	for i,val in enumerate(S):
		I.append(I[-1] + (int(val) * 10**i))

	dol = {}
	ans = 0
	for i in range(len(I)):
		tmp = I[i] % 2019
		if tmp in dol:
			ans += 1
		else:
			dol[tmp] = 1

	print(ans)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

##### 割り算の切り上げ #####
# tmp = -(-4 // 3)

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	S = input()
	S = S[:-1]
	S = S[::-1]
	I = [0]

	for i,val in enumerate(S):
		I.append(I[-1] + (int(val) * 10**i))

	dol = {}
	ans = 0
	for i in range(len(I)):
		tmp = I[i] % 2019
		if tmp in dol:
			dol[tmp] += 1
			ans += 1
		else:
			dol[tmp] = 1

	print(ans)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    A = input()[::-1]
    A = "0" + A
    # lis_num = [int(s) for s in A]

    # print(lis_num)

    # S = [0] * len(lis_num)
    S = [0] * len(A)
    cnt = [0] * 2019
    cnt[0] = 1

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

    # print(S)


    # for i in S:
    #     cnt[i % 2019] += 1

    ans = 0

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

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

Submission status: Time Limit Exceeded

Solution code:
A = input()[::-1]
A = "0" + A
# lis_num = [int(s) for s in A]

# print(lis_num)

# S = [0] * len(lis_num)
S = [0] * len(A)
cnt = [0] * 2019
cnt[0] = 1

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

# print(S)


# for i in S:
#     cnt[i % 2019] += 1

ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
A = "0"+input()[::-1]

# lis_num = [int(s) for s in A]

# print(lis_num)

# S = [0] * len(lis_num)
S = [0] * len(A)

for i in range(len(A) - 1):
    S[i + 1] = S[i] + int(A[i + 1]) * pow(10, i)
# print(S)

cnt = [0] * 2019

for i in S:
    cnt[i % 2019] += 1

ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
s = input()[::-1]
m = 2019
t = 1
ans = 0
c = np.zeros(2019, dtype=np.int64)
c[0] = 1
r = 0
for d in s:
  d = int(d)
  r = (d * t + r) % 2019
  ans += c[r]  # ここまでであまりが同じものを探す.
  c[r] += 1
  t *= 10

print(ans)

Submission status: Time Limit Exceeded

Solution code:
A = input()[::-1]
A = "0" + A
# lis_num = [int(s) for s in A]

# print(lis_num)

# S = [0] * len(lis_num)
S = [0] * len(A)

for i in range(len(A) - 1):
    S[i + 1] = S[i] + int(A[i + 1]) * 10 ** i
# print(S)

cnt = [0] * 2019

for i in S:
    cnt[i % 2019] += 1

ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
A = input()[::-1]
A = "0" + A
lis_num = [int(s) for s in A]

# print(lis_num)

S = [0] * len(lis_num)
for i in range(len(lis_num) - 1):
    S[i + 1] = S[i] + lis_num[i + 1] * 10 ** i
# print(S)

cnt = [0] * 2019

for i in S:
    cnt[i % 2019] += 1

ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()[::-1]
N = len(S)
sum_of_mod = [0] * 2019
sum_of_mod[0] += 1
digit = 0
now = 0
for i in range(N):
    now = int(S[i])*(10**digit) + now
    sum_of_mod[now % 2019] += 1
    digit += 1

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

Submission status: Time Limit Exceeded

Solution code:
S = input()[::-1]
sum_of_mod = [0] * 2019
now = int(S[0])
digit = 1
sum_of_mod[now % 2019] += 1
for c in S:
    now = int(c)*(10**digit) + now
    sum_of_mod[now % 2019] += 1
    digit += 1
ans = 0
for m in sum_of_mod:
    ans += m * (m-1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = input()
N_list = []
 
 
for i in range(len(N)):
    N_list.append(N[i])
 
answer = []
for i in range(len(N)-2):
    for j in range(3,len(N)+1):
        number = [n for n in N_list[i:j+1]]
        str_number = ""
        for k in range(len(number)):
            
            str_number += number[k]
       
        if str_number != "" :
            if int(str_number) % 2019 == 0 and j < len(N):
                
                answer.append([i+1,j+1])
print(len(answer))

Submission status: Time Limit Exceeded

Solution code:
target = 2019
S = input()
N = len(S)
l = [ int( S[ : i + 1 ] ) for i in range(len(S)) ]
r =[ 1 ] + [ 0 ] * ( target - 1 )
 
d = 0
cnt = 0
for i in reversed( l ):
  r[ ( i * 10 ** d) % target ] += 1
  if r[ ( i * 10 ** d) % target ] > 1:
    cnt += r[ ( i * 10 ** d) % target ] - 1
  d += 1
    
print( cnt )

Submission status: Time Limit Exceeded

Solution code:
mult = set()
for i in range(200012//2019):
  mult.add(i*2019)
s = input()
n = len(s)
ans = 0
for i in range(n+1):
  for j in range(i+1,n+1):
    ans += int(int(s[i:j]) in mult)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
s = input()
count = 0

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

print(count)

Submission status: Time Limit Exceeded

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

n = 0
d = 0

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

print(ans+rem[0])

Submission status: Time Limit Exceeded

Solution code:
S = input()
rem = [0]*2019

n = 0
d = 0

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

print(ans+rem[0])

Submission status: Time Limit Exceeded

Solution code:
S = input()
rem = [0]*2019

n = 0
d = 0

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

print(ans+rem[0])

Submission status: Time Limit Exceeded

Solution code:
s = input()
count = 0

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
S = input()
rem = [0]*2019

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

print(ans+rem[0])

Submission status: Time Limit Exceeded

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

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

print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

print(total)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys

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

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
len_s = len(s)
count = 0
for i in range(len_s - 4):
  for j in range(i + 4, len_s + 1):
    check = int(s[i:j])
    # print(check)
    if(check % 2019 == 0):
      count+=1
      # print(i," ",j)
print(count)

Submission status: Time Limit Exceeded

Solution code:
ss = input()[::-1]
sv = ''
mods = [ 0 for i in range(2019) ]

for s in ss:
    sv = s + sv
    i = int(sv) % 2019
    mods[i] += 1

ans = 0
for i,v in enumerate(mods):
    if i == 0 and v > 0:
        ans += 1
    else:
        ans += (v * (v - 1)) // 2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
ss = input()[::-1]
sv = ''
mods = {}

for s in ss:
    sv = s + sv
    i = int(sv) % 2019
    try:
        mods[i] += 1
    except KeyError:
        mods[i] = 1

ans = 0
for k,v in mods.items():
    if k == 0:
        ans += 1
    else:
        ans += (v * (v - 1)) // 2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
ss = input()[::-1]
sv = ''
mods = {}

for s in ss:
    sv = s + sv
    i = int(sv) % 2019
    try:
        mods[i] += 1
    except KeyError:
        mods[i] = 1

if 0 in mods.keys():
    ans = 1
else:
    ans = 0

for v in mods.values():
    ans += (v * (v - 1)) // 2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
 
count = 0
 
if len(s) < 4:
    print(0)
else:
  for i in range(1, len(s) + 1):
      #if len(s) - 2 > i:
      #    continue
      for j in range(i, len(s) + 1):
          if (int(s[i-1:j]) % 2019) == 0:
              count += 1
  print(count)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
mo=[0]*len(s)
t=0
ans=0

sr=s[::-1]

dic={}
dic.setdefault(0,0)

for n in range(len(sr)):
    t=t+int(sr[n])*10**n
    mo=t%2019
    dic.setdefault(mo,0)
    dic[mo]+=1

ans=dic[0]+list(dic.values()).count(2)

print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

Submission status: Time Limit Exceeded

Solution code:
# encoding="UTF-8"

N = input()
n = int(N)
GET = 0
for i in range(len(N)):
    for j in range(len(N)-i):
        if int(N[i:j+i+1])%2019 == 0:
            GET += 1
print(GET)

Submission status: Time Limit Exceeded

Solution code:
from scipy.special import comb
from collections import Counter, deque

D = deque(map(int, list(input())))
num_mod = {}
num_mod[0] = 1
value = 0

for i in range(len(D)):
    value = value + D.pop() * 10**i
    modulo = value % 2019
    #print(modulo)
    if modulo in num_mod.keys():
        num_mod[modulo] += 1
    else:
        num_mod[modulo] = 1

ans = [comb(i, 2, exact=True) for i in num_mod.values() if i > 1]
print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
s=input()
mo=[0]*len(s)
t=0
ans=0

sr=s[::-1]

for n in range(len(sr)):
    t=t+int(sr[n])*10**n
    mo[n]=t%2019
    if mo[n]==0:
        ans+=1

ans=ans+len(mo)-len(set(mo))

print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import collections

mod = 2019

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from scipy.special import comb
from collections import Counter, deque

D = deque(map(int, list(input())))
num_mod = {}
value = 0

for i in range(len(D)):
    value = value + D.pop() * 10**i
    modulo = value % 2019
    #print(modulo)
    if modulo in num_mod.keys():
        num_mod[modulo] += 1
    else:
        num_mod[modulo] = 1

fix = 0
ans = [comb(i, 2, exact=True) for i in num_mod.values() if i > 1]
#print(b)
if 0 in num_mod.keys():
    if num_mod[0] == 1:
        fix = 1
print(sum(ans)+fix)

Submission status: Time Limit Exceeded

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

tmp = 0
for i in range(len(S)):
    #modの世界
    tmp += int(S[i])*pow(10, i)
    tmp %= 2019
    dp[tmp] += 1
ans = 0
for i in dp:
    if i >= 2:
        #数列の和の公式
        ans += i*(i-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
s = [int(c) for c in input()]
count = 0
prev = {}
for num in s:
    dp = collections.defaultdict(int)
    dp[num] += 1
    for k,v in prev.items():
        i = (k * 10 + num) % 2019
        dp[i] += v
    count += dp[0]
    prev = dp
print(count)

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
n=len(s)
ans=0
l=[0 for i in range(2019)]
for i in range(n):
  t=int(s[i:])
  l[t%2019]+=1
#print(l[1817])
ans+=l[0]
l[0]=0
for i in l:
  ans+=int(i*(i-1)//2)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
S = input()
n = len(S)
x = 0
Timod_list = []

for i in range(n):
  Ti_list = S[i:n+1]
  Ti_str = ""
  for y in Ti_list:
    Ti_str = Ti_str + y
  Ti = int(Ti_str)
  Timod = Ti % 2019
  Timod_list = Timod_list + [Timod]

Timod_list.sort()
Timodlist_len= len(Timod_list)

Tlist0 = Timod_list.count(0)
x = x + Tlist0

Timod_lists = [x for x in Timod_list if Timod_list.count(x) > 1]
Timodlists_len = len(Timod_lists)

for j in range(Timodlists_len):
  Tlist = Timod_lists.count(Timod_lists[j])
  x = x + 1 / 2 * math.factorial(Tlist) / Tlist
    
x = int(x)
print(x)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import math
S_list = list(input())
n = len(S_list)
x = 0
Timod_list = []

for i in range(n):
  Ti_list = S_list[i:n+1]
  Ti_str = ""
  for y in Ti_list:
    Ti_str = Ti_str + y
  Ti = int(Ti_str)
  Timod = Ti % 2019
  Timod_list = Timod_list + [Timod]

Timod_list.sort()
Timodlist_len= len(Timod_list)

Tlist0 = Timod_list.count(0)
x = x + Tlist0

Timod_lists = [x for x in Timod_list if Timod_list.count(x) > 1]
Timodlists_len = len(Timod_lists)

for j in range(Timodlists_len):
  Tlist = Timod_lists.count(Timod_lists[j])
  x = x + 1 / 2 * math.factorial(Tlist) / Tlist
    
x = int(x)
print(x)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
def main():
    count = 0
    for i in range(len(S)):
        for j in range(i+1,len(S)):
            B = int(''.join(S[i:j+1]))
            if B % 2019 == 0:
                count += 1
    print(count)
main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

S = input()
l = len(S)
mod_num = 2019
mods = []
ans = 0

tmp_mod = 0
mod_sum = defaultdict(int)
mod_sum[0] += 1

for n in range(l):
    tmp_mod += int(S[l - n - 1]) * pow(10,n) % mod_num
    mod_sum[tmp_mod % mod_num] += 1

for i in mod_sum.items():
    if i[1] <= 1:
        continue
    else:
        ans += (i[1] * (i[1] - 1)) // 2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

S = input()
mod_num = 2019
mods = []
ans = 0

tmp_mod = 0
mod_sum = defaultdict(int)
mod_sum[0] += 1

for n in range(len(S)):
    tmp_mod += int(S[len(S) - n - 1]) * pow(10,n) % mod_num
    mod_sum[tmp_mod % mod_num] += 1

for i in mod_sum.items():
    if i[1] <= 1:
        continue
    else:
        ans += (i[1] * (i[1] - 1)) // 2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
S_list = list(input())
n = len(S_list)
x = 0
Timod_list = []

for i in range(n):
  Ti_list = S_list[i:n+1]
  Ti_str = ""
  for y in Ti_list:
    Ti_str = Ti_str + y
  Ti = int(Ti_str)
  Timod = Ti % 2019
  Timod_list = Timod_list + [Timod]

Timod_list.sort()
Timodlist_len= len(Timod_list)

Tlist0 = Timod_list.count(0)
x = x + Tlist0

for j in range(Timodlist_len-1):
  if Timod_list[j] == Timod_list[j + 1]:
    Tlist = Timod_list.count(Timod_list[j])
    x = x + 1 / 2 * math.factorial(Tlist) / (Tlist - 1)
    
x = int(x)
print(x)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

S = [i for i in input()]
mod_num = 2019
mods = []
ans = 0

S.reverse()
tmp_mod = 0
mod_sum = defaultdict(int)
mod_sum[0] += 1

for n,s in enumerate(S):
    mod = int(s) * 10**n % mod_num
    tmp_mod += mod
    mod_sum[tmp_mod % mod_num] += 1

for i in mod_sum.items():
    if i[1] <= 1:
        continue
    else:
        ans += (i[1] * (i[1] - 1)) // 2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

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 = defaultdict(int)
mod_sum[0] += 1

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


for i in mod_sum.items():
    if i[1] <= 1:
        continue
    else:
        ans += (i[1] * (i[1] - 1)) // 2

print(ans)