prithivMLmods/Coder-Stat huggingface dataset:
Problem p02702 (Python)

Buggy submission (status: Time Limit Exceeded):
S = input()
N = len(S)

map_ = [-1] * 2019
map_[0] = 0
ans = 0
now = 0
for i in range(0, N + 1):
    now += (int(S[N - i - 1]) * (10**i)) % 2019
    map_[now % 2019] += 1
    ans += map_[now % 2019]

print(ans)


Fixed submission:
S = input()
N = len(S)

map_ = [-1] * 2019
map_[0] = 0
ans = 0
now = 0
for i in range(0, N):
    now = (now + int(S[N - i - 1]) * pow(10, i, 2019)) % 2019
    map_[now] += 1
    ans += map_[now]

print(ans)
Problem p02702 (Python)

Buggy submission (status: Time Limit Exceeded):
S = str(input())
# 余りのリスト
counts = [0] * 2019
n, d = 0, 1

for s in S[::-1]:
    n += int(s) * d
    d *= 10
    n %= 2019
    counts[n] += 1
ans = counts[0]
for count in counts:
    ans += count * (count - 1) // 2
print(ans)


Fixed submission:
S = str(input())
# 余りのリスト
counts = [0] * 2019
n, d = 0, 1

for s in S[::-1]:
    n += int(s) * d
    d *= 10
    n %= 2019
    d %= 2019
    counts[n] += 1
ans = counts[0]
for count in counts:
    ans += count * (count - 1) // 2
print(ans)
Problem p02702 (Python)

Buggy submission (status: Time Limit Exceeded):
from collections import Counter


S = input()
N = len(S)

L = [0] * (N + 1)
s = 0
for i in range(N):
    s += int(S[N - 1 - i]) * pow(10, i)
    L[i + 1] = s % 2019

C = Counter(L)
cnt = 0
for key in list(C.keys()):
    k = C[key]
    cnt += (k * (k - 1)) // 2

print(cnt)


Fixed submission:
from collections import Counter


S = input()
N = len(S)

L = [0] * (N + 1)
s = 0
for i in range(N):
    s += int(S[N - 1 - i]) * pow(10, i, 2019)
    L[i + 1] = s % 2019

C = Counter(L)
cnt = 0
for key in list(C.keys()):
    k = C[key]
    cnt += (k * (k - 1)) // 2

print(cnt)
Problem p02702 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10**7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

S = list(input())
L = len(S)

c = 0
t = 0
md = [0] * 2019
md[0] += 1
for i in range(L - 1, -1, -1):
    t += int(S[i]) * pow(10, c)
    c += 1
    md[t % 2019] += 1

ans = 0
for i in range(2019):
    m = md[i]

    if m == 0:
        continue

    ans += m * (m - 1) // 2

print(ans)


Fixed submission:
import sys

