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

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

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

  // 2019 = 3 * 673(prime)
  // 673の倍数判定で組をためる
  int cur = 0;
  int d = 1;
  vector<vector<int>> q(673, vector<int>());
  q[0].push_back(N);
  for (int i = N - 1; i >= 0; i--) {
    cur = (cur + d * (S[i] - '0')) % 673;
    q[cur].push_back(i);
    d = (d * 10) % 673;
  }

  // 3の倍数判定をためた組に行う
  int th[200010];
  th[N] = th[N + 1] = 0;
  for (int i = N - 1; i >= 0; i--) {
    th[i] = th[i + 1] + (S[i] - '0');
  }

  long long count = 0;
  for (int i = 0; i < 673; i++) {
    for (int j = 0; j < (int)q[i].size(); j++) {
      for (int k = j + 1; k < (int)q[i].size(); k++) {
        if ((th[q[i][k]] - th[q[i][j]]) % 3 == 0)
          count++;
      }
    }
  }
  cout << count << endl;
}

Error: TLE

Fixed submission:

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

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

  // 2019 = 3 * 673(prime)
  // 673の倍数判定で組をためる
  int res[200010];
  res[N] = 0;
  int d = 1;
  for (int i = N - 1; i >= 0; i--) {
    res[i] = (d * (S[i] - '0') + res[i + 1]) % 673;
    d = (d * 10) % 673;
  }
  res[N] = 0;
  queue<pair<int, int>> q;
  for (int i = 0; i < N; i++) {
    for (int j = i; j < N; j++) {
      if (res[i] == res[j + 1]) {
        q.push(make_pair(i, j));
      }
    }
  }

  // 3の倍数判定をためた組に行う
  int th[200010];
  th[N] = 0;
  for (int i = N - 1; i >= 0; i--) {
    th[i] = th[i + 1] + (S[i] - '0');
  }

  long long count = 0;
  while (!q.empty()) {
    int i = q.front().first, j = q.front().second;
    if ((th[i] - th[j + 1]) % 3 == 0)
      count++;
    q.pop();
  }
  cout << count << 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++)
using ll = long long;
using P = pair<int, int>;
ll modPow(long long a, long long n, long long p) {
  return (n == 1) ? a : a * modPow(a, n - 1, p) % p;
}
int main() {
  ll mod = 2019;
  string s;
  cin >> s;
  s += '0';
  int n = s.size();
  map<ll, ll> cnt;
  ll ans = 0;
  ll x = 0;
  int keta = 10;
  for (int i = n - 1; i >= 0; i--) {
    ll m = s[i] - '0';

    x += m * keta;
    keta *= 10;
    keta %= mod;
    x %= mod;
    if (x < 0)
      x += mod;
    ans += cnt[x];
    cnt[x]++;
  }
  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++)
