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.

Solution in OCaml (4.10.0) (judge verdict: Time Limit Exceeded)

open Batteries
(* 前からst文字を切り抜く *)
let slice st str = String.init (String.length str - st) (fun a -> String.get str (a+st))
(* 文字列をcharのarrayにする *)
let explode s = List.init (String.length s) (String.get s) |> Array.of_list
let s = read_line () |> explode
let sl = Array.length s

let get_start_to_end i j =
  String.init (j - i + 1) @@ fun a -> s.(a + i)
let t = Big_int.big_int_of_int 2019

let rec loop1 i ans=
  let rec loop2 j ans1=
    if j >= sl then ans1 else
      (
        if Big_int.eq_big_int ( Big_int.mod_big_int ((get_start_to_end i j) |> Big_int.big_int_of_string) t) Big_int.zero_big_int 
        then loop2 (j+i+1) (ans1+1)
        else loop2 (j+1) ans1
      )
  in if i >= sl-4 then ans else (loop1 (i+1) (ans+(loop2 (i+1) 0)))

let _ = loop1 0 0 |> Printf.printf "%d\n"