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

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define MAX_C 1000000001
#define MOD 1000000007

int main() {
  string s;
  cin >> s;
  int len = s.size();
  vector<int> snum(len);
  for (int i = 0; i < len; i++) {
    snum[i] = (int)(s[i] - '0');
  }

  vector<int> memo(len); // memo[i]:下からi桁のあまり
  int tmp = 0, tmp_ten = 1;
  for (int i = 0; i < len; i++) {
    // tmp = (tmp * 10 + snum[len-1-i]) % MOD;
    tmp = (tmp_ten * snum[len - 1 - i] + tmp) % 2019;
    tmp_ten = (10 * tmp_ten) % 2019;
    memo[i] = tmp;
  }
  // cout << memo[0] << memo[1] << endl;

  sort(memo.begin(), memo.end());
  // cout << "sort" << endl;
  ll cnt = 0;

  ll c_tmp = 1;
  int c = 0;
  for (int i = 0; i < len; i++) {
    if (memo[i] == c)
      c_tmp++;
    else {
      cnt += (ll)(c_tmp * (c_tmp - 1)) / 2;
      c_tmp = 1;
      c = memo[i];
    }
  }
  cnt += (ll)(c_tmp * (c_tmp - 1)) / 2;

  cout << (ll)cnt << endl;
}


Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define MAX_C 1000000001
#define MOD 1000000007

