alexjercan/bugnet huggingface dataset:
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 < (n); ++i)
#define repr(i, n) for (int i = (n - 1); i >= 0; --i)

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

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

const int MAX = 200005;
const int INF = 1001001001;
const int MOD = 1000000007;

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

  int N = S.size();
  reverse(S.begin(), S.end());

  vector<int> f(N + 1);
  int k = 1;
  rep(i, N) {
    f[i + 1] = (f[i] + (S[i] - '0') * k) % 2019;
    k *= 10;
    k %= 2019;
  }

  vector<ll> count(2019);
  rep(i, N + 1) count[f[i]]++;

  ll ans = 0;
  rep(i, 2019) ans += count[i] * (count[i] - 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 < (n); ++i)
#define repr(i, n) for (int i = (n - 1); i >= 0; --i)

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

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

const int MAX = 200005;
const int INF = 1001001001;
const int MOD = 1000000007;

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

  int N = S.size();

  unordered_set<int> old;
  unordered_set<int> st;
  st.emplace(0);

  ll ans = 0;
  rep(i, N) {
    old.clear();
    swap(st, old);
    st.emplace(0);
    for (auto &j : old) {
      int x = S[i] - '0';
      int res = (j * 10 + x) % 2019;
      if (res == 0)
        ans++;
      st.emplace(res);
    }
  }

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

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  string S;
  cin >> S;
  vector<long long> dp(2019, 0);
  vector<int> a(S.length() + 1), b(S.length() + 1);
  a[0] = 1;
  b[0] = 0;
  int i, j;
  for (i = 1; i <= S.length(); i++) {
    a[i] = (a[i - 1] * 202) % 2019;
    b[i] = ((b[i - 1] - a[i] * (S[i - 1] - '0')) % 2019 + 2019) % 2019;
  }
  long long cnt = 0;
  for (i = 0; i < S.length(); i++) {
    dp[((long long)a[i + 1] * (S[i] - '0') + b[i + 1]) % 2019] += 1;
    cnt += dp[b[i + 1]];
  }
  cout << cnt << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <string>
