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

#include "bits/stdc++.h"
using namespace std;
const int maxN = 2e5 + 10;
int N;
string S;
int num[maxN], cnt[maxN];
int main() {
  cin >> S;
  N = S.size();
  S = '$' + S;
  int pw = 1;
  for (int i = N; i >= 1; i--) {
    int tmp = S[i] - '0';
    num[i] = (num[i + 1] + pw * tmp) % 2019;
    pw = pw * 10 % 2019;
    //		cout<<num[i]<<endl;
  }
  cnt[0] = 1;
  int ans = 0;
  for (int i = N; i >= 1; i--) {
    ans += cnt[num[i]];
    cnt[num[i]]++;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef short sho;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pld> vpd;

#define LOCAL
#define MP make_pair
#define FF first
#define SS second
#define sz(x) (int)x.size()
#define begin(x) (x).begin()
#define end(x) (x).end()
#define rbegin(x) (x).rbegin()
#define rend(x) (x).rend()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define PF push_front
#define PB push_back
#define PP push
#define lb lower_bound
#define ub upper_bound
#define lowbit(x) x & (-x)
#define testbit(x, i) x & (1 << i)

inline void checkmin(int &x, int y) { x = min(x, y); }
inline void checkmax(int &x, int y) { x = max(x, y); }

const int maxN = 2e5 + 10;
string S;

int a[maxN];

int main() {
#ifndef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  /* start code */
  cin >> S;
  for (int i = 0; i < S.size(); i++)
    a[i] = S[i] - '0';
  long long ans = 0LL;
  for (int i = 0; i < S.size(); i++) {
    int num = 0;
    long long cnt = 0LL;
    for (int j = i; j < S.size(); j++) {
      num = (num * 10 + a[j]) % 2019;
      if (num == 0)
        cnt++;
    }
    ans += cnt;
  }
  cout << ans << endl;
  /* end code */
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long
int main() {
  string S;
  cin >> S;
  reverse(S.begin(), S.end());
  map<int, int> mp;
  ll ans = 0;
  ll sum = 0;
  ll base = 1;
  mp[0] = 1;
  for (char c : S) {
    sum += base * (c - '0');
    sum %= 2019;
    base *= 10;
    base %= 2019;
    if (mp.count(sum))
      ans += mp[sum];
    mp[sum]++;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  string s;
  cin >> s;
  s = '!' + s;
  ll R, r, p, j, i = 1;
  set<ll> a[s.size() + 9];
  ll ans = 0;
  while (i < s.size()) {
    R = 0;
    r = 0;
    p = 0;
    j = i;
    while (j <= s.size()) {
      R *= 10;
      R %= 3;
      R += (ll)(s[j] - '0');
      R %= 3;
      r *= 10;
      r %= 673;
      r += (ll)(s[j] - '0');
      r %= 673;
      if (a[j].find(r) != a[j].end())
        break;
      a[j].insert(r);
      if (r == 0 && R == 0)
        p++;
      j++;
    }
    ans += p * (p + 1) / 2;
    i++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <ctime>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using ppi = pair<pii, int>;

int f_dir[2][4] = {{-1, 0, 1, 0}, {0, 1, 0, -1}}; // ↑→↓←
int e_dir[2][8] = {{-1, -1, -1, 0, 0, 1, 1, 1}, {-1, 0, 1, -1, 1, -1, 0, 1}};
bool is_in_field(int y, int x, int h, int w) {
  return (y >= 0 && y < h && x >= 0 && x < w);
}

int main() {
  // start-----------------------------------------------
  string s;
  cin >> s;
  vector<ll> nows(2020);
  vector<ll> rem(2020, 0);
  ll now = 0;
  ll t = 1;
  rem[0] = 1;
  for (int i = s.length() - 1; i >= 0; i--) {
    now += t * (s[i] - '0');
    now %= 2019;
    rem[now]++;
    t *= 10;
    t %= 2019;
  }
  ll ans = 0;
  for (int i = 0; i < 2019; i++) {
    ans += rem[i] * (rem[i] - 1) / 2;
  }
  cout << ans << endl;

  // end-----------------------------------------------
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <ctime>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using ppi = pair<pii, int>;

int f_dir[2][4] = {{-1, 0, 1, 0}, {0, 1, 0, -1}}; // ↑→↓←
int e_dir[2][8] = {{-1, -1, -1, 0, 0, 1, 1, 1}, {-1, 0, 1, -1, 1, -1, 0, 1}};
bool is_in_field(int y, int x, int h, int w) {
  return (y >= 0 && y < h && x >= 0 && x < w);
}

int main() {
  // start-----------------------------------------------
  string s;
  cin >> s;
  vector<ll> nows(2020);
  vector<ll> rem(2020, 0);
  ll now = 0;
  ll t = 1;
  rem[0] = 1;
  for (int i = s.length() - 1; i >= 0; i--) {
    now += (s[i] - '0') * t;
    now %= 2019;
    nows[i] = now;
    rem[now]++;
    t *= 10;
    t %= 2019;
  }
  ll ans = 0;
  for (int i = 0; i < s.length(); i++) {
    ans += rem[i] * (rem[i] - 1) / 2;
  }
  cout << ans << endl;

  // end-----------------------------------------------
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#ifdef DEBUG
#include <util.h>
#else
#define Dump(x)
#endif

using namespace std;

using ll_t = long long;
using ull_t = unsigned long long;
using ld_t = long double;

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

  vector<int> M(S.size(), 0);
  int k = 1;
  for (int i = 0; i < S.size(); ++i) {
    M[i] = ((int)(S[S.size() - 1 - i] - '0') * k) % 2019;
    if (i > 0) {
      M[i] += M[i - 1];
      M[i] %= 2019;
    }
    k = (k * 10) % 2019;
  }

  unordered_map<int, int> mods;
  for (auto m : M) {
    auto it = mods.insert({m, 1});
    if (not it.second)
      ++it.first->second;
  }

  size_t n(0);
  for (const auto &p : mods) {
    if (p.first == 0)
      n += p.second + (p.second * (p.second - 1) / 2);
    else if (p.second > 1)
      n += p.second * (p.second - 1) / 2;
  }

  cout << n << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ull_t = unsigned long long;

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

  size_t n = 0;
  for (int i = 0; i < S.size() - 3; ++i) {
    int x = 0;
    for (int j = i; j < S.size(); ++j) {
      x = (x * 10 + (int)(S[j] - '0'));
      if (x > 0 and x % 2019 == 0)
        ++n;
      x %= 2019;
    }
  }

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#define ll long long
#define MAX 25 * 1e13
#define N 200010

#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define MP(A, B) make_pair(A, B)
#define PB(X) push_back(X)
#define EB(X) emplace_back(X)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)
#define range(a) a.begin(), a.end()
#define VI vector<int>;

using namespace std;
int MOD = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int lens = (int)s.size();
  vector<vector<ll>> r(2, vector<ll>(2019));

  REP(j, 2019) r[0][j] = 0;
  REP(j, 2019) r[1][j] = 0;

  ll num = 0;
  ll M = 2019;
  ll cnt = 0;
  int odd = 0;
  for (int i = 0; i < lens; i++) {
    odd = (odd + 1) % 2;
    num = num * 10 + s[i] - '0';
    num %= M;

    //    cout << num  << " " << r[1][num] << " " << r[0][num] <<  endl;
    if (odd % 2 == 0) {
      if (num == 0) {
        cnt++;
      }
      cnt += r[0][num];
      r[0][num]++;
    } else {
      if (num == 0) {
        cnt++;
      }
      cnt += r[1][num];
      r[1][num]++;
    }

    if (odd % 2 == 0) { // 0 -> 1
      REP(j, 2019) r[1][j] = 0;
      REP(j, 2019) if (r[0][j] > 0) {
        ll newr = (ll)j * 10;
        newr %= M;
        r[1][newr] = r[0][j];
      }
    } else {
      REP(j, 2019) r[0][j] = 0;
      REP(j, 2019) if (r[1][j] > 0) {
        ll newr = (ll)j * 10;
        newr %= M;
        r[0][newr] = r[1][j];
      }
    }
  }
  cout << cnt << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#define ll long long
#define MAX 25 * 1e13
#define N 200010

#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define MP(A, B) make_pair(A, B)
#define PB(X) push_back(X)
#define EB(X) emplace_back(X)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)
#define range(a) a.begin(), a.end()
#define VI vector<int>;

using namespace std;
int MOD = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int lens = (int)s.size();
  map<int, int> r;
  REP(i, 2019) { r[i] = 0; }
  int num = 0;
  int M = 2019;
  int cnt = 0;
  for (int i = 0; i < lens; i++) {
    num = num * 10 + s[i] - '0';
    num %= M;

    // cout << num  << " " << r[num] << endl;
    if (num == 0) {
      r[num] += 1;
    }

    cnt += r[num];
    r[num]++;

    map<int, int> r2;
    REP(j, 2019) r2[i] = 0;
    REP(j, 2019) if (r[j] > 0) {
      int newr = j * 10;
      newr %= M;
      r2[newr] = r[j];
    }
    swap(r, r2);
  }
  cout << cnt << endl;

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

#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

int main() {
  std::string S;
  std::cin >> S;
  const unsigned int mod = 2019;

  std::vector<unsigned int> dp(S.size());
  unsigned int pow = 10;
  for (unsigned int i = S.size(); i > 0; --i) {
    if (i == S.size()) {
      dp.at(i - 1) = S[i - 1] - '0';
    } else {
      dp.at(i - 1) = (dp.at(i) + (S[i - 1] - '0') * pow) % mod;
      pow = (pow * 10) % mod;
    }
  }

  std::unordered_map<unsigned int, unsigned long> map;
  map.emplace(0, 1);
  for (unsigned int i = 0; i < S.size(); ++i) {
    if (map.count(dp.at(i)) == 0) {
      map.emplace(dp.at(i), 1);
    } else {
      map.at(dp.at(i)) += 1;
    }
  }

  unsigned long ans = 0;
  for (const auto &x : map) {
    const unsigned long num = x.second;
    ans += num * (num - 1) / 2;
  }

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


Error: 0

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

int main() {
  std::string S;
  std::cin >> S;
  const unsigned int mod = 2019;

  std::vector<std::vector<unsigned int>> dp(
      S.size(), std::vector<unsigned int>(S.size(), 0));
  for (unsigned int start = 0; start < S.size(); ++start) {
    dp.at(start).at(0) = (S[start] - '0') % mod;
  }

  unsigned long ans = 0;

  for (unsigned int keta = 1; keta < S.size(); ++keta) {
    for (unsigned int start = 0; start < S.size() - keta; ++start) {
      dp.at(start).at(keta) =
          (dp.at(start).at(keta - 1) * 10 + (S[start + keta] - '0')) % mod;
      if (dp.at(start).at(keta) == 0) {
        ans += 1;
      }
    }
  }

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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(x) (x).begin(), (x).end()

#define READ(x) (cin >> (x))

#define WRITE_N(x) (cout << (x) << endl)
#define WRITE(x) (cout << (x))

#define DIV 2019

vector<vector<int>> mul10_mod(DIV, vector<int>(9, -1));

int get_mul10_mod(int x, int y) {
  if (mul10_mod[x][y - 1] == -1) {
    mul10_mod[x][y - 1] = (x * 10 + y) % DIV;
  }

  return mul10_mod[x][y - 1];
}

int main() {
  // get values from input
  cin.tie(0);
  ios::sync_with_stdio(false);

  string S;
  READ(S);

  // main procedure
  size_t len_S = S.size();

  vector<int> digits_vec(len_S);
  for (size_t i = 0; i < len_S; ++i) {
    digits_vec[i] = S[i] - '0';
  }

  ll ans = 0;

  vector<ll> mod_count_map(DIV, 0);
  for (size_t i = 0; i < len_S; ++i) {
    vector<ll> tmp_map = mod_count_map;
    mod_count_map = vector<ll>(DIV, 0);

    for (size_t j = 0; j < DIV; ++j) {
      mod_count_map[get_mul10_mod(j, digits_vec[i])] += tmp_map[j];
    }
    mod_count_map[digits_vec[i]]++;

    ans += mod_count_map[0];
  }

  // output
  WRITE_N(ans);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(x) (x).begin(), (x).end()

#define READ(x) (cin >> (x))

#define WRITE_N(x) (cout << (x) << endl)
#define WRITE(x) (cout << (x))

#define DIV 2019

vector<vector<int>> mul10_mod(DIV, vector<int>(9, -1));

int get_mul10_mod(int x, int y) {
  if (mul10_mod[x][y - 1] == -1) {
    mul10_mod[x][y - 1] = (x * 10 + y) % DIV;
  }

  return mul10_mod[x][y - 1];
}

int main() {
  // get values from input
  cin.tie(0);
  ios::sync_with_stdio(false);

  string S;
  READ(S);

  // main procedure
  size_t len_S = S.size();

  vector<int> digits_vec(len_S);
  for (size_t i = 0; i < len_S; ++i) {
    digits_vec[i] = S[i] - '0';
  }

  ll ans = 0;

  unordered_map<int, ll> mod_count_map;
  for (size_t i = 0; i < len_S; ++i) {
    unordered_map<int, ll> tmp_map;

    for (size_t j = 0; j < DIV; ++j) {
      tmp_map[get_mul10_mod(j, digits_vec[i])] += mod_count_map[j];
    }
    tmp_map[digits_vec[i]]++;

    mod_count_map = tmp_map;
    ans += mod_count_map[0];
  }

  // output
  WRITE_N(ans);

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

#include <bits/stdc++.h>
using namespace std;
int foo[3005];

int main() {
  string s;
  cin >> s;
  foo[0] = 1;
  long long tmp = 0;
  long long ten = 1;
  long long ans = 0;
  for (int i = s.size() - 1; i >= 0; i--) {
    tmp = (tmp + (s[i] - '0') * ten) % 2019;
    ans += foo[tmp]++;
    ten = (ten * 10) % 2019;
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long a[200005];

long long pow_mod[200005];
int foo[3005];

int main() {
  string s;
  cin >> s;
  s = "0" + s;
  long long tmp = 0;
  pow_mod[0] = 1;
  for (int i = 1; i <= s.size(); i++) {
    pow_mod[i] = (pow_mod[i - 1] * 10) % 2019;
  }
  for (int i = s.size(); i >= 1; i--) {
    tmp = (tmp * 10) + (s[i] - '0');
    tmp %= 2019;
    a[i] = (tmp * pow_mod[s.size() - i]) % 2019;
    foo[a[i]]++;
  }
  long long ans = 0;
  for (int i = 0; i < 2019; i++) {
    ans += foo[i] * (foo[i] - 1) / 2;
  }
  cout << ans;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

template <typename T> using lim = numeric_limits<T>;

const int MAXN = 2e5;
int dp[2][2019];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  memset(dp, 0, sizeof dp);
  string s;
  cin >> s;

  int n = s.size();
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 2019; j++)
      dp[i % 2][j] = 0;
    dp[i % 2][s[i - 1] - '0']++;
    for (int j = 0; j < 2019; j++) {
      if (dp[(i - 1 + 2) % 2][j] > 0) {
        dp[i % 2][(j * 10 + s[i - 1] - '0') % 2019] += dp[(i - 1 + 2) % 2][j];
      }
    }
    ans += dp[i % 2][0];
  }

  cout << ans << "\\n";
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

template <typename T> using lim = numeric_limits<T>;

const int MAXN = 2e5;
int dp[MAXN + 1][2019];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  memset(dp, 0, sizeof dp);
  string s;
  cin >> s;

  int n = s.size();
  for (int i = 1; i <= n; i++) {
    dp[i][s[i - 1] - '0']++;
    for (int j = 0; j < 2019; j++) {
      if (dp[i - 1][j] > 0) {
        dp[i][(j * 10 + s[i - 1] - '0') % 2019] += dp[i - 1][j];
      }
    }
  }

  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += dp[i][0];
    /*
    if (dp[i][0] > 0)
            cout << i << " " << dp[i][0] << "\\n";
    */
  }
  cout << ans << "\\n";
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
#define debug(x) cout << #x << " = " << x << '\\n'
#define debug_arr(a, n)                                                        \\
  for (int i = 0; i < n; i++)                                                  \\
  cout << a[i] << " "
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define vi vector<int>
#define vll vector<ll>
#define inf 1000000000
#define mod 1000000007

const int max_n = 1e5 + 9;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
ll power(ll a, ll b) {
  ll prod = 1;
  while (b) {
    if (b & 1)
      prod = (prod * a) % 2019;
    a = (a * a) % 2019;
    b >>= 1;
  }
  return prod;
}
int main() {
  string s;
  cin >> s;

  int n = s.size();
  int t[n + 1] = {0};
  for (int i = n - 1; i >= 0; i--) {
    t[i] = (t[i + 1] + (s[i] - '0') * power(10, n - i - 1)) % 2019;
  }

  map<int, int> m;
  m[0] = 1;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    // debug(t[i]);
    ans = (ans + m[t[i]]) % mod;
    m[t[i]]++;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
#define debug(x) cout << #x << " = " << x << '\\n'
#define debug_arr(a, n)                                                        \\
  for (ll i = 0; i < n; i++)                                                   \\
  cout << a[i] << " "
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define vi vector<ll>
#define vll vector<ll>
#define inf 1000000000
#define mod 1000000007

const ll max_n = 2e5 + 9;

typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
ll power(ll a, ll b) {
  ll prod = 1;
  while (b) {
    if (b & 1)
      prod = (prod * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return prod;
}
int main() {
  char s[max_n];
  scanf("%s", s);

  int n = strlen(s);

  ll num = 0;
  set<int> st[n];

  for (int i = 0; i < n; i++) {
    int rem = 0;
    ll pref = 0;
    for (int j = i; j < n; j++) {
      rem = (10 * rem + (s[j] - '0')) % 2019;
      if (st[j].count(rem)) {
        break;
      } else {
        st[j].insert(rem);
      }
      if (rem == 0)
        pref++;
    }
    num += pref * (pref + 1) / 2;
  }

  printf("%lld\\n", num);
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define all(n) n.begin(), n.end()
#define print(n) cout << n << endl

ll maxpow(ll a) {
  ll ans = 1;
  rep(i, a) ans *= 10;
  return ans;
}

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

  string s;
  cin >> s;
  vector<ll> mod(2019);
  int sum = 0, order = 1;

  rep(i, s.size()) {
    sum = (sum + (int)(s.at(s.size() - i - 1) - '0') * order) % 2019;
    mod[sum]++;
    order = (order * 10) % 2019;
  }
  ll ans = 0;
  mod[0]++;
  rep(i, 2019) { ans += mod[i] * (mod[i] - 1) / 2; }

  print(ans);
  return 0;
}

Error: 0

Fixed submission:

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

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n); i > 0; i--)
#define all(n) n.begin(), n.end()
#define print(n) cout << n << endl

ll maxpow(ll a) {
  ll ans = 1;
  rep(i, a) ans *= 10;
  return ans;
}

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

  string s;
  cin >> s;
  ll n = stoll(s), temp, size = s.size() - 1, ans = 0;
  rep(i, s.size() - 1) {
    temp = n;
    rep(j, size) {
      if (temp % 2019 == 0)
        ans++;
      temp /= 10;
      if (temp < 2019)
        break;
    }
    n %= maxpow(size);
    size--;
  }
  print(ans);
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

using namespace std;

int main() {
  string S;
  int count = 0, base, *s, mod[2019] = {1};

  cin >> S;

  s = new int[S.length() + 1];
  s[0] = 0;
  base = 1;
  for (int i = 1; i <= S.length(); i++) {
    s[i] = (s[i - 1] + (S[S.length() - i] - '0') * base) % 2019;
    base = (base * 10) % 2019;
    ++mod[s[i]];
  }

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

  cout << count << endl;

  delete s;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
// #include<chrono>

using namespace std;

int main() {
  // auto start = chrono::steady_clock::now();
  string S;
  int count = 0;

  cin >> S;

  for (int j = S.length() - 1; j >= 3; j--) {
    int sum = 0;
    int base = 1;
    for (int i = j; i >= 0; i--) {
      sum += (S[i] - '0') * base;
      sum %= 2019;
      base *= 10;
      base %= 2019;
      count += (sum == 0);
    }
  }

  cout << count << endl;

  // auto end = chrono::steady_clock::now();
  // chrono::duration<double> d = end - start;
  // cout << d.count() << endl;
  return 0;
}