x

from collections import Counter

def count_divisible_by_2019(S):
mod = 2019
count = Counter()
count[0] = 1 # ban đầu có 1 cách (đoạn rỗng)

current = 0
power10 = 1
ans = 0

# duyệt từ phải sang trái
for ch in reversed(S):
    digit = int(ch)
    current = (current + digit * power10) % mod
    ans += count[current]
    count[current] += 1
    power10 = (power10 * 10) % mod

return ans

--- MAIN ---

q = int(input())
for _ in range(q):
S = input().strip()
print(count_divisible_by_2019(S))

Bình luận

Mới nhất
Tải bình luận...

Không có bình luận nào.