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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #define int long long

template <class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }
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;
}

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

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

  string S;
  cin >> S;

  ll P = 2019, N = (ll)S.length();

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

  return 0;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define int long long

template <class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }
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;
}

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

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

  string s;
  cin >> s;

  ll ans = 0;
  REP(i, (int)s.length()) {
    FOR(j, 4, (int)s.length() - i + 1) {
      // cout << s.substr(i, j) << endl;
      if (stoll(s.substr(i, j)) % 2019 == 0)
        ans++;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define REP(i, m, n) for (int i = (int)(m); i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
typedef long long ll;

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

const long long INF = 1LL << 60;
const int MAX = 200100;
const int MOD = 2019;

int dp[MOD];
int dpdp[MOD];

int main() {
  string s;
  cin >> s;
  int n = s.size();
  vector<int> a(n + 1);
  a[0] = 0;
  rep(i, n) a[i + 1] = s[i] - '0';

  int ans = 0;
  REP(i, 1, n) {
    fill(dpdp, dpdp + MOD, 0);
    REP(j, 0, 2018) { dpdp[(j * 10 + a[i]) % MOD] += dp[j]; }
    dpdp[a[i]]++;
    REP(j, 0, 2018) { dp[j] = dpdp[j]; }
    ans += dp[0];
  }

  cout << ans << endl;

  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define REP(i, m, n) for (int i = (int)(m); i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
typedef long long ll;

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

const long long INF = 1LL << 60;
const int MAX = 200100;
const int MOD = 2019;

int dp[MAX][MOD];

int main() {
  string s;
  cin >> s;
  int n = s.size();
  vector<int> a(n + 1);
  a[0] = 0;
  rep(i, n) a[i + 1] = s[i] - '0';

  int ans = 0;
  REP(i, 1, n) {
    REP(j, 0, 2018) { dp[i][(j * 10 + a[i]) % MOD] += dp[i - 1][j]; }
    dp[i][a[i]]++;
    ans += dp[i][0];
  }

  cout << ans << endl;

  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  string s;
  cin >> s;
  int v = 2019, pow = 1, val = 0;
  int n = s.size();
  map<int, int> m;
  int ans = 0;
  m[0]++;
  for (int i = n - 1; ~i; i--) {
    int k = s[i] - '0';
    val = (val + (k * pow)) % 2019;
    ans += m[val];
    m[val]++;
    pow = pow * 10 % 2019;
  }
  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
int main() {
  string s;
  cin >> s;
  int n = s.size();
  int ans = 0;
  map<ll, int> m;
  m[0] = 1;
  for (int i = n - 1; i >= 0; i--) {
    string tmp = s.substr(i, n - i);
    ll conv;
    if (tmp.size() <= (ll)1e18)
      conv = stoll(tmp);
    else
      continue;
    for (auto it : m) {
      if ((conv - it.first) % 2019 == 0)
        ans++;
    }
    m[conv % 2019] = 1;
  }
  cout << ans;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  string S;
  cin >> S;
  int l = S.size();

  long long mod[2019] = {0};
  mod[0]++;
  long long tmp = 0;
  long long n = 1;
  long long ans = 0;

  for (long long i = 0; i < l; i++) {
    long long add = S[l - i - 1] - '0';
    tmp += add * n % 2019;
    mod[tmp % 2019]++;
    n = n * 10 % 2019;
  };

  for (int i = 0; i < 2019; i++) {
    ans += (mod[i] * (mod[i] - 1) / 2);
  };

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

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

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

  long long l = S.size();
  long long mod[2019] = {0};
  mod[0]++;
  long long tmp = 0;
  long long ans = 0;

  for (long long i = 0; i < l; i++) {
    long long tmp_num = (int)S[l - i - 1] - '0';
    long long n = pow(10.0, (float)i);
    tmp += tmp_num * n % 2019;
    mod[tmp % 2019] += 1;
  };

  for (int i = 0; i < 2019; i++) {
    if (mod[i] <= 1)
      continue;
    else
      ans += (mod[i] * (mod[i] - 1) / 2);
  };

  cout << ans << endl;

  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define SUM(x) accumulate(ALL(x), 0)
#define INF 1000000000000
#define MOD 10000007
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define MAXR 100000

ll solve(const string &S) {
  int N = SIZE(S);
  vector<ll> val(2019, 0);
  ll fac = 1;
  ll cur = 0;
  val[cur]++;
  REP(i, N) {
    ll add = S[N - 1 - i] - '0';
    cur = (cur + fac * add) % 2019;
    fac = (fac * 10) % 2019;
    val[cur]++;
  }
  ll res = 0;
  REP(i, SIZE(val)) { res += val[i] * (val[i] - 1) / 2; }
  return res;
}

int main() {
  string S;
  cin >> S;
  cout << solve(S) << endl;
  ;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define SUM(x) accumulate(ALL(x), 0)
#define INF 1000000000000
#define MOD 10000007
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define MAXR 100000

int main() {
  string s;
  cin >> s;
  int n = SIZE(s);
  int cnt = 0;

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

  REP(i, n - 3) {
    REP(j, n) {
      int end = j + 4;
      if (i + end - 1 == n)
        break;
      string s_ = s.substr(i, end);
      long double l_ = stold(s_);
      if (l_ == 0)
        continue;
      if (fmod(l_, 2019) == 0)
        cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

using namespace std;

typedef long long ll;

int dp[2019] = {0};

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

  ll ans = 0;
  for (int i = 0; i < s.length(); i++) {
    int next[2019] = {0};
    next[s[i] - '0'] += 1;

    if (i > 0) {
      for (int j = 0; j < 2019; j++) {
        next[(j * 10 + (s[i] - '0')) % 2019] += dp[j];
      }
    }
    ans += next[0];
    for (int i = 0; i < 2019; i++) {
      dp[i] = next[i];
    }
  }
  cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

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

using namespace std;

typedef long long ll;

int dp[200000][2019] = {0};

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

  ll ans = 0;
  for (int i = 0; i < s.length(); i++) {
    dp[i][s[i] - '0'] += 1;

    if (i == 0)
      continue;
    for (int j = 0; j < 2019; j++) {
      dp[i][(j * 10 + (s[i] - '0')) % 2019] += dp[i - 1][j];
    }
    ans += dp[i][0];
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

ll pow(ll x, ll y, ll mod) {
  if (y == 0) {
    return 1;
  }
  if (y % 2 == 0) {
    return pow(x * x % mod, y / 2, mod) % mod;
  } else {
    return x * pow(x, y - 1, mod) % mod;
  }
}

void solve() {
  string S;
  cin >> S;
  ll N = S.size();
  vector<ll> M(N + 1, 0);
  reverse(S.begin(), S.end());
  rep(i, N) {
    ll d = S[i] - '0';
    M[i + 1] = (M[i] + d * pow(10, i, 2019)) % 2019;
  }
  map<ll, ll> cnt;
  ll ans = 0;
  rep(i, N + 1) {
    ll a = M[i];
    ans += cnt[a];
    cnt[a]++;
  }
  cout << ans << '\\n';
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

vector<ll> divisor(ll N) {
  vector<ll> res;
  for (ll i = 1L; i * i <= N; ++i) {
    if (N % i == 0) {
      res.push_back(i);
      ll j = N / i;
      if (j != i) {
        res.push_back(j);
      }
    }
  }
  sort(res.begin(), res.end());
  return res;
}

void solve() {
  string S;
  cin >> S;
  ll N = S.size();
  vector<vector<ll>> M(N, vector<ll>(N + 1, 0));
  ll ans = 0;
  rep(i, N) {
    rep2(j, i, N) {
      ll d = S[j] - '0';
      M[i][j + 1] = ((M[i][j] * 10) + d) % 2019;
    }
    rep2(j, i + 1, N + 1) {
      if (M[i][j] == 0)
        ans++;
    }
  }
  cout << ans << '\\n';
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

long long ans, p, cur, t, cnt[200010];
string s;

int main() {
  cin >> s;
  p = 2019;
  cnt[0] = 1, t = 1;
  for (int i = s.size() - 1; i >= 0; i--) {
    cur = (cur + t * (s[i] - '0') % p) % p;
    t = t * 10 % p;
    ans += cnt[cur]++;
  }

  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  string s;
  cin >> s;
  int res = 0;

  for (int i = 0; i < s.size() - 4; i++) {
    int x = 0;
    for (int j = i; j <= i + 4; j++)
      x = x * 10 + s[j] - '0';
    if (x % 2019 == 0)
      res++;
  }

  cout << res << endl;

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

#include <bits/stdc++.h>

using namespace std;
#define sd(x) scanf("%d", &x);
#define sdl(x) scanf("%lld", &x);
#define pr(x) printf("%d\\n", x);
#define prl(x) printf("%lld\\n", x);

unordered_map<int, unordered_map<int, int>> dp;
unordered_map<int, unordered_map<int, int>> done;
string s;

long long fast_exp(long long a, long long n, long long mod1) {

  if (n == 0)
    return 1;
  else if (n == 1)
    return a;
  if (n % 2 == 0)
    return fast_exp((a * a) % mod1, n / 2, mod1);
  else
    return (a * fast_exp((a * a) % mod1, n / 2, mod1)) % mod1;
}
bool is_square(long long a) {
  long long int b = sqrt(a);
  if (b * b == a)
    return true;
  else
    return false;
}

void solve() {
  cin >> s;
  long long dp[200001];
  unordered_map<int, int> arr;
  long long till_now = 0;
  long long res = 0;
  arr[0] = 1;
  for (int c = 0; c < s.size(); c++) {
    till_now = (till_now * 10 + s[c] - '0') % 2019;
    dp[c] = arr[(till_now * fast_exp(202, c, 2019)) % 2019];

    arr[(till_now * fast_exp(202, c, 2019)) % 2019]++;
    res += dp[c];
    //  cout << till_now << " " << (till_now*fast_exp(202,c,2019))%2019 << endl;
  }
  cout << res << endl;
}

int main() {

  int t;
  // cin >> t;
  t = 1;

  for (int tt = 1; tt <= t; tt++) {
    solve();
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define sd(x) scanf("%d", &x);
#define sdl(x) scanf("%lld", &x);
#define pr(x) printf("%d\\n", x);
#define prl(x) printf("%lld\\n", x);

unordered_map<int, unordered_map<int, int>> dp;
unordered_map<int, unordered_map<int, int>> done;
string s;
void calc(int n, int m) {
  if (n == 0) {
    if (m == s[0] - '0')
      dp[0][s[0] - '0'] = 1;
    done[n][m] = 1;
    return;
  }
  if (done[n][m] != 0)
    return; // m-1  dp[n-1][x]  x*10+y)%==m-1
  done[n][m] = 1;
  calc(n - 1, (((m - (s[n] - '0') + 2019) % 2019) * 202) % 2019),
      dp[n][m] = dp[n - 1][(((m - (s[n] - '0') + 2019) % 2019) * 202) % 2019] +
                 (m == s[n] - '0' ? 1 : 0);
  return;
}
void solve() {
  cin >> s;
  if (s.size() < 4)
    cout << 0 << endl;
  else {
    for (int c = 3; c < s.size(); c++)
      calc(c, 0);
    long long tot = 0;
    for (int c = 0; c < s.size(); c++)
      tot += dp[c][0];
    cout << tot << endl;
  }
}

int main() {

  int t;
  // cin >> t;
  t = 1;

  for (int tt = 1; tt <= t; tt++) {
    solve();
  }

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

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

string s;
long long f[2019];
long long p = 1;
long long ans;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> s;
  f[0]++;
  long long x = 0;
  for (int i = s.size() - 1; i >= 0; i--) {
    x = ((s[i] - '0') * p + x) % 2019;
    ans += f[x];
    f[x]++;
    p = (p * 10) % 2019;
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

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

int mod(string x) {
  vector<int> v;
  int m = 0;
  for (int i = 0; i < x.size(); i++) {
    m = m * 10 + (x[i] - '0');
    m = m % 673;
  }
  return m;
}
int sum[200001];
string s;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> s;
  sum[0] = s[0] - '0';
  for (int i = 1; i < s.size(); i++) {
    sum[i] = sum[i - 1] + s[i] - '0';
  }
  int n = s.size();
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 3; j < n; j++) {
      if (i == 0) {
        if (sum[j] % 3 == 0 && mod(s.substr(i, j - i + 1)) == 0)
          ans++;
      } else if ((sum[j] - sum[i - 1]) % 3 == 0 &&
                 mod(s.substr(i, j - i + 1)) == 0)
        ans++;
    }
  }
  cout << ans;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF (1 << 29)

using namespace std;

int main() {
  long long a[2020], ans = 0;
  string s;
  cin >> s;

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

  int t = 0, p = 1;

  for (int i = 0; i < 2020; i++)
    a[i] = 0;
  a[0] = 1;

  for (int i = 0; i < (int)s.size(); i++) {
    t += (s[i] - '0') * p;
    t %= 2019;
    p *= 10;
    p %= 2019;
    a[t]++;
  }

  for (int i = 0; i < 2019; i++) {
    ans += a[i] * (a[i] - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF (1 << 29)

using namespace std;

int main() {
  int a[2020], ans = 0;
  string s;
  cin >> s;

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

  for (int i = 0; i < 2020; i++)
    a[i] = 0;

  for (int i = 0; i < (int)s.size(); i++) {
    string sub = s.substr(0, i + 1);
    reverse(sub.begin(), sub.end());
    // cout << sub << endl;
    long long n = stoll(sub);
    n %= 2019;
    // cout << n << endl;
    if (n == 0)
      ans++;
    a[n]++;
  }

  for (int i = 0; i < 2019; i++) {
    ans += a[i] * (a[i] - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

typedef long long ll;

int main() {
  string s;
  cin >> s;
  int n = s.size(), a = 0, b = 1, r[2019];
  rep(i, 2019) r[i] = 0;
  rep(i, n) {
    a = (a + (s[n - 1 - i] - '0') * b) % 2019;
    b = (b * 10) % 2019;
    r[a]++;
  }
  ll res = r[0];
  rep(i, 2019) res += r[i] * (r[i] - 1) / 2;
  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

typedef long long ll;

int main() {
  string s;
  cin >> s;
  int n = s.size(), a = 0, b = 1, r[2019];
  rep(i, 2019) r[i] = 0;
  rep(i, n) {
    a = (a + (s[n - 1 - i] - '0') * b) % 2019;
    b = (b * 10) % 2019;
    r[a]++;
  }

  ll res = 0;
  rep(i, n) res += r[i] * (r[i] + 1) / 2;

  cout << res << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef pair<int, int> pi;
typedef pair<lli, lli> plli;
typedef vector<int> vi;
typedef vector<lli> vlli;
typedef vector<pi> vpi;
typedef vector<plli> vplli;
const lli M = 1000000007ll;
#define debug(x) cout << "debugg:: " << (x) << '\\n';

int main() {
  fast_io string s;
  cin >> s;
  int m = 2019;

  vi suf(m, 0);
  suf[0] = 1;

  int pot = 1, val = 0;
  lli ans = 0;
  for (int i = s.size() - 1; i >= 0; --i) {
    val = (val + (s[i] - '0') * pot) % m;
    pot = (pot * 10) % m;
    ans += suf[val];
    suf[val]++;
  }
  cout << ans << '\\n';
  return 0;
}

Error: WA

Fixed submission:

// C++ program to count subsequences of a
// string divisible by n.
#include <cstring>
#include <iostream>
#define fast_io()                                                              \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

using namespace std;

// Returns count of subsequences of str
// divisible by n.
long long countDivisibleSubseq(string str, int n) {
  int len = str.length();

  // division by n can leave only n remainder
  // [0..n-1]. dp[i][j] indicates number of
  // subsequences in string [0..i] which leaves
  // remainder j after division by n.
  long long int dp[len][n];
  memset(dp, 0LL, sizeof(dp));

  long long int count = 0LL;
  dp[0][(str[0] - '0') % n]++;

  for (int i = 1; i < len; i++) {
    dp[i][(str[i] - '0') % n]++;

    for (int j = 0; j < n; j++) {
      dp[i][(j * 10 + (str[i] - '0')) % n] += dp[i - 1][j];
    }
    count += dp[i][0];
  }
  return count;
}

// Driver code
int main() {
  fast_io();
  string str;
  cin >> str;
  int n = 2019;
  cout << countDivisibleSubseq(str, n);
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int cnt[2019];
int ans = 0;

int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  int t = 0;
  int p = 1;
  cnt[0] = 1;
  for (int i = 0; i < s.length(); i++) {
    t = t + p * (s[i] - '0');
    t = t % 2019;
    ans += cnt[t];
    cnt[t]++;
    p *= 10;
    p = p % 2019;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  string s;
  cin >> s;
  int count = 0;
  for (int i = 0; i < s.length() - 3; i++) {
    for (int j = i + 3; j < s.length(); j++) {
      if (atoi(s.substr(i, j + 1 - i).c_str()) % 2019 == 0) {
        count++;
      }
    }
  }
  cout << count << endl;
  return 0;
}