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

#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
/* function */
ll digitSum(ll n) {
  ll tmp = n;
  ll ret = 0;
  while (n > 0) {
    ret += n % 10;
    n /= 10;
  }
  return ret;
}
/* main */
int main() {
  string S;
  cin >> S;
  int m = 2019;
  reverse(S.begin(), S.end());

  vector<int> cnt(2020);
  cnt[0] = 1;
  int p = 1;
  int sum = 0;
  for (char c : S) {
    sum += p * (c - '0');
    sum %= m;
    cnt[sum]++;
    p *= 10;
    p %= m;
  }

  int ans = 0;
  for (int i = 0; i < cnt.size(); i++)
    ans += cnt[i] * (cnt[i] - 1) / 2;
  cout << ans << '\\n';
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
/* function */
ll digitSum(ll n) {
  ll tmp = n;
  ll ret = 0;
  while (n > 0) {
    ret += n % 10;
    n /= 10;
  }
  return ret;
}
/* main */
int main() {
  /*
  ll a = 2019;
  for (int i = 0; i < 200; i++) {
      cout << i << " : " << 2019 * i << " : " << digitSum(2019 * i) << '\\n';
  }
  for (int i = 0; i < 200; i++) {
      cout << i << " : " << 673 * i << " : " << digitSum(673 * i) << '\\n';
  }
  */
  string S;
  cin >> S;
  ll m = 2019;
  ll ans = 0;
  for (int i = 0; i < S.size(); i++) {
    for (int j = 5; j < S.size(); j++) {
      if (i + j > S.size())
        continue;
      ll tmp = stol(S.substr(i, j));
      if (tmp % m == 0) {
        ans++;
        // cout << tmp << '\\n';
      }
    }
  }
  cout << ans << '\\n';
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

using i64 = int64_t;
using P = pair<i64, i64>;
i64 mod = 1000000007;
int int_max = 2147483647;
int max_n = 1e5;

int main(int argc, char **argv) {
  string s;
  cin >> s;
  int n = s.size();
  std::vector<int> v(2019, 0);
  v[0] = 1;
  int carry{0};
  int pw{1};
  for (int i = 0; i < n; ++i) {
    int next = s[n - i - 1] - '0';
    carry = carry + pw * next;
    carry %= 2019;

    pw = (pw * 10) % 2019;

    ++v[carry];
  }

  i64 ans{0};
  for (auto i : v) {
    ans += (i * (i - 1) / 2);
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

using i64 = int64_t;
using P = pair<i64, i64>;
i64 mod = 1000000007;
int int_max = 2147483647;
int max_n = 1e5;

int main(int argc, char **argv) {
  string s;
  cin >> s;
  int n = s.size();
  int ans{0};
  for (int i = 0; i < n; ++i) {
    int carry = s[i] - '0';
    for (int j = i + 1; j < n; ++j) {
      int next = s[j] - '0';
      int num = next + carry * 10;
      carry = num % 2019;
      if (!carry) {
        ++ans;
      }
    }
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
#define INF 1LL << 30
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()

ll cnt[3000];

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

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

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

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
#define INF 1LL << 30
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()

ll cnt[3000];

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

  reverse(all(s));
  for (int i = 1; i <= s.size(); i++) {
    string t = s.substr(0, i);
    reverse(all(t));
    ll x = stoll(t);
    // cout<<x<<endl;
    // cout<<x%2019<<endl;
    cnt[x % 2019]++;
  }

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

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

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

short dp[200005][2020];

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

  rep(i, S.size()) {
    {
      int nxt = (S[i] - '0') % 2019;
      dp[i + 1][nxt] += 1;
    }
    rep(j, 2019) {
      {
        int nxt = (j * 10 + (S[i] - '0')) % 2019;
        dp[i + 1][nxt] += dp[i][j];
      }
    }
  }

  int ret = 0;
  rep(i, S.size() + 1) { ret += dp[i][0]; }
  cout << ret << endl;

  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

int dp[200005][2020];

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

  rep(i, S.size()) {
    {
      int nxt = (S[i] - '0') % 2019;
      dp[i + 1][nxt] += 1;
    }
    rep(j, 2019) {
      {
        int nxt = (j * 10 + (S[i] - '0')) % 2019;
        dp[i + 1][nxt] += dp[i][j];
      }
    }
  }

  int ret = 0;
  rep(i, S.size() + 1) { ret += dp[i][0]; }
  cout << ret << endl;

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

#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = s; i < (n); i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
using namespace std;

const int mod = 2019;
int main() {
  string s;
  cin >> s;
  int n = s.size();
  vector<int> dp(n + 1);
  vector<int> count(mod, 0);
  dp[n] = 0;
  count[0]++;
  int x = 1;
  repr(i, n) {
    int num = (s[i] - '0');
    num = (num * x) % mod;
    x = (x * 10) % mod;
    dp[i] = (dp[i + 1] + num) % mod;
    count[dp[i]]++;
  }
  int ans = 0;
  rep(i, mod) { ans += count[i] * (count[i] - 1) / 2; }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = s; i < (n); i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
using namespace std;

const int mod = 2019;
int main() {
  string s;
  cin >> s;
  int n = s.size();
  vector<int> dp(n + 1);
  vector<int> count(mod, 0);
  dp[n] = 0;
  count[0]++;
  repr(i, n) {
    int num = (s[i] - '0');
    rep(j, n - i - 1) num = (num * 10) % mod;
    dp[i] = (dp[i + 1] + num) % mod;
    count[dp[i]]++;
  }
  int ans = 0;
  rep(i, mod) { ans += count[i] * (count[i] - 1) / 2; }
  cout << ans << endl;

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


#include <bits/stdc++.h>
#include <iostream>
#include <string>

typedef uint64_t u64;
typedef int64_t i64;
typedef uint32_t u32;
typedef int32_t i32;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;

#define REP(idx, N) for (u64 idx = 0; idx < N; idx++)

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

  map<u32, u32> mods;
  u32 mod = 0;
  u32 mod_dig = 1;

  u32 digit = 0;
  mods[0] = 1;
  for (auto it = --s.end(); it >= s.begin(); it--) {
    u64 m = (*it - '0');
    mod = (mod + m * mod_dig) % 2019;
    mod_dig = mod_dig * 10 % 2019;
    mods[mod] += 1;
    digit++;
  }

  u32 ans = 0;
  for (auto &m : mods) {
    ans += m.second * (m.second - 1) / 2;
  }
  cout << ans;

  return 0;
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>
#include <iostream>
#include <string>

typedef uint64_t u64;
typedef int64_t i64;
typedef uint32_t u32;
typedef int32_t i32;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;

#define REP(idx, N) for (u64 idx = 0; idx < N; idx++)

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

  map<u32, u32> mods;
  u32 mod = 0;

  u32 digit = 0;
  mods[0] = 1;
  for (auto it = --s.end(); it >= s.begin(); it--) {
    u64 m = (*it - '0');
    REP(j, digit) {
      m = m * 10;
      m = m % 2019;
    }
    mod = (mod + m) % 2019;
    mods[mod] += 1;
    digit++;
  }

  u32 ans = 0;
  for (auto &m : mods) {
    ans += m.second * (m.second - 1) / 2;
  }
  cout << ans;

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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vl = vector<ll>;
using pll = pair<ll, ll>;

#define rep(i, n) for (ll i = 0, xxx_rep_end = (n); i < xxx_rep_end; ++i)
#define all(x) (x).begin(), (x).end()

template <class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3fLL;
constexpr ll mod = 2019;

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

  ll N = S.size();

  map<ll, ll> mp;
  ll cur = 0;
  ll fac = 1;
  ++mp[cur];
  for (int i = 1; i <= N; ++i) {
    cur = (cur + (S[N - i] - '0') * fac) % mod;
    ++mp[cur];
    fac *= 10;
    fac %= mod;
  }

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

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vl = vector<ll>;
using pll = pair<ll, ll>;

#define rep(i, n) for (ll i = 0, xxx_rep_end = (n); i < xxx_rep_end; ++i)
#define all(x) (x).begin(), (x).end()

template <class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3fLL;
constexpr ll mod = 1000000007;

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

  ll ans = 0;
  for (int i = 0; i < S.size(); ++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;
}
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;
typedef long long ll;

int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  int sl = s.size();
  vector<ll> cnt(2019);
  int ten = 1, tot = 0, ans = 0;
  rep(i, sl) {
    cnt[tot]++;
    int x = (s[i] - '0') * ten % 2019;
    tot += x;
    tot %= 2019;
    ans += cnt[tot];
    ten = ten * 10 % 2019;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int INF = 1e9;
const int MOD = 2019;

ll modpow(ll a, ll b) {
  ll r = 1;
  while (b) {
    if (b & 1)
      r = r * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return r;
}

int main() {
  ll ans = 0;
  string s;
  cin >> s;
  ll sl = s.size();
  ll m[sl + 2];
  rep(i, sl + 2) m[i] = 0;
  m[sl - 1] = s[sl - 1] - '0';
  for (int i = sl - 2; i >= 0; --i) {
    m[i] = (m[i + 1] + (s[i] - '0') * modpow(10, sl - 1 - i)) % 2019;
    // cout << m[i] << endl;
  }

  for (int i = 0; i < sl; ++i) {
    for (int j = i + 3; j <= sl; ++j) {
      if (m[i] - m[j] == 0) {
        ans++;
      }
    }
  }

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = int64_t;
using P = pair<ll, ll>;
template <class T> using V = vector<T>;

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

  int n = s.length();
  V<int> curr(2020), prev(2020);

  ll ans = 0;
  rep(i, n) {
    fill(curr.begin(), curr.end(), 0);
    int p = s[i] - '0';
    if (p != 0)
      curr[p] += 1;

    rep(j, 2020) {
      if (prev[j] > 0) {
        int p = (j * 10 + s[i] - '0') % 2019;
        curr[p] += prev[j];
      }
    }
    ans += curr[0];
    prev = curr;
  }
  cout << ans << endl;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = int64_t;
using P = pair<ll, ll>;
template <class T> using V = vector<T>;

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

  int n = s.length();
  V<V<int>> dp(n + 1, V<int>(2020, 0));

  ll ans = 0;
  rep(i, n) {
    int p = s[i] - '0';
    if (p != 0)
      dp[i + 1][p] += 1;

    rep(j, 2020) {
      if (dp[i][j] > 0) {
        int p = (j * 10 + s[i] - '0') % 2019;
        dp[i + 1][p] += dp[i][j];
      }
    }
    ans += dp[i + 1][0];
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
using namespace std;
using ll = long long;
using ld = long double;
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  string S;
  cin >> S;
  int N = sz(S);

  map<int, int> MP;
  MP[0]++;
  int now = 1;
  int key = 0;
  int ans = 0;
  for (int i = N - 1; i >= 0; i--) {
    key = (key + now * (S.at(i) - '0')) % 2019;
    now = now * 10 % 2019;
    ans += MP[key];
    MP[key]++;
  }
  // for(const auto& [a,b]:MP) cout << a << "," << b << endl;
  cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
using namespace std;
using ll = long long;
using ld = long double;
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int dp[200020][2019];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  string S;
  cin >> S;
  int N = sz(S);

  int x = S.at(0) - '0';
  dp[0][x] = 1;
  int ans = dp[0][0];
  for (int i = 1; i < N; i++) {
    x = S.at(i) - '0';
    dp[i][x] = 1;
    for (int j = 0; j < 2019; j++) {
      dp[i][(j * 10 + x) % 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;

int main() {
  string S;
  cin >> S;
  vector<int> base(S.size());
  int N = S.size();
  for (int i = 0; i < N; i++) {
    if (i == 0) {
      base.at(i) = 1;
    } else {
      base.at(i) = base.at(i - 1) * 10 % 2019;
    }
  }
  vector<int> mod(S.size());
  for (int i = 0; i < N; i++) {
    if (i == 0) {
      string s;
      s = s + S.at(N - 1);
      int ai = stoi(s);
      mod.at(i) = ai;
    } else {
      string s;
      s = s + S.at(N - i - 1);
      int ai = stoi(s);
      mod.at(i) = (mod.at(i - 1) + base.at(i) * ai) % 2019;
    }
  }
  int count = 0;
  for (int i = 0; i < 2019; i++) {
    int sum = 0;
    for (int j = 0; j < N; j++) {
      if (mod.at(j) == i) {
        sum += 1;
      }
    }
    if (i == 0) {
      count += sum * (sum - 1) / 2 + sum;
    } else {
      count += sum * (sum - 1) / 2;
    }
  }
  cout << count << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  string S;
  cin >> S;
  vector<int> base(S.size());
  int N = S.size();
  for (int i = 0; i < N; i++) {
    if (i == 0) {
      base.at(i) = 1;
    } else {
      base.at(i) = base.at(i - 1) * 10 % 2019;
    }
  }
  vector<int> mod(S.size());
  for (int i = 0; i < N; i++) {
    if (i == 0) {
      string s;
      s = s + S.at(N - 1);
      int ai = stoi(s);
      mod.at(i) = ai;
    } else {
      string s;
      s = s + S.at(N - i - 1);
      int ai = stoi(s);
      mod.at(i) = (mod.at(i - 1) + base.at(i) * ai) % 2019;
    }
  }
  int count = 0;
  for (int i = 0; i < N; i++) {
    if (mod.at(i) == 0) {
      count += 1;
    }
    for (int j = i + 1; j < N; j++) {
      if (mod.at(i) == mod.at(j)) {
        count += 1;
      }
    }
  }
  cout << count << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const ll INF = 1<<21;
static const llint MOD = 2019;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

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

  llint n;
  n = s.size();

  map<llint, llint> mp;
  mp[0] = 1;
  llint now = 0, ten = 1;
  for (int in = 1; in <= n; in++) {
    now += ten * (s.at(n - in) - '0');
    now %= MOD;
    ten *= 10;
    ten %= MOD;
    mp[now]++;
  }

  llint ans = 0;
  for (auto im : mp) {
    ans += im.second * (im.second - 1) / 2;
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const ll INF = 1<<21;
static const llint MOD = 2019;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

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

  llint a = 0, n;
  n = s.size();

  llint dp[n + 1][MOD];
  for (int in = 0; in < n + 1; in++) {
    for (int im = 0; im < MOD; im++) {
      dp[in][im] = 0;
    }
  }

  map<llint, llint> mp;
  mp[0] = 1;
  llint now = 0, ten = 1;
  for (int in = 1; in <= n; in++) {
    now += ten * (s.at(n - in) - '0');
    now %= MOD;
    ten *= 10;
    ten %= MOD;
    mp[now]++;
  }

  llint ans = 0;
  for (auto im : mp) {
    ans += im.second * (im.second - 1) / 2;
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
typedef long long ll;
char s[200005];
int modulo(int a, int b) {
  if (b == 0)
    return 1;
  const int m = 2019;
  ll x = 1, y = a;
  while (b > 0) {
    if (b % 2 == 1)
      x = x * y % m;
    y = y * y % m;
    b /= 2;
  }
  return x % m;
}
int main() {
  scanf("%s", s);
  int n = strlen(s);
  if (n < 5) {
    puts("0");
    return 0;
  }
  unordered_map<int, int> m;
  int sum = 0;
  ll cnt = 0;
  m[0] = 1;
  for (int i = n - 1; i >= 0; i--) {
    sum = (sum + (s[i] - '0') * modulo(10, n - 1 - i)) % 2019;
    if (m.find(sum) != m.end())
      cnt += m[sum];
    m[sum]++;
  }
  printf("%lld\\n", cnt);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
typedef long long ll;
char s[200005];
int modulo(int a, int b) {
  const int m = 2019;
  ll x = 1, y = a;
  while (b > 0) {
    if (b % 2 == 1)
      x = x * y % m;
    y = y * y % m;
    b /= 2;
  }
  return x % m;
}
int main() {
  scanf("%s", s);
  int n = strlen(s);
  if (n < 5) {
    puts("0");
    return 0;
  }
  ll cnt = 0;
  for (int i = n - 1; i >= 4; i--) {
    int sum = 0, y = 1;
    for (int x = i; x >= i - 3; x--) {
      sum += y * (s[x] - '0');
      y *= 10;
    }
    sum %= 2019;
    for (int j = i - 4; j >= 0; j--) {
      sum = (sum + (s[j] - '0') * modulo(10, i - j)) % 2019;
      if (sum == 0)
        cnt++;
    }
  }
  printf("%lld\\n", cnt);
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long ans = 0LL;
string S;
int main() {
  cin >> S;
  long long dp[2019], dp2[2019];
  memset(dp, 0LL, sizeof dp);
  memset(dp2, 0LL, sizeof dp2);
  dp[S[0] - '0'] = 1LL;
  for (int i = 1; i < S.size(); i++) {
    int n = S[i] - '0';
    for (int j = 0; j < 2019; j++) {
      if (dp[j] != 0LL) {
        dp2[(j * 10 + n) % 2019] += dp[j];
      }
    }
    for (int j = 0; j < 2019; j++) {
      dp[j] = dp2[j], dp2[j] = 0LL;
    }
    dp[n]++;
    ans += dp[0];
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string S;
int main() {
  cin >> S;
  long long dp[2019][S.size()];
  memset(dp, 0, sizeof dp);
  dp[S[0] - '0'][0] = 1LL;
  for (int i = 1; i < S.size(); i++) {
    int n = S[i] - '0';
    dp[n][i] = 1LL;
    for (int j = 0; j < 2019; j++) {
      if (dp[j][i - 1] != 0LL) {
        dp[(j * 10 + n) % 2019][i] += dp[j][i - 1];
      }
    }
  }
  long long ans = 0LL;
  for (int i = 0; i < S.size(); i++)
    ans += dp[0][i];
  cout << ans << endl;
  return 0;
}