alexjercan/bugnet huggingface dataset:
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)
#define all(i, n) (i), (i + n)

int dx4[4] = {1, 0, -1, 0};
int dy4[4] = {0, -1, 0, 1};
int dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1};
int dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dx9[9] = {0, 1, 0, -1, 1, -1, 1, 0, -1};
int dy9[9] = {0, 1, 1, 1, 0, 0, -1, -1, -1};

typedef pair<int, int> P;
typedef pair<string, int> SP;
typedef long long ll;
typedef pair<ll, ll> PLL;

const int INF = 1e9;
const ll LLINF = 1e18;
const int MAX_V = 1e6 + 1;
const ll mod = 1000000007;

// --------------------------------------
ll val[2100];
string s;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> s;
  ll ten = 1;
  ll res = 0;
  ll n = 0;
  val[0] = 1;
  for (int i = s.length() - 1; i >= 0; i--) {
    n = (((s[i] - '0') * ten) % 2019 + n) % 2019;
    res += val[n];
    val[n]++;
    ten = ten * 10 % 2019;
  }

  cout << res << endl;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)
#define all(i, n) (i), (i + n)

int dx4[4] = {1, 0, -1, 0};
int dy4[4] = {0, -1, 0, 1};
int dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1};
int dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dx9[9] = {0, 1, 0, -1, 1, -1, 1, 0, -1};
int dy9[9] = {0, 1, 1, 1, 0, 0, -1, -1, -1};

typedef pair<int, int> P;
typedef pair<string, int> SP;
typedef long long ll;
typedef pair<ll, ll> PLL;

const int INF = 1e9;
const ll LLINF = 1e18;
const int MAX_V = 1e6 + 1;
const ll mod = 1000000007;

// --------------------------------------
string s;
int dp[200005][2019];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> s;

  REP(i, s.length()) {
    int s_l = s[i] - '0';
    REP(j, 2019) { dp[i + 1][(j * 10 + s_l) % 2019] += dp[i][j]; }
    dp[i + 1][s_l] += 1;
  }

  int ans = 0;
  FOR(i, 1, s.length() + 1) { ans += dp[i][0]; }

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  cin >> s;
  int n = s.length();
  vector<ll> v1(2019, 0);
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    vector<ll> v2(2019);
    int cur = s[i - 1] - '0';
    v2[cur]++;
    for (int j = 0; j < 2019; j++) {
      ll a = v1[j];
      v2[(10 * j + cur) % 2019] += a;
    }
    ans += v2[0];
    v1 = vector<ll>(v2.begin(), v2.end());
  }
  cout << ans;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  cin >> s;
  int n = s.length();
  ll dp[n + 1][2222];
  for (int i = 1; i <= n; i++) {
    int cur = s[i - 1] - '0';
    dp[i][cur]++;
    for (int j = 0; j < 2019; j++) {
      ll a = dp[i - 1][j];
      dp[i][(10 * j + cur) % 2019] += a;
    }
  }
  // for(int i=0;i<=n;i++){for(int j=0;j<2019;j++)cout<<dp[i][j]<<'
  // ';cout<<endl;}
  ll ans = 0;
  for (int i = 0; i <= n; i++)
    ans += dp[i][0];
  cout << ans;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