int main() {
  string s;
  cin >> s;
  int len = s.size();
  vector<int> snum(len);
  for (int i = 0; i < len; i++) {
    snum[i] = (int)(s[i] - '0');
  }

  vector<int> memo(len); // memo[i]:下からi桁のあまり
  int tmp = 0, tmp_ten = 1;
  for (int i = 0; i < len; i++) {
    // tmp = (tmp * 10 + snum[len-1-i]) % MOD;
    tmp = (tmp_ten * snum[len - 1 - i] + tmp) % 2019;
    tmp_ten = (10 * tmp_ten) % 2019;
    memo[i] = tmp;
  }
  // cout << memo[0] << memo[1] << endl;

  sort(memo.begin(), memo.end());
  // cout << "sort" << endl;
  ll cnt = 0;
  for (int i = 0; i < 2019; i++) {
    auto itl = lower_bound(memo.begin(), memo.end(), i);
    auto itu = upper_bound(memo.begin(), memo.end(), i);
    ll c = (ll)(itu - itl);
    // if (c != 0) cout << i << ", " << c << endl;
    if (i == 0)
      c++;
    cnt += (c * (c - 1)) / 2;
  }

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

#include <bits/stdc++.h>
using namespace std;
int ans, rmd, idx = 1, rmds[2019] = {1};
string s;
int main() {
  cin >> s;
  reverse(s.begin(), s.end());
  for (auto i : s) {
    rmd = (idx * (i - '0') + rmd) % 2019;
    rmds[rmd]++;
    idx = idx * 10 % 2019;
  }
  for (auto i : rmds) {
    ans += i * (i - 1) / 2;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int DIVISOR = 2019;
int len, num, len1, len2, cnt, P;
string S, strs;
map<string, bool> MAP;
bool check(string str);
int main() {
  cin >> S;
  len1 = S.length() - 3;
  for (int i = 0; i < len1; i++) {
    len2 = S.length() - i;
    for (int j = 4; j <= len2; j++) {
      if (check(S.substr(i, j))) {
        cnt++;
      }
    }
  }
  cout << cnt << endl;
}
bool check(string str) {
  if (MAP.find(str) != MAP.end()) {
    return MAP[str];
  }
  len = str.length();
  num = 0;
  P = 0;
  for (int i = 0; i < len; i++) {
    P += str[i] - '0';
  }
  if (P % 3) {
    return false;
  }
  for (int i = 0; i < len; i++) {
    num = num * 10 + (str[i] - '0');
    num %= DIVISOR;
  }
  return MAP[str] = (num ? false : true);
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const ll MOD = 2019;

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

  vector<ll> cnt(MOD, 0);
  ll total = 0;
  ll x = 1;
  ll ans = 0;
  rep(i, n) {
    cnt.at(total)++;
    total += (s.at(i) - '0') * x;
    total %= MOD;
    x = x * 10 % MOD;
    ans += cnt.at(total);
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

const ll MOD = 2019;

int main() {
  string s;
  cin >> s;
  vector<ll> imod(s.size());
  for (ll i = 0; i < (ll)s.size(); ++i) {
    if (i == 0)
      imod.at(i) = (s.at(i) - '0') % MOD;
    else
      imod.at(i) = (imod.at(i - 1) * 10 + (s.at(i) - '0')) % MOD;
  }
  // rep(i,s.size()) cout << imod.at(i) << " ";
  // cout << endl;

  ll ans = 0;
  rep(i, s.size()) if (imod.at(i) == 0)++ ans;
  for (ll i = 0; i < (ll)s.size() - 1; ++i) {
    ll x = imod.at(i);
    for (ll j = i + 1; j < (ll)s.size(); ++j) {
      x *= 10;
      x %= MOD;
      if (x == imod.at(j))
        ++ans;
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
#define lli long long int
#define inf 2147483647
#define infll LLONG_MAX
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define endl "\\n"
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(10);
  map<int, int> m;
  string s;
  cin >> s;
  lli sum = 0, now = 0, p = 1;

  if (s.size() <= 3) {
    cout << "0" << endl;
    return 0;
  }

  m[now]++;
  reverse(all(s));
  for (auto c : s) {
    now = now + (c - '0') * p;
    p = p * 10 % 2019;
    now %= 2019;
    m[now]++;
  }

  for (auto p : m) {
    sum += (p.second) * (p.second - 1) / 2;
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define lli long long int
#define inf 2147483647
#define infll LLONG_MAX
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define endl "\\n"
using namespace std;

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

  string s;
  cin >> s;
  lli sum = 0;

  if (s.size() <= 3) {
    cout << "0" << endl;
    return 0;
  }

  int size = s.size();
  bool flag = false;
  for (int i = 0; i <= size - 4; i++) {
    int cn = stoi(s.substr(i, 3));
    for (int j = 3 + i; j < size; j++) {
      // cn = cn * 10 + stoi(s.substr(j, 1));
      cn = cn * 10 + s[j] - '0';
      cn %= 2019;
      if (cn == 0)
        sum++;
    }
  }
  cout << sum << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

string S, T, U;
ll ANS, SUM, NOW, N, M, A, B, C, D, E, F[1100000];

ll mod(ll n) {
  if (n == 1)
    return 10;
  if (n % 2 == 0)
    return (mod(n / 2) * mod(n / 2)) % 2019;
  return (mod(n - 1) * 10) % 2019;
}

int main() {
  cin >> S;
  N = S.size();
  F[0] = 0;
  T = S.substr(N - 1, 1);
  F[1] = stoll(T);
  for (ll i = 2; i <= N; i++) {
    U = S.substr(N - i, 1);
    F[i] = (F[i - 1] + stoll(U) * mod(i - 1)) % 2019;
  }
  sort(F, F + N + 1);
  ANS = 0;
  NOW = F[0];
  SUM = 1;
  for (ll i = 1; i <= N; i++) {
    if (NOW == F[i]) {
      SUM++;
      ANS += (SUM - 1);
    } else {
      SUM = 1;
      NOW = F[i];
    }
  }
  cout << ANS << endl;
  return 0;
}

Error: 0

Fixed submission:

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

string S;
ll ANS, SUM, NOW, N, M, A, B, C, D, E, F[1100000];

int main() {
  cin >> S;
  N = S.size();
  F[0] = 0;
  for (ll i = 1; i <= N; i++) {
    string T = S.substr(N - i);
    B = stoll(T);
    F[i] = B % 2019;
  }
  sort(F, F + N + 1);
  ANS = 0;
  NOW = F[0];
  SUM = 1;
  rep1(i, N) {
    if (F[i] == NOW) {
      SUM++;
    } else {
      ANS += (SUM * (SUM - 1) / 2);
      SUM = 1;
      NOW = F[i];
    }
  }
  if (SUM > 1)
    ANS += (SUM * (SUM - 1) / 2);
  cout << ANS << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;
using ll = long long;

int main() {
  string s;
  cin >> s;
  ll n = static_cast<ll>(s.size());

  ll a = 0;
  ll b = 1;
  vector<ll> vec(2020, 0);
  vec[0] = 1;
  for (ll i = n - 1; 0 <= i; --i) {
    a = (a + b * (s[i] - '0')) % 2019;
    ++vec[a];
    b = (b * 10) % 2019;
  }

  ll ans = 0;
  for (auto &v : vec)
    ans += (v * (v - 1)) / 2;

  cout << ans << endl;
}

Error: 0

Fixed submission:

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

using namespace std;
using ll = long long;

int main() {
  string s;
  cin >> s;
  int n = static_cast<int>(s.size());

  int a = 0;
  int b = 1;
  vector<long long> vec(2000, 0);
  vec[0] = 1;
  for (int i = n - 1; 0 <= i; --i) {
    a = (a + b * (s[i] - '0')) % 2019;
    ++vec[a];
    b = (b * 10) % 2019;
  }

  long long ans = 0;
  for (auto &v : vec)
    ans += (v * (v - 1)) / 2;

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

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

int main() {

  string s;
  cin >> s;

  int len = s.size();
  map<int, int> modulocount;
  int current_mod = 0;
  int mod10n = 0;

  modulocount[0] = 1;
  mod10n = 1;
  for (int i = len - 1; i >= 0; --i) {
    current_mod += (mod10n * (s[i] - '0'));
    while (current_mod >= 2019) {
      current_mod -= 2019;
    }

    modulocount[current_mod]++;
    // fprintf(stderr,"i:%d current:%d mod10n:%d\\n",i,current_mod,mod10n);

    mod10n *= 10;
    while (mod10n >= 2019) {
      mod10n -= 2019;
    }
  }

  int count = 0;
  for (pair<int, int> p : modulocount) {
    int combination = p.second * (p.second - 1) / 2;
    // fprintf(stderr,"mod %d, count %d, combination
    // %d\\n",p.first,p.second,combination);
    count += combination;
  }
  cout << count << endl;
}


Error: TLE

Fixed submission:

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

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

  int size = s.size();
  int residue;
  int count = 0;
  for (int i = 0; i < size - 3; ++i) {
    residue = 0;
    for (int j = i; j < size; ++j) {
      residue *= 10;
      residue += s[j] - '0';
      while (residue >= 2019) {
        residue -= 2019;
      }
      if (residue == 0) {
        ++count;
      }
    }
  }
  cout << count << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

// OPTIMIZATONS
#pragma GCC optimize("02")
// CODE
#include <bits/stdc++.h>
#define int long long
#define float double
#define endl "\\n"
#define debug cout << "Good uptill here" << endl << flush;
#define deb(x) cout << #x << ":" << x << endl << flush;

using namespace std;
const int N = 2e5;
const int d = 2019;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  // CODE FOR SPECIFIED INPUT AND OUTPUT
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  vector<int> dp(d, 0), last(d, 0);
  string s;
  cin >> s;
  int n = s.length();
  last[s[0] - '0'] = 1;
  int ans = last[0];
  for (int i = 1; i < n; i++) {
    fill(dp.begin(), dp.end(), 0);
    int cur = s[i] - '0';
    dp[cur] = 1;
    for (int j = 0; j < d; j++) {
      if (last[j] == 0)
        continue;
      dp[(j * 10 + cur) % d] += last[j];
    }
    ans += dp[0];
    last = dp;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

// OPTIMIZATONS
#pragma GCC optimize("02")
// CODE
#include <bits/stdc++.h>
#define int long long
#define float double
#define endl "\\n"
#define debug cout << "Good uptill here" << endl << flush;
#define deb(x) cout << #x << ":" << x << endl << flush;

using namespace std;
const int N = 2e5;
const int d = 2019;
vector<vector<int>> dp(N, vector<int>(d, 0));
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  // CODE FOR SPECIFIED INPUT AND OUTPUT
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  string s;
  cin >> s;
  int n = s.length();
  dp[0][s[0] - '0'] = 1;
  for (int i = 1; i < n; i++) {
    int cur = s[i] - '0';
    dp[i][cur] = 1;
    for (int j = 0; j < d; j++) {
      if (dp[i - 1][j] == 0)
        continue;
      dp[i][(j * 10 + cur) % d] += dp[i - 1][j];
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += dp[i][0];
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (long long i = 0; i < n; i++)
#define REPR(i, n) for (long long i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (long long i = m; i <= n; i++)
#define FORR(i, m, n) for (long long i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(), (v).end()
#define FIN                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
ll power(ll a, ll b) {
  return b ? power(a * a % 2019, b / 2) * (b % 2 ? a : 1) % 2019 : 1;
}

int main() {
  FIN

      string s;
  cin >> s;

  ll n = s.size();
  map<ll, ll> mp;
  ll count = 0;
  ll dig;
  FORR(i, n - 1, 0) {

    if (i == n - 1)
      dig = s[i] - '0';
    else
      dig += power(10, count) * (s[i] - '0');

    mp[dig % 2019]++;
    dig %= 2019;
    count++;
  }

  mp[0]++;
  ll ans = 0;
  for (auto p : mp) {
    ll m = p.second;
    ans += m * (m - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (long long i = 0; i < n; i++)
#define REPR(i, n) for (long long i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (long long i = m; i <= n; i++)
#define FORR(i, m, n) for (long long i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(), (v).end()
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;

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

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

  vvll dp(n + 2, vll(2022));
  if (n < 4) {
    cout << 0 << endl;
    return 0;
  }

  dp[1][s[0] - '0'] = 1;

  FOR(i, 1, n - 1) {

    ll keta = s[i] - '0';
    dp[i + 1][keta]++;

    REP(j, 2019) { dp[i + 1][(j * 10 + keta) % 2019] += dp[i][j]; }
  }
  ll ans = 0;

  FOR(i, 1, n) { ans += dp[i][0]; }

  cout << ans << 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;
  int64_t N = S.size();
  reverse(S.begin(), S.end());
  vector<int64_t> Count(2019, 0);
  Count[0]++;
  int64_t num = 1, cur = 0, ans = 0;
  for (int i = 1; i <= N; i++) {
    int sint = S[i - 1] - '0';
    cur = (cur + sint * num) % 2019;
    Count[cur]++;
    num *= 10;
    num %= 2019;
  }
  for (int64_t c : Count) {
    ans += c * (c - 1) / 2;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  string S;
  cin >> S;
  int64_t N = S.size();
  int64_t ans = 0, l = 0, r = 3;
  for (int i = 0; i < N - 4; i++) {
    int64_t a = stoi(S.substr(i, 5));
    int64_t r = a % 2019;
    if (r == 0)
      ans++;
    for (int j = i + 5; j < N; j++) {
      int64_t b = S.at(j) - '0';
      r = (r * 10 + b) % 2019;
      if (r == 0) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const double PI = 3.14159265358979323846;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
// #define rep(i, n) for (ll i = 0; i < (n); ++i)
// typedef pair<ll, ll> P;
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

double equal(double a, double b) { return fabs(a - b) < DBL_EPSILON; }

std::istream &operator>>(std::istream &in, set<string> &o) {
  string a;
  in >> a;
  o.insert(a);
  return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
  ll a;
  in >> a;
  o.push(a);
  return in;
}

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

// ofstream outfile("log.txt");
// outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0')
// << rank << endl;
//  std::cout << std::bitset<8>(9);
// const ll mod = 1e10;

typedef priority_queue<ll, vector<ll>, greater<ll>> PQ_ASK;

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

  string s;
  cin >> s;

  int n = s.size();
  vector<int> prev(2019);
  int ans = 0;
  rep(i, n) {
    vector<int> next(2019);

    int k = s[i] - '0';
    next[k] += 1;

    rep(j, 2019) {
      int l = j * 10 + k;
      l = l % 2019;
      next[l] += prev[j];
    }
    prev = next;
    ans += prev[0];
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const double PI = 3.14159265358979323846;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
// #define rep(i, n) for (ll i = 0; i < (n); ++i)
// typedef pair<ll, ll> P;
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

double equal(double a, double b) { return fabs(a - b) < DBL_EPSILON; }

std::istream &operator>>(std::istream &in, set<string> &o) {
  string a;
  in >> a;
  o.insert(a);
  return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
  ll a;
  in >> a;
  o.push(a);
  return in;
}

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

// ofstream outfile("log.txt");
// outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0')
// << rank << endl;
//  std::cout << std::bitset<8>(9);
// const ll mod = 1e10;

typedef priority_queue<ll, vector<ll>, greater<ll>> PQ_ASK;

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

  string s;
  cin >> s;

  int n = s.size();
  map<int, ll> prev;
  int ans = 0;
  rep(i, n) {
    map<int, ll> next;

    int k = s[i] - '0';
    next[k] += 1;

    for (auto e : prev) {
      int j = e.first;
      int l = j * 10 + k;
      l = l % 2019;
      next[l] += e.second;
    }
    prev = next;
    ans += prev[0];
  }

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ll long long
using namespace std;
int dp[2][2020];
int main() {
  string str;
  ll ans = 0;
  cin >> str;
  for (int i = 0; i < str.length(); i++) {
    for (int j = 0; j < 2019; j++) {
      dp[(i + 1) % 2][j] = 0;
    }
    int k = str[i] - '0';
    dp[(i + 1) % 2][k] = 1;
    for (int j = 0; j < 2019; j++) {
      dp[(i + 1) % 2][(j * 10 + k) % 2019] += dp[i % 2][j];
    }
    ans += dp[i % 2][0];
  }
  ans += dp[str.length() % 2][0];
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ll long long
using namespace std;
set<string> S;
ll a[2000010];
ll s[2000010];
int main() {
  string str;

  cin >> str;
  str = "&" + str;
  ll ans = 0;
  for (int j = 1; j <= str.length(); j++) {
    a[j - 1] = 0;
    int flag = 0;
    for (int i = j; i <= str.length(); i++) {
      a[i] = (a[i - 1] * 10 + (str[i] - '0')) % 2019;

      if (a[i] == 0) {
        flag = 1;
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

ll dp[200001];
long long multiple_of_2019(string s) {
  int n = (int)s.size();
  int mul = 1;
  for (int i = n - 1; i >= 0; i--) {
    int digit = (int)s[i] - '0';
    dp[i] = (digit * mul + dp[i + 1]) % 2019;
    mul = (mul * 10) % 2019;
  }
  map<int, ll> mp;
  for (int i = 0; i <= n; i++) {
    mp[dp[i]] += 1;
  }
  ll ans = 0;
  for (auto i = mp.begin(); i != mp.end(); i++) {
    // cout << i->first << " => " << i->second << endl;
    ll a = i->second;
    ans += a * (a - 1) / 2;
  }
  /*
  for(int i=0;i<=n;i++) {
    cout << i << " -> " << dp[i] << endl;
  }
  */
  return ans;
}

int main() {
  string s;
  cin >> s;
  cout << multiple_of_2019(s);
}

Error: TLE

Fixed submission:

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

using namespace std;

long long multiple_of_2019(string s) {
  map<int, int> m;
  long long t = 0;
  long long n = s.size();
  m[0] = 1;
  for (int i = n - 1; i >= 0; i--) {
    long long acc = 1;
    for (int j = 0; j < n - i - 1; j++) {
      acc *= 10;
      acc = acc % 2019;
    }
    long long a = s[i] - '0';
    acc *= a;
    acc = acc % 2019;
    t += acc;
    t = t % 2019;
    m[t] = m[t] + 1;
  }
  long long ans = 0;
  for (auto i = m.begin(); i != m.end(); i++) {
    long long v = i->second;
    if (v == 0)
      continue;
    ans += (v * (v - 1)) / 2;
  }
  return ans;
}

int main() {
  string s;
  cin >> s;
  cout << multiple_of_2019(s);
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <string.h>
int main(void) {
  int i = 0;
  char S[200001];
  long long a = 0, d = 1LL, x = 0, count[2019] = {1LL};
  i = scanf("%s", S);
  i = strlen(S);
  while (i--) {
    x = (x + (S[i] - '0') * d) % 2019;
    a += count[x]++;
    d = d * 10 % 2019;
  }
  return printf("%lld\\n", a), 0;
}

Error: WA

Fixed submission:

#include <stdio.h>
int main() {
  int c, i = 0;
  long long a = 0, d = 1LL, x = 0, count[2020] = {0}, S[200001];
  count[0]++;
  for (;;) {
    c = getchar();
    S[i] = c;
    if (c == EOF)
      break;
    i++;
  }
  while (i--) {
    x = (x + (S[i] - '0') * d) % 2019;
    a += count[x]++;
    d = d * 10 % 2019;
  }
  return printf("%lld", a), 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;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  string str;
  cin >> str;
  reverse(str.begin(), str.end());
  int n = str.size();
  vector<int> a(n), s(n + 1);
  int pow_10_mod = 1;
  s[0] = 0;
  rep(i, n) {
    a[i] = ((str[i] - '0') * pow_10_mod) % 2019;
    s[i + 1] = (s[i] + a[i]) % 2019;
    pow_10_mod = (pow_10_mod * 10) % 2019;
  }
  vector<int> cnt(2019);
  int ans = 0;
  rep(i, n + 1) {
    ans += cnt[s[i]];
    cnt[s[i]]++;
  }
  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;

int main() {
  string str;
  cin >> str;
  if (str.length() <= 4) {
    int fst = 0;
    if (stoi(str, nullptr) % 2019 == 0 && str.length() == 4)
      fst = 1;
    cout << fst << endl;
  }
  int n = str.length();
  int count = 0;
  if (str.length() > 4) {
    for (int i = 0; i < n - 3; i++) {
      for (int j = 1; j < n - i + 1; j++) {
        string sub = str.substr(i, j);
        int temp = stoi(sub, nullptr);
        if (temp % 2019 == 0)
          count++;
      }
    }
    cout << count << endl;
  }
}