alexjercan/bugnet huggingface dataset:
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 MAX = 3000000;
const ll MOD = 1000000007;
string s;
// ll k;

int main() {
  cin >> s;
  ll n = s.length();
  ll tmp = 0;
  ll count = 0;
  bool checked[n];
  for (ll i = 0; i < n; i++) {
    checked[i] = false;
  }
  for (ll i = 0; i < n; i++) {
    if (checked[i] != true) {
      ll smallCount = 0;
      for (ll j = i; j < n; j++) {
        tmp = tmp * 10 + (s[j] - 48);
        tmp = tmp % 2019;
        if (tmp == 0) {
          smallCount++;
          // cout << i << " " << j << " " << smallCount << endl;
          count = count + smallCount;
          checked[j + 1] = true;
        }
      }
    }
    tmp = 0;
  }
  cout << count << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MAX = 3000000;
const ll MOD = 1000000007;
string s;
// ll k;

int main() {
  cin >> s;
  ll n = s.length();
  ll tmp = 0;
  ll count = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = i; j < n; j++) {
      tmp = tmp * 10 + (s[j] - 48);
      tmp = tmp % 2019;
      if (tmp == 0) {
        count++;
      }
    }
    tmp = 0;
  }
  cout << count << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

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

  uint32_t mod = 2019;
  uint32_t remainder = 0;
  uint32_t base = 1;
  std::vector<uint32_t> mod_num(mod);
  mod_num[0]++;
  for (size_t i = 0; i < S.size(); i++) {
    // |S| max = 200,000よりuint128_tでも到底足りないので各桁毎逐次処理
    uint32_t d_i = S[S.size() - 1 - i] - '0';
    remainder = (remainder + base * d_i) % mod;
    mod_num[remainder]++;

    base = (base * 10) % mod;
  }

  uint32_t ans = 0;
  for (auto &&i : mod_num)
    ans += i * (i - 1) / 2;

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

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

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

  uint32_t ans = 0;
  for (size_t i = 0; i < S.size(); i++) {
    uint32_t remainder = std::stoi(S.substr(i, 4)) % 2019;
    if (remainder == 0)
      ans++;
    for (size_t j = i + 4; j < S.size(); j++) {
      remainder = (remainder * 10 + static_cast<uint32_t>(S[j] - '0')) % 2019;
      if (remainder == 0) {
        ans++;
      }
    }
  }

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

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

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

const int mod = 2019;

