Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1000000007;
const int INF = 1000000000; // 1e9
const int NIL = -1;
const long long LINF = 1000000000000000000; // 1e18
const long double EPS = 1E-10;

template <class T, class S> inline bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> inline bool chmin(T &a, const S &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  std::string S;
  std::cin >> S;
  int N(S.size());
  long long ans(0);
  // m[i] = i文字目から最後まで
  std::vector<int> m(N + 1);
  m[N] = 0;
  int keta(1);
  // m[i]: U_i % 2019 = i
  std::vector<int> cnt(2019, 0);
  cnt[0] = 1;
  for (int i(N - 1); i >= 0; --i) {
    m[i] = (keta * (S[i] - '0') + m[i + 1]) % 2019;
    if (m[i] < 0)
      m[i] += 2019;
    ans += cnt[m[i]];
    ++cnt[m[i]];
    keta = 10 * keta % 2019;
    if (keta < 0)
      keta += 2019;
  }
  std::cout << ans << std::endl;
  return 0;
}


Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1000000007;
const int INF = 1000000000; // 1e9
const int NIL = -1;
const long long LINF = 1000000000000000000; // 1e18
const long double EPS = 1E-10;

template <class T, class S> inline bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> inline bool chmin(T &a, const S &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  std::string S;
  std::cin >> S;
  int N(S.size());
  // dp1[i][j] := i文字目(0-order)まで使うで余りjの数
  std::vector<std::vector<long long>> dp1(N, std::vector<long long>(2019, 0));
  // dp2[i][j] := i文字目(0-order)までは使わないが余りjの数
  std::vector<std::vector<long long>> dp2(N, std::vector<long long>(2019, 0));
  dp1[0][S[0] - '0'] = 1;
  for (int i(1); i < N; ++i) {
    ++dp1[i][S[i] - '0'];
    for (int j(0); j < 2019; ++j) {
      dp2[i][j] = dp1[i - 1][j] + dp2[i - 1][j];
      dp1[i][(10 * j + S[i] - '0') % 2019] += dp1[i - 1][j];
    }
  }
  std::cout << dp1[N - 1][0] + dp2[N - 1][0] << std::endl;
  return 0;
}