🥰Last year when life was better
https://www.hackerrank.com/contests/codenection-2021-open-category-finals/challenges/last-year-when-life-was-better
Last updated
https://www.hackerrank.com/contests/codenection-2021-open-category-finals/challenges/last-year-when-life-was-better
Last updated
from collections import defaultdict
def count_divisible_by_2019(s):
mod = 2019
count = 0
mod_count = defaultdict(int) # Stores frequency of prefix mod values
mod_count[0] = 1 # To handle cases where prefix itself is divisible
prefix = 0
power = 1 # To handle positional values correctly (like 10^0, 10^1, ...)
# Traverse the string from right to left (ensuring digit order remains)
for digit in reversed(s):
prefix = (prefix + int(digit) * power) % mod
count += mod_count[prefix] # If the same mod value was seen before, we found substrings
mod_count[prefix] += 1
power = (power * 10) % mod # Maintain correct positional contribution
return count
print(count_divisible_by_2019(input()))