using namespace std;
int main() {
  string S;
  cin >> S;
  int N = S.length();
  int a[N];
  map<int, int> p;
  int r = 0;
  for (int i = 0; i < 2019; i++) {
    p[i] = 0;
  }
  p[0]++;
  int ten = 1;
  int amari[N + 1] = {};
  for (int i = N - 1; i >= 0; i--) {
    a[i] = S[i] - '0';

    r = ten * a[i] % 2019;
    r = (amari[i + 1] + r) % 2019;
    ten = (ten * 10) % 2019;
    p[r]++;
    amari[i] = r;
  }
  int count = 0;
  for (int i = 0; i < 2019; i++) {
    count += p[i] * (p[i] - 1) / 2;
  }
  cout << count << endl;
  return 0;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  string S;
  cin >> S;
  int N = S.length();
  int a[N];
  for (int i = 0; i < N; i++) {
    a[i] = S[i] - '0';
  }
  int count = 0;
  for (int i = 0; i < N; i++) {
    int r = 0;
    for (int j = i; j < N; j++) {
      r = r * 10;
      r += a[j];
      r = r % 2019;
      if (r == 0) {
        count++;
      }
    }
  }
  cout << count << endl;
  return 0;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define mp makepair
#define deb(x) cout << x << "\\n";
#define deB(x, y) cout << x << " " << y << "\\n";
#define Deb(x, y, z) cout << x << " " << y << " " << z << "\\n"
#define YES cout << "YES\\n";
#define Yes cout << "Yes\\n";
#define NO cout << "NO\\n";
#define No cout << "No\\n";
#define all(v) (v).begin(), (v).end()
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  string s;
  cin >> s;
  ll i = 0, ans = 0, j, val;
  ll dp[2020] = {}, a[2020] = {};
  while (i < s.size()) {
    a[s[i] - '0'] = 1;
    j = 0;
    while (j < 2019) {
      val = ((j * 10) + s[i] - '0') % 2019;
      a[val] += dp[j];
      j++;
    }
    ans += a[0];
    j = 0;
    while (j < 2019) {
      dp[j] = a[j];
      a[j] = 0;
      j++;
    }
    i++;
  }
  deb(ans);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define mp makepair
#define deb(x) cout << x << "\\n";
#define deB(x, y) cout << x << " " << y << "\\n";
#define Deb(x, y, z) cout << x << " " << y << " " << z << "\\n"
#define YES cout << "YES\\n";
#define Yes cout << "Yes\\n";
#define NO cout << "NO\\n";
#define No cout << "No\\n";
#define all(v) (v).begin(), (v).end()
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  string s;
  cin >> s;
  ll val, i = 0, ans = 0;
  unordered_map<ll, ll> M, m;
  while (i < s.size()) {
    if (i % 2) {
      M.clear();
      M[s[i] - '0']++;
      for (auto x : m) {
        val = ((10 * x.ff % 2019) + s[i] - '0') % 2019;
        M[val] += x.ss;
      }
      ans += M[0];
    } else {
      m.clear();
      m[s[i] - '0']++;
      for (auto x : M) {
        val = ((10 * x.ff % 2019) + s[i] - '0') % 2019;
        m[val] += x.ss;
      }
      ans += m[0];
    }
    i++;
  }
  deb(ans);
}
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 all(v) v.begin(), v.end()
#define int long long
#define _GLIBCXX_DEBUG

