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

#include "bits/stdc++.h"
using namespace std;

using ll = long long int;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()

#define pi 3.14159265358979

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

bool my_cp(pair<int, int> a, pair<int, int> b) {
  return (double)b.first / (double)(b.second + 1) <
         (double)a.first / (double)(a.second + 1);
}

ll cnt_rep = 0;
ll powmod(ll N, ll P, ll M) {
  cnt_rep++;
  if (P == 0)
    return 1;
  if (P % 2 == 0) {
    ll t = powmod(N, P / 2, M);
    return t * t % M;
  }
  return (N * powmod(N, P - 1, M)) % M;
}

int N;

void dfs(string s, char m, int N) {
  size_t len = s.length();
  if (len == N) {
    cout << s << endl;
  } else {
    for (char c = 'a'; c <= m; c++) {
      dfs(s + c, ((c == m) ? (m + 1) : m), N);
    }
  }
}

ll gcd(ll a, ll b) {
  ll resi, g, l;
  if (a > b) {
    g = a;
    l = b;
  } else if (b > a) {
    g = b;
    l = a;
  } else {
    return a;
  }
  resi = g % l;
  if (resi != 0) {
    return gcd(l, resi);
  } else {
    return l;
  }
}

// substr(start, len)

int main() {

  string s;
  cin >> s;

  vector<ll> m(2019, 0);
  m[0]++;

  ll a = 0, digit = 0;

  // 左から桁を増やし，それぞれのmodを求める
  for (ll i = s.length() - 1; i >= 0; i--) {
    a += (s[i] - '0') * powmod(10, digit, 2019);
    a %= 2019;
    m[a]++;
    digit++;
  }

  ll ans = 0;

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

  cout << ans << endl;

  // cout << setprecision(8) << ans << endl;  // For double
  // CHECK if it's possible to overflow!!
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

using ll = long long int;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()

#define pi 3.14159265358979

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

bool my_cp(pair<int, int> a, pair<int, int> b) {
  return (double)b.first / (double)(b.second + 1) <
         (double)a.first / (double)(a.second + 1);
}

ll cnt_rep = 0;
ll powmod(ll N, ll P, ll M) {
  cnt_rep++;
  if (P == 0)
    return 1;
  if (P % 2 == 0) {
    ll t = powmod(N, P / 2, M);
    return t * t % M;
  }
  return (N * powmod(N, P - 1, M)) % M;
}

int N;

void dfs(string s, char m, int N) {
  size_t len = s.length();
  if (len == N) {
    cout << s << endl;
  } else {
    for (char c = 'a'; c <= m; c++) {
      dfs(s + c, ((c == m) ? (m + 1) : m), N);
    }
  }
}

ll gcd(ll a, ll b) {
  ll resi, g, l;
  if (a > b) {
    g = a;
    l = b;
  } else if (b > a) {
    g = b;
    l = a;
  } else {
    return a;
  }
  resi = g % l;
  if (resi != 0) {
    return gcd(l, resi);
  } else {
    return l;
  }
}

// substr(start, len)

int main() {

  string s;
  cin >> s;

  ll ans = 0;
  int r = s.length() - 1;

  while (true) {
    int digit = 0;
    ll num = s[r] - '0';

    for (int l = r - 1; l >= 0; l--) {
      digit++;
      num = num + (s[l] - '0') * pow(10, digit);
      if (digit >= 4) {
        num %= 2019;
        if (num == 0) {
          ans++;
          cout << l + 1 << "," << r + 1 << endl;
        }
      }
    }

    r--;
    if (r == 2)
      break;
  }

  cout << ans << endl;

  // cout << setprecision(8) << ans << endl;  // For double
  // CHECK if it's possible to overflow!!
}