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

#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int INF = 100100100;
const double PI = 3.14159265358979323846;
typedef long long ll;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) for (ll i = 0; i < (n); ++i)

int main() {
  string s;
  cin >> s;
  short a[200002] = {0};
  int count[2020] = {0}, ans = 0, num = 0, r = 0;
  for (int i = 0; i < s.size(); ++i) {
    a[i] = s[i] - '0';
  }
  int mod10 = 1;
  for (int i = s.size() - 1; i >= 0; --i) {
    num += a[i] * mod10;
    r = num % 2019;
    if (a[i] != 0)
      count[r]++;
    mod10 *= 10;
    mod10 %= 2019;
  }
  ans += count[0];
  for (int i = 0; i < 2019; ++i) {
    if (count[i] >= 2)
      ans += count[i] * (count[i] - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int INF = 100100100;
const double PI = 3.14159265358979323846;
typedef long long ll;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) for (ll i = 0; i < (n); ++i)

int main() {
  string s;
  cin >> s;
  short a[200002] = {0};
  int count = 0;
  for (int i = 0; i < s.size(); ++i) {
    a[i] = s[i] - '0';
  }
  for (int i = 0; i < s.size() - 3; ++i) {
    int num = 1000 * a[i] + 100 * a[i + 1] + 10 * a[i + 2] + a[i + 3];
    int r = num % 2019;
    int logi = 0;
    for (int j = i + 3; j < s.size(); ++j) {
      for (int k = 4; i + k <= j; k++) {
        num = r * 10 + a[i + k];
        r = num % 2019;
      }
      if (r == 0) {
        count++;
        j += 4;
        logi = j;
      }
    }
    i = logi;
  }
  cout << count << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define P_B push_back
#define E_B emplace_back
#define fi first
#define se second

using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  string S;
  ll N, ans = 0;

  cin >> S;
  N = S.size();

  vector<int> s(N);
  vector<vector<int>> A(2, vector<int>(2019, 0));
  vector<vector<int>> B(2019, vector<int>(10));

  rep(i, N) { s[i] = S[i] - '0'; }

  rep(i, 2019) {
    rep(j, 10) { B[i][j] = (i * 10 + j) % 2019; }
  }

  // A[0].assign(2019, 0);
  A[0][s[0]] = 1;

  rep(i, N - 1) {
    A[1][s[i + 1]] += 1; //'0'があれば if(s[i+1] != 0) が必要
    rep(j, 2019) { A[1][B[j][s[i + 1]]] += A[0][j]; }
    rep(j, 2019) {
      A[0][j] = A[1][j];
      A[1][j] = 0;
    }
    ans += A[0][0];
  }

  cout << ans << endl;

  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define P_B push_back
#define E_B emplace_back
#define fi first
#define se second

using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  string S;
  ll N, ans = 0;

  cin >> S;
  N = S.size();

  vector<int> s(N);
  vector<vector<int>> A(N, vector<int>(2019, 0));
  vector<vector<int>> B(2019, vector<int>(10));

  rep(i, N) { s[i] = S[i] - '0'; }

  rep(i, 2019) {
    rep(j, 10) { B[i][j] = (i * 10 + j) % 2019; }
  }

  // A[0].assign(2019, 0);
  A[0][s[0]] = 1;

  rep(i, N - 1) {
    A[i + 1][s[i + 1]] += 1; //'0'があれば if(s[i+1] != 0) が必要
    rep(j, 2019) { A[i + 1][B[j][s[i + 1]]] += A[i][j]; }
  }

  rep(i, N) { ans += A[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;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep2(i, i0, n) for (int i = i0; i < n; ++i)

int main() {
  string s;
  cin >> s;
  int n = s.size();

  vector<ll> dp(2019, 0);
  std::vector<ll> ndp(2019, 0);

  ll ans = 0;
  rep(i, n) {
    fill(ndp.begin(), ndp.end(), ll{0});
    int k = s[i] - '0';
    rep(j, 2019) {
      int nj = (10 * j + k) % 2019;
      ndp[nj] += dp[j];
    }
    ndp[k] += 1;
    ans += ndp[0];
    dp = ndp;
  }

  cout << ans << endl;
  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 < n; ++i)
#define rep2(i, i0, n) for (int i = i0; i < n; ++i)

int main() {
  string s;
  cin >> s;
  int n = s.size();

  vector<vector<ll>> dp(n + 1, vector<ll>(2019, 0));

  ll ans = 0;
  rep(i, n) {
    int k = s[i] - '0';
    rep(j, 2019) {
      int nj = (10 * j + k) % 2019;
      dp[i + 1][nj] += dp[i][j];
    }
    dp[i + 1][k] += 1;
    ans += dp[i + 1][0];
  }

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

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

void solver() {
  string s;
  cin >> s;

  vector<int> cnts(2019);
  cnts[0] = 1;

  long long res = 0;
  long long d = 1;
  for (long long i = s.size() - 1; i >= 0; i--) {
    res = res + (s[i] - 48) * d;
    d *= 10;
    d %= 2019;
    res %= 2019;
    cnts[res]++;
  }

  long long ans = 0;
  for (int i = 0; i < cnts.size(); i++) {
    ans += (cnts[i] * (cnts[i] - 1)) / 2;
  }

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

int main() {
  solver();
  return 0;
}


Error: TLE

Fixed submission:

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

void solver() {
  string s;
  cin >> s;

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

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

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

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

int main() {
  string s;
  cin >> s;
  const long num = 2019;
  long n = s.length();
  vector<vector<long>> dp(2, vector<long>(num, 0));
  long fans = 0;

  for (long i = 1; i <= n; i++) {
    long d = s[i - 1] - '0';
    for (long r = 0; r < num; r++)
      dp[1][(r * 10 + d) % num] = dp[0][r];
    dp[1][d]++;
    dp[0] = dp[1];
    fans += dp[1][0];
  }

  cout << fans;
  return 0;
}

Error: TLE

Fixed submission:

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

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

  string s;
  cin >> s;
  const long num = 2019;
  long n = s.length();
  vector<vector<long>> dp(n + 1, vector<long>(num, 0));
  dp[0][0] = 0;
  long fans = 0;
  for (long i = 1; i <= n; i++) {
    long d = s[i - 1] - '0';
    dp[i][d] += 1;
    for (long r = 0; r < num; r++)
      dp[i][(r * 10 + d) % num] += dp[i - 1][r];
    fans += dp[i][0];
  }

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

// url:
// problem name:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
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;
}
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pl = pair<long long, long long>;
using veci = vector<int>;
using vecl = vector<long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;

vecl cnt(2019, 0);

int main() {
  string S;
  cin >> S;
  ll ans = 0;
  int ex = 1;
  int num = 0;
  for (int i = S.size() - 1; i >= 0; i--) {
    num = (num + ex * (S[i] - '0')) % 2019;
    ex = (ex * 10) % 2019;

    cnt[num]++;
  }
  for (auto x : cnt)
    ans += x * (x - 1) / 2;
  ans += cnt[0];
  cout << ans << endl;
}

Error: 0

Fixed submission:

// url:
// problem name:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
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;
}
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pl = pair<long long, long long>;
using veci = vector<int>;
using vecl = vector<long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;

veci cnt(2019, 0);

int main() {
  string S;
  cin >> S;
  ll ans = 0;
  for (int i = S.size() - 1; i >= 0; i--) {
    // cout << stoll(S.substr(i,S.size()-i)) <<  " " <<
    // stoll(S.substr(i,S.size()-i))%2019 << endl;
    cnt[stoll(S.substr(i, S.size() - i)) % 2019]++;
  }
  for (auto x : cnt)
    ans += x * (x - 1) / 2;
  ans += cnt[0];
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

using ll = long long;

template <class T> using grid = vector<vector<T>>;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)

#define input(...)                                                             \\
  __VA_ARGS__;                                                                 \\
  in(__VA_ARGS__)

template <class T> void print(vector<T> a) {
  cout << "[ ";
  REP(i, a.size()) cout << a[i] << " ";
  cout << "]" << endl;
}

void print() { std::cout << std::endl; }

template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
  std::cout << head << " ";
  print(std::forward<Tail>(tail)...);
}

void in() {}

template <class Head, class... Tail> void in(Head &&head, Tail &&...tail) {
  cin >> head;
  in(std::forward<Tail>(tail)...);
}

// vector<int> d;
// ll dp(int i, int m) {
//   if (i == d.size()) {
//     return 0;
//   }
//   int value = (m * 10 + d[i]) % 2019;
//   return dp(i + 1, value) + (value == 0);
// }

int main() {
  string input(s);
  int n = s.size();
  vector<int> d(n);
  REP(i, n) d[n - i - 1] = s[i] - '0';

  vector<int> p(n);
  ll m = 1;
  ll pp = 0;
  REP(i, n) {
    p[i] = pp = (m * d[i] + pp) % 2019;
    (m *= 10) %= 2019;
  }

  map<int, int> cnt;
  for (auto v : p) {
    cnt[v] += 1;
  }

  ll ans = 0;
  for (auto v : cnt) {
    if (v.first == 0)
      ans += v.second;
    if (v.second >= 2) {
      ans += (v.second * (v.second - 1)) / 2;
    }
  }
  print(ans);
}


Error: TLE

Fixed submission:

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

using namespace std;

using ll = long long;

template <class T> using grid = vector<vector<T>>;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)

#define input(...)                                                             \\
  __VA_ARGS__;                                                                 \\
  in(__VA_ARGS__)

void print() { std::cout << std::endl; }

template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
  std::cout << head << " ";
  print(std::forward<Tail>(tail)...);
}

void in() {}

template <class Head, class... Tail> void in(Head &&head, Tail &&...tail) {
  cin >> head;
  in(std::forward<Tail>(tail)...);
}

int main() {
  string input(s);
  int n = s.size();
  vector<int> d(n);
  REP(i, n) d[i] = s[i] - '0';

  vector<vector<ll>> table(n + 1, vector<ll>(2019, 0));
  REPD(i, n) {
    REPD(m, 2019) {
      int value = (m * 10 + d[i]) % 2019;
      table[i][m] = table[i + 1][value] + (value == 0);
    }
  }
  ll ans = 0;
  REP(i, n) { ans += table[i][0]; }
  print(ans);
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

const int MAXN = 200005;
const int M = 2019;

char s[MAXN];
int cnt[M + 3];

int main() {
  ll res = 0;
  string s;
  cin >> s;
  int len = s.size(), t = 0, p = 1;
  for (int i = len - 1; i >= 0; --i) {
    ++cnt[t];
    t += (s[i] - 48) * p;
    t %= M;
    res += cnt[t];
    p = p * 10 % M;
  }
  cout << res;
  return 0;
}

Error: 0

Fixed submission:

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

#define ll long long
#define ar array
#define endl "\\n"

int main() {
  //
  // freopen("input.txt", "r", stdin);
  //
  ios::sync_with_stdio(0);
  cin.tie(0);
  string s;
  cin >> s;
  ll ans = 0;
  int n = (int)s.size();
  unordered_map<int, int> mods;
  string sum = "";
  for (int i = n - 1; i >= 0; --i) {
    sum = s[i] + sum;
    int temp = stoll(sum) % 2019;
    if (!temp)
      ans++;
    ans += (long long)mods[temp % 2019];
    mods[temp]++;
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

const int M = 2019;

int cnt[M + 3];

int main() {
  ll res = 0;
  string s;
  cin >> s;
  int len = s.size(), t = 0, p = 1;
  for (int i = len - 1; i >= 0; --i) {
    ++cnt[t];
    t += (s[i] - 48) * p;
    t %= M;
    res += cnt[t];
    p = p * 10 % M;
  }
  cout << res;
  return 0;
}


Error: 0

Fixed submission:

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

#define ll long long
#define ar array
#define endl "\\n"

int main() {
  //
  // freopen("input.txt", "r", stdin);
  //
  ios::sync_with_stdio(0);
  cin.tie(0);
  string s;
  cin >> s;
  ll ans = 0;
  int n = (int)s.size();
  unordered_map<int, ll> mods;
  string sum = "";
  for (int i = n - 1; i >= 0; --i) {
    sum = s[i] + sum;
    int temp = stoll(sum) % 2019;
    if (!temp)
      ans++;
    ans += (ll)mods[temp];
    mods[temp]++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename 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 INT_INF = 1 << 30;
#define MOD 1000000007LL
#define endl "\\n"

ll ncr(ll n, ll r) {
  if (n < r)
    return 0;
  if (r > n - r)
    r = n - r;

  ll a = 1, b = 1;
  for (int i = 0; i < r; i++) {
    a *= (n - i);
  }
  for (int i = 0; i < r; i++) {
    b *= (i + 1);
  }
  return a / b;
}

ll modbeki(ll x, ll n) {
  ll res = 1LL;
  while (n > 0) {
    if (n & 1) {
      res *= x % 2019;
      res %= 2019;
    }
    x = x * x % 2019;
    n >>= 1;
  }
  return res % 2019;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  string S;
  cin >> S;
  ll N = S.size();
  vll num(2019, 0);
  vll dp(N, INF);
  ll initial = S.at(N - 1) - '0';
  dp.at(N - 1) = initial;
  num.at(dp.at(N - 1))++;
  num.at(0) = 1;
  for (int i = N - 2; i >= 0; i--) {
    ll x = S.at(i) - '0';
    dp.at(i) = (x * modbeki(10, (N - 1) - i) + dp.at(i + 1)) % 2019;
    num.at(dp.at(i))++;
  }
  ll ans = 0;
  for (int i = 0; i < 2019; i++) {
    ans += ncr(num.at(i), 2);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename 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 INT_INF = 1 << 30;
#define MOD 1000000007LL
#define endl "\\n"

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  string S;
  cin >> S;
  int N = S.size();
  vvll dp(N + 1, vll(2019, 0));
  vll dp2(N + 1, INF);
  ll initial = S.at(0) - '0';
  dp[1][initial] = 1;
  dp2[1] = initial;
  for (int i = 1; i < N; i++) {
    ll num = S.at(i) - '0';
    dp2[i + 1] = (10 * dp2[i] + num) % 2019;
    for (int j = 0; j < 2019; j++) {
      dp[i + 1][(10 * j + num) % 2019] += dp[i][j];
    }
    dp[i + 1][num]++;
  }
  ll ans = 0;
  for (int i = 1; i <= N; i++) {
    ans += dp[i][0];
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm> // count_ifに必要
#include <array>     // sizeはコンパイル時定数で固定
#include <iomanip> // cout << setprecision(15) << x で小数の出力精度を指定
#include <iostream>
#include <map>
#include <numeric> // accumulate, gcd
#include <queue>   // priority_queue も入ってる
#include <set>     // 要素数は size() で取得 multisetもある
#include <stack>
#include <string>
#include <tuple>
#include <utility> //pair
#include <vector>

using namespace std;
// 問題が起こるまでintは使用禁止2020-04-13
using ll = long long;

template <class InputIterator, class T>
bool contains(InputIterator iterator, const T &value) {
  if (find(iterator.begin(), iterator.end(), value) == iterator.end())
    return false;
  else
    return true;
}

template <class InputIterator> auto sum(InputIterator iterator) {
  return accumulate(iterator.begin(), iterator.end(), 0);
}

// =========ここまでテンプレ============

int main() {
  string s;
  cin >> s;
  ll size = s.size();
  ll count = 0;

  // multiset<ll> xs;
  array<ll, 2019> bucket{0};
  ll last = 0;

  vector<ll> tenpow;
  ll temp = 1;
  for (ll i = 0; i < s.size(); i++) {
    tenpow.push_back(temp);
    temp = temp * 10 % 2019;
  }

  for (ll i = 0; i < s.size(); i++) {
    last = (last + (s[size - 1 - i] - '0') * tenpow[i]) % 2019;
    // xs.insert(last);
    bucket[last]++;
  }
  bucket[0]++;
  for (ll i = 0; i < 2019; i++) {
    count += bucket[i] * (bucket[i] - 1) / 2;
  }
  cout << count;
}

// for (ll i = 0; i < s.size() - 1; i++)
// {
//     ll value = 0;
//     for (ll j = i; j < s.size(); j++)
//     {
//         // string x = s.substr(i, j - i + 1);
//         char x = s[j];
//         ll y = x - '0';
//         value = (value * 10 + y) % 2019;
//         if (value == 0)
//         {
//             count++;
//         }
//     }
// }
// cout << count;

Error: TLE

Fixed submission:

#include <algorithm> // count_ifに必要
#include <array>     // sizeはコンパイル時定数で固定
#include <iomanip> // cout << setprecision(15) << x で小数の出力精度を指定
#include <iostream>
#include <map>
#include <numeric> // accumulate, gcd
#include <queue>   // priority_queue も入ってる
#include <set>     // 要素数は size() で取得
#include <stack>
#include <string>
#include <tuple>
#include <utility> //pair
#include <vector>

using namespace std;
// 問題が起こるまでintは使用禁止2020-04-13
using ll = long long;

template <class InputIterator, class T>
bool contains(InputIterator iterator, const T &value) {
  if (find(iterator.begin(), iterator.end(), value) == iterator.end())
    return false;
  else
    return true;
}

template <class InputIterator> auto sum(InputIterator iterator) {
  return accumulate(iterator.begin(), iterator.end(), 0);
}

// =========ここまでテンプレ============

int main() {
  string s;
  cin >> s;
  ll count = 0;
  for (ll i = 0; i < s.size() - 1; i++) {
    ll value = 0;
    for (ll j = i; j < s.size(); j++) {
      // string x = s.substr(i, j - i + 1);
      char x = s[j];
      ll y = x - '0';
      value = (value * 10 + y) % 2019;
      if (value == 0) {
        count++;
      }
    }
  }
  cout << count;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int digit(const string &str, size_t i) { return stoi(str.substr(i, 1)); }

int main() {
  string str;
  cin >> str;
  size_t n = str.size();

  std::vector<int> mod2019(2019, 0);
  ++mod2019[0];
  int m{0};
  int m10{1};
  for (auto i = n; i; --i) {
    m10 = (m10 * 10) % 2019;
    m = (m10 * digit(str, i - 1) + m) % 2019;

    ++mod2019[m];
  }

  auto cnt = std::accumulate(begin(mod2019), end(mod2019), 0,
                             [](int a, int b) { return a + b * (b - 1) / 2; });

  cout << cnt << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

int main() {
  string str;
  cin >> str;
  size_t n = str.size();

  int cnt{};
  int num;
  for (size_t i = 0; i < n; ++i) {
    num = 0;
    for (size_t j = i; j < n; ++j) {
      num = (num * 10 + stoi(str.substr(j, 1))) % 2019;
      if (!num) {
        ++cnt;
      }
    }
  }

  cout << cnt << 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 n = s.size();
  map<int, int> f;
  f[0] = 1;
  int mod = 0, cur = 1, ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    mod = (mod + cur * (s[i] - '0')) % 2019;
    cur = (cur * 10) % 2019;
    ans += f[mod];
    f[mod]++;
  }
  cout << ans;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  int n = s.size();
  map<int, int> f;
  int mod = 0, cur = 1, ans = 0;
  for (int i = n - 1; i >= 0; i++) {
    mod = (mod + cur * (s[i] - '0')) % 2019;
    cur = (cur * 10) % 2019;
    ans += f[mod];
    f[mod]++;
  }
  cout << ans;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (i = 0; i < (n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define MOD 2019
#define PI 3.14159265358979323846
#define INF 1 << 30

using namespace std;
typedef long long ll;
typedef pair<int, ll> pp;

ll Pow(ll n, ll k) {
  ll ans = 1, a = n % MOD;
  while (k > 0) {
    if (k & 1) {
      ans *= a;
      ans %= MOD;
    }
    a *= a;
    a %= MOD;
    k >>= 1;
  }
  return ans;
}

int main(void) {
  int num, i, j, a;
  ll x, ans = 0;
  string s;
  cin >> s;
  vector<int> d(s.size() + 1, 0);
  map<int, ll> mp;
  mp[0] = 1;
  for (i = s.size() - 1; i >= 0; i--) {
    a = s[i] - '0';
    d[i] = (d[i + 1] + a * Pow(10, s.size() - i - 1)) % 2019;
    mp[d[i]]++;
  }
  for (auto it = mp.begin(); it != mp.end(); it++) {
    x = (*it).second;
    ans += x * (x - 1) / 2;
  }
  cout << ans << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (i = 0; i < (n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define MOD 1000000007
#define PI 3.14159265358979323846
#define INF 1 << 30

using namespace std;
typedef long long ll;
typedef pair<int, ll> pp;

int main(void) {
  int num, i, j, a;
  ll ans = 0;
  string s;
  cin >> s;
  vector<map<int, ll>> dp(s.size() + 1);
  rep(i, s.size()) {
    a = s[i] - '0';
    for (auto it = dp[i].begin(); it != dp[i].end(); it++) {
      dp[i + 1][((*it).first * 10 + a) % 2019] += (*it).second;
    }
    dp[i + 1][a]++;
  }
  rep(i, dp.size()) { ans += dp[i][0]; }
  cout << ans << "\\n";
  return 0;
}