signed main() {
  string S;
  cin >> S;
  int ans = 0;
  int N = S.size();

  vector<int> ten(N);
  ten[0] = 1;
  rep(i, N - 1) ten[i + 1] = (ten[i] * 10) % 2019;

  vector<int> amari(N, 0);
  unordered_map<int, int> map;
  amari[N - 1] = S[N - 1] - '0';

  for (int i = N - 2; i >= 0; i--) {
    amari[i] = (S[i] - '0') * ten[N - i - 1];
    amari[i] += amari[i + 1];
    amari[i] %= 2019;
  }

  map[0]++;
  rep(i, N) { map[amari[i]]++; }

  for (auto p : map) {
    int g = p.second;
    ans += g * (g - 1) / 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 all(v) v.begin(), v.end()
#define int long long
#define _GLIBCXX_DEBUG

signed main() {
  string S;
  cin >> S;
  int ans = 0;
  int N = S.size();

  vector<int> amari(N);
  amari[0] = 1;
  rep(i, N - 1) amari[i + 1] = (amari[i] * 10) % 2019;

  for (int i = N - 1; i >= 3; i--) {
    int h = 0;
    for (int j = i; j >= 0; j--) {
      h += amari[i - j] * (S[j] - '0');
      h %= 2019;
      if (h == 0)
        ans++;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
unsigned short F[200000][2019] = {};
int main() {
  string S;
  cin >> S;
  int i, r, s, N = S.size(), R[10][2019] = {};
  for (r = 0; r < 2019; r++) {
    for (i = 1; i < 10; i++) {
      for (s = 0; s < 2019; s++) {
        if ((10 * s + i) % 2019 == r) {
          R[i][r] = s;
          break;
        }
      }
    }
  }
  for (i = 0; i < N; i++) {
    F[i][S[i] - '0'] = 1;
  }
  for (i = 1; i < N; i++) {
    for (r = 0; r < 2019; r++) {
      F[i][r] += F[i - 1][R[S[i] - '0'][r]];
    }
  }
  long ans = 0;
  for (i = 0; i < N; i++) {
    ans += F[i][0];
  }
  cout << ans << "\\n";
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int F[200000][2019] = {};
int main() {
  string S;
  cin >> S;
  int i, r, s, N = S.size(), R[10][2019] = {};
  for (r = 0; r < 2019; r++) {
    for (i = 1; i < 10; i++) {
      for (s = 0; s < 2019; s++) {
        if ((10 * s + i) % 2019 == r) {
          R[i][r] = s;
          break;
        }
      }
    }
  }
  for (i = 0; i < N; i++) {
    F[i][S[i] - '0'] = 1;
  }
  for (i = 1; i < N; i++) {
    for (r = 0; r < 2019; r++) {
      F[i][r] += F[i - 1][R[S[i] - '0'][r]];
    }
  }
  long ans = 0;
  for (i = 0; i < N; i++) {
    ans += F[i][0];
  }
  cout << ans << "\\n";
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  string s;
  cin >> s;
  ll ans = 0;
  int n = s.size();
  vector<ll> v(2020);
  rep(i, n) {
    int now = (int)(s[i] - '0');
    vector<ll> n = v;
    rep(j, 2019) {
      if (v[j] == 0)
        continue;
      ll next = (j * 10 + now) % 2019;
      n[next] += v[j];
      n[j] -= v[j];
      // if(i<5) cout << i << " " << j << " " << next << " " << v[next] << " "
      // << v[j] << endl;
    }
    v = n;
    v[now]++;
    ans += v[0];
  }
  // rep(i, 2019) cout << i << " :" << v[i] << endl;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  string s;
  cin >> s;
  ll ans = 0;
  int n = s.size();
  vector<vector<ll>> dp(n, vector<ll>(n));
  vector<ll> v(2020);
  rep(i, n) {
    int now = (int)(s[i] - '0');
    vector<ll> n = v;
    rep(j, 2019) {
      if (v[j] == 0)
        continue;
      ll next = (j * 10 + now) % 2019;
      n[next] += v[j];
      n[j] -= v[j];
      // if(i<5) cout << i << " " << j << " " << next << " " << v[next] << " "
      // << v[j] << endl;
    }
    v = n;
    v[now]++;
    ans += v[0];
  }
  // rep(i, 2019) cout << i << " :" << v[i] << endl;
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

using namespace std;

typedef long long ll;
typedef long double ld;

void solve() {
  string s;
  cin >> s;
  int n = int(s.size());
  vector<int> counter(2019, 0);
  vector<int> newcounter(2019, 0);
  ll ans = 0;
  for (int i = n - 1; i >= 0; --i) {
    newcounter.assign(2019, 0);
    newcounter[0] = 1;
    if (i == n - 1) {
      counter = newcounter;
      continue;
    }
    for (int j = 0; j < 2019; ++j) {
      int newmod = (j * 10 + s[i + 1] - '0') % 2019;
      newcounter[j] += counter[newmod];
    }
    ans += newcounter[s[i] - '0'];
    counter = newcounter;
  }
  cout << ans << "\\n";
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

using namespace std;

typedef long long ll;
typedef long double ld;

void solve() {
  string s;
  cin >> s;
  int n = int(s.size());
  vector<vector<int>> counter(n, vector<int>(2019, 0));
  ll ans = 0;
  for (int i = n - 1; i >= 0; --i) {
    counter[i][0] = 1;
    if (i == n - 1)
      continue;
    for (int j = 0; j < 2019; ++j) {
      int newmod = (j * 10 + s[i + 1] - '0') % 2019;
      counter[i][j] += counter[i + 1][newmod];
    }
    ans += counter[i][s[i] - '0'];
  }
  cout << ans << "\\n";
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vin = vector<int>;
using vll = vector<long long>;
using vvin = vector<vector<int>>;
using vvll = vector<vector<long long>>;
using vstr = vector<string>;
using vvstr = vector<vector<string>>;
using vch = vector<char>;
using vvch = vector<vector<char>>;
using vbo = vector<bool>;
using vvbo = vector<vector<bool>>;
using vpii = vector<pair<int, int>>;
using pqsin = priority_queue<int, vector<int>, greater<int>>;
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define decp(n) cout << fixed << setprecision((int)n)
#define _GLIBCXX_DEBUG
const int inf = 1e9 + 7;

int main() {
  string s;
  cin >> s;
  int ss = s.size();
  vvin dp(2, vin(2019));
  rep(i, 2019) dp[0][i] = 0;
  dp[0][s[0] - '0']++;
  int ans = 0;
  rep(i, ss - 1) {
    int tmp = (i + 1) % 2;
    dp[tmp][s[i + 1] - '0']++;
    rep(j, 2019) {
      dp[tmp][(10 * j + (s[i + 1] - '0')) % 2019] += dp[(tmp + 1) % 2][j];
    }
    ans += dp[tmp][0];
    rep(i, 2019) dp[(tmp + 1) % 2][i] = 0;
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vin = vector<int>;
using vll = vector<long long>;
using vvin = vector<vector<int>>;
using vvll = vector<vector<long long>>;
using vstr = vector<string>;
using vvstr = vector<vector<string>>;
using vch = vector<char>;
using vvch = vector<vector<char>>;
using vbo = vector<bool>;
using vvbo = vector<vector<bool>>;
using vpii = vector<pair<int, int>>;
using pqsin = priority_queue<int, vector<int>, greater<int>>;
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define decp(n) cout << fixed << setprecision((int)n)
#define _GLIBCXX_DEBUG
const int inf = 1e9 + 7;

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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long int ll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const ll mod = 1e9 + 7;

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

  string s;
  cin >> s;
  ll n = s.length();
  vector<ll> dp1(2019, 0), dp2(2019, 0);
  dp1[s[0] - '0'] = 1;
  ll ans = 0;
  for (int i = 1; i < n; i++) {
    ll num = s[i] - '0';
    dp2[num] = 1;
    for (int j = 0; j < 2019; j++) {
      dp2[(10 * j + num) % 2019] += dp1[j];
    }
    ans += dp2[0];
    dp1 = dp2;
    dp2.assign(2019, 0);
  }

  cout << ans << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long int ll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const ll mod = 1e9 + 7;

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

  string s;
  cin >> s;
  ll n = s.length();
  vector<vector<ll>> dp(n, vector<ll>(2019, 0));
  vector<ll> dp1(2019, 0), dp2(2019, 0);
  dp1[s[0] - '0'] = 1;
  ll ans = 0;
  for (int i = 1; i < n; i++) {
    ll num = s[i] - '0';
    dp2[num] = 1;
    for (int j = 0; j < 2019; j++) {
      dp2[(10 * j + num) % 2019] += dp1[j];
    }
    ans += dp2[0];
    dp1 = dp2;
    dp2.assign(2019, 0);
  }

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

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

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

void Main() {
  string S;
  cin >> S;

  long long mod[2019];
  memset(mod, 0, sizeof(mod));
  mod[0] = 1;
  long long tmp = 0;
  for (int i = S.length() - 1; i >= 0; i--) {
    tmp = (tmp + modpow(10, S.length() - i - 1, 2019) * (S[i] - '0')) % 2019;
    mod[tmp]++;
  }

  /*
  for (int i = 0; i < 2019; i++) {
    if (mod[i]) cout << i << " " << mod[i] << endl;
  }
  return;
  */

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

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


Error: TLE

Fixed submission:

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

void Main() {
  string S;
  cin >> S;

  long long ans = 0;
  for (int i = 0; i < S.size() - 4; i++) {
    int tmp = 0;
    for (int j = i; j < S.size(); j++) {
      tmp *= 10;
      tmp += S[j] - '0';
      tmp %= 2019;
      if (tmp == 0)
        ans++;
    }
  }
  cout << ans << endl;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

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

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

  map<int, int> mp;
  mp[0]++;

  ll num = 0, ten = 1;
  for (auto &&x : s) {
    num = (num + (x - '0') * ten) % 2019;
    ten = ten * 10 % 2019;
    mp[num]++;
  }

  ll ans = 0;
  for (auto &&x : mp) {
    int a = x.second;
    ans += a * (a - 1) / 2;
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

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

int DP(int n, string &s, map<int, int> &mods, int size) {
  if (n == size)
    return 0;

  int a =
      (DP(n + 1, s, mods, size) + (s[n] - '0') * nplex(size - n - 1)) % 2019;
  mods[a]++;
  return a;
}

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

  int size = s.size();
  map<int, int> mods;
  mods[0]++;

  DP(0, s, mods, size);

  ll ans = 0;
  for (auto &&x : mods) {
    int a = x.second;
    ans += a * (a - 1) / 2;
  }

  cout << ans << endl;

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

#include <cstring>
#include <iostream>

using namespace std;
int dp[2019];
string s;

int main() {
  cin >> s;
  int len = s.size();
  memset(dp, 0, sizeof(dp));
  dp[s[0] - '0'] = 1;
  long long ans = 0;
  for (int i = 1; i < len; i++) {
    int tmp[2019];
    memset(tmp, 0, sizeof(tmp));
    int d = s[i] - '0';
    tmp[d] = 1;
    for (int j = 0; j < 2019; j++) {
      int r = (j * 10 + d) % 2019;
      if (r == 0)
        ans += dp[j];
      tmp[r] += dp[j];
    }
    for (int j = 0; j < 2019; j++)
      dp[j] = tmp[j];
  }
  cout << ans << endl;
}


Error: -11

Fixed submission:

#include <cstring>
#include <iostream>

using namespace std;
#define ll long long
const int MAX = 2e6 + 111;
int n;
ll dp[MAX][32], a[32], b[MAX];
string s;

bool check(int j) {
  if ((j & (j - 1)) == 0)
    return true;
  else
    return false;
}

int main() {
  a[0] = b[0] = 1;
  for (int i = 1; i < 32; i++) {
    a[i] = a[i - 1] * 2;
  }
  for (int i = 1; i < MAX; i++)
    b[i] = (b[i - 1] * 10) % 2019;
  ll ans = 0;
  cin >> s;
  int len = s.size();
  for (int i = 0; i < len; i++)
    dp[i][0] = s[i] - '0';
  for (int j = 1; j < 32; j++) {
    for (int i = 0; i + (1 << j) - 1 < len; i++) {
      dp[i][j] =
          (b[a[j - 1]] * dp[i][j - 1] % 2019 + dp[i + a[j - 1]][j - 1]) % 2019;
      if (dp[i][j] == 0)
        ans++;
    }
  }
  for (int j = 5; j < len; j++) {
    if (check(j))
      continue;
    for (int i = 0; i + j - 1 < len; i++) {
      int p = j, c = 0;
      ll res = 0;
      for (int k = 0; p; k++, p /= 2) {
        if (p % 2) {
          res = ((res + b[j - c - a[k]] * dp[i + c][k]) % 2019) % 2019;
          c += a[k];
        }
      }
      if (res == 0)
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

int main(void) {
  string s;
  cin >> s;
  reverse(begin(s), end(s));

  ll cnt[2019] = {};
  ll num = 0;
  ll d = 1;
  cnt[0] = 1;
  for (char &c : s) {
    num += (c - '0') * d;
    num %= 2019;

    d *= 10;
    d %= 2019;

    cnt[num]++;
  }

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

  cout << ans;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

int cnt = 0;

void chk(string s) {
  if (s.length() > 9) {
    int x = atoi(s.substr(0, 9).c_str());

    int a = x % 2019;
    if (a == 0) {
      chk(s.substr(9, s.length() - 8));
    } else {
      chk(to_string(a) + s.substr(9, s.length() - 8));
    }
  } else {
    int x = atoi(s.c_str());
    if (x % 2019 == 0) {
      cnt++;
    }
  }
}

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

  for (int i = 1; i <= (int)s.length(); i++) {
    for (int j = 0; j < i; j++) {
      chk(s.substr(j, i - j));
    }
  }

  cout << cnt;

  return 0;
}