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 Perl (5.26.1) (judge verdict: Time Limit Exceeded)

#!/usr/bin/env perl
use 5.10.0;
use utf8;
use strict;
use warnings;
use open qw( :utf8 :std );
use Data::Dumper;

chomp( my $s = readline );
my $length = length $s;

my $founds = 0;

my %map = concrete_map( );
#warn Dumper \%map;

for my $i ( 0..( $length - 1 ) ) {
    for my $j ( $i..( $length - 1 ) ) {
        my $value = substr $s, $i, $j - $i + 1;
        #warn "\$i: $i, \$j, $j, $value";

        $founds++
            if $map{ $value };

        #warn "($i, $j, $value)" if $map{ $value };
    }
}

say $founds;

exit;

sub concrete_map {
    my @values;
    my $value = 2019;

    while ( $value <= 200_000 ) {
        push @values, $value;
        $value = $value + 2019;
    }

    return map { ( $_ => 1 ) } @values;
}