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

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

#define ll long long
#define pb push_back
#define mp make_pair
#define f(i, a, n) for (int i = a; i < (int)n; i++)
#define rf(i, n, a) for (int i = n; i >= a; i--)
#define F first
#define S second
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()
#define fast ios::sync_with_stdio(false), cin.tie(nullptr);

typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef vector<pii> vpi;

const int inf = 1e9;
const int MOD = inf + 7;
int dp[2019];
int dp1[2019];

string s;
int n, c, ans, i, j;
int32_t main() {
  fast;
  cin >> s;

  n = sz(s);
  dp[s[0] - '0']++;

  for (i = 1; i < n; i++) {
    for (j = 0; j < 2019; j++)
      dp1[j] = 0;

    c = s[i] - '0';
    dp1[c]++;

    for (j = 0; j < 2019; j++) {
      dp1[(j * 10 + c) % 2019] += dp[j];
    }
    ans += dp1[0];

    for (int j = 0; j < 2019; j++)
      dp[j] = dp1[j];
  }

  cout << ans;
}


Error: TLE

Fixed submission:

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

#define ll long long
#define pb push_back
#define mp make_pair
#define f(i, a, n) for (int i = a; i < (int)n; i++)
#define rf(i, n, a) for (int i = n; i >= a; i--)
#define F first
#define S second
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()
#define fast ios::sync_with_stdio(false), cin.tie(nullptr);

typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef vector<pii> vpi;

