alexjercan/bugnet huggingface dataset:
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define SZ(x) ((int)(x).size())
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fore(i, a) for (auto &i : a)
#define min(...) min({__VA_ARGS__})
#define max(...) max({__VA_ARGS__})
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define answer cout << ans << endl;
signed _main();
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> using v = vector<T>;
template <class T> using vv = vector<v<T>>;
const int MOD = 1e9 + 7;
const long long INF = 1LL << 60;

signed _main() {
  string S;
  cin >> S;
  int N = SZ(S);
  v<int> dp(N, 0);
  unordered_map<int, int> mp;
  int tenscale = 1;
  dp[0] = S[N - 1] - '0';
  mp[0]++;
  mp[dp[0]]++;
  rep(i, N - 1) {
    tenscale = tenscale * 10 % 2019;
    dp[i + 1] = (dp[i] + (S[N - i - 2] - '0') * tenscale % 2019) % 2019;
    // cout << dp[i+1] << endl;
    mp[dp[i + 1]]++;
  }
  int ans = 0;
  fore(i, mp) {
    // cout << i.second << endl;
    ans += i.second * (i.second - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define SZ(x) ((int)(x).size())
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fore(i, a) for (auto &i : a)
#define min(...) min({__VA_ARGS__})
#define max(...) max({__VA_ARGS__})
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define answer cout << ans << endl;
signed _main();
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> using v = vector<T>;
template <class T> using vv = vector<v<T>>;
const int MOD = 1e9 + 7;
const long long INF = 1LL << 60;

signed _main() {
  string S;
  cin >> S;
  v<int> num(1000000000);
  rep(i, 100000000) { num[i] = 2019 * (i + 1); }
  int ans = 0;
  rep(i, 100000000) {
    if (S.find(to_string(num[i])))
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: 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();
}

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;
}

Error: MLE

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();
}

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;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <vector>

#define REP(i, n, N) for (ll i = (n); i < (N); i++)
// #define RREP(i, n ,N) for(ll i = (N-1); i >= (n); i--)
#define p(s) cout << (s) << endl
#define p2(a, b) cout << (a) << " " << (b) << endl
#define INF 1e9
#define vint vector<int>

using namespace std;
typedef long long ll;

// const long long M = 1e9+7;
int ctoi(const char c) {
  switch (c) {
  case '0':
    return 0;
  case '1':
    return 1;
  case '2':
    return 2;
  case '3':
    return 3;
  case '4':
    return 4;
  case '5':
    return 5;
  case '6':
    return 6;
  case '7':
    return 7;
  case '8':
    return 8;
  case '9':
    return 9;
  default:
    return -1;
  }
}

int main() {
  string s;
  cin >> s;
  ll n = 0;
  vector<ll> count(s.size() + 1);
  char c;
  int cand = 1;
  for (int i = s.size() - 1; i >= 0; i--) {
    //        n = stoll(s.substr(s.size() - i, i));
    //        c = s[i];
    n = (s[i] - '0') * cand % 2019;
    //        p(n);
    //        p2(s.substr(s.size() - i, i), n%2019);
    count[i] = (count[i + 1] + n) % 2019;
    cand *= 10;
    cand %= 2019;
  }

  vector<ll> tmp(2019);
  ll ans = 0;
  for (int i = s.size(); i >= 0; i--) {
    ans += tmp[count[i]];
    tmp[count[i]]++;
  }

  p(ans);
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <vector>

#define REP(i, n, N) for (ll i = (n); i < (N); i++)
// #define RREP(i, n ,N) for(ll i = (N-1); i >= (n); i--)
#define p(s) cout << (s) << endl
#define p2(a, b) cout << (a) << " " << (b) << endl
#define INF 1e9
#define vint vector<int>

using namespace std;
typedef long long ll;

// const long long M = 1e9+7;
int ctoi(const char c) {
  switch (c) {
  case '0':
    return 0;
  case '1':
    return 1;
  case '2':
    return 2;
  case '3':
    return 3;
  case '4':
    return 4;
  case '5':
    return 5;
  case '6':
    return 6;
  case '7':
    return 7;
  case '8':
    return 8;
  case '9':
    return 9;
  default:
    return -1;
  }
}

int main() {
  string s;
  cin >> s;
  ll n = 0;
  vector<ll> count(s.size() + 1);
  char c;
  REP(i, 0, s.size()) {
    //        n = stoll(s.substr(s.size() - i, i));
    c = s[s.size() - i - 1];
    n += ll(ctoi(c)) * ll(pow(10, i));
    //        p(n);
    //        p2(s.substr(s.size() - i, i), n%2019);
    count[i] = n % 2019;
  }

  vector<ll> tmp(2019);
  ll ans = 0;
  REP(i, 0, s.size() + 1) {
    ans += tmp[count[i]];
    tmp[count[i]]++;
  }

  p(ans);
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

// 4/26 リアルタイム参加

#include <iostream>
#include <string>
#include <vector>
using namespace std;

// int memo[200000][200000];

int main() {
  string S;
  cin >> S;
  int N = S.size();
  // cout << "S is " << S << endl;
  // cout << "N is " << N << endl;

  int ans = 0;

  // for (int i = 0; i < N ; i++) {
  //     string sub;
  //     int amari = 0;
  //     for (int j = i; j < N; j++) {
  //         // int s = atoi(S.substr(i, j - i + 1).c_str());
  //         // if (s % 2019 == 0) {
  //         //     ans++;
  //         //     // cout << "i, j = " << i << " " << j << endl;
  //         // }d

  //         // cout << atoi(S.substr(j, 1).c_str()) << endl;
  //         // amari = (amari * 10 + atoi(S.substr(j, 1).c_str())) % 2019;

  //         int s;
  //         s = S[j] - '0';
  //         // cout << s << endl;

  //         amari = (amari * 10 + s) % 2019;

  //         if (amari == 0) {
  //             ans++;
  //         }
  //     }
  // }

  int dp[2019] = {0};
  // cout << T[134] << endl;
  // int T[N + 1];

  int moddigit = 1;

  // T[N] = 0;
  // T[N - 1] = S[N - 1] - '0';
  dp[0] += 1;

  int val = 0;

  for (int i = 0; i < N; i++) {
    val = (val + 10 * (S[N - i - 1] - '0') * moddigit) % 2019;
    dp[val]++;
    moddigit = moddigit * 10 % 2019;
  }

  // for (int i = N - 2; i >= 0; i--) {
  //     int s = S[i] - '0';
  //     T[i] = (T[i + 1] + s * moddigit) % 2019;

  //     cout << "i, T[i] = " << i << ", " << T[i] << endl;
  //     dp[T[i]]++;

  //     moddigit = (moddigit * 10) % 2019;
  // }

  // cout << ans << endl;
  // int ans = 0;
  for (int i = 0; i < 2019; i++) {
    if (dp[i] > 1) {
      // cout << "i, dp[i] = " << i << ", " << dp[i] << endl;
      ans += (dp[i] * (dp[i] - 1)) / 2;
    }
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

// 4/26 リアルタイム参加

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  string S;
  cin >> S;
  int N = S.size();
  // cout << "S is " << S << endl;
  // cout << "N is " << N << endl;

  int ans = 0;

  for (int i = 0; i < N; i++) {
    string sub;
    int amari = 0;
    for (int j = i; j < N; j++) {
      // int s = atoi(S.substr(i, j - i + 1).c_str());
      // if (s % 2019 == 0) {
      //     ans++;
      //     // cout << "i, j = " << i << " " << j << endl;
      // }d

      // cout << atoi(S.substr(j, 1).c_str()) << endl;
      // amari = (amari * 10 + atoi(S.substr(j, 1).c_str())) % 2019;

      int s;
      s = S[j] - '0';
      // cout << s << endl;

      amari = (amari * 10 + s) % 2019;

      if (amari == 0) {
        ans++;
      }
    }
  }

  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, s, n) for (int i = s; i < n; i++)
#define fi first
#define se second
#define all(c) c.begin(), c.end()
typedef long long int ll;
typedef pair<int, int> P;
typedef long double ld;
void Ans(bool x) {
  if (x)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

const ll INF = 1001001001;
const ll mod = 10000000007;

ll k(ll n) {
  ll u = 0;
  while (n > 0) {
    n /= 10;
    u++;
  }
  return u;
}

int main() {
  string s;
  cin >> s;
  ll a[s.size()];
  rep(i, s.size()) {
    if (s[i] == '1') {
      a[i] = 1;
    }
    if (s[i] == '2') {
      a[i] = 2;
    }
    if (s[i] == '3') {
      a[i] = 3;
    }
    if (s[i] == '4') {
      a[i] = 4;
    }
    if (s[i] == '5') {
      a[i] = 5;
    }
    if (s[i] == '6') {
      a[i] = 6;
    }
    if (s[i] == '7') {
      a[i] = 7;
    }
    if (s[i] == '8') {
      a[i] = 8;
    }
    if (s[i] == '9') {
      a[i] = 9;
    }
  }
  ll dp[s.size()];
  int ans = 0;
  map<int, int> mp;
  int now = 1;
  int sum = 0;
  rep(i, s.size()) {
    int u = s.size() - 1 - i;
    sum += a[u] * now;
    sum %= 2019;
    now = now * 10;
    now %= 2019;
    mp[sum]++;
  }

  for (auto u : mp) {
    if (u.fi == 0)
      ans += u.se;
    ans += (u.se - 1) * (u.se) / 2;
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, s, n) for (int i = s; i < n; i++)
#define fi first
#define se second
#define all(c) c.begin(), c.end()
typedef long long int ll;
typedef pair<int, int> P;
typedef long double ld;
void Ans(bool x) {
  if (x)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

const ll INF = 1001001001;
const ll mod = 10000000007;

ll k(ll n) {
  ll u = 0;
  while (n > 0) {
    n /= 10;
    u++;
  }
  return u;
}

int main() {
  string s;
  cin >> s;
  ll a[s.size()];
  rep(i, s.size()) {
    if (s[i] == '1') {
      a[i] = 1;
    }
    if (s[i] == '2') {
      a[i] = 2;
    }
    if (s[i] == '3') {
      a[i] = 3;
    }
    if (s[i] == '4') {
      a[i] = 4;
    }
    if (s[i] == '5') {
      a[i] = 5;
    }
    if (s[i] == '6') {
      a[i] = 6;
    }
    if (s[i] == '7') {
      a[i] = 7;
    }
    if (s[i] == '8') {
      a[i] = 8;
    }
    if (s[i] == '9') {
      a[i] = 9;
    }
  }
  ll dp[s.size()];
  int ans = 0;
  rep(i, s.size()) {
    dp[i] = a[i];
    Rep(j, i, s.size()) {
      if (j != i) {
        dp[j] = (dp[j - 1] * 10 + a[j]) % 2019;
      }
      if (dp[j] == 0)
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdint.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using ldouble = long double;

#define repi(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(i, n) for (ll i = 0; i < (n); ++i)

ll MOD = 1000000007;

int ctoi(char c) {
  switch (c) {
  case '0':
    return 0;
  case '1':
    return 1;
  case '2':
    return 2;
  case '3':
    return 3;
  case '4':
    return 4;
  case '5':
    return 5;
  case '6':
    return 6;
  case '7':
    return 7;
  case '8':
    return 8;
  case '9':
    return 9;
  default:
    return -1;
  }
}

int main() {
  ll x = 10;
  uint m10[20];
  rep(i, 20) {
    m10[i] = x;
    x *= x;
    x %= 2019;
  }
  string s;
  cin >> s;
  vector<ll> t(s.size() + 1);
  t[0] = 0;
  vector<int> c(2019);
  rep(i, 2019) c[i] = 0;
  c[0]++;
  for (uint i = 1; i < s.size() + 1; ++i) {
    int md10 = 1;
    rep(j, 20) {
      if ((((i - 1) >> j) & 1) == 1) {
        md10 *= m10[j];
        md10 %= 2019;
      }
    }
    int su = ctoi(s[s.size() - i]);
    t[i] = (t[i - 1] + (su * md10)) % 2019;
    c[t[i]]++;
  }
  ll res = 0;
  rep(i, 2019) res += c[i] * (c[i] - 1) / 2;
  cout << res << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdint.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using ldouble = long double;

int ctoi(char c) {
  switch (c) {
  case '0':
    return 0;
  case '1':
    return 1;
  case '2':
    return 2;
  case '3':
    return 3;
  case '4':
    return 4;
  case '5':
    return 5;
  case '6':
    return 6;
  case '7':
    return 7;
  case '8':
    return 8;
  case '9':
    return 9;
  default:
    return -1;
  }
}

// 正剰余
long long mod(long long a, long long b) {
  return a - (long long)std::floor((double)a / (double)b) * b;
}

#define repi(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
ll MOD = 1000000007;
int dp[200000][2019];
int main() {
  string s;
  cin >> s;
  ull cnt = 0;
  for (int i = 0; i < s.size(); ++i) {
    int suzi = ctoi(s[i]);
    int p = suzi;
    rep(j, 2019) {
      if (p >= 2019)
        p -= 2019;
      dp[i + 1][p] = dp[i][j];
      p += 10;
    }
    dp[i + 1][suzi]++;
    cnt += dp[i + 1][0];
  }
  cout << cnt << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long

int m2019of_ten(int n, vector<int> ten_chart) {
  if (ten_chart[n] != 2) {
    return ten_chart[n];
  } else {
    return m2019of_ten(n - 1, ten_chart);
  }
}

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

  int N = s.size();

  vector<int> left(N + 1, 0), right(N + 2, 0);
  vector<int> ten_chart(N + 2, 2);

  ten_chart[0] = 1;

  for (int i = 0; i < N; i++)
    ten_chart[i + 1] = (ten_chart[i] * 10) % 2019;

  map<int, int> nums;

  for (int i = 0; i < N; i++) {
    left[i + 1] = (left[i] + int(s[i] - '0') * ten_chart[N - i - 1]) % 2019;
  }

  for (int i = 0; i < N + 1; i++) {
    nums[left[i]]++;
  }

  ll ans = 0;
  for (auto it : nums) {
    ll num = it.second;
    ans += num * (num - 1) / 2;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

#define ll long long

int m2019of_ten(int n) {
  int x = 1;
  for (int i = 0; i < n; i++) {
    x *= 10;
    x %= 2019;
  }
  return x;
}

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

  int N = s.size();

  vector<int> left(N + 1, 0), right(N + 2, 0);

  map<int, int> nums;

  for (int i = 0; i < N; i++) {
    left[i + 1] = (left[i] + int(s[i] - '0') * m2019of_ten(N - i - 1)) % 2019;
  }

  for (int i = 0; i < N + 1; i++) {
    nums[left[i]]++;
  }

  for (int i = N + 1; i > 1; i--) {
    right[i - 1] =
        (right[i] + (int(s[i - 2] - '0') * m2019of_ten(N + 1 - i))) % 2019;
  }

  ll ans = 0;
  for (auto it : nums) {
    ll num = it.second;
    ans += num * (num - 1) / 2;
  }

  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>

using namespace std;

int dp[2][2019];

int main() {
  string S;
  cin >> S;
  long long ans = 0;
  for (int i = 0; i < S.size(); ++i) {
    int curr = i % 2;
    int prev = 1 - curr;
    for (int j = 0; j < 2019; ++j) {
      dp[curr][j] = 0;
    }
    int digit = S[i] - '0';
    dp[curr][digit] = 1;
    if (i > 0) {
      for (int j = 0; j < 2019; ++j) {
        dp[curr][(j * 10 + digit) % 2019] += dp[prev][j];
      }
    }
    ans += dp[curr][0];
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <iostream>

using namespace std;

int dp[200000][2019];

int main() {
  string S;
  cin >> S;
  long long ans = 0;
  for (int i = 0; i < S.size(); ++i) {
    int digit = S[i] - '0';
    dp[i][digit] = 1;
    if (i > 0) {
      for (int j = 0; j < 2019; ++j) {
        dp[i][(j * 10 + digit) % 2019] += dp[i - 1][j];
      }
    }
    ans += dp[i][0];
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define rep(i, strt, end) for (unsigned long long i = strt; i < end; i++)

int main() {
  string s;
  cin >> s;
  long long counter = 0; /*
   rep(i,0,s.length() - 3){
       long long jStop = s.length() - i;
       unsigned long long num = 0;
       rep(j,0,jStop){
           if(s.length() <= i+j)break;
           num += s[i+j] - 48;
           num %= 2019;
           if(num == 0){
               counter++;
           }
           num *= 10;
       }
   }*/
  map<int, unsigned long long> array;
  long slth = s.length();
  reverse(s.begin(), s.end());
  array[0] = 1;
  unsigned int ten = 1;
  unsigned int num = 0;
  rep(i, 0, slth) {
    num += ten * (s[i] - 48);
    num %= 2019;
    // cout << num << endl;
    array[num]++;
    ten *= 10;
    ten %= 2019;
  }
  rep(i, 0, 2019) {
    unsigned long long tmp = array[i];
    // if(tmp > 1)cout << i << endl;
    counter += tmp * (tmp - 1) / 2;
  }
  cout << counter << endl;
  //--------------
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, strt, end) for (unsigned long long i = strt; i < end; i++)

int main() {
  string s;
  cin >> s;
  long long counter = 0;
  rep(i, 0, s.length() - 3) {
    long long jStop = s.length() - i;
    unsigned long long num = 0;
    rep(j, 0, jStop) {
      if (s.length() <= i + j)
        break;
      num += s[i + j] - 48;
      num %= 2019;
      if (num == 0) {
        counter++;
      }
      if (922337203685477580 < num)
        break;
      num *= 10;
    }
  }
  cout << counter << endl;
  //--------------
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

const long MOD = 1000000007;
typedef pair<int, int> P;
typedef long long ll;

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

  const int NUM = 2019;

  reverse(s.begin(), s.end());

  ll t = 0;
  ll d = 1;
  ll l[NUM];
  fill(l, l + NUM, 0);
  for (int i = 0; i < s.length(); i++) {
    t = t + d * (s[i] - '0');
    t = t % NUM;
    // cout << t << endl;
    l[t]++;

    d = d * 10;
    d = d % NUM;
  }

  ll ans = 0;
  for (int i = 0; i < NUM; i++) {
    if (i == 0)
      ans = ans + l[i];
    ans = ans + l[i] * (l[i] - 1) / 2;
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

const long MOD = 2019;
typedef pair<int, int> P;
typedef long long ll;
long ans;
map<P, int> m;

void dfs(string v, int s, int e) {
  P se = P(s, e);
  m[se] = 1;

  int len = v.length();
  long base = 1;
  long sum = 0;
  for (int i = len - 1; i >= 0; i--) {
    sum = sum + base * (v[i] - '0') % MOD;
    sum = sum % MOD;
    base = base * 10;
    base = base % MOD;
  }
  if (sum == 0)
    ans++;
  if (v.length() == 4)
    return;

  std::string d1(v);
  std::string d2(v);

  d1.erase(d1.begin());
  d2.erase(d2.begin() + len - 1);

  P p1 = P(s + 1, e);
  P p2 = P(s, e - 1);

  if (m.count(p1) == 0)
    dfs(d1, s + 1, e);
  if (m.count(p2) == 0)
    dfs(d2, s, e - 1);
  return;
}

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

  ans = 0;
  dfs(S, 0, S.length() - 1);

  cout << ans << endl;

  return 0;
}