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 Rust (1.42.0) (judge verdict: Runtime Error)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use std::io;
use std::collections::{HashMap};

fn main() {
    let s: String = parse_line();
    let mut count: usize = 0;
    let mut judge = MultipleJudge::new();
    for i in 0..s.len() {
        for j in i+4..s.len()+1 {
            let value= s[i..j].to_string();
            if judge.is_multiple(value, 2019) {
                count += 1;
            }
        }
    }
    println!("{}", count);
}

struct MultipleJudge {
    cache: HashMap<String, bool>
}

impl MultipleJudge {
    fn new() -> Self {
        let empty = HashMap::new();
        MultipleJudge{cache: empty}
    }

    fn is_multiple(&mut self, s: String, i: usize) -> bool {
        let mut buf = s.clone();
        if self.cache.contains_key(&buf) {
            return self.cache[&buf];
        }
        while buf.len() >= 50 {
            let head = &buf[0..buf.len()-10000];
            let middle: usize = buf[buf.len()-10000..buf.len()-1].parse().unwrap();
            let tail: usize = buf[buf.len()-1..buf.len()].parse().unwrap();
            let new_tail = middle - tail * 2;
            buf = format!("{}{}", head, new_tail);
        }
        let value: usize = buf.parse().unwrap();
        let result = value % i == 0;
        self.cache.insert(s.clone(), result);
        result
    }
}


#[allow(dead_code)]
fn parse_line<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}
*/

fn main() {
    let exe = "/tmp/binA38E6750";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";