using ll = long long;
using P = pair<int, int>;
ll modPow(long long a, long long n, long long p) {
  return (n == 1) ? a : a * modPow(a, n - 1, p) % p;
}
int main() {
  ll mod = 2019;
  string s;
  cin >> s;
  s += '0';
  int n = s.size();
  map<ll, ll> cnt;
  ll ans = 0;
  ll x = 0;
  for (int i = n - 1; i >= 0; i--) {
    ll m = s[i] - '0';

    x += m * modPow(10, n - i, mod);
    x %= mod;
    if (x < 0)
      x += mod;
    ans += cnt[x];
    cnt[x]++;
  }
  cout << ans << endl;

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

#include <iostream>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <vector>

int main() {
  std::string s;
  std::cin >> s;
  std::unordered_map<int64_t, int64_t> modMap;
  const int64_t mod = 2019;
  int64_t tmp = 0;
  int64_t digit = 1;
  modMap[0] = 1;
  for (int i = s.size() - 1; i >= 0; --i) {
    int num = static_cast<int>(s[i] - '0');
    digit %= mod;
    tmp = tmp + digit * num;
    tmp %= mod;
    if (modMap.find(tmp) != modMap.end()) {
      modMap[tmp]++;
    } else {
      modMap[tmp] = 1;
    }

    digit *= 10;
  }

  int64_t ans = 0;
  for (auto itr : modMap) {
    ans += itr.second * (itr.second - 1) / 2;
  }

  std::cout << ans << std::endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <unordered_set>
#include <vector>

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

  int64_t count = 0;
  for (int i = 0; i < s.size() - 3; ++i) {
    int64_t digit = 1;
    int64_t compare = 0;
    for (int j = i + 2; j >= i; --j) {
      compare += static_cast<int64_t>(s[j] - '0') * digit;
      digit *= 10;
    }
    for (int j = i + 3; j < s.size(); ++j) {
      compare = compare * 10 + static_cast<int64_t>(s[j] - '0');
      if (compare % 2019 == 0) {
        ++count;
      }
    }
  }
  std::cout << count << std::endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iostream>

using namespace std;
#define ll long long int

int powe(ll a, ll b) {
  if (b == 0)
    return 1;
  ll k = powe(a, b / 2);
  ll ans = (k * k) % 2019;
  if (b % 2 != 0)
    ans = (ans * a) % 2019;

  return ans;
}
string s;
int main() {

  cin >> s;
  if (s.empty()) {

    cout << 0;
    return 0;
  }

  ll pas = 0;
  unordered_map<ll, ll> arr;
  ll count = 0;
  for (ll i = s.length() - 1; i >= 0; i--) {
    ll no = s[i] - '0';
    no = (no * powe(10, s.length() - i - 1)) % 2019;
    no += pas;
    no = no % 2019;
    if (arr.find(no) == arr.end())
      arr[no] = 0;

    count += arr[no];

    arr[no]++;

    if (no == 0)
      count++;
    pas = no;
  }

  cout << count;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
vector<ll> arr;

string s;
int main() {

  cin >> s;
  if (s.empty()) {

    cout << 0;
    return 0;
  }
  ll count = 0;

  for (ll j = 0; j < s.length(); j++) {
    arr.clear();

    arr.push_back(int64_t(s[j]) - int64_t('0'));
    if (arr[0] == 0)
      count++;
    for (ll i = 1 + j; i < s.length(); i++) {

      ll n = arr[arr.size() - 1];
      n = n * 10 + int64_t(s[i]) - int64_t('0');
      n = n % 2019;
      arr.push_back(n);
      if (arr[arr.size() - 1] == 0)
        count++;
    }
  }
  cout << count << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

const int mod = 2019;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int f[mod];
  // set<int> s1;
  for (int i = 0; i < mod; i++) {
    f[(i * 10) % mod] = i;
    ///   s1.insert((i*10)%mod);
  }
  string s;
  cin >> s;
  int n = s.size();
  ll ans = 0;
  int pre[2019];
  for (int j = 0; j < 2019; j++) {
    if (j == s[0] - '0')
      pre[j] = 1;
    else
      pre[j] = 0;
  }
  int gre[2019];
  ans += pre[0];
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 2019; j++) {
      int y = (j - (s[i] - '0') + mod) % mod;
      int r = f[y];
      gre[j] = pre[r];
      if (j == s[i] - '0') {
        gre[j]++;
      }
    }
    ans += gre[0];
    for (int j = 0; j < mod; j++) {
      pre[j] = gre[j];
    }
  }
  cout << ans << '\\n';
}


Error: 0

Fixed submission:

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

const int mod = 2019;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int f[mod];
  for (int i = 0; i < mod; i++) {
    f[(i * 10) % mod] = i;
  }
  string s;
  cin >> s;
  int n = s.size();
  ll ans = 0;
  int dp[n][2019];
  for (int j = 0; j < 2019; j++) {
    if (j == s[0] - '0')
      dp[0][j] = 1;
    else
      dp[0][j] = 0;
  }
  ans += dp[0][0];
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 2019; j++) {
      int y = (j - (s[i] - '0') + mod) % mod;
      int r = f[y];
      dp[i][j] = dp[i - 1][r];
      if (j == s[i] - '0') {
        dp[i][j]++;
      }
    }
    ans += dp[i][0];
  }
  cout << ans << '\\n';
}
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 ten = 1, sum = 0, cnt[2019] = {};
  long long ans = 0;
  for (int i = 0; i < s.size(); i++) {
    cnt[sum]++;
    sum += (s[i] - '0') * ten;
    sum %= 2019;
    ans += cnt[sum];
    ten = (ten * 10) % 2019;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  string s;
  long long sol = 0;
  cin >> s;
  for (int i = 0; i + 4 <= s.size(); i++) {
    for (int j = 4; i + j <= s.size(); j++) {
      string sub;
      sub = s.substr(i, j);
      stringstream fn(sub);
      int x;
      fn >> x;
      if (x % 2019 == 0)
        sol++;
    }
  }
  cout << sol << '\\n';
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

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


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  int ten = 1, sum = 0, cnt[2019] = {};
  long long ans = 0;
  for (int i = 0; i < s.size(); i++) {
    cnt[sum]++;
    sum += ((s[i] - '0') * ten) % 2019;
    ans += cnt[sum];
    ten = (ten * 10) % 2019;
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

const ll mod = 2019;

int main(void) {
  string s;
  cin >> s;
  const int n = s.size();

  vector<ll> m(2019, 0);
  vector<ll> next(2019, 0);
  m[0] = 1;
  ll r = 0;
  ll sum = 0;
  rep(i, n) {
    next.assign(2019, 0);
    rep(j, 2019) { next[(j * 10LL) % mod] += m[j]; }
    ll b = r * 10 + (int)(s[i] - '0');
    b %= mod;
    r = b;
    sum += next[b];
    next[b]++;
    swap(m, next);
  }
  cout << sum << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

const ll mod = 2019;

int main(void) {
  string s;
  cin >> s;
  const int n = s.size();

  map<int, int> m;
  m[0] = 1;
  ll r = 0;
  ll sum = 0;
  rep(i, n) {
    map<int, int> next;
    for (auto p : m) {
      if (p.second == 0)
        continue;
      next[(p.first * 10LL) % mod] += p.second;
    }
    ll b = r * 10 + (int)(s[i] - '0');
    b %= mod;
    r = b;
    sum += next[b];
    next[b]++;
    swap(m, next);
  }
  cout << sum << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

const int S_MAX = 1e5 * 2;
const int K = 2019;
int dp[2][K];

int main() {

  // input from txt （提出時にこの箇所は削除すること）
  // std::ifstream in("input.txt");
  // std::cin.rdbuf(in.rdbuf());

  string S;
  cin >> S;
  int s = S.size();

  int ans = 0;

  dp[0][S.at(0) - '0'] = 1;
  for (int i = 1; i < s; i++) {
    int x = S.at(i) - '0';
    dp[1][x]++;
    for (int j = 0; j < K; j++) {
      dp[1][(j * 10 + x) % K] += dp[0][j];
    }
    ans += dp[1][0];
    for (int j = 0; j < K; j++) {
      dp[0][j] = dp[1][j];
      dp[1][j] = 0;
    }
  }

  cout << ans << endl;
}


Error: MLE

Fixed submission:

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

const int S_MAX = 1e5 * 2;
const int K = 2019;
int dp[S_MAX][K];

int main() {

  // input from txt （提出時にこの箇所は削除すること）
  // std::ifstream in("input.txt");
  // std::cin.rdbuf(in.rdbuf());

  string S;
  cin >> S;
  int s = S.size();

  dp[0][S.at(0) - '0'] = 1;
  for (int i = 1; i < s; i++) {
    int x = S.at(i) - '0';
    dp[i][x]++;
    for (int j = 0; j < K; j++) {
      dp[i][(j * 10 + x) % K] += dp[i - 1][j];
    }
  }

  int ans = 0;
  for (int i = 0; i < s; i++) {
    ans += dp[i][0];
  }

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

// by Isaunoya
#include <bits/stdc++.h>
#define int long long
using namespace std;
struct io {
  char buf[1 << 25 | 3], *s;
  int f;
  io() { f = 0, buf[fread(s = buf, 1, 1 << 25, stdin)] = '\\n'; }
  io &operator>>(int &x) {
    for (x = f = 0; !isdigit(*s); ++s)
      f |= *s == '-';
    while (isdigit(*s))
      x = x * 10 + (*s++ ^ 48);
    return x = f ? -x : x, *this;
  }
};

const int maxn = 2e5 + 52;
int g[maxn];
signed main() {
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
#endif
  string s;
  cin >> s;
  int n = s.length(), res = 0;
  int x, y;
  x = y = 0;
  g[0] = 1;
  for (int i = 1; i <= n; i++)
    g[i] = g[i - 1] * 10 % 2019;
  static int a[23333];
  int ans = 0;
  a[0] = 1;
  for (int i = n - 1; ~i; --i) {
    x = (s[i] ^ '0') * g[n - i - 1] % 2019;
    ans += a[(x + y) % 2019]++;
    y = (x + y) % 2019;
  }
  cout << ans << '\\n';
  return 0;
}


Error: 0

Fixed submission:

// by Isaunoya
#include <bits/stdc++.h>
#define int long long
using namespace std;
struct io {
  char buf[1 << 25 | 3], *s;
  int f;
  io() { f = 0, buf[fread(s = buf, 1, 1 << 25, stdin)] = '\\n'; }
  io &operator>>(int &x) {
    for (x = f = 0; !isdigit(*s); ++s)
      f |= *s == '-';
    while (isdigit(*s))
      x = x * 10 + (*s++ ^ 48);
    return x = f ? -x : x, *this;
  }
};

const int maxn = 1e5 + 51;
int g[maxn];
signed main() {
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
#endif
  string s;
  cin >> s;
  int n = s.length(), res = 0;
  int x, y;
  x = y = 0;
  g[0] = 1;
  for (int i = 1; i <= n; i++)
    g[i] = g[i - 1] * 10 % 2019;
  static int a[23333];
  int ans = 0;
  a[0] = 1;
  for (int i = n - 1; ~i; --i) {
    x = (s[i] ^ '0') * g[n - i - 1] % 2019;
    ans += a[(x + y) % 2019]++;
    y = (x + y) % 2019;
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <map>
#include <string>
using namespace std;

int main(void) {

  string S;

  cin >> S;

  map<int, int> mp;

  int count = 0;
  int res = 0;
  int Ti[S.length()];
  int pow_10 = 1;
  for (int i = S.length() - 1; i >= 0; i--) {
    string str = S.substr(i, 1);
    int num = atoi(str.c_str());

    res = ((num % 2019) * pow_10 + res) % 2019;

    if (mp.find(res) != mp.end()) {
      mp[res] += 1;
    } else {
      mp[res] = 1;
    }

    pow_10 = (pow_10 * 10) % 2019;

    // cout << num << " " << res << " " << pow_10 << endl;
  }

  for (auto const &[key, value] : mp) {
    if (key == 0)
      count += value;
    int counter = value * (value - 1) / 2;
    // if (counter >= 1) cout << key << " " << value << endl;
    count += counter;
  }

  cout << count;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main(void) {

  string S;

  cin >> S;

  map<int, int> mp;

  int count = 0;
  int res;
  for (int i = 0; i < S.length(); i++) {
    string str(1, S[i]);

    for (int j = i + 1; j < S.length(); j++) {
      str += S[j];
      int num = atoi(str.c_str());
      if (mp.find(num) != mp.end()) {
        res = mp[num];
      } else {
        res = num % 2019;
        mp[num] = res;
      }
      if (res == 0) {
        count++;
      }

      // cout << str << " " << num << " " << count << endl;
    }
  }
  cout << count;

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

/*
 * Matheus Oliveira
 * 09:22:26 26/04/2020
 * d.cpp
 */

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

#define EPS
#define INF
#define MAXN 200002
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define sc(x) scanf("%d", &x);
#define sc2(x, y) scanf("%d %d", &x, &y);
#define sc3(x, y, z) scanf("%d %d %d", &x, &y, &z);
#define debug(x) printf("%s: %d\\n", #x, x);
#define debugarr(x, bg, ed)                                                    \\
  printf("%s: ", #x);                                                          \\
  for (int i = bg; i < ed; i++)                                                \\
    printf("%d%c", x[i], (i == ed - 1) ? '\\n' : ' ');

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

const int mod = 2019;

int szn;
char n[MAXN];
int remo[mod + 10];

int main() {
  ll ans = 0;
  int idx, m;

  scanf("%s", n);
  szn = strlen(n);

  idx = szn - 1;
  m = n[idx] - '0';

  remo[m] = 1;
  remo[0] = 1;

  idx--;
  int p = 10;
  while (idx >= 0) {
    m = (m + (n[idx] - '0') * p) % mod;

    // printf("m: %d, idx: %d\\n", m, idx);

    ans += ((ll)remo[m]);
    remo[m]++;

    p = (p * 10) % mod;
    idx--;
  }

  printf("%lld\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

/*
 * Matheus Oliveira
 * 09:22:26 26/04/2020
 * d.cpp
 */

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

#define EPS
#define INF
#define MAXN 200002
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define sc(x) scanf("%d", &x);
#define sc2(x, y) scanf("%d %d", &x, &y);
#define sc3(x, y, z) scanf("%d %d %d", &x, &y, &z);
#define debug(x) printf("%s: %d\\n", #x, x);
#define debugarr(x, bg, ed)                                                    \\
  printf("%s: ", #x);                                                          \\
  for (int i = bg; i < ed; i++)                                                \\
    printf("%d%c", x[i], (i == ed - 1) ? '\\n' : ' ');

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

const int mod = 2019;

int szn;
char n[MAXN];

queue<pii> q;
bool vis[MAXN][mod + 1];

int main() {
  ll ans = 0;
  int idx, m;

  scanf("%s", n);
  szn = strlen(n);

  for (int i = 1; i < szn; i++)
    q.push(mp(i, n[i - 1] - '0'));
  pii aux;

  while (not q.empty()) {
    aux = q.front();
    q.pop();

    idx = aux.ff, m = aux.ss;
    if (m == 0)
      ans++;
    if (idx >= szn)
      continue;

    if (vis[idx][m])
      continue;
    vis[idx][m] = true;

    m = (m * 10) % mod;
    m = (m + n[idx] - '0') % mod;

    q.push(mp(idx + 1, m));
  }

  printf("%lld\\n", ans);
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

void solve_A() {
  int S, W;
  std::cin >> S >> W;
  // std::string result = (S <= W ? "unsafe" : "safe");
  std::cout << (S <= W ? "unsafe" : "safe") << std::endl;
}

void solve_B() {
  int A, B, C, D;
  std::cin >> A >> B >> C >> D;
  while (!(A <= 0 || C <= 0)) {
    C -= B;
    if (C <= 0)
      break;
    A -= D;
  }
  std::cout << (A <= 0 ? "No" : "Yes") << std::endl;
}

#include <algorithm>
#include <vector>

void solve_C() {
  int N;
  std::vector<std::string> v;
  std::cin >> N;
  for (int i = 0; i < N; i++) {
    std::string tmp;
    std::cin >> tmp;
    v.push_back(tmp);
  }
  std::sort(v.begin(), v.end());
  auto last = std::unique(v.begin(), v.end());
  v.erase(last, v.end());
  std::cout << v.size() << std::endl;
}

#include <set>

void solve_C2() {
  int N;
  std::set<std::string> st;
  std::cin >> N;
  for (int i = 0; i < N; i++) {
    std::string s;
    std::cin >> s;
    st.insert(s);
  }
  std::cout << st.size() << std::endl;
}

void solve_D() { // TLE
  std::string S;
  std::cin >> S;
  int N = S.length();
  int ans = 0;
  for (int i = 0; i < N - 3; i++) {
    for (int j = i + 3; j < N; j++) {
      int num = atoi(S.substr(i, j - i + 1).c_str());
      if (num % 2019 == 0) {
        ans++;
      }
    }
  }
  std::cout << ans << std::endl;
}

void solve_D2() {
  std::string s;
  std::cin >> s;
  int n = s.size();
  std::reverse(s.begin(), s.end());
  int x = 1, ans = 0, total = 0; // 累積和
  const int m = 2019;
  std::vector<int> cnt(m);
  for (int i = 0; i < n; i++) {
    cnt[total]++;
    total += atoi(s.substr(i, 1).c_str()) * x;
    total %= m;
    ans += cnt[total];
    x = x * 10 % m;
  }
  std::cout << ans << std::endl;
}

int main() {
  // solve_A();
  // solve_B();
  // solve_C();
  solve_D2();
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>

void solve_A() {
  int S, W;
  std::cin >> S >> W;
  // std::string result = (S <= W ? "unsafe" : "safe");
  std::cout << (S <= W ? "unsafe" : "safe") << std::endl;
}

void solve_B() {
  int A, B, C, D;
  std::cin >> A >> B >> C >> D;
  while (!(A <= 0 || C <= 0)) {
    C -= B;
    if (C <= 0)
      break;
    A -= D;
  }
  std::cout << (A <= 0 ? "No" : "Yes") << std::endl;
}

#include <algorithm>
#include <vector>

void solve_C() {
  int N;
  std::vector<std::string> v;
  std::cin >> N;
  for (int i = 0; i < N; i++) {
    std::string tmp;
    std::cin >> tmp;
    v.push_back(tmp);
  }
  std::sort(v.begin(), v.end());
  auto last = std::unique(v.begin(), v.end());
  v.erase(last, v.end());
  std::cout << v.size() << std::endl;
}

void solve_D() {
  std::string S;
  std::cin >> S;
  int N = S.length();
  int ans = 0;
  for (int i = 0; i < N - 3; i++) {
    for (int j = i + 3; j < N; j++) {
      int num = atoi(S.substr(i, j - i + 1).c_str());
      if (num % 2019 == 0) {
        ans++;
      }
    }
  }
  std::cout << ans << std::endl;
}

int main() {
  // solve_A();
  // solve_B();
  // solve_C();
  solve_D();
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long
#define PB push_back
#define POP pop_back()
#define PII pair<int, int>
#define FI first
#define SE second
#define ULL unsigned long long
#define endl '\\n'
#define c(x) cout << x << endl
using namespace std;
const int INF = 0x3f3f3f3f;
const double pi = acos(-1), eps = 1e-8;
map<int, int> num;
int main() {
  //	ios::sync_with_stdio(false);
  //	cin.tie(0);cout.tie(0);
  string s;
  cin >> s;
  int len = s.length();
  int ans = 0;
  int tmp = 0, p = 1;
  num[0]++;
  for (int i = len - 1; i >= 0; i--) {
    tmp = (tmp + (s[i] - '0') * p) % 2019;
    tmp = tmp % 2019;
    ans += num[tmp]++;
    p = p * 10 % 2019;
    // cout<<tmp<<endl;
  }
  c(ans);
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false), cin.tie(0)
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
string num[3000];
int cnt = 0;
string s;
inline bool check(int tmp) {
  while (tmp) {
    if (tmp % 10 == 0)
      return 0;
    tmp /= 10;
  }
  return 1;
}
inline string f(int x) {
  string s;
  while (x) {
    s += x % 10 + '0';
    x /= 10;
  }
  reverse(s.begin(), s.end());
  return s;
}
void init() {
  for (int i = 1; i <= 10000; i++) {
    if (check(i * 2019)) {
      num[cnt++] = f(i * 2019);
    }
  }
}
typedef pair<int, int> pl;
vector<pl> ind;
void solve(int x) {
  for (int i = 0; i < cnt; i++) {
    int len = num[i].length();
    int flag = 1;
    for (int j = 0; j < len; j++) {
      if (s[j + x] != num[i][j]) {
        flag = 0;
        break;
      }
    }
    if (flag) {
      // cout<<num[i]<<endl;
      ind.push_back(pl(x, x + len));
    }
  }
}
int bb[500050];
int ee[500050];
int main() {
  IO;
  init();

  cin >> s;
  int len = s.length();
  for (int i = 0; i < len; i++) {
    solve(i);
  }
  memset(bb, 0, sizeof(bb));
  memset(ee, 0, sizeof(ee));
  len = ind.size();
  pl tmp;
  for (int i = 0; i < len; i++) {
    int ta = ind[i].first, tb = ind[i].second;
    bb[ta]++;
    ee[tb]++;
  }
  int ans = ind.size();
  for (int i = 0; i < 50050; i++) {
    ans += bb[i] * ee[i];
  }
  cout << ans << endl;
  return 0;
}