sys.setrecursionlimit(10**7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

S = list(input())
L = len(S)

c = 0
t = 0
md = [0] * 2019
md[0] += 1
for i in range(L - 1, -1, -1):
    t += int(S[i]) * pow(10, c, 2019)
    c += 1
    md[t % 2019] += 1

ans = 0
for i in range(2019):
    m = md[i]

    if m == 0:
        continue

    ans += m * (m - 1) // 2

print(ans)
Problem p02702 (Python)

Buggy submission (status: Time Limit Exceeded):
from collections import defaultdict

S = input()
ans = 0
N = len(S)
mod = 2019
dp = defaultdict(int)
cur = 0
for i in range(N, -1, -1):
    if i == N:
        dp[0] += 1
    else:
        cur += ((10 ** (N - i - 1)) * int(S[i])) % mod
        cur %= mod
        dp[cur] += 1
for v in dp.values():
    if v > 1:
        ans += v * (v - 1) // 2
print(ans)


Fixed submission:
from collections import defaultdict


def solve(input_s: str):
    ans = 0
    n = len(input_s)
    mod = 2019
    dp = defaultdict(int)
    cur = 0
    for i in range(n, -1, -1):
        if i == n:
            dp[0] += 1
        else:
            # cur += ((10 ** (n - i - 1)) * int(input_s[i])) % mod これはnがでかくなるとめちゃめちゃ遅い
            cur += pow(10, n - i - 1, 2019) * int(input_s[i]) % mod
            cur %= mod
            dp[cur] += 1
    for v in dp.values():
        if v > 1:
            ans += v * (v - 1) // 2
    return ans


if __name__ == "__main__":
    S = input()
    print(solve(S))
Problem p02702 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    s = input()
    rem = [0] * 2019  # the count of the each remainder: 0 ~ 2018
    rem[0] = 1  # why? can't understand.

    temp = 0
    digit = 1
    for i, c in enumerate(s[::-1]):
        # No time to pow.
        temp += int(c) * digit
        rem[temp % 2019] += 1
        digit *= 10

    return sum(c * (c - 1) // 2 for c in rem)


if __name__ == "__main__":
    print(main())


Fixed submission:
def main():
    s = input()
    rem = [0] * 2019  # the count of the each remainder: 0 ~ 2018
    rem[0] = 1  # why? can't understand.

    temp = 0
    digit = 1
    for i, c in enumerate(s[::-1]):
        # No time to pow.
        temp += int(c) * digit
        rem[temp % 2019] += 1
        digit *= 10
        digit %= 2019

    return sum(c * (c - 1) // 2 for c in rem)


if __name__ == "__main__":
    print(main())
Problem p02702 (Python)

Buggy submission (status: Time Limit Exceeded):
s = list(input())
s.reverse()
n = len(s)
now = 0
mods = [0] * 2019
mods[0] += 1
for i in range(n):
    now += pow(10, i) * int(s[i])
    now %= 2019
    mods[now] += 1
ans = 0
for i in mods:
    ans += (i * (i - 1)) // 2
print(ans)


Fixed submission:
if __name__ == "__main__":
    s = list(input())
    s.reverse()
    n = len(s)
    now = 0
    mods = [0] * 2019
    mods[0] += 1
    ten = 1
    for i in range(n):
        now += ten * int(s[i])
        now %= 2019
        ten *= 10
        ten %= 2019
        mods[now] += 1
    ans = 0
    for i in mods:
        ans += (i * (i - 1)) // 2
    print(ans)
Problem p02702 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    s = input()
    mod = 2019

    n = 0
    R = [0] * mod
    R[0] += 1
    for i in range(len(s)):
        n += int(s[-i - 1]) * (10**i) % mod
        R[n % mod] += 1

    print(sum([r * (r - 1) // 2 for r in R]))


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    s = input()
    mod = 2019

    n = 0
    R = [0] * mod
    R[0] += 1
    for i in range(len(s)):
        n += int(s[-i - 1]) * pow(10, i, mod)
        R[n % mod] += 1

    print(sum([r * (r - 1) // 2 for r in R]))


if __name__ == "__main__":
    main()
Problem p02702 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    s = list(input())
    s.reverse()
    MOD = 2019
    tot = 0
    ans = 0
    d = [0 for _ in range(MOD)]
    for i in range(len(s)):
        d[tot] += 1
        x = (10**i) % MOD
        p = int(s[i]) * x
        p %= MOD
        tot = (tot + p) % MOD
        ans += d[tot]
    return ans


if __name__ == "__main__":
    print(main())


Fixed submission:
def main():
    s = list(input())
    s.reverse()
    MOD = 2019
    tot = 0
    ans = 0
    d = [0 for _ in range(MOD)]
    for i in range(len(s)):
        d[tot] += 1
        x = pow(10, i, MOD)
        p = int(s[i]) * x
        p %= MOD
        tot = (tot + p) % MOD
        ans += d[tot]
    return ans


if __name__ == "__main__":
    print(main())
Problem p02702 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LLI long long int
#define pii pair<int, int>
#define fst first
#define sec second
#define ULTRA 2134567890
#define de(x) if (x != 0 && MODE == x)
#define MODE 0
using namespace std;

char S[107];
int cnt[2019] = {};

int main() {
  int i, j;

  scanf("%s", S);

  int N = strlen(S);

  LLI ans = 0;
  int r = 0;
  int mul = 1;
  for (i = N - 1; i >= 0; i--) {
    r = (r + mul * (S[i] - '0')) % 2019;
    mul = mul * 10 % 2019;

    de(1) printf("r = %d\n", r);

    if (r == 0) {
      ans += cnt[r] + 1;
    } else {
      ans += cnt[r];
    }

    cnt[r]++;
  }

  printf("%lld\n", ans);

  return 0;
}

//
//          ___ ___  __________  __________
//         /////\  \/\   ______\/\\\\\\\\\\\  Code by ~Ice Tea~ of
//        //// \ \  \ \  \_____/\ \\\\_____/      New Empire of CHSH ...
//       ////   \ \  \ \  \      \ \\\\\\\\\\\
//      ////__   \ \  \ \  \      \ \\\\_____/
//     ///////\   \ \  \ \  \______\ \\\\_______   ________
//    ////____/    \ \__\ \___   ___\ \\\\\\\\\\\ _\  ___  \_
//   /__/           \/__/\/__/\  \__/\ \\\\_____//\  .\_/\_  \
//                           \ \  \   \ \\\\\\\\\\\\  \_\/_\  \
// ... with the AC Power of   \ \  \   \ \\\\_____/ \   _____  \
//   The Great Tsundere Doggy. \ \  \   \ \\\\     \ \  \  \ \  \
//                              \ \__\   \ \\\\\\\\\\ \__\  \ \__\
//                               \/__/    \/________/\/__/   \/__/
//


Fixed submission:
#include <bits/stdc++.h>
#define LLI long long int
#define pii pair<int, int>
#define fst first
#define sec second
#define ULTRA 2134567890
#define de(x) if (x != 0 && MODE == x)
#define MODE 0
using namespace std;

char S[200007];
int cnt[2019] = {};

int main() {
  int i, j;

  scanf("%s", S);

  int N = strlen(S);

  LLI ans = 0;
  int r = 0;
  int mul = 1;
  for (i = N - 1; i >= 0; i--) {
    r = (r + mul * (S[i] - '0')) % 2019;
    mul = mul * 10 % 2019;

    de(1) printf("r = %d\n", r);

    if (r == 0) {
      ans += cnt[r] + 1;
    } else {
      ans += cnt[r];
    }

    cnt[r]++;
  }

  printf("%lld\n", ans);

  return 0;
}

//
//          ___ ___  __________  __________
//         /////\  \/\   ______\/\\\\\\\\\\\  Code by ~Ice Tea~ of
//        //// \ \  \ \  \_____/\ \\\\_____/      New Empire of CHSH ...
//       ////   \ \  \ \  \      \ \\\\\\\\\\\
//      ////__   \ \  \ \  \      \ \\\\_____/
//     ///////\   \ \  \ \  \______\ \\\\_______   ________
//    ////____/    \ \__\ \___   ___\ \\\\\\\\\\\ _\  ___  \_
//   /__/           \/__/\/__/\  \__/\ \\\\_____//\  .\_/\_  \
//                           \ \  \   \ \\\\\\\\\\\\  \_\/_\  \
// ... with the AC Power of   \ \  \   \ \\\\_____/ \   _____  \
//   The Great Tsundere Doggy. \ \  \   \ \\\\     \ \  \  \ \  \
//                              \ \__\   \ \\\\\\\\\\ \__\  \ \__\
//                               \/__/    \/________/\/__/   \/__/
//
Problem p02702 (Python)

Buggy submission (status: Time Limit Exceeded):
S = input()
cnt = 0
for i in range(len(S)):
    for j in range(i + 4, len(S)):
        if int(S[i : j + 1]) % 2019 == 0:
            cnt += 1
print(cnt)


Fixed submission:
import bisect, collections, copy, heapq, itertools, math, string
import sys


def I():
    return int(sys.stdin.readline().rstrip())


def MI():
    return map(int, sys.stdin.readline().rstrip().split())


def LI():
    return list(map(int, sys.stdin.readline().rstrip().split()))


def S():
    return sys.stdin.readline().rstrip()


def LS():
    return list(sys.stdin.readline().rstrip().split())


from collections import deque
from collections import defaultdict


def main():
    S_ = S()
    mod_list = defaultdict(int)
    ans = 0
    num = 0
    point = 1
    for i in S_[::-1]:
        num += int(i) * point
        mod = int(num) % 2019
        mod_list[mod] += 1
        point *= 10
        point %= 2019
    values_ = mod_list.values()
    ans += mod_list[0]
    for value in values_:
        ans += (value - 1) * value // 2

    print(ans)


if __name__ == "__main__":
    main()
Problem p02702 (Python)

Buggy submission (status: Runtime Error):
S = list(map(int, list(input())))

mod = 2019
ss = [0]
tmp = 0
for i, s in enumerate(S[::-1]):
    tmp += s * pow(10, i, mod)
    tmp %= mod
    ss.append(tmp)

cnts = [0] * 2018
ans = 0
for s in ss:
    ans += cnts[s]
    cnts[s] += 1

print(ans)


Fixed submission:
S = list(map(int, list(input())))

mod = 2019
ss = [0]
tmp = 0
for i, s in enumerate(S[::-1]):
    tmp += s * pow(10, i, mod)
    tmp %= mod
    ss.append(tmp)

cnts = [0] * 2019
ans = 0
for s in ss:
    ans += cnts[s]
    cnts[s] += 1

print(ans)
Problem p02702 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    A = input()[::-1]
    A = "0" + A
    # lis_num = [int(s) for s in A]

    # print(lis_num)

    # S = [0] * len(lis_num)
    S = [0] * len(A)
    cnt = [0] * 2019
    cnt[0] = 1

    for i in range(len(A) - 1):
        S[i + 1] = (S[i] + int(A[i + 1]) * pow(10, i)) % 2019
        cnt[S[i + 1] % 2019] += 1

    # print(S)

    # for i in S:
    #     cnt[i % 2019] += 1

    ans = 0

    for i in cnt:
        ans += i * (i - 1) // 2

    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    A = input()[::-1]
    A = "0" + A
    # lis_num = [int(s) for s in A]

    # print(lis_num)

    # S = [0] * len(lis_num)
    S = [0] * len(A)
    cnt = [0] * 2019
    cnt[0] = 1

    for i in range(len(A) - 1):
        S[i + 1] = (S[i] + int(A[i + 1]) * pow(10, i, 2019)) % 2019
        cnt[S[i + 1] % 2019] += 1

    # print(S)

    # for i in S:
    #     cnt[i % 2019] += 1

    ans = 0

    for i in cnt:
        ans += i * (i - 1) // 2

    print(ans)


if __name__ == "__main__":
    main()
Problem p02702 (Python)

Buggy submission (status: Time Limit Exceeded):
from collections import Counter

# forを逆から回して下から素直にmodを取るとTLEだった
# 累積和的な計算で高速化、やりやすくするためにreverse
S = input()[::-1]
# ex. 1817181712114 → 4112171817181
# print(S)

# 0桁目までのMODを0とすることで、
# 1桁目を含む数が2019の倍数の時に都合が良くなる
X = [0]

# 4,14,114,2114,12114,...のmod2019を計算

for i, s in enumerate(S):
    X.append((X[-1] + int(s) * 10**i) % 2019)
# print(X)


C = Counter(X)
# print(C)

ans = 0
# Xが同じになったところを2つ選べば題意を満たす
# v_C_2の計算
for v in C.values():
    ans += v * (v - 1) // 2

print(ans)


Fixed submission:
from collections import Counter

# forを逆から回して下から素直にmodを取るとTLEだった
# 累積和的な計算で高速化、やりやすくするためにreverse
S = input()[::-1]
# ex. 1817181712114 → 4112171817181
# print(S)

# 0桁目までのMODを0とすることで、
# 1桁目を含む数が2019の倍数の時に都合が良くなる
X = [0]

# 4,14,114,2114,12114,...のmod2019を計算

for i, s in enumerate(S):
    X.append((X[-1] + int(s) * pow(10, i, 2019)) % 2019)
# print(X)


C = Counter(X)
# print(C)

ans = 0
# Xが同じになったところを2つ選べば題意を満たす
# v_C_2の計算
for v in C.values():
    ans += v * (v - 1) // 2

print(ans)
Problem p02702 (Python)

Buggy submission (status: Time Limit Exceeded):
s = str(input())

num = [int(s[-1])]
for i in range(1, len(s)):
    num.append(num[-1] + pow(10, i) * int(s[-i - 1]))

mod = [1] + [0] * 2018
ans = 0
for i in num:
    m = i % 2019
    ans += mod[m]
    mod[m] += 1
print(ans)


Fixed submission:
s = str(input())

num = [int(s[-1])]
for i in range(1, len(s)):
    tmp = num[-1] + pow(10, i, 2019) * int(s[-i - 1])
    num.append(tmp % 2019)

mod = [1] + [0] * 2018
ans = 0
for i in num:
    m = i % 2019
    ans += mod[m]
    mod[m] += 1
print(ans)
Problem p02702 (Python)

Buggy submission (status: Time Limit Exceeded):
def solve(s):
    dp = [0] * 2019
    dp[0] = 1
    r = 0
    for i in range(1, len(s) + 1):
        r += int(s[-i]) * (10 ** (i - 1))
        r %= 2019
        dp[r] += 1
    res = 0
    for r in range(2019):
        res += dp[r] * (dp[r] - 1) // 2
    return res


def main():
    s = input()
    res = solve(s)
    print(res)


def test():
    assert solve("1817181712114") == 3
    assert solve("14282668646") == 2
    assert solve("2119") == 0


if __name__ == "__main__":
    test()
    main()


Fixed submission:
def solve(s):
    dp = [0] * 2019
    dp[0] = 1
    r = 0
    for i in range(1, len(s) + 1):
        r += int(s[-i]) * pow(10, i - 1, 2019)
        r %= 2019
        dp[r] += 1
    res = 0
    for r in range(2019):
        res += dp[r] * (dp[r] - 1) // 2
    return res


def main():
    s = input()
    res = solve(s)
    print(res)


def test():
    assert solve("1817181712114") == 3
    assert solve("14282668646") == 2
    assert solve("2119") == 0


if __name__ == "__main__":
    test()
    main()
Problem p02702 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys
from collections import Counter

input = sys.stdin.readline
sys.setrecursionlimit(2 * 10**6)


def inpl():
    return list(map(int, input().split()))


def main():
    S = input().strip()[::-1]
    MOD = 2019
    T = [0, int(S[0])]
    for d, s in enumerate(S):
        if d == 0:
            continue
        tmp = int(s) * (10**d)
        T.append((T[-1] + tmp) % MOD)

    # print(T)

    cT = Counter(T)
    # print(cT)
    ans = 0
    for k in T:
        cT[k] -= 1
        ans += cT[k]
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
import sys
from collections import Counter

input = sys.stdin.readline
sys.setrecursionlimit(2 * 10**6)


def inpl():
    return list(map(int, input().split()))


def main():
    S = input().strip()[::-1]
    MOD = 2019
    T = [0, int(S[0])]
    t10 = 1
    for s in S[1:]:
        t10 *= 10
        t10 %= MOD
        T.append((T[-1] + int(s) * t10) % MOD)

    # print(T)

    cT = Counter(T)
    # print(cT)
    ans = 0
    for k in T:
        cT[k] -= 1
        ans += cT[k]
    print(ans)


if __name__ == "__main__":
    main()
Problem p02702 (Python)

Buggy submission (status: Time Limit Exceeded):
S = input()
M = 2019
dp = [0] * M
dp[0] = 1
t = 0
for i, s in enumerate(S[::-1]):
    t = (t + int(s) * (10**i)) % M
    dp[t] += 1

r = 0
for d in dp:
    r += d * (d - 1) // 2

print(r)


Fixed submission:
S = input()
M = 2019
dp = [0] * M
dp[0] = 1
t = 0
for i, s in enumerate(S[::-1]):
    t = (t + int(s) * (pow(10, i, M))) % M
    dp[t] += 1

r = 0
for d in dp:
    r += d * (d - 1) // 2

print(r)
Problem p02702 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef tuple<ll, ll, ll> ti;
ll mod = 1E9 + 7;

#define REP(a, b, c) for (ll a = (b); a < (c); a++)
#define PER(a, b, c) for (ll a = (b); a >= (c); a--)

ll ii() {
  ll x;
  cin >> x;
  return x;
}
string is() {
  string x;
  cin >> x;
  return x;
}
double id() {
  double x;
  cin >> x;
  return x;
}
void oi(ll x) { cout << x; }
void od(ld x) { cout << fixed << setprecision(10) << x; }
void os(string x) { cout << x; }
void oe() { cout << endl; }
void oie(ll x) {
  oi(x);
  oe();
}
void ode(ld x) {
  od(x);
  oe();
}
void ose(string x) {
  os(x);
  oe();
}

int memo[202020][2020];

int main() {
  string S = is();
  ll ret = 0;
  REP(i, 0, S.length()) memo[i][0]++;

  REP(i, 0, S.length()) {
    ll a = (S[i] - '0');
    REP(j, 0, 2019) {
      if (memo[i][j] > 0) {
        ll y = (j * 10 + a) % 2019;
        if (y == 0) {
          ret += memo[i][j];
        }
        memo[i + 1][y] += memo[i][j];
      }
    }
  }
  oie(ret);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef tuple<ll, ll, ll> ti;
ll mod = 1E9 + 7;

#define REP(a, b, c) for (ll a = (b); a < (c); a++)
#define PER(a, b, c) for (ll a = (b); a >= (c); a--)

ll ii() {
  ll x;
  cin >> x;
  return x;
}
string is() {
  string x;
  cin >> x;
  return x;
}
double id() {
  double x;
  cin >> x;
  return x;
}
void oi(ll x) { cout << x; }
void od(ld x) { cout << fixed << setprecision(10) << x; }
void os(string x) { cout << x; }
void oe() { cout << endl; }
void oie(ll x) {
  oi(x);
  oe();
}
void ode(ld x) {
  od(x);
  oe();
}
void ose(string x) {
  os(x);
  oe();
}

short memo[202020][2020];

int main() {
  string S = is();
  ll ret = 0;
  REP(i, 0, S.length()) memo[i][0]++;

  REP(i, 0, S.length()) {
    ll a = (S[i] - '0');
    REP(j, 0, 2019) {
      if (memo[i][j] > 0) {
        ll y = (j * 10 + a) % 2019;
        if (y == 0) {
          ret += memo[i][j];
        }
        memo[i + 1][y] += memo[i][j];
      }
    }
  }
  oie(ret);

  return 0;
}
Problem p02702 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
#define INF 1LL << 30
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()

ll cnt[3000];

int main() {
  string s;
  cin >> s;

  reverse(all(s));
  for (int i = 1; i <= s.size(); i++) {
    string t = s.substr(0, i);
    reverse(all(t));
    ll x = stoll(t);
    // cout<<x<<endl;
    // cout<<x%2019<<endl;
    cnt[x % 2019]++;
  }

  ll ans = 0;
  rep(i, 2019) {
    if (cnt[i] >= 2)
      ans += (cnt[i] * (cnt[i] - 1)) / 2;
  }
  ans += cnt[0];

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
#define INF 1LL << 30
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()

ll cnt[3000];

int main() {
  string s;
  cin >> s;

  int tot = 0;
  int p = 1;
  int n = s.size();
  for (int i = n - 1; i >= 0; i--) {
    tot = (tot + (s[i] - '0') * p) % 2019;
    cnt[tot]++;
    p = (p * 10) % 2019;
  }

  ll ans = 0;
  rep(i, 2019) {
    if (cnt[i] >= 2)
      ans += (cnt[i] * (cnt[i] - 1)) / 2;
  }
  ans += cnt[0];

  cout << ans << endl;
}
Problem p02702 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

long long N;
string S;
int P;

long long solve() {
  vector<long long> val(P, 0);
  long long tenfactor = 1;
  long long cur = 0;
  val[cur]++;
  for (int i = 0; i < N; ++i) {
    cur = (cur + (S[N - i - 1] - '0') * tenfactor) % P;
    tenfactor = (tenfactor * 10) % P;
    val[cur]++;
  }
  long long res = 0;
  for (int p = 0; p < P; ++p)
    res += val[p] * (val[p] - 1) / 2;
  return res;
}

int main() {
  cin >> N >> S;
  P = 2019;
  cout << solve() << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

long long N;
string S;
int P;

long long solve() {
  vector<long long> val(P, 0);
  long long tenfactor = 1;
  long long cur = 0;
  val[cur]++;
  for (int i = 0; i < N; ++i) {
    cur = (cur + (S[N - i - 1] - '0') * tenfactor) % P;
    tenfactor = (tenfactor * 10) % P;
    val[cur]++;
  }
  long long res = 0;
  for (int p = 0; p < P; ++p)
    res += val[p] * (val[p] - 1) / 2;
  return res;
}

int main() {
  cin >> S;
  N = S.length();
  P = 2019;
  cout << solve() << endl;
}
Problem p02702 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  string S;
  cin >> S;
  vector<int> mods(2019, 0);
  mods[0] = 1;
  for (int i = 0; i < (int)S.size(); i++) {
    mods[stoll(S.substr(i)) % 2019]++;
  }
  int result = 0;
  for (int j : mods) {
    if (j >= 2) {
      result += j * (j - 1) / 2;
    }
  }

  cout << result << endl;
}


Fixed submission:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  string S;
  cin >> S;
  vector<int> mods(2019, 0);
  mods[0] = 1;
  int current = 0;
  int keta = 1;
  for (int i = (int)S.size() - 1; i >= 0; i--) {
    current = (stoi(S.substr(i, 1)) * keta + current) % 2019;
    mods[current]++;
    keta = keta * 10 % 2019;
  }
  int result = 0;
  for (int j : mods) {
    if (j >= 2) {
      result += j * (j - 1) / 2;
    }
  }

  cout << result << endl;
}
Problem p02702 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  string S;
  cin >> S;
  int L = S.size();
  vector<int> M(L + 1);
  M.at(L) = 0;
  M.at(L - 1) = (S.at(L - 1) - '0') % 2019;
  int Mod = 1;
  for (int i = 0; i < L - 1; i++) {
    Mod = (Mod * 10) % 2019;
    M.at(L - 2 - i) = (S.at(L - 2 - i) - '0') * Mod + M.at(L - 1 - i);
    M.at(L - 2 - i) %= 2019;
  }
  int Ans = 0;
  for (int i = 4; i <= L; i++) {
    for (int j = 0; j < L - i + 1; j++) {
      if (M.at(j) == M.at(j + i)) {
        Ans++;
      }
    }
  }
  cout << Ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  string S;
  cin >> S;
  int L = S.size();
  vector<int> M(L + 1);
  M.at(L) = 0;
  M.at(L - 1) = (S.at(L - 1) - '0') % 2019;
  int Mod = 1;
  for (int i = 0; i < L - 1; i++) {
    Mod = (Mod * 10) % 2019;
    M.at(L - 2 - i) = (S.at(L - 2 - i) - '0') * Mod + M.at(L - 1 - i);
    M.at(L - 2 - i) %= 2019;
  }
  int Ans = 0;
  vector<int> No(2019);
  for (int i = 0; i <= L; i++) {
    No.at(M.at(i))++;
  }
  for (int i = 0; i < 2019; i++) {
    Ans += No.at(i) * (No.at(i) - 1) / 2;
  }
  cout << Ans << endl;
}
Problem p02702 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
const int maxn = 10000;
const int INF32 = 1'050'000'000;
const long long INF64 = 4'000'000'000'000'000'000;
const int MOD7 = 1'000'000'007;
const int MOD9 = 1'000'000'009;
const int mod = MOD7;

void ERROR(int num) { printf("ERROR%d!\n", num); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int dx[8] = {-1, 0, 1, 0, 1, 1, -1, -1};
int dy[8] = {0, -1, 0, 1, 1, -1, 1, -1};

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  string S;
  cin >> S;

  int l = S.length();
  reverse(S.begin(), S.end());
  vector<ll> sum;
  sum.resize(l + 1, 0);
  vector<ll> cnt;
  cnt.resize(2019, 0);

  ll total = 0;
  ll ten = 1;
  cnt[0] = 1;
  for (int i = 0; i <= l; i++) {
    sum[i + 1] = (sum[i] + (S[i] - '0') * ten % 2019 + 2019) % 2019;
    // cout << "sum[i+1]: " << sum[i+1] << " , total=" << total << endl;
    total += cnt[sum[i + 1]];
    cnt[sum[i + 1]]++;
    ten = ten * 10 % 2019;
  }

  cout << total << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
const int maxn = 10000;
const int INF32 = 1'050'000'000;
const long long INF64 = 4'000'000'000'000'000'000;
const int MOD7 = 1'000'000'007;
const int MOD9 = 1'000'000'009;
const int mod = MOD7;

void ERROR(int num) { printf("ERROR%d!\n", num); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int dx[8] = {-1, 0, 1, 0, 1, 1, -1, -1};
int dy[8] = {0, -1, 0, 1, 1, -1, 1, -1};

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  string S;
  cin >> S;

  int l = S.length();
  reverse(S.begin(), S.end());
  vector<ll> sum;
  sum.resize(l + 1, 0);
  vector<ll> cnt;
  cnt.resize(2019, 0);

  ll total = 0;
  ll ten = 1;
  cnt[0] = 1;
  for (int i = 0; i < l; i++) {
    sum[i + 1] = (sum[i] + (S[i] - '0') * ten % 2019 + 2019) % 2019;
    // cout << "sum[i+1]: " << sum[i+1] << " , total=" << total << endl;
    total += cnt[sum[i + 1]];
    cnt[sum[i + 1]]++;
    ten = ten * 10 % 2019;
  }

  cout << total << endl;
}
Problem p02702 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int dp[200009][2020];
int main() {
  string s;
  cin >> s;
  memset(dp, 0, sizeof(dp));
  dp[0][s[0] - '0']++;
  for (int i = 1; i < s.length(); i++) {
    dp[i][s[i] - '0']++;
    for (int j = 0; j < 2019; j++) {
      dp[i][(j * 10 + s[i] - '0') % 2019] += dp[i - 1][j];
    }
  }
  int ans = 0;
  for (int i = 0; i < s.length(); i++) {
    ans += dp[i][0];
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

short dp[200009][2020];
int main() {
  string s;
  cin >> s;
  memset(dp, 0, sizeof(dp));
  dp[0][s[0] - '0']++;
  for (int i = 1; i < s.length(); i++) {
    dp[i][s[i] - '0']++;
    for (int j = 0; j < 2019; j++) {
      dp[i][(j * 10 + s[i] - '0') % 2019] += dp[i - 1][j];
    }
  }
  int ans = 0;
  for (int i = 0; i < s.length(); i++) {
    ans += dp[i][0];
  }
  cout << ans << endl;
}
Problem p02702 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mod 1e+7
#define int long long
#define garou                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
using namespace std;
int dp[2019];
signed main() {
  garou;
  string s;
  cin >> s;
  int n = s.length();
  dp[0] = 1;
  int c = 1;
  int l = 0;
  for (int i = n - 1; i >= 0; i--) {
    l += ((s[i] - '0') * c);
    l %= 2019;
    c *= 10;
    dp[l]++;
  }
  int ans = 0;
  for (int i = 0; i < 2019; i++) {
    ans += (dp[i] * (dp[i] - 1)) / 2;
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define mod 1e+7
#define int long long
#define garou                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
using namespace std;
int dp[2019];
signed main() {
  garou;
  string s;
  cin >> s;
  int n = s.length();
  dp[0] = 1;
  int c = 1;
  int l = 0;
  for (int i = n - 1; i >= 0; i--) {
    l += ((s[i] - '0') * c);
    l %= 2019;
    c = (c * 10) % 2019;
    dp[l]++;
  }
  int ans = 0;
  for (int i = 0; i < 2019; i++) {
    ans += (dp[i] * (dp[i] - 1)) / 2;
  }
  cout << ans;
}
Problem p02702 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll ten(ll x) {
  if (x == 0) {
    return 1;
  } else {
    ll res = 1;
    for (int i = 0; i < x; i++) {
      res *= 10;
      res %= 2019;
    }
    return res;
  }
}

int main() {
  string S;
  cin >> S;
  ll lon = (ll)(S.length());
  ll Am[2019] = {0};
  Am[0]++;
  ll back = 0;
  ll ans = 0;
  for (ll i = lon - 1; i >= 0; i--) {
    ll X = S[i] - 48;
    ll ag = ((X * ten(lon - 1 - i)) % 2019 + back) % 2019;
    Am[ag]++;
    back = ag;
  }
  for (int i = 0; i < 2019; i++) {
    if (Am[i] >= 2) {
      ans += (Am[i] * (Am[i] - 1)) / 2;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll ten(ll x) {
  if (x == 0) {
    return 1;
  } else {
    x %= 672;
    ll res = 1;
    for (int i = 0; i < x; i++) {
      res *= 10;
      res %= 2019;
    }
    return res;
  }
}

int main() {
  string S;
  cin >> S;
  ll lon = (ll)(S.length());
  ll Am[2019] = {0};
  Am[0]++;
  ll back = 0;
  ll ans = 0;
  for (ll i = lon - 1; i >= 0; i--) {
    ll X = S[i] - 48;
    ll ag = ((X * ten(lon - 1 - i)) % 2019 + back) % 2019;
    Am[ag]++;
    back = ag;
  }
  for (int i = 0; i < 2019; i++) {
    if (Am[i] >= 2) {
      ans += (Am[i] * (Am[i] - 1)) / 2;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fi first
#define se second
using namespace std;
const ll INF = 1LL << 60;
const int inf = 1 << 30;
inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
const int maxn = 2e5 + 5;
char s[maxn];
int f[maxn][2020];
int main() {
  fastio();
  cin >> s + 1;
  int n = strlen(s + 1);
  for (int i = 1; i <= n; i++)
    s[i] = s[i] - '0';
  for (int i = 1; i <= n; i++)
    f[i][s[i]] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2019; j++) {
      f[i + 1][(10 * j + s[i + 1]) % 2019] += f[i][j];
    }
  }
  ll ans = 0;
  for (int i = 4; i <= n; i++)
    ans += f[i][0];
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fi first
#define se second
using namespace std;
const ll INF = 1LL << 60;
const int inf = 1 << 30;
inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
const int maxn = 2e5 + 5;
char s[maxn];
unsigned short f[maxn][2020];
int main() {
  fastio();
  cin >> s + 1;
  int n = strlen(s + 1);
  for (int i = 1; i <= n; i++)
    s[i] = s[i] - '0';
  for (int i = 1; i <= n; i++)
    f[i][s[i]] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2019; j++) {
      f[i + 1][(10 * j + s[i + 1]) % 2019] += f[i][j];
    }
  }
  ll ans = 0;
  for (int i = 4; i <= n; i++)
    ans += f[i][0];
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
typedef long long ll;
typedef pair<int, int> pii;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  vector<ll> vec(n);
  vector<ll> ls(2020, 0);
  vec[n - 1] = s[n - 1] - '0';
  ls[n - 1] += 1;
  ll t = 1;
  for (int i = n - 2; i >= 0; i--) {
    t = t * 10 % 2019;
    vec[i] = (t * (s[i] - '0') + vec[i + 1]) % 2019;
    ls[(t * (s[i] - '0') + vec[i + 1]) % 2019] += 1;
  }
  ll ans = 0;
  ls[0]++;
  rep(i, 2019) { ans += ls[i] * (ls[i] - 1) / 2; }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
typedef long long ll;
typedef pair<int, int> pii;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  vector<ll> vec(n);
  vector<ll> ls(2020, 0);
  vec[n - 1] = s[n - 1] - '0';
  ls[s[n - 1] - '0'] += 1;
  ll t = 1;
  for (int i = n - 2; i >= 0; i--) {
    t = t * 10 % 2019;
    vec[i] = (t * (s[i] - '0') + vec[i + 1]) % 2019;
    ls[(t * (s[i] - '0') + vec[i + 1]) % 2019] += 1;
  }
  ll ans = 0;
  ls[0]++;
  rep(i, 2019) { ans += ls[i] * (ls[i] - 1) / 2; }
  cout << ans << endl;

  return 0;
}
Problem p02702 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>
#define N 2019
typedef long long LL;
using namespace std;
char s[305000];
short dp[305000][N + 1];
int main() {
  memset(dp, 0LL, sizeof(dp));
  scanf("%s", s + 1);
  int ls = strlen(s + 1);
  int a;
  LL ans = 0;
  for (int i = 1; i <= ls; i++) {
    a = s[i] - '0';
    dp[i][a % N]++;
    for (int j = 0; j < N; j++)
      dp[i][(a + j * 10) % N] += dp[i - 1][j];
    ans += dp[i][0];
  }
  printf("%lld\n", ans);
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>
#define N 2019
typedef long long LL;
using namespace std;
char s[200005];
short dp[200005][N + 1];
int main() {
  memset(dp, 0LL, sizeof(dp));
  scanf("%s", s + 1);
  int ls = strlen(s + 1);
  int a;
  LL ans = 0;
  for (int i = 1; i <= ls; i++) {
    a = s[i] - '0';
    dp[i][a % N]++;
    for (int j = 0; j < N; j++)
      dp[i][(a + j * 10) % N] += dp[i - 1][j];
    ans += dp[i][0];
  }
  printf("%lld\n", ans);
}
Problem p02702 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a) for (int i = 0; i < (a); i++)
const ll MOD = 1000000007;
// const ll MOD=998244353;

int cnt[2020];

int main() {
  string S;
  cin >> S;
  cnt[0] = 1;
  ll sum = 0, pre = 0, x = 1;
  for (int i = S.length() - 1; i >= 0; i--) {
    int tmp = (pre + (S[i] - '0') * x) % 2019;
    sum += cnt[tmp];
    cnt[tmp]++;
    pre = tmp;
    (x *= 10) % MOD;
  }
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a) for (int i = 0; i < (a); i++)
const ll MOD = 1000000007;
// const ll MOD=998244353;

int cnt[2020];

int main() {
  string S;
  cin >> S;
  cnt[0] = 1;
  ll sum = 0, pre = 0, x = 1;
  for (int i = S.length() - 1; i >= 0; i--) {
    int tmp = (pre + (S[i] - '0') * x) % 2019;
    sum += cnt[tmp];
    cnt[tmp]++;
    pre = tmp;
    x = x * 10 % 2019;
  }
  cout << sum << endl;
  return 0;
}
Problem p02702 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int m = 2019;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  reverse(s.begin(), s.end());
  int x = 1;
  int tot = 0;
  vector<int> count(m);
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    count[tot]++;
    tot += (s[i] - '0') * x;
    ans += count[tot];
    x = x * 10 % m;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int m = 2019;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  reverse(s.begin(), s.end());
  int x = 1;
  int tot = 0;
  vector<int> count(m);
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    count[tot]++;
    tot += (s[i] - '0') * x;
    tot %= m;
    ans += count[tot];
    x = x * 10 % m;
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int cnt[2020];
int num[100001];

int main(void) {
  string s;
  cin >> s;
  int tmp = 0;
  int mul = 1;
  cnt[0]++;
  int size = s.size();
  for (int i = 0; i < size; i++) {
    tmp += (s[size - i - 1] - '0') * mul % 2019;
    tmp %= 2019;
    mul *= 10;
    mul %= 2019;
    cnt[tmp]++;
    num[size - i - 1] = tmp;
  }
  long long ans = 0;
  for (int i = 0; i < size; i++) {
    cnt[num[i]]--;
    ans += cnt[num[i]];
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int cnt[2020];
int num[200001];

int main(void) {
  string s;
  cin >> s;
  int tmp = 0;
  int mul = 1;
  cnt[0]++;
  int size = s.size();
  for (int i = 0; i < size; i++) {
    tmp += (s[size - i - 1] - '0') * mul % 2019;
    tmp %= 2019;
    mul *= 10;
    mul %= 2019;
    cnt[tmp]++;
    num[size - i - 1] = tmp;
  }
  long long ans = 0;
  for (int i = 0; i < size; i++) {
    cnt[num[i]]--;
    ans += cnt[num[i]];
  }
  cout << ans << endl;
}
Problem p02702 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e4 + 9;
int main() {
  char s[maxn];
  scanf("%s", s + 1);
  int len = strlen(s + 1);
  ll d[maxn] = {0}, b[maxn] = {0};
  ll ans = 0;
  for (int i = 1; i <= len; i++) {
    int num = s[i] - '0';
    d[num]++;
    for (int j = 0; j < 2019; j++) {
      d[(j * 10 + num) % 2019] += b[j];
    }
    for (int j = 0; j < 2019; j++) {
      b[j] = d[j];
      d[j] = 0;
    }
    ans += b[0];
  }
  printf("%lld\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 9;
int main() {
  char s[maxn];
  scanf("%s", s + 1);
  int len = strlen(s + 1);
  ll d[maxn] = {0}, b[maxn] = {0};
  ll ans = 0;
  for (int i = 1; i <= len; i++) {
    int num = s[i] - '0';
    d[num]++;
    for (int j = 0; j < 2019; j++) {
      d[(j * 10 + num) % 2019] += b[j];
    }
    for (int j = 0; j < 2019; j++) {
      b[j] = d[j];
      d[j] = 0;
    }
    ans += b[0];
  }
  printf("%lld\n", ans);
}
Problem p02702 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

char s[200200];
long long p[2019], q[2019], a;

int main() {
  scanf("%s", s);
  for (int i = 0; s[i]; i++) {
    for (int j = 0; j < 2019; j++)
      p[j] = q[j], q[j] = 0;
    p[0]++;
    for (int i = 0; i < 2019; i++)
      q[(i * 10 + s[i] - '0') % 2019] += p[i];
    a += q[0];
  }
  printf("%lld\n", a);
  return 0;
}

Fixed submission:
#include <stdio.h>

char s[200200];
long long p[2019], q[2019], a;

int main() {
  scanf("%s", s);
  for (int i = 0; s[i]; i++) {
    for (int j = 0; j < 2019; j++)
      p[j] = q[j], q[j] = 0;
    p[0]++;
    for (int j = 0; j < 2019; j++)
      q[(j * 10 + s[i] - '0') % 2019] += p[j];
    a += q[0];
  }
  printf("%lld\n", a);
  return 0;
}
Problem p02702 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }

int main() {
  string S;
  cin >> S;
  ll ans = 0;
  for (int i = 0; i < S.size(); ++i) {
    for (int k = i; k < S.size(); ++k) {
      string tmp = S.substr(i, k - i + 1);
      ll res = 0;
      REP(i, tmp.size()) {
        res += char_to_int(tmp[i]);
        res %= 2019;
        if (i != tmp.size() - 1)
          res *= 10;
      }
      if (res == 0)
        ans++;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }

int main() {
  string S;
  cin >> S;
  ll ans = 0;
  unordered_map<ll, ll> m;
  m[0] = 1;
  reverse(all(S));
  ll tmp = 0, ten = 1;
  REP(i, S.size()) {
    tmp += char_to_int(S[i]) * ten;
    tmp %= 2019;
    ten *= 10;
    ten %= 2019;
    m[tmp]++;
  }
  for (auto x : m) {
    ans += 0.5 * x.second * (x.second - 1);
  }
  cout << ans << endl;
}
Problem p02702 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll INF = 1001001001;
const ll LINF = 1001001001001001001;

void prvec(vector<ll> vec) { // for debug
  ll n = vec.size();
  cout << "------------------------------------\n";
  rep(i, n) cout << i << " " << vec.at(i) << "\n";
  cout << "------------------------------------\n";
}

void pr2d(vector<vector<ll>> vec) { // for debug
  ll h = vec.size();
  ll w = vec.at(0).size();
  cout << "------------------------------------\n";
  rep(i, h) {
    rep(j, w) { cout << vec.at(i).at(j) << " "; }
    cout << "\n";
  }
  cout << "------------------------------------\n";
}

// main starts here

int main() {
  unsigned short dp[200000][2019];
  string s;
  cin >> s;
  ll ss = s.size();
  ll x = s.at(0) - '0';
  dp[0][x] = 1;
  ll ans = 0;
  for (int i = 1; i < ss; i++) {
    x = s.at(i) - '0';
    for (int j = 0; j < 2019; j++) {
      ll y = (j * 10 + x) % 2019;
      dp[i][y] += dp[i - 1][j];
    }
    dp[i][x]++;
    ans += dp[i][0];
  }
  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll INF = 1001001001;
const ll LINF = 1001001001001001001;

void prvec(vector<ll> vec) { // for debug
  ll n = vec.size();
  cout << "------------------------------------\n";
  rep(i, n) cout << i << " " << vec.at(i) << "\n";
  cout << "------------------------------------\n";
}

void pr2d(vector<vector<ll>> vec) { // for debug
  ll h = vec.size();
  ll w = vec.at(0).size();
  cout << "------------------------------------\n";
  rep(i, h) {
    rep(j, w) { cout << vec.at(i).at(j) << " "; }
    cout << "\n";
  }
  cout << "------------------------------------\n";
}

// main starts here

int main() {
  vector<vector<unsigned short>> dp(200000, vector<unsigned short>(2019, 0));
  string s;
  cin >> s;
  ll ss = s.size();
  ll x = s.at(0) - '0';
  dp[0][x] = 1;
  ll ans = 0;
  for (int i = 1; i < ss; i++) {
    x = s.at(i) - '0';
    for (int j = 0; j < 2019; j++) {
      ll y = (j * 10 + x) % 2019;
      dp[i][y] += dp[i - 1][j];
    }
    dp[i][x]++;
    ans += dp[i][0];
  }
  cout << ans << "\n";
  return 0;
}
Problem p02702 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

int dp[200005][2020];

int main() {
  string S;
  cin >> S;

  rep(i, S.size()) {
    {
      int nxt = (S[i] - '0') % 2019;
      dp[i + 1][nxt] += 1;
    }
    rep(j, 2019) {
      {
        int nxt = (j * 10 + (S[i] - '0')) % 2019;
        dp[i + 1][nxt] += dp[i][j];
      }
    }
  }

  int ret = 0;
  rep(i, S.size() + 1) { ret += dp[i][0]; }
  cout << ret << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

short dp[200005][2020];

int main() {
  string S;
  cin >> S;

  rep(i, S.size()) {
    {
      int nxt = (S[i] - '0') % 2019;
      dp[i + 1][nxt] += 1;
    }
    rep(j, 2019) {
      {
        int nxt = (j * 10 + (S[i] - '0')) % 2019;
        dp[i + 1][nxt] += dp[i][j];
      }
    }
  }

  int ret = 0;
  rep(i, S.size() + 1) { ret += dp[i][0]; }
  cout << ret << endl;

  return 0;
}
Problem p02702 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  string S;
  cin >> S;
  int64_t N = S.size();
  int64_t ans = 0, l = 0, r = 3;
  for (int i = 0; i < N - 4; i++) {
    int64_t a = stoi(S.substr(i, 5));
    int64_t r = a % 2019;
    if (r == 0)
      ans++;
    for (int j = i + 5; j < N; j++) {
      int64_t b = S.at(j) - '0';
      r = (r * 10 + b) % 2019;
      if (r == 0) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  string S;
  cin >> S;
  int64_t N = S.size();
  reverse(S.begin(), S.end());
  vector<int64_t> Count(2019, 0);
  Count[0]++;
  int64_t num = 1, cur = 0, ans = 0;
  for (int i = 1; i <= N; i++) {
    int sint = S[i - 1] - '0';
    cur = (cur + sint * num) % 2019;
    Count[cur]++;
    num *= 10;
    num %= 2019;
  }
  for (int64_t c : Count) {
    ans += c * (c - 1) / 2;
  }
  cout << ans << endl;
}
Problem p02702 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

const int MOD = 2019;

int c[MOD];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int cur = 0;
  c[cur]++;
  string s;
  cin >> s;
  int ans = 0;
  reverse(s.begin(), s.end());
  int p = 1;
  for (auto x : s) {
    int ncur = cur + p * (x - '0');
    ncur %= MOD;
    ans += c[ncur];
    cur = ncur;
    c[cur]++;
    p *= 10;
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

const int MOD = 2019;

int c[MOD];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int cur = 0;
  c[cur]++;
  string s;
  cin >> s;
  int ans = 0;
  reverse(s.begin(), s.end());
  int p = 1;
  for (auto x : s) {
    int ncur = cur + p * (x - '0');
    ncur %= MOD;
    ans += c[ncur];
    cur = ncur;
    c[cur]++;
    p *= 10;
    p %= MOD;
  }
  cout << ans << endl;
}