#include <utility>
using namespace std;
int main() {
  string S;
  cin >> S;
  map<pair<int, int>, long long> dp;
  int i, j;
  for (i = 0; i < S.length(); i++) {
    dp[pair<int, int>(i + 1, S[i] - '0')] = 1;
  }
  for (i = 0; i < S.length(); i++) {
    for (j = 0; j < 2019; j++) {
      dp[pair<int, int>(i + 1, (j * 10 + S[i] - '0') % 2019)] +=
          dp[pair<int, int>(i, j)];
    }
  }
  long long cnt = 0;
  for (i = 0; i < S.length(); i++)
    cnt += dp[pair<int, int>(i + 1, 0)];
  cout << cnt << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
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;
  ll N = S.size();

  vector<int> d(N + 1);
  ll count = 1;
  for (int i = N - 1; 0 <= i; i--) {
    int a = (S[i] - '0') * count % 2019;
    d[i] = (d[i + 1] + a) % 2019;
    count *= 10;
    count %= 2019;
  }

  vector<int> sum(2019);
  ll answer = 0;
  for (int i = N; 0 <= i; i--) {
    answer += sum[d[i]];
    sum[d[i]]++;
  }

  cout << answer << endl;
}


Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  if (10 % 2019 == 0) { // 2,5
    ll ans = 0;
    rep(r, n) {
      if ((s[r] - '0') % 2019 == 0) {
        ans += r + 1;
      }
    }
    cout << ans << endl;
    return 0;
  }

  vector<int> d(n + 1);
  int ten = 1;
  for (int i = n - 1; i >= 0; --i) {
    int a = (s[i] - '0') * ten % 2019;
    d[i] = (d[i + 1] + a) % 2019;
    ten *= 10;
    ten %= 2019;
  }
  vector<int> cnt(2019);
  ll ans = 0;
  for (int i = n; i >= 0; --i) {
    ans += cnt[d[i]];
    cnt[d[i]]++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

const ll mod = 1000000007;

int main() {

  string s;
  cin >> s;

  int n = s.size();

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

  vector<int> vec(2019, 0);

  ll t = 1;
  ll k = 0;
  ++vec[0];

  for (int i = 0; i < n; ++i) {
    k = (((s[i] - '0') * t) + k) % 2019;
    ++vec[k];
    t = (t * 10) % 2019;
  }

  ll ans = 0;

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

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

int main() {

  string num;
  cin >> num;

  int count = 0;

  for (long long i = 0; i != num.size() + 1; ++i) {
    for (long long j = i + 1; j != num.size() + 1; ++j) {
      string tmp = num.substr(i, j - i);
      // cout<<"tmp1:"<<tmp<<endl;
      long long n = stoll(tmp);
      if (n % 2019 == 0) {

        // cout<<"tmp:"<<tmp<<" "<<"i:"<<i<<" "<<"j:"<<j<<endl;
        ++count;
      }
    }
  }

  cout << count << endl;

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

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

int main() {
  int i, ans = 0;
  string s;
  cin >> s;
  map<int, int> lst;
  int dp[s.size()], ten[s.size()];
  dp[0] = s[s.size() - 1] - '0';
  ten[0] = 1;
  for (i = 1; i < s.size(); i++) {
    ten[i] = (ten[i - 1] * 10) % 2019;
  }
  for (i = 1; i < s.size(); i++) {
    dp[i] = (dp[i - 1] + ten[i] * (s[s.size() - i - 1] - '0')) % 2019;
  }
  for (i = 0; i < s.size(); i++) {
    if (dp[i] == 0)
      ans++;
    if (lst.count(dp[i]))
      lst[dp[i]]++;
    else
      lst[dp[i]] = 1;
  }
  for (auto l : lst) {
    auto value = l.second;
    ans += value * (value - 1) / 2;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int i, j, ans = 0;
  string s;
  cin >> s;
  int mod10[s.size()];
  mod10[0] = 1;
  for (i = 1; i < s.length(); i++) {
    mod10[i] = (mod10[i - 1] * 10) % 2019;
  }
  int modsum = ((s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 +
                s[3] - '0') %
               2019;
  if (modsum == 0)
    ans++;
  int copy;
  for (i = 4; i < s.length(); i++) {
    modsum = (modsum * 10 + s[i] - '0') % 2019;
    if (modsum == 0)
      ans++;
    copy = modsum;
    for (j = 0; j < i - 3; j++) {
      copy -= mod10[i - j] * (s[j] - '0');
      copy %= 2019;
      if (copy == 0)
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
#define f first
#define s second
#define pb push_back
#define all(v) v.begin(), v.end()
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

string s;
ll ans, n, t, rem[2020], mul = 1;

int main() {
  fastio;
  cin >> s;
  n = s.length();
  s = " " + s;
  rem[0] = 1;
  t = 0;
  DEC(i, n, 1) {
    t = (t + mul * (s[i] - '0')) % 2019;
    rem[t]++;
    mul = (10 * mul) % 2019;
  }
  FOR(i, 0, 2018) ans += (rem[i] * (rem[i] - 1)) / 2;
  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
#define f first
#define s second
#define pb push_back
#define all(v) v.begin(), v.end()
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

string s;
ll ans, n;
bool vis[200005][2020];

int __attribute__((optimize("Ofast"), target("arch=sandybridge"))) main() {
  fastio;
  cin >> s;
  n = s.length();
  s = " " + s;
  FOR(i, 1, n) {
    ll rem = 0, res = 0;
    FOR(j, i, n) {
      rem = (10 * rem + s[j] - '0') % 2019;
      if (vis[j][rem])
        break;
      vis[j][rem] = 1;
      if (rem == 0)
        res++;
    }
    ans += (res * (res + 1)) / 2;
  }
  cout << ans;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define oo 0x3f3f3f3f
#define ooLL 0x3f3f3f3f3f3f3f3f
#define mod 1e9 + 7
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
typedef long long ll;
typedef vector<pair<int, int>> vii;
typedef pair<int, int> pii;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
  string s;
  cin >> s;
  const int N = s.size();
  ll ans = 0LL;
  int rest = 0, pot = 1;
  vector<int> memo(2019);
  memo[0] = 1;
  for (int i = N - 1; i >= 0; --i) {
    int digit = s[i] - '0';
    rest = (rest + digit * pot) % 2019;
    ans += memo[rest];
    memo[rest]++;
    pot = (pot * 10) % 2019;
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define oo 0x3f3f3f3f
#define ooLL 0x3f3f3f3f3f3f3f3f
#define mod 1e9 + 7
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
typedef long long ll;
typedef vector<pair<int, int>> vii;
typedef pair<int, int> pii;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  cin >> s;
  const int N = s.size();
  ll ans = 0LL;
  for (int i = 0; i < N; ++i) {
    int rest = 0, divisor = 2019;
    for (int j = i; j < N; ++j) {
      int digit = s[j] - '0';
      rest = (rest * 10 + digit) % divisor;
      if (rest == 0)
        ans++;
    }
  }
  // 1817181712114
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define lli long long int
#define pb push_back
#define in insert
#define pi pair<int, int>
#define pii pair<int, pi>
#define mp make_pair
#define fir first
#define sec second
#define MAXN 101
#define MAXS 901
#define INF 1000000000
#define mod 1000000007

signed main() {
  string s;
  cin >> s;
  lli ans = 0;
  map<int, int> m;
  m[0]++;
  int t = 0, p2 = 1;
  for (int i = s.size() - 1; i >= 0; i--) {
    t = (t + (s[i] - '0') * p2) % 2019;
    ans += m[t];
    m[t]++;
    p2 = p2 * 10 % 2019;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

#define lli long long int
#define pb push_back
#define in insert
#define pi pair<int, int>
#define pii pair<int, pi>
#define mp make_pair
#define fir first
#define sec second
#define MAXN 101
#define MAXS 901
#define INF 1000000000
#define mod 1000000007

lli ans;

void get_substr(string s) {
  for (int i = 0; i < s.size(); i++) {
    char temp[s.size() - i + 1];
    int tempindex = 0;
    for (int j = i; j < (i + 3) && j < s.size(); j++)
      temp[tempindex++] = s[j];
    for (int j = (i + 3); j < s.size(); j++) {
      temp[tempindex++] = s[j];
      temp[tempindex] = '\\0';
      if (strlen(temp) <= 16) {
        lli num = stoll(temp);
        if (num % 2019 == 0)
          ans++;
      }
    }
  }
}
signed main() {
  string s;
  cin >> s;
  ans = 0;
  get_substr(s);
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void solve() {
  string s;
  cin >> s;
  map<int, int> m;
  m[0]++;
  int mod = 2019;
  int k = 1;
  int l = 0;
  int n = s.length();
  for (int i = 0; i < s.length(); ++i) {
    l = ((s[n - i - 1] - '0') * k + l) % mod;
    m[l]++;
    k = (k * 10) % mod;
  }
  ll ans = 0;
  for (auto i : m) {
    ans += i.second * (i.second - 1) / 2;
  }
  cout << ans << endl;
}

int main() {
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}

Error: TLE

Fixed submission:

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

void solve() {
  string s;
  cin >> s;
  ll n = 0, r = 0, p;
  set<ll> q[s.length()];
  for (ll i = 0; i < s.length(); ++i) {
    r = 0;
    p = 0;
    for (ll j = i; j < s.length(); ++j) {
      r = (10 * r + s[j] - '0') % 2019;
      if (q[j].find(r) != q[j].end())
        break;
      q[j].insert(r);
      if (r == 0) {
        p++;
      }
    }
    n += p * (p + 1) / 2;
  }
  cout << n << endl;
}

int main() {
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

using namespace std;

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

  int64_t ans = 0;
  vector<int64_t> v0(2019), v1(2019);
  for (int i = 0; i < S.size(); i++) {
    for (int j = 0; j < 2019; j++) {
      v1[(10 * j + S[i] - '0') % 2019] += v0[j];
    }
    v1[S[i] - '0']++;
    ans += v1[0];
    swap(v0, v1);
    fill(v1.begin(), v1.end(), 0);
  }

  cout << ans << endl;
  // vector<int[2019]> v(S.size()+1);
  // for(int i=0; i<S.size(); i++) {
  //     for(int j=0; j<2019; j++) {
  //         v[i+1][(j*10 + S[i] - '0')%2019] += v[i][j];
  //     }
  //     v[i+1][S[i] - '0']++;
  // }

  // int64_t ans = 0;
  // for(int i=0; i<=S.size(); i++) {
  //     ans += v[i][0];
  // }

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


Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

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

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

  int64_t ans = 0;
  for (int i = 0; i <= S.size(); i++) {
    ans += v[i][0];
  }

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

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <sstream>

// #include <string>
using namespace std;

using ll = long long; // int64
const int mod = 1e9 + 7;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  int cnt[2020] = {0};
  cnt[0] = 1; // 2019で割り切れる数ならば それ自体が答えになる.
  int d = 1;
  int ans = 0;
  int acc = 0;
  int m = 0;

  for (int i = n - 1; i >= 0; i--) {
    m = s[i] - '0';
    acc = (d * m + acc) % 2019;
    ans += cnt[acc];
    cnt[acc]++;
    d = (d * 10) % 2019;
  }

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

Error: TLE

Fixed submission:

#include <iostream>
#include <sstream>
// #include <string>
using namespace std;

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

  int muls[10000];
  muls[0] = 2019;
  for (int i = 1; muls[i] <= 1e5; i++) {
    muls[i] = muls[i - 1] + 2019;
  }

  int *subs[2 * 10 ^ 5];
  int len = 0;
  for (len = 0; s[len] != '\\0'; len++) {
  }
  // cout << len << endl;

  int ans = 0;
  for (int i = 0, l = i + 4; l <= len; i++, l = i + 4) {
    int subcnt = len - l + 1;
    subs[i] = new int[subcnt];
    for (int j = 0; j <= subcnt - 1; j++) {
      istringstream(s.substr(j, l)) >> subs[i][j];
      if (subs[i][j] % 2019 == 0)
        ans++;
      // cout << "subs[" << i << "][" << j << "]: " << subs[i][j] << endl;
    }
  }
  cout << ans << endl;
  // cout << s[1] << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

// #define int long long
const int MOD = 2019;
using ll = long long;
using intpair = pair<int, int>;
struct Fast {
  Fast() {
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
} fast;
inline int mod(ll a, int m = MOD) { return (a % m + m) % m; }

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define ceiv(a, b) ((int)((a) + (b)-1) / (int)(b))

int modpow(int a, int n) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = (ret * a) % MOD;
    a = (a * a) % MOD;
    n >>= 1;
  }
  return ret % MOD;
}

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

  int n = (int)s.length();

  vector<ll> t(n + 1);
  t[n] = 0;

  for (int i = n - 1; i >= 0; --i) {
    t[i] = mod((ll)(s[i] - '0') * modpow(10, n - 1 - i) + t[i + 1]);
  }

  map<int, ll> m;
  for (ll i : t)
    m[i]++;

  ll ans = 0;
  for (auto [mod, p] : m) {
    ans += p * (p - 1) / 2;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

#define int long long
struct Fast {
  Fast() {
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
} fast;
using intpair = pair<int, int>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define ceiv(a, b) ((int)((a) + (b)-1) / (int)(b))

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

  vector<vector<int>> dp(s.length() + 1, vector<int>(2019, 0));

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

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

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

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

int main() {
  string n;
  while (cin >> n) {
    int k = n.size();
    vector<int> dp(2019, 0);

    int res = 0;
    for (int i = 0; i < k; ++i) {
      vector<int> dp1(2019, 0);
      int m = n[i] - '0';
      if (m != 0) {
        dp1[m] += 1;
      }
      for (int j = 0; j < 2019; ++j) {
        dp1[(j * 10 + m) % 2019] += dp[j];
      }
      res += dp1[0];
      dp = dp1;
    }
    cout << res << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

int main() {
  string n;
  while (cin >> n) {
    int k = n.size();
    vector<vector<int>> dp(k + 1, vector<int>(2019));
    int res = 0;
    for (int i = 0; i < k; ++i) {
      int m = n[i] - '0';
      if (m != 0) {
        dp[i + 1][m] += 1;
      }
      for (int j = 0; j < 2019; ++j) {
        dp[i + 1][(j * 10 + m) % 2019] += dp[i][j];
      }
      res += dp[i + 1][0];
    }
    cout << res << endl;
  }
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

long int c[2020];
int main() {
  string S;
  cin >> S;
  int N = S.length();
  int i, j;
  long int ans = 0;
  long int pow = 10;
  long int sum = S[N - 1] - '0';
  c[sum]++;
  for (i = 1; i < N; i++) {
    sum = (sum + (S[N - 1 - i] - '0') * pow) % 2019;
    c[sum]++;
    pow = (pow * 10) % 2019;
  }
  for (i = 1; i < 2019; i++)
    ans += (c[i] * (c[i] - 1)) / 2;
  ans += c[0] + (c[0] * (c[0] - 1)) / 2;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  string S;
  cin >> S;
  int N = S.length();
  int i, j;
  long int ans = 0;
  long int pow = 10;
  sum[1] = S[N - 1] - '0';
  for (i = 1; i < N; i++) {
    sum[i + 1] = (sum[i] + (S[N - 1 - i] - '0') * pow) % 2019;
    pow = (pow * 10) % 2019;
  }
  for (i = 1; i < N + 1; i++) {
    for (j = 0; j < N + 1 - i; j++) {
      if ((sum[i + j] - sum[j]) % 2019 == 0)
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5 + 500;
int A[maxn], B[maxn];
int n;
map<string, int> mp;
int T[maxn], C[2019];
string str;
int main() {
  cin >> str;
  n = str.length();
  T[n] = 0;
  int mod = 1;
  C[0] = 1;
  for (int i = n - 1; i >= 0; i--) {
    T[i] = ((str[i] - '0') * mod + T[i + 1]) % 2019;
    C[T[i]]++;
    mod = (mod * 10) % 2019;
  }
  int res = 0;
  for (int i = 0; i < 2019; i++)
    res += C[i] * (C[i] - 1) / 2;
  cout << res << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 500;
int A[maxn], B[maxn];
int n;
map<string, int> mp;
int T[maxn], C[2019];
string str;
int main() {
  cin >> str;
  n = str.length();
  T[n] = 0;
  int mod = 1;
  for (int i = n - 1; i >= 0; i--) {
    T[i] = ((str[i] - '0') * mod % 2019 + T[i + 1]) % 2019;
    C[T[i]]++;
    mod = mod * 10 % 2019;
  }
  int res = 0;
  for (int i = 2; i <= 2019; i++)
    res += C[i] * (C[i] - 1);
  cout << res << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll mod = 1000000007;

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

int main() {
  string S;
  cin >> S;
  int n = S.size();
  vector<int> s(n);
  for (int i = 0; i < n; i++) {
    s[i] = S[i] - '0';
  }
  ll c = 0;
  vector<int> k(2019);
  vector<int> a(n + 1, 0);
  k[0]++;
  int p10 = 1;
  for (int i = n - 1; i >= 0; i--) {
    a[i] = (p10 * s[i] + a[i + 1]) % 2019;
    p10 *= 10;
    p10 %= 2019;
    k[a[i]]++;
  }
  for (int i = 0; i < 2019; i++) {
    if (k[i] > 1) {
      c += (k[i] * (k[i] - 1) / 2);
    }
  }
  cout << c << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll mod = 1000000007;

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

int main() {
  string S;
  cin >> S;
  int n = S.size();
  vector<int> s(n);
  for (int i = 0; i < n; i++) {
    s[i] = S[i] - '0';
  }
  ll c = 0;
  if (n < 5) {
    cout << 0 << endl;
  } else {
    for (int i = 0; i < n - 4; i++) {
      int p = s[i] * 1000 + s[i + 1] * 100 + s[i + 2] * 10 + s[i + 3];
      for (int j = i + 4; j < n; j++) {
        p = p * 10 + s[j];
        p %= 2019;
        if (p == 0) {
          c++;
        }
      }
    }
    cout << c << endl;
  }
}