🥰Last year when life was better

https://www.hackerrank.com/contests/codenection-2021-open-category-finals/challenges/last-year-when-life-was-better

Question

2019 was an amazing year. I loved 2019 so much that I want to find 2019 everywhere. So, I found a random string S that contains digits 1-9. I want to find number of pairs of integers {i,j  1ijS}\{i,j\ |\ 1\le i \le j \le|S|\} where characters between ith and jth positions form an integer which is a multiple of 2019.

Input Format

S

Constraints

1S21051 \le |S| \le 2*10^5

Output Format

Print the answer.

Sample Inputs:

Input

1817181712114

Output

3

Explanation

Three pairs - (1,5), (5,9), and (9,13) - form such integers:

  • 18171 = 2019 * 9

  • 12114 = 2019 * 6


Solution - Modular

A modular solution traversing reversed string is able to solve the problem. We first stores the frequency of prefix mod values (3 * 673), then we try to loop through the length of the string, getting their modular values.

If we got same mod value, then we prove that there's a substring which is the multiple of 2019.

After traversed all strings, output the counter.

Last updated