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


/* Author: Rennan Rocha
 * Time: $%Y%$-$%M%$-$%D%$ $%h%$:$%m%$:$%s%$
 **/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define F first
#define S second
#define PB push_back
#define MP make_pair

#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define pi pair<int, int>
#define pll pair<ll, ll>
#define vpi vector<pi>
#define vpll vector<pll>

#define INF 1e18
#define MOD 1000000007

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
//*find_by_order(); order_of_key();

ll freq[2019];
char s[200010];
ll m[200010];

int main() {
  ios_base::sync_with_stdio(false);
  scanf("%s", s);
  int n = strlen(s);
  m[0] = 1;
  for (int i = 1; i <= 200000; i++) {
    m[i] = (m[i - 1] * 10) % 2019;
  }
  ll ans = 0;
  ll acum = s[n - 1] - '0';
  freq[acum]++;
  freq[0]++;
  for (int i = n - 2; i >= 0; i--) {
    acum = ((s[i] - '0') * m[n - i - 1] + acum) % 2019;
    if (i < n - 3)
      ans += freq[acum];
    freq[acum]++;
    // cout << "acum: " << acum << endl;
  }
  printf("%lld\\n", ans);
}


Error: TLE

Fixed submission:


/* Author: Rennan Rocha
 * Time: $%Y%$-$%M%$-$%D%$ $%h%$:$%m%$:$%s%$
 **/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define F first
#define S second
#define PB push_back
#define MP make_pair

#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define pi pair<int, int>
#define pll pair<ll, ll>
#define vpi vector<pi>
#define vpll vector<pll>

#define INF 1e18
#define MOD 1000000007

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
//*find_by_order(); order_of_key();

int dp[200001][2019];
char s[200010];
int n;
int solve(int i, int r) {
  if (i >= n)
    return 0;
  if (dp[i][r] != -1)
    return dp[i][r];
  int val = ((r * 10) + (s[i] - '0'));
  while (val >= 2019)
    val -= 2019;
  return dp[i][r] = (val == 0) + solve(i + 1, val);
}