int main() {
  string S;
  cin >> S;
  int n = S.size();
  int T[n + 1];
  int ten = 1;
  int dp[mod];
  for (int i = 0; i < mod; i++)
    dp[i] = 0;
  for (int i = 0; i <= n; i++)
    T[i] = 0;
  for (int i = n - 1; i >= 0; i--) {
    T[i] = T[i + 1] + (S[i] - '0') * ten;
    T[i] %= mod;
    ten *= 10;
    ten %= mod;
  }
  for (int i = 0; i <= n; i++) {
    dp[T[i]]++;
  }
  int ans = 0;
  for (int i = 0; i < mod; i++) {
    dp[i] = dp[i] * (dp[i] - 1) / 2;
    ans += dp[i];
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

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

int main() {
  string S;
  cin >> S;
  string tmp = "";
  ll count = 0;
  for (ll i = 0; i < S.length() - 3; i++) {
    tmp += S[i];
    tmp += S[i + 1];
    tmp += S[i + 2];
    tmp += S[i + 3];
    for (ll j = i + 4; j <= S.length(); j++) {
      if (stoll(tmp) % 2019 == 0) {
        count++;
      }
      tmp += S[j];
    }
    tmp = "";
  }
  cout << count << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)

typedef long long ll;
#define INF 1e18

#define MOD 2019

int main() {
  string s;
  cin >> s;
  ll n = s.size();
  ll t[2019] = {}; // 「左からi~n(i:0~n)の数値」を2019で割ったあまりが何個あるか
  t[0]++;
  ll tmp = 0, power = 1;
  REPR(i, n) {
    tmp += (s[i] - '0') * power;
    power *= 10;
    power %= MOD;
    tmp %= MOD;
    // cout<<tmp<<endl;
    t[tmp]++;
  }
  ll ans = 0;
  REP(i, MOD) { ans += t[i] * (t[i] - 1) / 2; }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)

typedef long long ll;
#define INF 1e18

#define MOD 2019

int main() {
  string s;
  cin >> s;
  ll n = s.size();
  ll t[2019] = {}; // 「左からi~n(i:0~n)の数値」を2019で割ったあまりが何個あるか
  t[0]++;
  ll tmp = 0;
  REPR(i, n) {
    tmp += (ll)(pow(10, n - i - 1) * (s[i] - '0')) % MOD;
    tmp %= MOD;
    // cout<<tmp<<endl;
    t[tmp]++;
  }
  ll ans = 0;
  REP(i, MOD) { ans += t[i] * (t[i] - 1) / 2; }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int mod = 2019;

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


Error: TLE

Fixed submission:

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

int main() {
  string s;
  cin >> s;
  int ans = 0;
  int n = s.size();
  for (int i = 0; i < n - 3; ++i) {
    for (int j = 4; j + i <= n; ++j) {
      // string st=s.substr(i,j);
      // cout << st << endl;
      long long cal = 0;
      int l = i;
      for (int k = j; k > 0; --k) {
        cal += ((s[l] - '0') * pow(10, k - 1));
        // cal=cal*pow(10,k-1);
        l++;
      }
      // cout << cal << endl;
      if (cal % 2019 == 0)
        ans++;
    }
  }

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

#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  char s[200001];
  scanf("%s", s);
  int sz = strlen(s), sum = 0;
  vector<int> v(sz), vv(sz), vsum(sz + 1);
  v[0] = 1;
  vsum[0] = 0;

  for (int i = 0; i < sz; ++i) {
    if (i != 0)
      v[i] = (v[i - 1] * 10) % 2019;
    vv[i] = (v[i] * ((int)s[sz - i - 1] - 48)) % 2019;
    vsum[i + 1] = (vsum[i] + vv[i]) % 2019;
  }

  vector<int> smap(2019);
  rep(i, sz + 1) { ++smap[vsum[i]]; }
  rep(i, 2019) {
    if (smap[i] != 0) {
      int tmp = smap[i];
      sum += tmp * (tmp - 1) / 2;
    }
  }

  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  char s[200001], rs[200001];
  scanf("%s", s);
  int sz = strlen(s), sum = 0;
  vector<int> v(sz);
  v[0] = 1;

  for (int i = 0; i < sz; ++i) {
    rs[i] = s[sz - i - 1];
  }

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

  for (int i = 0; i < sz; ++i) {
    int suma = 0;
    for (int j = i; j <= sz; ++j) {
      suma += v[j - i] * ((int)rs[j] - 48);
      // cout << suma << endl;
      suma %= 2019;
      if (!suma)
        sum++;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
const int maxn = 10000;
const int INF32 = 1'050'000'000;
const long long INF64 = 4'000'000'000'000'000'000;
const int MOD7 = 1'000'000'007;
const int MOD9 = 1'000'000'009;
const int mod = MOD7;

void ERROR(int num) { printf("ERROR%d!\\n", num); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int dx[8] = {-1, 0, 1, 0, 1, 1, -1, -1};
int dy[8] = {0, -1, 0, 1, 1, -1, 1, -1};

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

  string S;
  cin >> S;

  int l = S.length();
  reverse(S.begin(), S.end());
  vector<ll> sum;
  sum.resize(l + 1, 0);
  vector<ll> cnt;
  cnt.resize(2019, 0);

  ll total = 0;
  ll ten = 1;
  cnt[0] = 1;
  for (int i = 0; i < l; i++) {
    sum[i + 1] = (sum[i] + (S[i] - '0') * ten % 2019 + 2019) % 2019;
    // cout << "sum[i+1]: " << sum[i+1] << " , total=" << total << endl;
    total += cnt[sum[i + 1]];
    cnt[sum[i + 1]]++;
    ten = ten * 10 % 2019;
  }

  cout << total << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
const int maxn = 10000;
const int INF32 = 1'050'000'000;
const long long INF64 = 4'000'000'000'000'000'000;
const int MOD7 = 1'000'000'007;
const int MOD9 = 1'000'000'009;
const int mod = MOD7;

void ERROR(int num) { printf("ERROR%d!\\n", num); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int dx[8] = {-1, 0, 1, 0, 1, 1, -1, -1};
int dy[8] = {0, -1, 0, 1, 1, -1, 1, -1};

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

  string S;
  cin >> S;

  int l = S.length();
  reverse(S.begin(), S.end());
  vector<ll> sum;
  sum.resize(l + 1, 0);
  vector<ll> cnt;
  cnt.resize(2019, 0);

  ll total = 0;
  ll ten = 1;
  cnt[0] = 1;
  for (int i = 0; i <= l; i++) {
    sum[i + 1] = (sum[i] + (S[i] - '0') * ten % 2019 + 2019) % 2019;
    // cout << "sum[i+1]: " << sum[i+1] << " , total=" << total << endl;
    total += cnt[sum[i + 1]];
    cnt[sum[i + 1]]++;
    ten = ten * 10 % 2019;
  }

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

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

int main(int argc, char const *argv[]) {
  string S;
  cin >> S;
  long t;
  long counter = 0;
  long plus = 1;
  vector<int> vec(S.size() + 1, 0);

  for (int i = 0; i < S.size(); i++) {
    if (vec.at(i) == 1) {
      continue;
    }
    t = 0;
    plus = 1;
    for (int j = i; j < S.size(); j++) {
      t += (long)(S[j] - '0');
      t %= 2019;
      if (t == 0) {
        counter += plus;
        vec.at(j + 1) = 1;
        plus++;
      }
      t *= 10;
    }
  }

  cout << counter << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main(int argc, char const *argv[]) {
  string S;
  cin >> S;
  long t;
  long counter = 0;
  for (int i = 0; i < S.size(); i++) {
    t = (long)(S[i] - '0');
    for (int j = i + 1; j < S.size(); j++) {
      t *= 10;
      t += (long)(S[j] - '0');
      t %= 2019;
      if (t == 0) {
        counter++;
      }
    }
  }

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

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

int main() {
  int Q = 2019, x = 1;
  string S;
  cin >> S;
  reverse(S.begin(), S.end());
  int N = S.size();
  vector<int> p(N + 1);
  for (int i = 0; i < N; i++) {
    int a;
    if (S.at(i) == '0') {
      a = 0;
    }
    if (S.at(i) == '1') {
      a = 1;
    }
    if (S.at(i) == '2') {
      a = 2;
    }
    if (S.at(i) == '3') {
      a = 3;
    }
    if (S.at(i) == '4') {
      a = 4;
    }
    if (S.at(i) == '5') {
      a = 5;
    }
    if (S.at(i) == '6') {
      a = 6;
    }
    if (S.at(i) == '7') {
      a = 7;
    }
    if (S.at(i) == '8') {
      a = 8;
    }
    if (S.at(i) == '9') {
      a = 9;
    }
    p.at(i + 1) = (p.at(i) + a * x) % 2019;
    x = (x * 10) % 2019;
  }
  sort(p.begin(), p.end());
  int64_t A = 1, Z = 0;
  for (int i = 0; i < N; i++) {
    if (p.at(i) == p.at(i + 1)) {
      A++;
    } else {
      Z += A * (A - 1) / 2;
      A = 1;
    }
  }
  Z += A * (A - 1) / 2;
  cout << Z << endl;
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::at: __n (which is 13) >= this->size() (which is 13)


Fixed submission:

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

int main() {
  int Q = 2019, x = 1;
  string S;
  cin >> S;
  reverse(S.begin(), S.end());
  int N = S.size();
  vector<int> p(N + 1);
  for (int i = 1; i < N + 1; i++) {
    int a;
    if (S.at(i) == '0') {
      a = 0;
    }
    if (S.at(i) == '1') {
      a = 1;
    }
    if (S.at(i) == '2') {
      a = 2;
    }
    if (S.at(i) == '3') {
      a = 3;
    }
    if (S.at(i) == '4') {
      a = 4;
    }
    if (S.at(i) == '5') {
      a = 5;
    }
    if (S.at(i) == '6') {
      a = 6;
    }
    if (S.at(i) == '7') {
      a = 7;
    }
    if (S.at(i) == '8') {
      a = 8;
    }
    if (S.at(i) == '9') {
      a = 9;
    }
    p.at(i) = (p.at(i - 1) + a * x) % 2019;
    x = (x * 10) % 2019;
  }
  sort(p.begin(), p.end());
  int64_t A = 1, Z = 0;
  for (int i = 0; i < N; i++) {
    if (p.at(i) == p.at(i + 1)) {
      A++;
    } else {
      Z += A * (A - 1) / 2;
      A = 1;
    }
  }
  Z += A * (A - 1) / 2;
  cout << Z << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
string S;
map<int, int> mp;
int main() {
  long long int i, j, p = 0, a, b, t;
  cin >> S;
  mp[0] = 1;
  b = 0;
  a = 1;
  for (i = S.size() - 1; i >= 0; i--) {
    b = (b + (S[i] - '0') * a) % 2019;
    a = (a * 10) % 2019;
    p += mp[b];
    mp[b]++;
    // cout << b <<endl;
  }

  cout << p;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string S;
vector<pair<int, int>> A[200010];
int main() {
  long long int i, j, p = 0, a, b, t;
  cin >> S;
  S = S + '1';
  A[0].push_back(make_pair(1, S[0] - '0'));

  for (i = 1; i < S.size(); i++) {
    t = 1;
    for (j = 0; j < A[i - 1].size(); j++) {
      a = A[i - 1][j].first;
      b = A[i - 1][j].second;
      if (b == 0) {
        p = p + a;
        a++;
        t = 0;
      }
      A[i].push_back(make_pair(a, ((b * 10) + S[i] - '0') % 2019));
    }
    if (t)
      A[i].push_back(make_pair(1, S[i] - '0'));
  }

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

#include <ctype.h>
#include <map>
#include <stdio.h>
#include <vector>

// #define DEBUG

void func_to_create() {
  std::vector<int> map2019(2019, 0);
  std::vector<int> tempMap(2019, 0);
  int count = 0;
  int c = getchar_unlocked();
  while (!isdigit(c)) {
    c = getchar_unlocked();
  }
  while (true) {
    // reset map
    std::fill(tempMap.begin(), tempMap.end(), 0);
    for (int i = 0; i < 2019; i++) {
      int rnum = map2019[i];
      if (rnum > 0) {
        int new_remainder = (i * 10 + (c - '0')) % 2019;
        tempMap[new_remainder] = rnum;
      }
    }
    map2019.swap(tempMap);
    map2019[c - '0'] = map2019[c - '0'] + 1;
    count += map2019[0];
    c = getchar_unlocked();
    if (!isdigit(c)) {
      break;
    }
  }
  printf("%d\\n", count);
}

int main() {

#ifdef DEBUG
  int TEST_CASES = 3;
  while (TEST_CASES--) {
#endif // DEBBUG

    func_to_create();

#ifdef DEBUG
  }
#endif // DEBUG

  return 0;
}


Error: TLE

Fixed submission:

#include <ctype.h>
#include <stdio.h>
#include <vector>

// #define DEBUG

void func_to_create() {
  std::vector<int> s_remainder;
  std::vector<int> power_ten_remainder;
  s_remainder.reserve(200000);
  power_ten_remainder.reserve(200000);
  char c = getchar_unlocked();
  int power_ten = 1;
  int current_remainder = c - '0';
  int count = 0;

  s_remainder.push_back(current_remainder);
  power_ten_remainder.push_back(power_ten);

  while (true) {
    c = getchar_unlocked();
    if (!isdigit(c)) {
      break;
    }
    if (c == '0') {
      count++;
    }
    current_remainder = (current_remainder * 10 + (c - '0')) % 2019;
    power_ten = (power_ten * 10) % 2019;
    if (current_remainder == 0) {
      count++;
    }
    s_remainder.push_back(current_remainder);
    power_ten_remainder.push_back(power_ten);
  }

  for (auto i = 1ul; i < s_remainder.size(); i++) {
    for (auto j = i + 1; j < s_remainder.size(); j++) {
      int remain = (s_remainder[j] -
                    s_remainder[i - 1] * power_ten_remainder[j - i + 1]) %
                   2019;
      if (remain == 0) {
        count++;
      }
    }
  }
  printf("%d\\n", count);
}

int main() {

#ifdef DEBUG
  int TEST_CASES = 3;
  while (TEST_CASES--) {
#endif // DEBBUG

    func_to_create();

#ifdef DEBUG
  }
#endif // DEBUG

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

#include <bits/stdc++.h>
using namespace std;
long long a[200005], mp[2100], ans;
int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  long long y = 1;
  for (int i = 0; i < s.size(); i++) {
    a[i] = (y * (s[i] - '0')) % 2019;
    y = (y * 10) % 2019;
  }
  y = 0;
  for (int i = 0; i < s.size(); i++) {
    y = (y + a[i]) % 2019;
    mp[y]++;
  }
  for (int i = 0; i < 2019; i++)
    ans += mp[i] * (mp[i] - 1) / 2;
  ans += mp[0];
  cout << ans;
  return 0;
}

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


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf = 1e9 + 7;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(), c.end()
#define pb push_back
#define debug(x) cerr << #x << " = " << (x) << endl;
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;
}
void solve() {
  int n;
  cin >> n;
  set<string> se;
  vector<string> s(n);
  rep(i, n) {
    cin >> s[i];
    se.insert(s[i]);
  }
  cout << (int)se.size() << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout << fixed << setprecision(10);
  // int t; cin >> t; rep(i, t)solve();
  solve();
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  string S;
  cin >> S;
  int L = S.size();
  vector<int> M(L + 1);
  M.at(L) = 0;
  M.at(L - 1) = (S.at(L - 1) - '0') % 2019;
  int Mod = 1;
  for (int i = 0; i < L - 1; i++) {
    Mod = (Mod * 10) % 2019;
    M.at(L - 2 - i) = (S.at(L - 2 - i) - '0') * Mod + M.at(L - 1 - i);
    M.at(L - 2 - i) %= 2019;
  }
  int Ans = 0;
  vector<int> No(2019);
  for (int i = 0; i <= L; i++) {
    No.at(M.at(i))++;
  }
  for (int i = 0; i < 2019; i++) {
    Ans += No.at(i) * (No.at(i) - 1) / 2;
  }
  cout << Ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  string S;
  cin >> S;
  int L = S.size();
  vector<int> M(L + 1);
  M.at(L) = 0;
  M.at(L - 1) = (S.at(L - 1) - '0') % 2019;
  int Mod = 1;
  for (int i = 0; i < L - 1; i++) {
    Mod = (Mod * 10) % 2019;
    M.at(L - 2 - i) = (S.at(L - 2 - i) - '0') * Mod + M.at(L - 1 - i);
    M.at(L - 2 - i) %= 2019;
  }
  int Ans = 0;
  for (int i = 4; i <= L; i++) {
    for (int j = 0; j < L - i + 1; j++) {
      if (M.at(j) == M.at(j + i)) {
        Ans++;
      }
    }
  }
  cout << Ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int main() {
  string S;
  cin >> S;
  vector<int> mods(2019, 0);
  mods[0] = 1;
  int current = 0;
  int keta = 1;
  for (int i = (int)S.size() - 1; i >= 0; i--) {
    current = (stoi(S.substr(i, 1)) * keta + current) % 2019;
    mods[current]++;
    keta = keta * 10 % 2019;
  }
  int result = 0;
  for (int j : mods) {
    if (j >= 2) {
      result += j * (j - 1) / 2;
    }
  }

  cout << result << endl;
}


Error: 0

Fixed submission:

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

using namespace std;

int main() {
  string S;
  cin >> S;
  vector<int> mods(2019, 0);
  mods[0] = 1;
  for (int i = 0; i < (int)S.size(); i++) {
    mods[stoll(S.substr(i)) % 2019]++;
  }
  int result = 0;
  for (int j : mods) {
    if (j >= 2) {
      result += j * (j - 1) / 2;
    }
  }

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

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

#define MOD 1e9 + 7;
#define INF 1e17 + 9;
#define PI acos(-1);

using namespace std;

int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  long long keta = s.size();
  vector<long long> ten(keta, 0);

  ten[0] = 1;
  for (long long i = 1; i < keta; i++) {
    ten[i] = (ten[i - 1] * 10) % 2019;
  }

  long long ans = 0;
  vector<long long> n(keta, 0), m(2019, 0);
  for (long long i = 0; i < keta; i++) {
    long long now = (long long)(s[i] - '0');

    if (i == 0)
      n[i] = (ten[i] * now) % 2019;
    else
      n[i] = (n[i - 1] + (ten[i] * now) % 2019) % 2019;

    // cout << n[i] << endl;

    if (n[i] == 0)
      ans++;
    m[n[i]]++;
  }

  for (long long i = 0; i < 2019; i++) {
    if (m[i] > 0) {
      ans += (m[i] * (m[i] - 1)) / 2;
    }
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

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

#define MOD 1e9 + 7;
#define INF 1e17 + 9;
#define PI acos(-1);

using namespace std;

int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  vector<long long> ten(s.size(), 0);

  long long keta = s.size();

  ten[0] = 1;
  for (long long i = 1; i < keta; i++) {
    ten[i] = (ten[i - 1] * 10) % 2019;
  }

  long long ans = 0;
  vector<long long> n(3000, 0), m(3000, 0);
  for (long long i = 0; i < keta; i++) {
    long long now = (long long)(s[i] - '0');
    if (i == 0) {
      n[i] = (ten[i] * now) % 2019;
    } else {
      n[i] = (n[i - 1] + (ten[i] * now) % 2019) % 2019;
    }
    if (n[i] == 0)
      ans++;
    m[n[i]]++;
  }

  for (long long i = 0; i < 3000; i++) {
    if (m[i] > 1) {
      ans += (m[i] * (m[i] - 1)) / 2;
    }
  }

  cout << ans << endl;

  return 0;
}