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

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

// #define for(i,a,b) for (int i=(a);i<(b);++i)
typedef long long ll;
typedef pair<ll, ll> P;
#define REP(i, n) for (long long i = 0; i < (long long)(n); i++)
#define pb push_back // vectorに要素追加
#define INF (ll)1e18

ll mod = 1e9 + 7;
ll S_list[200000];
ll dp[2019][2];

void rec(ll i) {

  ll dp_tmp[2019][2];
  REP(j, 2019) {
    dp_tmp[j][0] = dp[j][0];
    dp_tmp[j][1] = dp[j][1];
  }

  REP(j, 2019) {
    if (dp[j][0] > 0) {
      // printf("j: %lld, j*10+i: %lld\\n", j, j*10+S_list[i]);
    }
    dp_tmp[j][1] += dp[j][0]; // 右が確定
    dp_tmp[(j * 10 + S_list[i]) % 2019][0] = dp[j][0];
  }
  dp_tmp[S_list[i]][0] += 1; // 右が未定
  REP(j, 2019) {
    dp[j][0] = dp_tmp[j][0];
    dp[j][1] = dp_tmp[j][1];
  }
}

// 検索タグ
int main() {
  // 入力
  string S;
  cin >> S;
  ll S_size = S.size();
  REP(i, S_size) { S_list[i] = stoi(S.substr(i, 1)); }

  // 解法
  REP(i, 2019) {
    dp[i][0] = 0;
    dp[i][1] = 0;
  }
  // dp[0][0] = ;
  REP(i, S_size) {
    rec(i);
    // printf("S: %lld, %lld, %lld\\n", S_list[i], dp[0][0],  dp[0][1]);
    // printf("[1817], %lld, %lld\\n", dp[1817][0],  dp[1817][1]);
  }
  ll ans = dp[0][0] + dp[0][1];
  // 出力
  printf("%lld\\n", ans);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

// #define for(i,a,b) for (int i=(a);i<(b);++i)
typedef long long ll;
typedef pair<ll, ll> P;
#define REP(i, n) for (long long i = 0; i < (long long)(n); i++)
#define pb push_back // vectorに要素追加
#define INF (ll)1e18

ll mod = 1e9 + 7;
const ll MAX_N = 3000;
/**/
int main() {
  // 入力
  string S;
  cin >> S;
  ll S_size = S.size();
  ll S_list[S_size];
  REP(i, S.size()) { S_list[i] = stoi(S.substr(i, 1)); }
  // REP(i,S.size()) printf("%lld, ", S_list[i]);
  // printf("\\n");

  // 解法
  ll ans = 0;
  REP(i, S_size + 1) {
    for (ll j = i + 1; j <= S_size; ++j) {
      ll tmp = 0;
      REP(k, j - i) {
        tmp *= 10;
        tmp += S_list[i + k];
      }
      // printf("%lld\\n", tmp);
      if (tmp % 2019 == 0)
        ans++;
    }
  }

  // 出力
  printf("%lld\\n", ans);
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {

  string S;
  cin >> S;

  int len = S.size();
  int count = 0;
  vector<int> bn(2019, 0), an(2019, 0);

  for (int i = 0; i < len; i++) {
    for (int j = 0; j < 2019; j++) {
      an.at((j * 10 + S.at(i) - '0') % 2019) = bn.at(j);
    }
    an.at(S.at(i) - '0')++;

    for (int j = 0; j < 2019; j++) {
      bn.at(j) = an.at(j);
    }
    count += bn.at(0);
  }

  cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;

  int count = 0;

  int d = s.size();
  for (int i = 0; i < d; i++) {
    int N = s.at(i) - '0';
    for (int j = i + 1; j < d; j++) {
      N *= 10;
      N += s.at(j) - '0';
      N %= 2019;
      if (N == 0)
        count++;
    }
  }

  cout << count << endl;

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

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

int ModPow(int N, int R, int M) {
  if (R == 0)
    return 1;
  if (R % 2 == 0) {
    int t = ModPow(N, R / 2, M);
    return (t * t) % M;
  }
  return N * ModPow(N, R - 1, M);
}

int main() {
  string S;
  long long int L;
  cin >> S;
  L = S.length();
  int *M = new int[L];

  int ans = 0;
  map<int, int> mp;
  mp[0] = 1;

  for (long long int i = 0; i < L; i++) {
    int x = stoi(S.substr(L - i - 1, 1));
    M[i] = i == 0 ? x : ((x * ModPow(10, i, 2019)) % 2019 + M[i - 1]) % 2019;
    if (mp.count(M[i]) > 0) {
      ans += mp[M[i]];
      mp[M[i]]++;
    } else
      mp[M[i]] = 1;
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

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

int main() {
  string S;
  long long int L;
  cin >> S;
  L = S.length();

  int ans = 0;
  map<int, bool> mp;
  mp[0] = true;

  for (int i = 0; i < L; i++) {
    int x = stoll(S.substr(i, L)) % 2019;
    if (mp.count(x) > 0)
      ans++;
    else
      mp[x] = true;
  }

  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;

  int64_t p = 1, now = 0, result = 0;
  vector<int64_t> mod(2019);
  mod[0] = 1;
  for (int64_t i = S.size() - 1; i >= 0; --i) {
    now = (now + (S[i] - '0') * p) % 2019;
    result += mod[now]++;
    p = p * 10 % 2019;
  }
  cout << result << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

int main() {
  string S;
  cin >> S;
  if (S.size() < 4) {
    cout << 0 << endl;
    return 0;
  }
  int64_t result = 0;
  for (int j = 0, size = S.size(); j < size - 2; ++j) {
    if (S[j] == '0')
      continue;
    string sub = S.substr(j, 3);
    int sum = 0;
    for (char c : sub) {
      sum += (c - '0');
    }
    for (int i = 3; j + i < size; ++i) {
      char c = S[j + i];
      sub += c;
      sum += (c - '0');
      if (sum % 3) {
        continue;
      }
      sum = 0;
      string tmp(sub);
      if (i > 17) {
        while (tmp.size() > 17) {
          int l = tmp.back() - '0';
          l = l * 2;
          tmp = tmp.substr(0, tmp.size() - 1);
          int base = stoi(tmp.substr(tmp.size() - 2, 2)) + 100;
          int res = base - l;
          if (res < 100) {
            for (int pos = tmp.size() - 3; pos >= 0; --pos) {
              if (tmp[pos] - '0' > 0) {
                tmp[pos] -= 1;
                break;
              }
            }
          }
          string tmp_res = to_string(res);
          tmp.back() = tmp_res.back();
          tmp[tmp.size() - 2] = tmp_res[tmp_res.size() - 2];
        }
      }
      auto num = stoll(tmp);
      if (num % 2019 == 0) {
        ++result;
      }
    }
  }
  cout << result << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

string s;
int cnt[2020];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> s;
  reverse(s.begin(), s.end());
  int n = s.length();
  long long res = 0;
  int base = 1;
  int rem = 0;
  memset(cnt, 0, sizeof(cnt));
  cnt[0] = 1;
  for (int i = 0; i < n; i++) {
    rem = (rem + (s[i] - '0') * base) % 2019;
    // cout << rem <<"\\n";
    cnt[rem]++;
    base = (base * 10) % 2019; // ** 0 also counts
  }
  for (int i = 0; i < 2019; i++)
    res += cnt[i] * (cnt[i] - 1) / 2;

  cout << res << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

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

string s;
unordered_set<int> p[200001];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  // for (int i = 1; i <= 20; i++)
  //     cout << 2019 * i << "\\n";

  cin >> s;
  int n = s.length();
  long long res = 0;
  for (int i = 0; i < n; i++) {
    int t = 0;
    long long cnt = 0;
    for (int j = i; j < n; j++) {
      t = (t * 10 + s[j] - '0') % 2019;
      if (p[j].find(t) != p[j].end()) // processed
        break;
      p[j].insert(t);
      if (t == 0)
        cnt++;
    }
    // cout << cnt <<"\\n";
    res += (cnt + 1) * cnt / 2;
  }
  cout << res << "\\n";

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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

using Field = vector<vector<int>>;
using Graph = vector<vector<int>>;
using VI = vector<int>;
using VC = vector<char>;
using PI = pair<int, int>;

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

const long long INF = 1LL << 60;
const int mod = 1000000007;

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

string S;
vector<int> cnt(2020, 0);

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

  cin >> S;

  ll N = S.size();
  reverse(ALL(S));

  ll total = 0;
  int ten = 1;

  cnt.at(0) = 1;

  REP(i, N) {
    total = (total + (S.at(i) - '0') * ten) % 2019;
    cnt.at(total)++;

    ten = (ten * 10) % 2019;
  }

  ll ans = 0;

  REP(i, 2020) { ans += cnt.at(i) * (cnt.at(i) - 1) / 2; }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

using Field = vector<vector<int>>;
using Graph = vector<vector<int>>;
using VI = vector<int>;
using VC = vector<char>;
using PI = pair<int, int>;

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

const long long INF = 1LL << 60;
const int mod = 1000000007;

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

string S;
vector<int> dp(200100, 0);

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

  cin >> S;

  ll N = S.size();

  REP(i, N) {
    for (int j = 1; i + j <= N; j++) {

      string sub = S.substr(i, j);
      ll ls = stoll(sub) % 2019;
      if (ls == 0) {
        dp.at(i)++;
      }
    }
  }

  ll ans = 0;
  REP(i, N) { ans += dp.at(i); }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

// return 10^n%2019
vector<int> vec(200030, 1);

int main() {
  for (auto i = 1; i < vec.size(); ++i) {
    vec[i] = (vec[i - 1] * 10) % 2019;
  }

  string s;
  cin >> s;
  int n = s.size();
  vector<int> T(n, 0);
  T[n - 1] = (s[n - 1] - '0') % 2019;
  for (auto i = n - 2; i >= 0; --i) {
    T[i] = ((s[i] - '0') * vec[(n - i - 1)] + T[i + 1]) % 2019;
  }
  map<int, int> mp;
  for (auto i = 0; i < T.size(); ++i) {
    mp[T[i]]++;
  }
  mp[0]++;
  ll ans = 0;
  for (const auto &e : mp) {
    ans += e.second * (e.second - 1) / 2;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

// return 10^n%2019
vector<int> vec(2020, 1);

int main() {
  for (int i = 1; i < (int)vec.size(); ++i) {
    vec[i] = (vec[i - 1] * 10) % 2019;
  }

  string s;
  cin >> s;
  int n = s.size();
  vector<int> T(n, 0);
  T[n - 1] = (s[n - 1] - '0') % 2019;
  for (int i = n - 2; i >= 0; --i) {
    T[i] = ((s[i] - '0') * vec[n - i - 1] + T[i + 1]) % 2019;
  }
  // for_each(T.begin(), T.end(),[](int x){cout<<x<<" ";});
  map<int, int> mp;
  for (int i = 0; i < (int)T.size(); ++i) {
    mp[T[i]]++;
  }
  mp[0]++;
  ll ans = 0;
  for (const auto &e : mp) {
    ans += e.second * (e.second - 1) / 2;
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
using namespace std;
typedef long long int ll;
typedef long double dl;
#define PI 3.14159265358979323846
#define Mod 1000000007
ll fast(ll n, ll a, ll m) {
  if (n == 0)
    return 1;
  else if (n == 1)
    return a;
  else {
    ll r = fast(n / 2, a, m);
    if (n % 2 == 0) {
      return (((r % m) * (r % m)) % m);
    } else {
      return (((((r % m) * a) % m) * r) % m);
    }
  }
}
ll ckck(ll N) {
  ll lo = 1, hi = 1e10;
  ll mid, ind;
  ll cnt;
  while (lo <= hi) {
    mid = lo + (hi - lo) / 2;
    cnt = mid * (mid + 1);
    cnt /= 2;
    cnt -= 1;
    if (N > cnt) {
      ind = mid;
      lo = mid + 1;
    } else {
      hi = mid - 1;
    }
  }
  return ind;
}

int main() {
  string s;
  cin >> s;
  int n = s.length();
  int cnt[2][2019];
  memset(cnt, 0, sizeof(cnt));
  int d;
  d = s[0] - '0';
  int i, j;
  cnt[0][d]++;
  cnt[1][d]++;
  ll ans = 0;
  for (i = 2; i <= n; i++) {
    for (j = 0; j < 2019; j++) {
      cnt[1][j] = 0;
    }
    d = s[i - 1] - '0';
    for (j = 0; j < 2019; j++) {

      cnt[1][(j * 10 + d) % 2019] += cnt[0][j];
    }

    cnt[1][d]++;
    ans += cnt[1][0];
    for (j = 0; j < 2019; j++) {
      cnt[0][j] = cnt[1][j];
    }
  }
  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
using namespace std;
typedef long long int ll;
typedef long double dl;
#define PI 3.14159265358979323846
#define Mod 1000000007
ll fast(ll n, ll a, ll m) {
  if (n == 0)
    return 1;
  else if (n == 1)
    return a;
  else {
    ll r = fast(n / 2, a, m);
    if (n % 2 == 0) {
      return (((r % m) * (r % m)) % m);
    } else {
      return (((((r % m) * a) % m) * r) % m);
    }
  }
}
ll ckck(ll N) {
  ll lo = 1, hi = 1e10;
  ll mid, ind;
  ll cnt;
  while (lo <= hi) {
    mid = lo + (hi - lo) / 2;
    cnt = mid * (mid + 1);
    cnt /= 2;
    cnt -= 1;
    if (N > cnt) {
      ind = mid;
      lo = mid + 1;
    } else {
      hi = mid - 1;
    }
  }
  return ind;
}
int main() {
  string s;
  cin >> s;
  int N = s.length();
  int i, j;
  ll res = 2019;
  int p[N + 1][res];
  ll ans = 0;
  memset(p, 0, sizeof(p));
  p[1][s[0] - '0']++;
  for (i = 2; i <= N; i++) {
    p[i][s[i - 1] - '0']++;
    for (j = 0; j < 2019; j++) {
      int d = s[i - 1] - '0';
      p[i][(j * 10 + d) % res] += p[i - 1][j];
    }
    ans += p[i][0];
  }
  cout << ans;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int m = 2019;

int main() {
  string S;
  cin >> S;
  vector<int> R(m, 0);
  int n = S.size();
  int sum = 0, x = 1;
  for (int i = n - 1; i >= 0; i--) {
    sum += (S.at(i) - '0') * x;
    sum %= m;
    R.at(sum)++;
    x = x * 10 % m;
  }
  R.at(0)++;

  int ans = 0;
  for (auto r : R) {
    if (r > 1)
      ans += r * (r - 1) / 2;
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

#include <boost/multiprecision/cpp_int.hpp>
using Bint = boost::multiprecision::cpp_int;

int main() {
  string S;
  cin >> S;
  vector<int> R(2019, 0);
  rep(i, S.size()) {
    Bint s(S.substr(i));
    Bint mod("2019");
    R.at((int)(s % mod))++;
  }
  R.at(0)++;

  int ans = 0;
  for (auto r : R) {
    if (r > 1)
      ans += r * (r - 1) / 2;
  }

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

#include <bits/stdc++.h> // clang-format off
using Int = long long;
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif // clang-format on

/**
 *    author:  knshnb
 *    created: Sun Apr 26 21:01:08 JST 2020
 **/

template <class T, class S> T make_vec(S x) { return x; }
template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) {
  return std::vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...));
}

const Int K = 2019;
signed main() {
  std::string s;
  std::cin >> s;
  Int n = s.size();
  auto dp = make_vec<Int>(2, K, 0);
  Int ans = 0;
  REP(i, n) {
    REP(j, K) {
      Int k = (10 * j + s[i] - '0') % K;
      dp[1][k] += dp[0][j];
    }
    dp[1][s[i] - '0'] += 1;
    ans += dp[1][0];
    std::swap(dp[0], dp[1]);
    std::fill(dp[1].begin(), dp[1].end(), 0);
  }
  std::cout << ans << std::endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h> // clang-format off
using Int = long long;
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif // clang-format on

/**
 *    author:  knshnb
 *    created: Sun Apr 26 21:01:08 JST 2020
 **/

template <class T, class S> T make_vec(S x) { return x; }
template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) {
  return std::vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...));
}

const Int K = 2019;
signed main() {
  std::string s;
  std::cin >> s;
  Int n = s.size();
  auto dp = make_vec<Int>(n + 1, K, 0);
  Int ans = 0;
  REP(i, n) {
    REP(j, K) {
      Int k = (10 * j + s[i] - '0') % K;
      dp[i + 1][k] += dp[i][j];
    }
    dp[i + 1][s[i] - '0'] += 1;
    ans += dp[i + 1][0];
  }
  std::cout << ans << std::endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  string s;
  cin >> s;
  int n = s.size();
  int m = 2019;
  reverse(s.begin(), s.end());
  int x = 1, total = 0;
  vector<int> val(2019, 0);
  val[total] = 1;
  ll ans = 0;
  rep(i, n) {
    total += (s[i] - '0') * x;
    total %= m;
    val[total]++;
    x = 10 * x % m;
  }
  rep(j, 2019) ans += (val[j] - 1) * val[j] / 2;
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

vector<int> pass[2005]; //         // dis[i] : startからiまでの距離

int main() {
  string s;
  cin >> s;
  ll ans = 0;
  ll a;
  for (ll k = 4; k <= s.size(); k++) {
    for (ll i = 0; i < s.size() - k + 1; i++) {
      string t = s.substr(i, k);
      a = stoll(t);
      if (a % (ll)2019 == 0)
        ans++;
    }
  }
  cout << ans << 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 < n; i++)
#define ll long long

signed main() {
  string S;
  cin >> S;
  int size = S.size();
  ll num = 0, keta = 1, sum = 0;
  int mod[2019] = {1, 0};
  reverse(S.begin(), S.end());
  rep(i, size) {
    num = (S.at(i) - '0') * keta;
    sum += num;
    mod[sum % 2019]++;
    keta *= 10;
    keta %= 2019;
  }
  int cnt = 0;
  rep(i, 2019) {
    if (mod[i] > 1)
      cnt += (mod[i] * (mod[i] - 1)) / 2;
  }
  cout << cnt << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  string S;
  cin >> S;
  int size = S.size();
  long long num = 0, keta = 10000;
  int mod[2019] = {0};
  rep(i, size) {
    num *= 10;
    num += (S.at(i) - '0');
  }
  //	cout<<num;
  rep(i, size - 3) {
    mod[(num % keta) % 2019]++;
    //	cout<<num%keta;
    keta *= 10;
  }
  int cnt = mod[0];
  //	rep(i,2019){ if(mod[i]>0)co(mod[i]);}
  rep(i, 2019) {
    if (i > 0 && mod[i] > 0 && mod[i] % 2 == 0)
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
#include <string.h>
int count = 0;

int main() {
  char S[200000];
  int count[2019];

  for (int i = 0; i < 2019; ++i) {
    count[i] = 0;
  }

  scanf("%s", S);

  int num = strlen(S);
  int ruiseki[num + 1];
  ruiseki[num] = 0;
  int digit = 1;

  for (int i = num - 1; i >= 0; i--) {
    ruiseki[i] = (ruiseki[i + 1] + digit * (S[i] - '0')) % 2019;
    digit = digit * 10 % 2019;
  }
  for (int i = 0; i < num + 1; i++) {
    count[ruiseki[i]]++;
  }

  int ans = 0;
  for (int i = 0; i < 2019; i++) {
    ans += (count[i] - 1) * count[i] / 2;
  }
  printf("%d", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <string.h>
int count = 0;

int main() {
  char S[200000];
  char t[200000];
  scanf("%s", S);
  int num = strlen(S);
  for (int i = 0; i < num - 3; ++i) {
    for (int j = i + 3; j < num; ++j) {
      if (atoi(strncpy(t, S + i, j)) % 2019 == 0) {
        count++;
      }
    }
  }
  printf("%d", count);
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  ll res = 0;
  vector<ll> bucket(2020, 0);
  int n = in.size();
  for (int i = 0; i < n; i++) {
    vector<ll> bucketNext(2020, 0);
    for (int j = 0; j < 2020; j++) {
      int next = (j * 10 + (in[i] - '0')) % 2019;
      bucketNext[next] += bucket[j];
    }
    res += bucketNext[0];
    bucket = bucketNext;
    bucket[in[i] - '0']++;
  }
  printf("%lld\\n", res);
}


Error: TLE

Fixed submission:

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

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

  ll res = 0;
  int n = in.size();
  for (int i = 0; i < n; i++) {
    ll mod = in[i] - '0';
    for (int j = i + 1; j < n; j++) {
      mod = mod * 10 + (in[j] - '0');
      mod %= 2019;
      if (mod == 0)
        res++;
    }
  }
  printf("%lld\\n", res);
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

constexpr int mod = 2019;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  string s;
  cin >> s;

  int n = s.size();
  map<ll, ll> mp;
  mp[0] = 1;

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

  int ten = 1;
  int cur = 0;
  for (int i = 0; i < n; i++) {
    cur = cur + (s[i] - '0') * ten;
    cur %= mod;
    mp[cur]++;
    ten *= 10;
    ten %= mod;
  }

  ll ans = 0;
  for (auto it : mp) {
    ans += it.second * (it.second - 1) / 2;
  }
  cout << ans << '\\n';
}

Error: MLE

Fixed submission:

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

const int N = 200020;
const int mod = 2019;

int dp[200020][2020];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  string s;
  cin >> s;

  int n = s.size();
  int val = s[0] - '0';

  dp[0][val] = 1;
  for (int i = 1; i < n; i++) {
    dp[i][s[i] - '0']++;
    for (int j = 0; j < 2019; j++) {
      int v = j * 10 + (s[i] - '0');
      v %= mod;
      dp[i][v] += dp[i - 1][j];
    }
  }

  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans += dp[i][0];
  }
  cout << ans << endl;
}