const int inf = 1e9;
const int MOD = inf + 7;
map<pii, int> dp;
string s;
int n, c, ans, i, j;
int32_t main() {
  fast;
  cin >> s;

  n = sz(s);

  for (i = 0; i < n; i++) {
    c = s[i] - '0';
    dp[{i, c}]++;
    if (i == 0)
      continue;
    for (j = 0; j < 2019; j++) {
      dp[{i, (j * 10 + c) % 2019}] += dp[{i - 1, j}];
    }
  }

  ans = 0;
  for (i = 0; i < n; i++)
    ans += dp[{i, 0}];

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

#include <bits/stdc++.h>

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

  std::vector<int> cnt(2019, 0);
  cnt[0] = 1;
  long long num = 0;
  int digit = 1;
  for (int i = S.size() - 1; i >= 0; i--) {
    num = (num + (S[i] - '0') * digit) % 2019;
    cnt[num]++;
    digit = (digit * 10) % 2019;
  }

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

  std::cout << ans << std::endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

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

  std::vector<int> test(10000);
  std::vector<int> test2(10000);
  int tmp2019 = 0;
  for (int i = 0; i < 10000; i++) {
    tmp2019 += 2019;
    int index = tmp2019 % 10000;
    int value = tmp2019 / 10000;
    test[index] = value;
    int dec = 0;
    while (value != 0) {
      value /= 10;
      dec++;
    }
    test2[index] = dec;
  }

  std::vector<std::pair<int, int>> find;
  for (int i = 0; i < S.size() - 3; i++) {
    int num = 0;
    for (int j = 0; j < 4; j++) {
      int tmp = S[i + j] - '0';
      num *= 10;
      num += tmp;
    }
    int value = test[num];
    int dec = test2[num];
    int num2 = 0;
    for (int j = dec; j > 0; j--) {
      if (i - j < 0) {
        num2 = -1;
        break;
      }
      int tmp = S[i - j] - '0';
      num2 *= 10;
      num2 += tmp;
    }
    if (num2 == value) {
      std::pair<int, int> findcur;
      findcur.first = i - dec;
      findcur.second = i + 3;
      find.push_back(findcur);
    }
  }

  int cnt = 0;
  int cnt_cont = 1;
  if (find.size() == 0) {
    std::cout << 0 << std::endl;
  } else {
    int cnt = 1;
    int cnt_cont = 1;
    for (int i = 1; i < find.size(); i++) {
      if (find[i].first + 1 == find[i - 1].second) {
        cnt_cont++;
      } else {
        cnt_cont = 1;
      }
      cnt += cnt_cont;
    }
    std::cout << cnt << std::endl;
  }

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

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

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

  string s;
  cin >> s;

  ll ans = 0;
  vector<ll> rem(2019, 0);
  rem[0] = 1;
  ll n = 0, ex = 1;
  for (ll i = (ll)s.size() - 1; i >= 0; i--) {
    n = (n + (s[i] - '0') * ex) % 2019;
    ans += rem[n];
    rem[n]++;
    ex = ex * 10 % 2019;
  }

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

Error: 0

Fixed submission:

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(18);

  string s;
  cin >> s;
  ll n = s.size();

  if (n < 4) {
    cout << 0 << "\\n";
    return 0;
  }

  ll ans = 0;
  for (ll i = 0; i <= n - 4; i++) {
    ll I = stoll(s.substr(i));
    for (ll j = i + 4; j <= n; j++) {
      ll J = (j == n ? 0LL : stoll(s.substr(j)));
      if ((I - J) % 2019 == 0)
        ans++;
    }
  }

  cout << ans << "\\n";
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {

  int sz;
  long long int tmp, ans = 0;
  string s;
  vector<int> v, d;

  cin >> s;
  sz = s.size();
  d.resize(sz, 0);
  v.resize(2040, 0);

  tmp = 1;
  for (int i = 0; i < sz; i++) {
    d[i] = tmp % 2019;
    tmp = 10 * d[i];
    // cout << d[i] << endl;
  }

  v[0] = 1;

  tmp = 0;
  for (int i = 0; i < sz; i++) {
    /*for (int j = 0;j < 2040;j++) {
            if (v[j] > 0) cout << j << " ";
    }
    cout << " #" << endl;*/

    tmp += (int)(s[sz - i - 1] - '0') * d[i];
    tmp = tmp % 2019;
    // cout << tmp << endl;
    ans += v[tmp];
    // if (v[tmp] > 0) cout << i << " " << v[tmp] << endl;
    v[tmp]++;
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {

  int sz;
  long long int tmp, ans = 0;
  string s;
  vector<int> v, d;

  cin >> s;
  sz = s.size();
  d.resize(2040, 0);
  v.resize(2040, 0);

  tmp = 1;
  for (int i = 0; i < 2040; i++) {
    d[i] = tmp % 2019;
    tmp = 10 * d[i];
    // cout << d[i] << endl;
  }

  v[0] = 1;

  tmp = 0;
  for (int i = 0; i < sz; i++) {
    /*for (int j = 0;j < 2040;j++) {
            if (v[j] > 0) cout << j << " ";
    }
    cout << " #" << endl;*/

    tmp += (int)(s[sz - i - 1] - '0') * d[i];
    tmp = tmp % 2019;
    // cout << tmp << endl;
    ans += v[tmp];
    // if (v[tmp] > 0) cout << i << " " << v[tmp] << endl;
    v[tmp]++;
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
int k[2020], f[2000005], a[2000005], g[2000005];
long long sum;
int main() {
  string s;
  cin >> s;
  int n = s.size();
  for (int i = 0; i < n; i++) {
    a[i + 1] = s[i] - '0';
  }
  int l = 1;
  for (int i = n; i >= 1; i--) {
    g[i] = l;
    l *= 10;
    l %= 2019;
  }
  for (int i = n; i >= 1; i--) {
    f[i] = (f[i + 1] + a[i] * g[i]) % 2019;
    k[f[i]]++;
  }
  sum = k[0];
  sum += (k[0] * (k[0] - 1) / 2);
  for (int i = 1; i <= 2018; i++) {
    sum += (k[i] * (k[i] - 1) / 2);
  }
  cout << sum;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
  string s;
  cin >> s;
  int i, j, y, l = 0;
  for (i = 0; i < s.size() - 4; ++i) {
    for (j = i + 3; j <= s.size(); ++j) {
      l = 0;
      string x = s.substr(i, j - i);
      for (int m = 0; m < x.size(); ++m) {
        l = l * 10 + (x[m] - '0');
      }

      if (l % 2019 == 0) {

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;
typedef vector<int> vi;

const double PI = acos(-1.0);
const int MOD = 2019;

string S;
int N;
int dp[2019];
void input() {
  cin >> S;
  for (int i = 0; i < 2019; ++i)
    dp[i] = 0;
  dp[0]++;
}

void solve() {
  N = S.length();
  int num[N];
  int POW = 1;
  num[0] = S[N - 1] - '0';
  dp[num[0]]++;
  for (int i = 1; i < N; ++i) {
    POW = (POW * 10) % MOD;
    num[i] = (num[i - 1] + POW * (S[N - 1 - i] - '0')) % MOD;
    dp[num[i]]++;
  }
  int ans = 0;
  for (int i = 0; i < 2019; ++i) {
    ans += dp[i] * (dp[i] - 1) / 2;
  }
  cout << ans << endl;
}

int main() {
  cin.tie();
  ios::sync_with_stdio(false);
  input();
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;
typedef vector<int> vi;

const double PI = acos(-1.0);
const int MOD = 2019;

string S;
int N;
int dp[2019];
void input() {
  cin >> S;
  for (int i = 0; i < 2019; ++i)
    dp[i] = 0;
  dp[0]++;
}

int POW(int a, int b) {
  int c = a;
  for (int i = 0; i < b - 1; ++i)
    c = (c * a) % MOD;
  return c;
}

void solve() {
  N = S.length();
  int num[N];
  num[0] = S[N - 1] - '0';
  dp[num[0]]++;
  for (int i = 1; i < N; ++i) {
    num[i] = (num[i - 1] + POW(10, i) * (S[N - 1 - i] - '0')) % MOD;
    dp[num[i]]++;
    // cout<<num[i]<<"\\n";
  }
  int ans = 0;
  for (int i = 0; i < 2019; ++i) {
    ans += dp[i] * (dp[i] - 1) / 2;
  }
  cout << ans << endl;
}

int main() {
  cin.tie();
  ios::sync_with_stdio(false);
  input();
  solve();
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a <= b)
    return 0;
  a = b;
  return 1;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a >= b)
    return 0;
  a = b;
  return 1;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  string s;
  cin >> s;
  int n = s.size();
  ll dp[2][2020] = {};
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    int now = i % 2, nx = (i + 1) % 2;
    for (int j = 0; j < 2019; j++) {
      dp[nx][j] = 0;
    }
    dp[nx][s[i] - '0']++;
    for (int j = 0; j < 2019; j++) {
      dp[nx][(j * 10 + (s[i] - '0')) % 2019] += dp[now][j];
    }
    ans += dp[nx][0];
  }
  cout << ans << endl;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a <= b)
    return 0;
  a = b;
  return 1;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a >= b)
    return 0;
  a = b;
  return 1;
}

int dp[200002][2020];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  string s;
  cin >> s;
  int n = s.size();
  // reverse(s.begin(),s.end());

  // dp[0][0] = 1;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    dp[i + 1][s[i] - '0']++;
    for (int j = 0; j < 2019; j++) {
      dp[i + 1][(j * 10 + (s[i] - '0')) % 2019] += dp[i][j];
    }
    ans += dp[i + 1][0];
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <limits>
#include <queue>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;

ll solve(const std::string s) {
  const int len = s.length();
  std::vector<int> count(2019, 0);
  std::vector<int> mul(2019, 0);
  std::vector<int> count_next(2019, 0);
  std::vector<int> mul_next(2019, 0);

  for (int i = 0; i < len; i++) {

    int n = s[i] - '0';
    if (i >= 3) {
      int m = (s[i - 3] - '0') * 100 + (s[i - 2] - '0') * 10 + (s[i - 1] - '0');
      mul[m]++;
    }

    for (int j = 0; j < 2019; j++) {
      int next = (j * 10 + n) % 2019;
      if (next == 0) {
        count[j] += mul[j];
      }
      count_next[next] = count[j];
      mul_next[next] = mul[j];
    }
    count_next.swap(count);
    mul_next.swap(mul);
  }

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

  return ans;
}

int main(int argc, char *argv[]) {
  std::string s;

  std::cin >> s;

  ll ans = solve(s);

  std::cout << ans << std::endl;

  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <limits>
#include <queue>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;

int solve(const std::string s, const int start, std::vector<int> &cache) {
  const int len = s.length();
  std::vector<std::pair<int, int>> marker;

  int n = 0;
  int count = 0;

  if (cache[start] != -1) {
    return cache[start];
  }

  for (int i = start; i < len; i++) {
    n = n * 10 + s[i] - '0';
    n %= 2019;
    if (n == 0) {
      count++;
      marker.push_back(std::make_pair(i + 1, count));
    }
  }

  cache[start] = count;
  for (const auto &it : marker) {
    if (it.first >= len) {
      break;
    }
    cache[it.first] = count - it.second;
  }

  return count;
}

int main(int argc, char *argv[]) {
  std::string s;

  std::cin >> s;

  const int len = s.length();
  std::vector<int> cache(len, -1);
  ll ans = 0;
  for (int i = 0; i < len; i++) {
    ans += solve(s, i, cache);
  }

  std::cout << ans << std::endl;

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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>

// #include <bits/stdc++.h>
using namespace std;
#define print(x) cout << (x) << endl
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ll long long int
#define pb push_back

long long N;
string S;
int P = 2019;

long long solve() {
  if (P == 2 || P == 5) {
    long long res = 0;
    for (int i = 0; i < N; ++i)
      if ((S[N - i - 1] - '0') % P == 0)
        res += N - i;
    return res;
  }
  vector<long long> val(P, 0);
  long long tenfactor = 1;
  long long cur = 0;
  val[cur]++;
  for (int i = 0; i < N; ++i) {
    cur = (cur + (S[N - i - 1] - '0') * tenfactor) % P;
    tenfactor = (tenfactor * 10) % P;
    val[cur]++;
  }
  long long res = 0;
  for (int p = 0; p < P; ++p)
    res += val[p] * (val[p] - 1) / 2;
  return res;
}

int main() {
  cin >> S;
  N = S.size();
  cout << solve() << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>

// #include <bits/stdc++.h>
using namespace std;
#define print(x) cout << (x) << endl
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ll long long int
#define pb push_back

int main() {
  string s;
  ll tmp, cnt = 0, N, u, t = 6;
  cin >> s;
  N = s.size();
  // 2019の倍数を文字列でくっつけていく
  for (int i = 4; i <= N; i++) {
    for (int j = 0; j <= N - i; j++) {
      if (i < t) {
        tmp = stoll(s.substr(j, i)); // longlongで足りなくなったらREになる
        if (tmp % 2019 == 0)
          cnt++;
      } else { // stollが死ぬ場合
        tmp = stoll(s.substr(j, t));
        u = tmp % 2019;
        for (int k = j + t; k < j + i; k++) {
          if (j + i - k < t)
            t = k;
          tmp = stoll(s.substr(k, t));
          u += pow(10, t) * u + tmp;
          u %= 2019;
          if (j + i - k < t) {
            k--;
            k += t;
          }
        }
        t = 6;
        if (u == 0)
          cnt++;
      }
    }
  }
  cout << cnt << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define rep(i, n)                                                              \\
  ;                                                                            \\
  for (long long i = 0; i < (n); i++)
using ll = long long;
using P = pair<long long, long long>;
template <class T> using vec = vector<T>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  ll n = s.size();
  ll m = 1;
  vec<ll> t(2019);
  t[0] = 1;
  ll ans = 0, cur = 0;
  rep(i, n) {
    cur = ((cur + (s[n - i - 1] - '0') * m) % 2019 + 2019) % 2019;
    m = m * 10 % 2019;
    ans += t[cur];
    t[cur]++;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
#define rep(i, n)                                                              \\
  ;                                                                            \\
  for (long long i = 0; i < (n); i++)
using ll = long long;
using P = pair<long long, long long>;
template <class T> using vec = vector<T>;
const ll mod = 1e9 + 7;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  ll n = s.size();
  vec<vec<ll>> dp(n + 1, vec<ll>(2020, 0));
  rep(i, n) {
    dp[i + 1][(s[i] - '0')]++;
    rep(j, 2019) {
      dp[i + 1][((j * 10 + (s[i] - '0')) % 2019 + 2019) % 2019] += dp[i][j];
    }
  }
  ll ans = 0;
  rep(i, n + 1) ans += dp[i][0];
  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)
typedef long long ll;
typedef pair<int, int> P;

const int m = 2019;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  reverse(s.begin(), s.end());
  int x = 1, tot = 0;
  vector<int> cnt(m);
  ll ans = 0;
  rep(i, n) {
    cnt[tot]++;
    tot += (s[i] - '0') * x; // 累積和
    tot %= m;                // 累積和のmod
    ans += cnt[tot]; // 今まででてきた累積和の数を加える(右側固定)
    x = x * 10 % m;
  }
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef float fl;
typedef long long ll;
string S;
ll tmp[200001];

int main() {
  cin >> S;
  int n = S.length();
  int p = 0;
  for (;;) {
    tmp[p] = 2019 * (p + 1);
    if (tmp[p] > (ll)atoi(S.c_str()))
      break;
    p++;
  }

  int count = 0;
  for (int i = 0; i < p; i++) {
    ll key = tmp[i];
    string str_key = to_string(key);
    for (int j = 0; j < n; j++) {
      if ((j + str_key.length()) <= n) {
        if (str_key == S.substr(j, str_key.length()))
          count++;
      } else {
        break;
      }
    }
  }
  cout << count << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;

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

  ll ans = 0;
  ll r[200001];
  unordered_map<ll, ll> mp;

  r[s.length()] = 0;
  mp[0] = 1;
  ll d = 1;
  for (ll i = s.length() - 1; i >= 0; i--) {
    r[i] = (r[i + 1] + (s[i] - '0') * d) % 2019;
    if (mp.find(r[i]) == mp.end()) {
      mp[r[i]] = 1;
    } else {
      mp[r[i]] += 1;
    }
    d = d * 10 % 2019;
  }

  for (auto it = mp.begin(); it != mp.end(); it++) {
    ans += (*it).second * ((*it).second - 1) / 2;
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;

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

  ll ans = 0;

  for (int i = 0; i < s.length() - 4; i++) {
    ll n = (s[i] - '0') * 1000 + (s[i + 1] - '0') * 100 +
           (s[i + 2] - '0') * 10 + (s[i + 3] - '0');
    ll t = n % 2019;
    for (int j = i + 4; j < s.length(); j++) {
      t = t * 10 + (s[j] - '0');
      if (t % 2019 == 0) {
        ans += 1;
      }
    }
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
#include <cctype>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) (v).begin(), (v).end()
typedef long long int ll;
#define pi 3.1415926535897932384
#define E9 1000000000
#define eps 1e-4
#define pii pair<int, int>

int main() {
  string S;
  cin >> S;
  int N = S.size();
  int d[N];
  rep(i, N) d[i] = (int)(S[i] - '0');

  vector<int> powmod(N); // powmod[i]: 10^iを2019で割った余り
  powmod[0] = 1;
  rep(i, N) {
    if (i == 0)
      continue;
    powmod[i] = (powmod[i - 1] * 10) % 2019;
  }

  vector<int> acum(N, 0);
  vector<int> cnt(2019, 0);
  cnt[0] = 1;
  for (int j = 0; j < N; j++) {
    if (j == 0)
      acum[j] = d[N - 1 - j];
    else
      acum[j] = (acum[j - 1] + d[N - 1 - j] * powmod[j]) % 2019;

    cnt[acum[j]]++;
  }

  int retval = 0;
  rep(i, 2019) retval += cnt[i] * (cnt[i] - 1) / 2;
  cout << retval << endl;

  // cout << fixed << setprecision(6);
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  stoi


Fixed submission:

#include <bits/stdc++.h>
#include <cctype>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) (v).begin(), (v).end()
typedef long long int ll;
#define pi 3.1415926535897932384
#define E9 1000000000
#define eps 1e-4
#define pii pair<int, int>

int main() {
  string S;
  cin >> S;
  int N = S.size();
  int cnt = 0;
  for (int i = 0; i < N - 3; i++) {
    for (int l = 3; l < N - i; l++) {
      if (stoi(S.substr(i, l)) % 2019 == 0) {
        cnt++;
        l += 3;
      }
    }
  }

  cout << cnt << endl;

  // cout << fixed << setprecision(6);
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
string S;
long cnt[2019];
int main() {
  cin >> S;
  int now = 0;
  long ans = 0, p = 1, pi = 1;
  cnt[0] = 1;
  for (int i = S.size(); i--;) {
    now = (now + (S[i] - '0') * p) % 2019;
    ans += cnt[now]++;
    p = p * 10 % 2019;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int main() {
  string S;
  cin >> S;
  int N = S.size(), cnt = 0, i = 0, j = 1;
  for (; i < N - 4; i++) {
    for (j = 4; j < N; j++) {
      string tmp = S.substr(i, j);
      long long num = atol(tmp.c_str());
      if (num % 2019 == 0) {
        cnt++;
        break;
      }
    }
  }
  i = j;
  cout << cnt << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

int main(int, char **) {
  std::string s;
  std::cin >> s;
  int l = s.size();
  std::map<int, int> mods;
  int prev = 0;
  int prev10 = 1;
  mods[0] = 1;
  for (int i = l; i > 0; i--) {
    int digit = s.at(i - 1) - '0';
    int mod = (prev + prev10 * digit) % 2019;
    prev = mod;
    prev10 = prev10 * 10 % 2019;
    if (mods.count(mod)) {
      mods.at(mod)++;
    } else {
      mods[mod] = 1;
    }
  }
  int count = 0;
  for (auto x : mods) {
    int value = x.second;
    count += value * (value - 1) / 2;
    //      std::cout << x.first << " " << x.second << std::endl;
  }
  std::cout << count << std::endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

int main(int, char **) {
  long long c = 0;
  std::string s;
  std::cin >> s;
  long long l = s.size();
  for (long long i = 4; i < l + 1; i++) {
    for (long long j = 0; j < l - i + 1; j++) {
      std::string sub = s.substr(j, i);
      long long sub_num = std::stoll(sub);
      if (sub_num % 2019 == 0) {
        c++;
      }
    }
  }
  std::cout << c << std::endl;
  return 0;
}