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

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

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define repi(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define uniqueV(x)                                                             \\
  sort(x.begin(), x.end());                                                    \\
  x.erase(unique(x.begin(), x.end()), x.end());
#define debug(x) cerr << #x << ": " << x << '\\n'
#define debug2(x, y)                                                           \\
  cerr << "(" << #x << ", " << #y << ") = "                                    \\
       << "(" << x << ", " << y << ")" << '\\n'

typedef long long int ll;
typedef pair<int, int> P;

const long long INF = 3e18 + 12;
const int inf = 1e9;

// ----------------------------------------------

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

  string S;
  cin >> S;

  ll N = S.size();
  int P = 2019;

  if (P == 2 || P == 5) {
    long long res = 0;
    for (int i = 0; i < N; ++i)
      if ((S[N - i - 1] - '0') % P == 0)
        res += N - i;
    return res;
  }
  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;

  cout << res << endl;
}

Error: 0
ss: 1817
ss: 18171
ss: 181718
ss: 1817181
ss: 18171817
ss: 181718171
ss: 1817181712
ss: 18171817121
ss: 181718171211
ss: 1817181712114
ss: 8171
ss: 81718
ss: 817181
ss: 8171817
ss: 81718171
ss: 817181712
ss: 8171817121
ss: 81718171211
ss: 817181712114
ss: 1718
ss: 17181
ss: 171817
ss: 1718171
ss: 17181712
ss: 171817121
ss: 1718171211
ss: 17181712114
ss: 7181
ss: 71817
ss: 718171
ss: 7181712
ss: 71817121
ss: 718171211
ss: 7181712114
ss: 1817
ss: 18171
ss: 181712
ss: 1817121
ss: 18171211
ss: 181712114
ss: 8171
ss: 81712
ss: 817121
ss: 8171211
ss: 81712114
ss: 1712
ss: 17121
ss: 171211
ss: 1712114
ss: 7121
ss: 71211
ss: 712114
ss: 1211
ss: 12114
ss: 2114


Fixed submission:

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

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define repi(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define uniqueV(x)                                                             \\
  sort(x.begin(), x.end());                                                    \\
  x.erase(unique(x.begin(), x.end()), x.end());
#define debug(x) cerr << #x << ": " << x << '\\n'
#define debug2(x, y)                                                           \\
  cerr << "(" << #x << ", " << #y << ") = "                                    \\
       << "(" << x << ", " << y << ")" << '\\n'

typedef long long int ll;
typedef pair<int, int> P;

const long long INF = 3e18 + 12;
const int inf = 1e9;

// ----------------------------------------------

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

  string s;
  cin >> s;

  ll n = s.size();

  if (n <= 3) {
    cout << 0 << endl;
    return 0;
  }

  ll ans = 0;

  rep(i, n) {
    rep(j, n - i - 3) {
      string ss = s.substr(i, j + 4);
      debug(ss);
      if (ss.size() < 4) {
        break;
      }
      ll a = stoull(ss);
      if (a % 2019 == 0) {
        ans++;
      }
    }
  }

  cout << ans << endl;

  return 0;
}