int main() {
  ios_base::sync_with_stdio(false);
  memset(dp, -1, sizeof dp);
  scanf("%s", s);
  n = strlen(s);
  ll ans = 0;
  for (int i = 0; i < n - 3; i++) {
    if (s[i] != '0') {
      ans += solve(i, 0);
    }
  }
  printf("%lld\\n", ans);
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1'000'000'007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a - 1; (ll)(b) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define rALL(x) x.rbegin(), x.rend()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

vector<vector<char>> g;
int main() {
  string s;
  cin >> s;
  map<ll, ll> mp;
  mp[0] = 1;
  reverse(ALL(s));
  ll now = 0, dig = 1;
  rep(i, s.size()) {
    now = (now + (s[i] - '0') * dig) % 2019;
    dig *= 10;
    dig %= 2019;
    mp[now]++;
  }
  ll res = 0;
  for (auto x : mp)
    res += x.second * (x.second - 1) / 2;
  cout << res << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  string s;
  cin >> s;
  int n = s.size(), ans = 0;
  vector<pair<int, int>> maybe;
  if (n < 4)
    ans = 0;
  else {
    vector<ll> sum3(n + 1, 0);
    for (int i = 0; i < n; i++)
      sum3[i + 1] = sum3[i] + (s[i] - '0');

    for (int i = 0; i < n - 3; i++) {
      for (int j = i + 4; j <= n; j++) {
        if ((sum3[j] - sum3[i]) % 3 == 0)
          maybe.push_back({i, j - 1});
      }
    }

    ll bef = -1, befl = -1, sum = 0;
    for (auto x : maybe) {
      int now = x.first, l = x.second;
      if (now == bef)
        now = befl + 1;
      else
        sum = 0;
      for (int i = now; i <= l; i++) {
        sum = (s[i] - '0') + sum * 10;
      }
      if (sum % 2019 == 0)
        ans += 1;
      bef = x.first;
      befl = l;
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>
#define N 2019
typedef long long LL;
using namespace std;
char s[200005];
short dp[200005][N + 1];
int main() {
  memset(dp, 0LL, sizeof(dp));
  scanf("%s", s + 1);
  int ls = strlen(s + 1);
  int a;
  LL ans = 0;
  for (int i = 1; i <= ls; i++) {
    a = s[i] - '0';
    dp[i][a % N]++;
    for (int j = 0; j < N; j++)
      dp[i][(a + j * 10) % N] += dp[i - 1][j];
    ans += dp[i][0];
  }
  printf("%lld\\n", ans);
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>
#define N 2019
typedef long long LL;
using namespace std;
char s[305000];
short dp[305000][N + 1];
int main() {
  memset(dp, 0LL, sizeof(dp));
  scanf("%s", s + 1);
  int ls = strlen(s + 1);
  int a;
  LL ans = 0;
  for (int i = 1; i <= ls; i++) {
    a = s[i] - '0';
    dp[i][a % N]++;
    for (int j = 0; j < N; j++)
      dp[i][(a + j * 10) % N] += dp[i - 1][j];
    ans += dp[i][0];
  }
  printf("%lld\\n", ans);
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;
typedef long long ll;

const int MX = 2019;
int A[MX], B[MX];

int main() {
  cin.sync_with_stdio(0);

  string s;
  cin >> s;
  int n = s.length();

  ll ans = 0;

  for (int i = 0; i < n; ++i) {
    int me = s[i] - '0';
    for (int j = 0; j < MX; ++j) {
      if (A[j] == 0)
        continue;
      B[(j * 10 + me) % MX] += A[j];
    }
    B[me]++;
    ans += 1ll * B[0];
    for (int j = 0; j < MX; ++j)
      A[j] = B[j], B[j] = 0;
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define long long long int

using namespace std;

int main() {
  cin.sync_with_stdio(0);

  string s;
  cin >> s;
  int n = s.length();

  long ans = 0;
  map<int, long> cnt;

  for (int i = 0; i < n; ++i) {
    map<int, long> now;
    int me = s[i] - '0';
    for (auto j : cnt) {
      int new_j = ((j.first * 10) + me) % 2019;
      now[new_j] += j.second;
    }
    now[me]++;
    swap(now, cnt);
    ans += cnt[0];
  }

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

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

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

int main() {

  int mod = 2019;

  string S;
  cin >> S;

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

  long long sum;
  vector<int> v(mod);

  long long count = 1;
  long long answer = 0;

  int m = 0;

  for (int i = 0; i < N; i++) {
    v[m]++;
    sum += (S[i] - '0') * count % mod;
    count = count * 10 % mod;
    m = sum % mod;
    answer += v[m];
  }

  cout << answer << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {

  string S;
  cin >> S;

  int answer = 0;
  set<pair<int, int>> ans;
  int start = 0;
  int end = 4;
  long long current = 0;

  if (S.size() <= 3) {
    cout << 0 << endl;
    return 0;
  } else {
    for (int i = 0; i < 4; i++) {
      current += ((int)S[S.size() - 1 - i] - '0') * pow(10, i);
    }
  }

  while (end < S.size()) {
    if (current % 2019 == 0) {
      // answer++;
      ans.insert(make_pair(S.size() - end + 1, S.size() - start));
      // cout << S.size() - end + 1 << " " << S.size() - start << endl;
      // cout << current << endl;
    }
    current += ((int)S[S.size() - end - 1] - '0') * pow(10, end - start);
    // cout << current << endl;
    if (end == S.size() - 1) {
      start++;
      end = start + 4;
      current = 0;
      for (int i = start; i < end; i++) {
        current += ((int)S[S.size() - i - 1] - '0') * pow(10, i - start);
      }
      // cout << current << endl;
    } else {
      end++;
    }
  }

  start = 0;
  end = 4;
  current = 0;

  for (int i = 0; i < 4; i++) {
    current += ((int)S[i] - '0') * pow(10, 3 - i);
  }

  while (start < S.size() - 3) {
    if (current % 2019 == 0) {
      answer++;
      // cout << start << " " << end + 1 << endl;
      // cout << current << endl;
      ans.insert(make_pair(start, end + 1));
    }
    current = current * 10 + ((int)S[start] - '0');
    // cout << current << endl;
    if (start == S.size() - 4) {
      end++;
      start = end - 4;
      current = 0;
      for (int i = start; i < end; i++) {
        current += ((int)S[i] - '0') * pow(10, 3 - (i - start));
      }
      // cout << current << endl;
    } else {
      start++;
    }
  }

  cout << ans.size() << endl;

  return 0;
}
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;
typedef pair<int, int> P;
typedef vector<vector<int>> Graph;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

string s;
ll dp[200020];
map<ll, ll> mp;

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

ll solve() {
  int n = s.size();
  mp[0]++;
  dp[n] = 0;
  for (int i = n - 1; i >= 0; i--) {
    dp[i] = dp[i + 1] + POW(10, n - i - 1) * (s[i] - '0');
    dp[i] %= 2019;
    mp[dp[i]]++;
  }
  ll ans = 0;
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    ll res = itr->second;
    res = res * (res - 1) / 2;
    ans += res;
  }
  return ans;
}

int main() {
  cin >> s;
  cout << solve() << 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;
typedef pair<int, int> P;
typedef vector<vector<int>> Graph;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

string s;
ll dp[200020];
map<ll, ll> mp;

ll POW(int x) {
  ll res = 1;
  while (x > 0) {
    res *= 10;
    res %= 2019;
    x--;
  }
  return res;
}

ll solve() {
  int n = s.size();
  mp[0]++;
  dp[n] = 0;
  for (int i = n - 1; i >= 0; i--) {
    dp[i] = dp[i + 1] + POW(n - i - 1) * (s[i] - '0');
    dp[i] %= 2019;
    mp[dp[i]]++;
  }
  ll ans = 0;
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    ll res = itr->second;
    res = res * (res - 1) / 2;
    ans += res;
  }
  return ans;
}

int main() {
  cin >> s;
  cout << solve() << 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;
  reverse(S.begin(), S.end());
  int dig = 1, sum = 0;
  map<int, int> MA;
  MA[0]++;
  for (int i = 0; i < S.size(); i++) {
    (sum += (S.at(i) - '0') * dig) %= 2019;
    (dig *= 10) %= 2019;
    MA[sum]++;
  }
  long ans = 0;
  for (auto ma : MA)
    ans += ma.second * (ma.second - 1) / 2;
  cout << ans << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::cpp_int;
using namespace std;

int main() {
  string S;
  cin >> S;
  long N = S.size();

  long ans = 0;
  for (long r = 0; r < N; r++) {
    for (long l = 0; l < r - 2; l++) {
      cpp_int tmp(S.substr(l, r - l + 1));
      cpp_int cp = 2019;
      if (tmp % cp != 0)
        continue;
      ans++;
    }
  }

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

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

#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

const ll mod = 1000000007;

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

  string s;
  cin >> s;

  int n = s.length();
  vector<int> t(n);
  for (int i = 0; i < n; i++)
    t[i] = s[i] - '0';

  vector<vector<int>> next(10, vector<int>(2019));
  for (int i = 1; i <= 9; i++) {
    vector<bool> visited(2019);
    for (int j = 0; j < 2019; j++)
      next[i][j] = j;
    for (int j = 0; j < 2019; j++) {
      if (!visited[j]) {
        int now = j;
        while (!visited[now]) {
          visited[now] = true;
          next[i][now] = (now * 10 + i) % 2019;
          now = next[i][now];
        }
      }
    }
  }

  vector<int> dp1(2019);
  vector<int> dp2(2019);
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (i & 1) {
      dp2.assign(2019, 0);
      for (int j = 0; j < 2019; j++) {
        dp2[next[t[i]][j]] += dp1[j];
      }
      dp2[t[i]]++;
      ans += dp2[0];
    } else {
      dp1.assign(2019, 0);
      for (int j = 0; j < 2019; j++) {
        dp1[next[t[i]][j]] += dp2[j];
      }
      dp1[t[i]]++;
      ans += dp1[0];
    }
  }

  cout << ans << endl;

  return 0;
}


Error: MLE

Fixed submission:

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

#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

const ll mod = 1000000007;

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

  string s;
  cin >> s;

  int n = s.length();
  vector<int> t(n);
  for (int i = 0; i < n; i++)
    t[i] = s[i] - '0';

  vector<vector<int>> next(10, vector<int>(2019));
  for (int i = 1; i <= 9; i++) {
    vector<bool> visited(2019);
    for (int j = 0; j < 2019; j++)
      next[i][j] = j;
    for (int j = 0; j < 2019; j++) {
      if (!visited[j]) {
        int now = j;
        while (!visited[now]) {
          visited[now] = true;
          next[i][now] = (now * 10 + i) % 2019;
          now = next[i][now];
        }
      }
    }
  }

  vector<vector<int>> dp(n + 1, vector<int>(2019));
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2019; j++) {
      dp[i + 1][next[t[i]][j]] += dp[i][j];
    }
    dp[i + 1][t[i]]++;
    ans += dp[i + 1][0];
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i <= int(b); ++i)
using namespace std;
template <class A> void pr(A a) { cout << (a) << endl; }
template <class A, class B> void pr(A a, B b) {
  cout << a << " ";
  pr(b);
}
template <class A, class B, class C> void pr(A a, B b, C c) {
  cout << a << " ";
  pr(b, c);
}
template <class A, class B, class C, class D> void pr(A a, B b, C c, D d) {
  cout << a << " ";
  pr(b, c, d);
}
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
int INF = numeric_limits<int>::max();
// for(int j=0;j<N;++j) {
int to_int(char c) { return c - '0'; }

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

int main(void) {
  string S;
  cin >> S;
  int n = S.length();
  int T[n + 10];
  vector<int> M(2020, 0);

  T[n] = 0;
  M[T[n]]++;

  for (int i = n - 1; i >= 0; i--) {
    // pr(to_int(S[n-i-1]));
    T[i] = (T[i + 1] + to_int(S[i]) * modpow(10, n - i - 1, 2019)) % 2019;
    M[T[i]]++;
  }

  ll ans = 0;

  rep(i, 0, 2018) {
    int m = M[i];
    ans += m * (m - 1) / 2;
  }

  pr(ans);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i <= int(b); ++i)
using namespace std;
template <class A> void pr(A a) { cout << (a) << endl; }
template <class A, class B> void pr(A a, B b) {
  cout << a << " ";
  pr(b);
}
template <class A, class B, class C> void pr(A a, B b, C c) {
  cout << a << " ";
  pr(b, c);
}
template <class A, class B, class C, class D> void pr(A a, B b, C c, D d) {
  cout << a << " ";
  pr(b, c, d);
}
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
int INF = numeric_limits<int>::max();
// for(int j=0;j<N;++j) {
int to_int(char c) { return c - '0'; }

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

int main(void) {
  string S;
  cin >> S;
  int n = S.length();
  int T[n + 10];
  vector<int> M(2018, 0);

  T[n] = 0;
  M[T[n]]++;

  for (int i = n - 1; i >= 0; i--) {
    // pr(to_int(S[n-i-1]));
    T[i] = (T[i + 1] + to_int(S[i]) * modpow(10, n - i - 1, 2019)) % 2019;
    M[T[i]]++;
  }

  ll ans = 0;

  for (auto m : M) {
    ans += m * (m - 1) / 2;
  }

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

#include <bits/stdc++.h>

#define whole(f, x, ...)                                                       \\
  ([&](decltype((x)) whole) {                                                  \\
    return (f)(begin(whole), end(whole), ##__VA_ARGS__);                       \\
  })(x)

using namespace std;
using ll = long long;

void solve() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  vector<int> v(2019);

  // int t = s[0] - '0';
  // int ten = 10;
  // ++v[t];

  int t = 0, ten = 1;
  ++v[t];

  for (auto &c : s) {
    t += ten * (c - '0');
    t %= 2019;
    ++v[t];
    ten *= 10;
    ten %= 2019;
  }

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

  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);

  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define whole(f, x, ...)                                                       \\
  ([&](decltype((x)) whole) {                                                  \\
    return (f)(begin(whole), end(whole), ##__VA_ARGS__);                       \\
  })(x)

using namespace std;
using ll = long long;

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

  int cnt = 0;
  for (int i = 1; i < 1e5; i++) {
    string ser = to_string(i * 2019);
    size_t pos = s.find(ser);
    while (pos != string::npos) {
      pos = s.find(ser, pos + 1);
      ++cnt;
    }
  }

  cout << cnt << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);

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

#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
typedef long long ll;

int main() {
  int N, P;
  string S;
  cin >> S;
  N = S.length();
  ll ans = 0;
  P = 2019;

  vector<ll> mods(P, 0);

  ll cur = 0, coef = 1;
  mods[cur]++;
  REP(i, 0, N) {
    cur = (cur + coef * (S[N - i - 1] - '0') % P) % P;
    coef = coef * 10 % P;
    mods[cur]++;
  }

  REP(i, 0, P) { ans += mods[i] * (mods[i] - 1) / 2; }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int ans = 0;
  string s;
  cin >> s;

  for (int i = 0; i <= s.length() - 3; i++) { // 2 * 10 ^ (5 - 2)

    for (int j = i + 3; j <= s.length(); j++) { // 2 * 10 ^ (5 - 2)

      if (atoi(s.substr(i, j - i).c_str()) % 2019 == 0) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

int cnt[2020];

int main() {
  string S;
  cin >> S;
  cnt[0] = 1;
  ll sum = 0, pre = 0, x = 1;
  for (int i = S.length() - 1; i >= 0; i--) {
    int tmp = (pre + (S[i] - '0') * x) % 2019;
    sum += cnt[tmp];
    cnt[tmp]++;
    pre = tmp;
    x = x * 10 % 2019;
  }
  cout << sum << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int cnt[2020];

int main() {
  string S;
  cin >> S;
  cnt[0] = 1;
  ll sum = 0, pre = 0, x = 1;
  for (int i = S.length() - 1; i >= 0; i--) {
    int tmp = (pre + (S[i] - '0') * x) % 2019;
    sum += cnt[tmp];
    cnt[tmp]++;
    pre = tmp;
    (x *= 10) % MOD;
  }
  cout << sum << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

char s[200200];
long long p[2019], q[2019], a;

int main() {
  scanf("%s", s);
  for (int i = 0; s[i]; i++) {
    for (int j = 0; j < 2019; j++)
      p[j] = q[j], q[j] = 0;
    p[0]++;
    for (int j = 0; j < 2019; j++)
      q[(j * 10 + s[i] - '0') % 2019] += p[j];
    a += q[0];
  }
  printf("%lld\\n", a);
  return 0;
}


Error: WA

Fixed submission:

#include <stdio.h>

char s[200200];
long long p[2019], q[2019], a;

int main() {
  scanf("%s", s);
  for (int i = 0; s[i]; i++) {
    for (int j = 0; j < 2019; j++)
      p[j] = q[j], q[j] = 0;
    p[0]++;
    for (int i = 0; i < 2019; i++)
      q[(i * 10 + s[i] - '0') % 2019] += p[i];
    a += q[0];
  }
  printf("%lld\\n", a);
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int p = 2019;
  string s;
  cin >> s;
  int n = s.size();
  int total = 0;
  int k = 1;
  long long ans = 0;
  vector<int> count(p);
  count[0] = 1;
  reverse(s.begin(), s.end());
  for (int i = 0; i < n; i++) {
    total += (s[i] - '0') * k;
    int mod = total % p;
    ans += count[mod];
    count[mod]++;
    k *= 10;
    k %= p;
  }
  cout << ans << endl;
}

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


Fixed submission:

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

string s;

int check(int i, int j) {
  if (i < j) {
    string tmp = s.substr(i, j - i);
    int num = stoi(tmp);
    if (num % 2019 == 0) {
      return 1;
    } else {
      return 0;
    }
  }
}

void search(string s) {
  int sum = 0;
  if (s.size() < 4) {
    cout << 0;
  } else {
    for (int i = 0; i < s.size() - 3; i++) {
      for (int j = i + 3; j < s.size(); j++) {
        sum += check(i, j);
      }
    }
  }
  cout << sum;
}

int main() {
  cin >> s;
  search(s);
}