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

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

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)

int solution(string &S) {
  int N = S.size();
  vector<int> num_appeared(2019, 0); // 余り i の現れた回数
  num_appeared[0] = 1;
  int ans = 0, res = 0, digit = 1;
  for (int i = N - 1; i >= 0; --i) {
    res = (digit * (S[i] - '0') + res) % 2019;
    digit = (10 * digit) % 2019;
    ans += num_appeared[res];
    ++num_appeared[res];
  }
  return ans;
}

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

  cout << solution(S) << endl;

  return 0;
}

Error: -11

Fixed submission:

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

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)

int solution(string &S) {
  int N = S.size();
  vector<int> num_appeared(2020, 0); // 余り i の現れた回数
  num_appeared[0] = 1;
  int ans = 0, res = 0, digit = 1;
  for (int i = N - 1; i >= 0; ++i) {
    res = (digit * (S[i] - '0') + res) % 2019;
    digit = (10 * digit) % 2019;
    ans += num_appeared[res];
    ++num_appeared[res];
  }
  return ans;
}

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

  cout << solution(S) << endl;

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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x.begin(), _x.end(), std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define R(_x, _y) (0 <= _x && _x <= M - 1 && 0 <= _y && _y <= N - 1)
#define A(a) (!(R(x + dx[a], y + dy[a]) && b[y + dy[a]][x + dx[a]]))
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
ll table[2019], t2[2019];
int main() {
  string S;
  cin >> S;
  ll num, C = 0, t;
  rep(i, 0, S.size() - 1) {
    table[S[i] - '0']++;
    rep(i2, 0, 2019) t2[i2] = 0;
    rep(i2, 0, 2019) {
      t2[(i2 * 10 + S[i + 1] - '0') % 2019] += table[i2];
      // printf("%d\\n", (i2 * 10 + S[i + 1] - '0') % 2019);
      if ((i2 * 10 + S[i + 1] - '0') % 2019 == 0)
        C += table[i2];
    }
    rep(i2, 0, 2019) table[i2] = t2[i2];
  }
  printf("%lld\\n", C);
  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x.begin(), _x.end(), std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define R(_x, _y) (0 <= _x && _x <= M - 1 && 0 <= _y && _y <= N - 1)
#define A(a) (!(R(x + dx[a], y + dy[a]) && b[y + dy[a]][x + dx[a]]))
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
int table[2020];
int main() {
  string S;
  cin >> S;
  deque<int> k;
  ll num, C = 0, t;
  k.push_back(S[0] - '0');
  rep(i, 1, S.size()) {
    t = k.size();
    k.push_back(S[i] - '0');
    rep(i2, 0, t) {
      if ((k.front() * 10 + S[i] - '0') % 2019 == 0)
        C++;
      k.push_back((k.front() * 10 + S[i] - '0') % 2019);
      // printf("%d\\n", (k.front() * 10 + S[i] - '0') % 2019);
      k.pop_front();
    }
  }
  printf("%lld\\n", C);
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end())
#define vecmin(A) *min_element(A.begin(), A.end())
#define vecmax(A) *max_element(A.begin(), A.end())
typedef long long ll;

int main() {
  vector<int> dmod(200000, 0);
  rep(i, 200000) {
    if (i == 0)
      dmod[0] = 1;
    if (i > 0)
      dmod[i] = (dmod[i - 1] * 10) % 2019;
  }
  string s;
  cin >> s;
  reverse(s);
  int n = s.size();
  vector<int> smod(n);
  rep(i, n) {
    if (i == 0)
      smod[0] = (s[i] - '0') * dmod[0];
    if (i > 0) {
      smod[i] = smod[i - 1] + (s[i] - '0') * dmod[i];
      smod[i] %= 2019;
    }
  }
  smod.push_back(0);
  int ans = 0;
  rep(i, 2019) {
    int m = count(smod.begin(), smod.end(), i);
    ans += m * (m - 1) / 2;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end())
#define vecmin(A) *min_element(A.begin(), A.end())
#define vecmax(A) *max_element(A.begin(), A.end())
typedef long long ll;

int main() {
  vector<int> dmod(200000, 0);
  rep(i, 200000) {
    if (i == 0)
      dmod[0] = 1;
    if (i > 0)
      dmod[i] = (dmod[i - 1] * 10) % 2019;
  }
  string s;
  cin >> s;
  reverse(s);
  int n = s.size();
  vector<int> smod(n);
  rep(i, n) {
    if (i == 0)
      smod[0] = (s[i] - '0') * dmod[0];
    if (i > 0) {
      smod[i] = smod[i - 1] + (s[i] - '0') * dmod[i];
      smod[i] %= 2019;
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {   // start
    for (int j = i; j < n; j++) { // end
      int m = 0;
      if (i == 0)
        m = smod[j];
      if (i > 0)
        m = smod[j] - smod[i - 1];
      if (m == 0)
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
constexpr auto INFI = 2147483647;
const ll INFL = 9223372036854775807;

int main() {
  string s;
  cin >> s;
  int s_length = s.size();
  int ans = 0;
  vector<int> count(2020, 0);
  reverse(s.begin(), s.end());
  int x = 1;
  int modSum = 0;
  rep(i, s_length) {
    count[modSum]++;
    modSum = (modSum + (s[i] - '0') * x) % 2019;
    ans += count[modSum];
    x = (x * 10) % 2019;
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
constexpr auto INFI = 2147483647;
const ll INFL = 9223372036854775807;

int main() {
  string s;
  cin >> s;
  int s_length = s.size();
  int ans = 0;
  vector<int> s_rev(200001);
  vector<int> modSum(200001);
  modSum[0] = 0;
  rep(i, s.size()) { s_rev[s_length - 1 - i] = (int)(s[i] - '0'); }
  int x = 1;
  rep(i, s_length) {
    modSum[i + 1] = (modSum[i] + s_rev[i] * x) % 2019;
    x = (x * 10) % 2019;
  }

  for (int i = 1; i <= s_length; i++) {
    for (int j = i + 1; j <= s_length; j++) {
      if (modSum[i - 1] == modSum[j])
        ans++;
    }
  }
  // rep(i, s_length) cout << modSum[i] << ' ' << endl;
  cout << ans << endl;

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

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

int main() {

  const int M = 224; // コレがミソ

  vector<int> mod(M);
  for (int i = 0; i < M; i++) {
    if (i == 0) {
      mod.at(i) = 1;
    } else {
      mod.at(i) = 10 * mod.at(i - 1) % 2019;
    }
  }

  // sort(mod.begin(),mod.end());
  /*for(int i=0; i<224; i++){
    cout << "i" << i << ":" << mod.at(i) <<endl;
  }*/

  string S;
  cin >> S;

  long long int N = (long int)(S.length()); // Nketa
  // cout << len << endl;

  vector<long int> s(N);
  // cout << (s.size()) << endl;

  for (long long int i = 0; i < N; i++) {
    long int tmp = S.at(i) - '0';
    s.at(i) = tmp;
    // cout << s.at(i) ;
  }

  vector<int> t(N + 1);
  for (long long int i = 0; i < N + 1; i++) {
    if (i == 0) {
      t.at(i) = 0;
    } else if (i == 1) {
      t.at(i) = s.at(N - i);
    } else {
      long int tmp;
      // tmp = i-1;
      tmp = (i - 1) % M;
      t.at(i) = (t.at(i - 1) + s.at(N - i) * mod.at(tmp)) % 2019;
    }
  }

  long long int count = 0;
  long long int sum = 1;

  sort(t.begin(), t.end());
  for (long long int i = 0; i < N; i++) {
    if (t.at(i) == t.at(i + 1)) {
      sum++;
      if (i == N - 1) {
        count += sum * (sum - 1) / 2;
      }
    } else {
      count += sum * (sum - 1) / 2;
      sum = 1;
    }
  }
  cout << count << endl;
}


Error: 0

Fixed submission:

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

int main() {

  const int M = 2019;

  vector<int> mod(M);
  for (int i = 0; i < M; i++) {
    if (i == 0) {
      mod.at(i) = 1;
    } else {
      mod.at(i) = 10 * mod.at(i - 1) % 2019;
    }
  }

  // sort(mod.begin(),mod.end());
  /*for(int i=0; i<224; i++){
    cout << "i" << i << ":" << mod.at(i) <<endl;
  }*/

  string S;
  cin >> S;

  long long int N = (long int)(S.length()); // Nketa
  // cout << len << endl;

  vector<long int> s(N);
  // cout << (s.size()) << endl;

  for (long long int i = 0; i < N; i++) {
    long int tmp = S.at(i) - '0';
    s.at(i) = tmp;
    // cout << s.at(i) ;
  }

  vector<int> t(N + 1);
  for (long long int i = 0; i < N + 1; i++) {
    if (i == 0) {
      t.at(i) = 0;
    } else if (i == 1) {
      t.at(i) = s.at(N - i);
    } else {
      int tmp;
      tmp = i - 1;
      // tmp = (i-1)%M;
      t.at(i) = (t.at(i - 1) + s.at(N - i) * mod.at(tmp)) % 2019;
    }
  }

  long long int count = 0;
  long long int sum = 1;

  sort(t.begin(), t.end());
  for (long long int i = 0; i < N; i++) {
    if (t.at(i) == t.at(i + 1)) {
      sum++;
      if (i == N - 1) {
        count += sum * (sum - 1) / 2;
      }
    } else {
      count += sum * (sum - 1) / 2;
      sum = 1;
    }
  }
  cout << count << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int MOD = 2019;
const int MAX = 2e5 + 10;

inline int add(int a, int b) { return (a + b) % MOD; }

inline int mul(int a, int b) { return (a * b) % MOD; }

int dp[2][MOD];

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

  string s;
  cin >> s;
  int n = s.length();
  int cur = 0;
  long long ans = 0;
  dp[cur][s[0] - '0'] = 1;
  for (int i = 0; i < n - 1; i++) {
    dp[(cur + 1) % 2][s[i + 1] - '0'] = 1;
    for (int j = 0; j < 2019; j++) {
      int val = add(mul(j, 10), s[i + 1] - '0');
      dp[(cur + 1) % 2][val] += dp[cur][j];
    }
    for (int i = 0; i < MOD; i++)
      dp[cur][i] = 0;
    cur = (cur + 1) % 2;
    ans += dp[cur][0];
  }
  cout << ans << '\\n';
  // for(int i = 0; i < n; i++) {
  // 	for(int j = 0; j < 20; j++) {
  // 		cout << dp[i][j] << ' ';
  // 	}
  // 	cout << '\\n';
  // }
  // long long ans = 0;
  // for(int i = 0; i < n; i++) ans += dp[i][0];
  // cout << ans << '\\n';

  return 0;
}


Error: MLE

Fixed submission:

/*input
2119
*/
#include <bits/stdc++.h>

using namespace std;

const int MOD = 2019;
const int MAX = 2e5 + 10;

inline int add(int a, int b) { return (a + b) % MOD; }

inline int mul(int a, int b) { return (a * b) % MOD; }

int dp[MAX][MOD];

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

  string s;
  cin >> s;
  int n = s.length();
  dp[0][s[0] - '0'] = 1;
  for (int i = 0; i < n - 1; i++) {
    dp[i + 1][s[i + 1] - '0'] = 1;
    for (int j = 0; j < 2019; j++) {
      int val = add(mul(j, 10), s[i + 1] - '0');
      dp[i + 1][val] += dp[i][j];
    }
  }
  // for(int i = 0; i < n; i++) {
  // 	for(int j = 0; j < 20; j++) {
  // 		cout << dp[i][j] << ' ';
  // 	}
  // 	cout << '\\n';
  // }
  long long ans = 0;
  for (int i = 0; i < n; i++)
    ans += dp[i][0];
  cout << ans << '\\n';

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

#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 200010;
int main() {
  char s[maxn];
  int mp[maxn] = {0}, ans = 0;
  cin >> s;
  int len = strlen(s);
  mp[0] = 1;
  int x = 0, cnt = 1;
  for (int i = len - 1; i >= 0; i--) {
    x = ((s[i] - '0') * cnt + x) % 2019;
    ans += mp[x];
    mp[x]++;
    cnt = cnt * 10 % 2019;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int maxn = 2e5 + 10;
char s[maxn];
vector<int> snum[maxn];
int num(int a, int b) {
  int ans = 0;
  for (int i = a; i <= b; i++) {
    ans = ans * 10 + (s[i] - '0');
  }
  return ans;
}
int main() {
  cin >> s;
  int res = 0;
  int len = strlen(s);
  for (int i = 1; i <= len; i++) {
    for (int j = i + 3; j <= len; j++) {
      if (j == i + 3) {
        int k = num(i - 1, j - 1);
        if (k % 2019 == 0)
          res++;
        snum[i].push_back(k);
      } else {
        int k = snum[i][j - i - 4] * 10 + (s[j - 1] - '0');
        if (k % 2019 == 0)
          res++;
        snum[i].push_back(k);
      }
    }
  }
  cout << res << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define REPvec(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define all(x) x.begin(), x.end()
#define MOD 1000000007 // 1e9+7
using namespace std;
typedef long long int ll;
typedef vector<int> vecint;
typedef vector<ll> vecll;
typedef vector<double> vecd;
typedef vector<string> vecstr;
typedef vector<char> vecchar;
typedef vector<bool> vecbool;
typedef vector<vector<bool>> vec2bool;
typedef vector<vector<int>> vec2int;
typedef vector<vector<double>> vec2d;
////////////////////////////////////////////////
void init() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
////////////////////////////////////////////////
int main() {
  init();
  string s;
  cin >> s;
  ll n = s.size();
  // vec2int dp(n, vecint(2019, 0));
  vecll dpold(2019, 0);
  vecll dpnew(2019, 0);
  ll ans = 0;
  dpold[s[0] - '0'] = 1;
  REP(i, n) {
    ll tmp = s[i] - '0';
    dpnew[tmp] = 1;
    REP(k, 2019) {
      if (i == 0)
        continue;
      dpnew[(10 * k + tmp) % 2019] += dpold[k];
    }
    ans += dpnew[0];
    REP(k, 2019) {
      // if (i == 0) continue;
      dpold[k] = dpnew[k];
      dpnew[k] = 0;
    }
  }
  cout << ans << endl;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define REPvec(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define all(x) x.begin(), x.end()
#define MOD 1000000007 // 1e9+7
using namespace std;
typedef long long int ll;
typedef vector<int> vecint;
typedef vector<ll> vecll;
typedef vector<double> vecd;
typedef vector<string> vecstr;
typedef vector<char> vecchar;
typedef vector<bool> vecbool;
typedef vector<vector<bool>> vec2bool;
typedef vector<vector<int>> vec2int;
typedef vector<vector<double>> vec2d;
////////////////////////////////////////////////
void init() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
////////////////////////////////////////////////
int main() {
  init();
  string s;
  cin >> s;
  int n = s.size();
  vec2int dp(n, vecint(2019, 0));
  REP(i, n) {
    int tmp = s[i] - '0';
    dp[i][tmp] = 1;
    REP(k, 2019) {
      if (i == 0)
        continue;
      dp[i][(10 * k + tmp) % 2019] += dp[i - 1][k];
    }
  }
  int ans = 0;
  REP(i, n) {
    // cout << dp[i][0] << endl;
    ans += dp[i][0];
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define F0(i, n) for (int i = 0; i < int(n); ++i)
#define F1(i, n) for (int i = 1; i <= int(n); ++i)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
typedef long long ll;
typedef pair<int, int> pii;
int m10[200010];
int mod_pow(int x, int pow) {
  // (x * 10^pow)%2019
  return (x * m10[pow]) % 2019;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  m10[0] = 1;
  F1(i, 200010 - 1) { m10[i] = (m10[i - 1] * 10) % 2019; }

  string s;
  cin >> s;

  int n = int(s.size());
  vector<int> t(n + 2);
  vector<int> ctr(2019, 0);
  t[n] = 0;
  ++ctr[0];
  for (int i = n - 1; i >= 0; --i) {
    t[i] = (t[i + 1] + mod_pow(s[i] - '0', n - 1 - i)) % 2019;
    ++ctr[t[i]];
  }
  ll a = 0;
  F0(i, 2019) {
    if (ctr[i])
      a += ctr[i] * (ctr[i] - 1) / 2;
  }
  cout << a << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define F0(i, n) for (int i = 0; i < int(n); ++i)
#define F1(i, n) for (int i = 1; i <= int(n); ++i)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
typedef long long ll;
typedef pair<int, int> pii;

int mod_pow(int x, int pow) {
  // (x * 10^pow)%2019
  for (int i = 0; i < pow; ++i)
    x = (x * 10) % 2019;
  return x;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  string s;
  cin >> s;

  int n = int(s.size());
  vector<int> t(n + 2);
  vector<int> ctr(2019, 0);
  t[n] = 0;
  ++ctr[0];
  for (int i = n - 1; i >= 0; --i) {
    t[i] = (t[i + 1] + mod_pow(s[i] - '0', n - 1 - i)) % 2019;
    ++ctr[t[i]];
  }
  ll a = 0;
  F0(i, 2019) {
    if (ctr[i])
      a += ctr[i] * (ctr[i] - 1) / 2;
  }
  cout << a << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

static constexpr int multiple = 2019;

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

  int len = c.length();
  std::vector<int> str(len);
  for (auto i = 0; i < len; i++) {
    str[i] = c[i] - '0';
  }

  std::vector<int> mods(len);
  mods[0] = 1;
  for (auto i = 1; i < len; i++) {
    mods[i] = mods[i - 1] * 10 % multiple;
  }

  std::vector<int> sum_mods(len + 1);
  std::map<int, std::vector<int>> same_mod;
  sum_mods[len] = 0;
  same_mod[0].emplace_back(len);
  for (auto i = len - 1; i >= 0; i--) {
    sum_mods[i] = (sum_mods[i + 1] + str[i] * mods[len - i - 1]) % multiple;
    same_mod[sum_mods[i]].emplace_back(i);
  }

  int count = 0;
  for (auto vec : same_mod) {
    int len = vec.second.size();
    count += len * (len - 1) / 2;
  }

  std::cout << count << std::endl;
}


Error: TLE

Fixed submission:

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

static constexpr int multiple = 2019;

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

  int len = c.length();
  std::vector<int> str(len);
  for (auto i = 0; i < len; i++) {
    str[i] = c[i] - '0';
  }

  std::vector<int> mods(len);
  mods[0] = 1;
  for (auto i = 1; i < len; i++) {
    mods[i] = mods[i - 1] * 10 % multiple;
  }

  std::vector<int> sum_mods(len + 1);
  sum_mods[len] = 0;
  for (auto i = len - 1; i >= 0; i--) {
    sum_mods[i] = (sum_mods[i + 1] + str[i] * mods[len - i - 1]) % multiple;
  }

  int count = 0;
  for (int i = 0; i < len - 3; i++) {
    for (int j = i + 3; j <= len; j++) {
      if (sum_mods[i] - sum_mods[j] == 0) {
        count++;
      }
    }
  }
  std::cout << count << std::endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int mod = 2019;
int cnt[mod + 1] = {1};
int main() {
  string str;
  cin >> str;
  int val = 0, pow = 1, ans = 0;
  int len = str.size();
  for (int i = len - 1; i >= 0; i--) {
    val += (str[i] - '0') * pow % mod;
    pow = pow * 10 % mod;
    val %= mod;
    ans += cnt[val];
    cnt[val]++;
  }
  cout << ans << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int mod = 2019;
int cnt[mod + 1] = {1};
int pow(int a, int b) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a % mod;
  else if (b & 1)
    return a * pow(a * a % mod, b >> 1);
  else
    return pow(a * a % mod, b >> 1);
}
int main() {
  string str;
  cin >> str;
  int val = 0;
  int ans = 0;
  int len = str.size();
  for (int i = len - 1; i >= 0; i--) {
    val += (str[i] - '0') * pow(10, len - 1 - i);
    val %= mod;
    ans += cnt[val];
    cnt[val]++;
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#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>;

int main() {
  string s;
  cin >> s;
  int n = s.length();
  vector<int> d(n + 1);
  int ten = 1;
  for (int i = n - 1; i >= 0; --i) {
    int a = (s[i] - '0') * ten % 2019;
    d[i] = (d[i + 1] + a) % 2019;
    ten *= 10;
    ten %= 2019;
  }
  vector<int> cnt(2019);
  ll ans = 0;
  for (int i = n; i >= 0; --i) {
    ans += cnt[d[i]];
    cnt[d[i]]++;
  }
  cout << ans << "\\n";
  return 0;
}

Error: 0

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>;

int main() {
  string str; // 数字列
  cin >> str;
  vector<int> nums(str.size()); // 同じ長さのint列

  // char -> int 変換
  transform(str.begin(), str.end(), nums.begin(),
            [](char ch) { return ch - '0'; });

  const int *result = nums.data(); // int列の先頭 と
  const size_t size = nums.size(); // 列の長さ

  ll ans = 0;
  for (int i = 0; i < size - 4; ++i) {
    int x = result[i] * 1000 + result[i + 1] * 100 + result[i + 2] * 10 +
            result[i + 3];
    for (int j = i + 4; j < size; ++j) {
      x = x * 10 + result[j];
      if (x % 2019 == 0) {
        ++ans;
      }
    }
  }
  cout << ans << "\\n";
  // 確認
  // for ( size_t i = 0; i < size; ++i ) cout << result[i] << ' ';
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

short dp[200009][2020];
int main() {
  string s;
  cin >> s;
  memset(dp, 0, sizeof(dp));
  dp[0][s[0] - '0']++;
  for (int i = 1; i < s.length(); i++) {
    dp[i][s[i] - '0']++;
    for (int j = 0; j < 2019; j++) {
      dp[i][(j * 10 + s[i] - '0') % 2019] += dp[i - 1][j];
    }
  }
  int ans = 0;
  for (int i = 0; i < s.length(); i++) {
    ans += dp[i][0];
  }
  cout << ans << endl;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int dp[200009][2020];
int main() {
  string s;
  cin >> s;
  memset(dp, 0, sizeof(dp));
  dp[0][s[0] - '0']++;
  for (int i = 1; i < s.length(); i++) {
    dp[i][s[i] - '0']++;
    for (int j = 0; j < 2019; j++) {
      dp[i][(j * 10 + s[i] - '0') % 2019] += dp[i - 1][j];
    }
  }
  int ans = 0;
  for (int i = 0; i < s.length(); i++) {
    ans += dp[i][0];
  }
  cout << ans << endl;
}