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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int mod = 1000000007;
#define ll long long
string s;
int tens[220000] = {0};
int temp[220000] = {0};

void pre() {
  tens[0] = 1;
  rep(i, 210000) { tens[i + 1] = (tens[i] * 10) % 2019; }
}

void solve() {
  temp[0] = s[s.size() - 1] - '0';
  rep(i, s.size() - 1) {
    temp[i + 1] = (temp[i] + tens[i + 1] * (s[s.size() - i - 2] - '0')) % 2019;
  }
}

int main() {
  int ans = 0, divi[2020] = {0};
  cin >> s;
  pre();
  solve();
  rep(i, s.size()) { divi[temp[i]]++; }
  rep(i, 2020) {
    if (divi[i] > 1)
      ans += divi[i] * (divi[i] - 1) / 2;
  }
  cout << ans + divi[0] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int mod = 1000000007;
#define ll long long
string s;
int tens[220000] = {0};
int temp[220000] = {0};

void pre() {
  tens[0] = 1;
  rep(i, 210000) { tens[i + 1] = (tens[i] * 10) % 2019; }
}

int solve() {
  temp[0] = s[s.size() - 1] - '0';
  rep(i, s.size() - 1) {
    temp[i + 1] = (temp[i] + tens[i + 1] * (s[s.size() - i - 2] - '0')) % 2019;
  }
}

int main() {
  int ans = 0, divi[2020] = {0};
  cin >> s;
  pre();
  solve();
  rep(i, s.size()) { divi[temp[i]]++; }
  rep(i, 2020) {
    if (divi[i] > 1)
      ans += divi[i] * (divi[i] - 1) / 2;
  }
  cout << ans + divi[0] << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int A[200009] = {0};
int power(int x, unsigned int y, int p) {
  int res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
             // equal to p

  if (x == 0)
    return 0; // In case x is divisible by p;

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}
int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  int n = s.size();
  long long int r = 0, sum = 0;
  A[0]++;
  int so = 1;
  for (int i = 0; i < n; i++) {
    // s*=10;
    r += (s[i] - '0') * so;
    r %= 2019;
    A[r]++;
    so *= 10;
    so %= 2019;
    // cout<<r<<"\\n";
  }
  for (int i = 0; i < 2019; i++) {
    if (A[i] >= 2)
      // cout<<A[i]<<" ";
      sum += (A[i] * (long long int)(A[i] - 1)) / 2;
  }
  cout << sum;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define mp makepair
#define deb(x) cout << x << "\\n";
#define deB(x, y) cout << x << " " << y << "\\n";
#define Deb(x, y, z) cout << x << " " << y << " " << z << "\\n"
#define YES cout << "YES\\n";
#define Yes cout << "Yes\\n";
#define NO cout << "NO\\n";
#define No cout << "No\\n";
#define all(v) (v).begin(), (v).end()
int main() {
  string s;
  cin >> s;
  s = '!' + s;
  ll R, r, p, j, i = 1;
  set<ll> a[s.size() + 9];
  ll ans = 0;
  while (i < s.size()) {
    R = 0;

    r = 0;
    p = 0;
    j = i;
    while (j <= s.size()) {
      R *= 10;
      R %= 3;
      R += (ll)(s[j] - '0');
      R %= 3;
      r *= 10;
      r %= 673;
      r += (ll)(s[j] - '0');
      r %= 673;
      if (a[j].find(r) != a[j].end())
        break;
      a[j].insert(r);
      if (r == 0 && R == 0)
        p++;
      j++;
    }
    ans += p * (p + 1) / 2;
    i++;
  }
  deb(ans);
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

#define int long long
int A[223456];

signed main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  for (int i = 0, t = 1; i < s.size(); i++) {
    int a = s[i] - '0';
    A[i + 1] = (A[i] + a * t) % 2019;
    (t *= 10) %= 2019;
  }
  map<int, int> m;
  for (int i = 0; i <= s.size(); i++)
    m[A[i]]++;
  int ans = 0;
  for (auto p : m)
    ans += p.second * (p.second - 1) / 2;
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

#define int long long
int A[123456];

signed main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  for (int i = 0, t = 1; i < s.size(); i++) {
    int a = s[i] - '0';
    A[i + 1] = (A[i] + a * t) % 2019;
    (t *= 10) %= 2019;
  }
  map<int, int> m;
  for (int i = 0; i <= s.size(); i++)
    m[A[i]]++;
  int ans = 0;
  for (auto p : m)
    ans += p.second * (p.second - 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 N = S.length();
  vector<int> cnt_mod(2019);
  cnt_mod[0] = 1;
  int now = 0;
  int exp = 1;
  long long ans = 0;
  for (int i = N - 1; i >= 0; i--) {
    int d = S[i] - '0';
    now += exp * d;
    now %= 2019;
    exp = (exp * 10) % 2019;
    ans += cnt_mod[now];
    cnt_mod[now]++;
  }
  cout << ans << endl;

  return 0;
}
/* vim:set fdm=marker: */


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  string S;
  cin >> S;
  int N = S.length();
  vector<set<long long>> vs(N + 5, set<long long>());
  vs[0].insert(0);
  for (int i = 0; i < N; i++) {
    int d = S[i] - '0';
    for (auto p : vs[i]) {
      int m = (p * 10 + d) % 2019;
      vs[i + 1].insert(m);
    }
    vs[i + 1].insert(d);
  }
  long long ans = 0;
  for (int i = 1; i <= N; i++) {
    ans += vs[i].count(0);
  }
  cout << ans << endl;

  return 0;
}
/* vim:set fdm=marker: */
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define rep(i, n) FOR(i, 0, n)
using ll = long long;
using namespace std;
#define all(x) (x).begin(), (x).end()
using pii = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;

int kk[2019];
int pp[200003];
int fact[2019];
int ncr(int n, int r) { return fact[n] / fact[r] / fact[n - r]; }
signed main() {
  string s;
  cin >> s;
  int n = s.size();
  pp[0] = 1;
  rep(i, n + 1) { pp[i + 1] = (pp[i] * 10) % 2019; }
  fact[0] = 1;
  rep(i, 2020) { fact[i + 1] = fact[i] * (i + 1); }
  vi a(n + 1);
  rep(i, n) {
    a[i + 1] = a[i] * 10 + (s[i] - '0');
    a[i + 1] %= 2019;
  }
  rep(i, n + 1) {
    // cerr<<i<<' '<<a[i]<<' '<<pp[n-i]<<endl;
    kk[(a[i] * pp[n - i]) % 2019]++;
  }
  // cout << b.size();
  // return 0;
  ll ans = 0; // n+1;
  // rep(i,2019) if(kk[i]>=1) ans--;
  rep(i, 2019) {
    if (kk[i] >= 2)
      ans += (kk[i] * (kk[i] - 1) / 2);
  }
  cout << ans << endl;

  // cout<<n+1 - b.size()<<endl;
  // rep(i,n+1) cout<<a[i]<<' ';
  // ll ans = 0;
  // rep(i, n+1) {
  //     ll p = 10;
  //     FOR(j, i+1, n+1) {
  //         if(ll(a[j] - a[i]* p) %2019==0) ans++;
  //         p *= 10;
  //     }
  // }
  // cout<<ans<<endl;
  return 0;
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define rep(i, n) FOR(i, 0, n)
using ll = long long;
using namespace std;
#define all(x) (x).begin(), (x).end()
using pii = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;

int kk[2019];
int fact[2019];
int ncr(int n, int r) { return fact[n] / fact[r] / fact[n - r]; }
signed main() {
  string s;
  cin >> s;
  int n = s.size();
  vi pp(n + 1);
  pp[0] = 1;
  rep(i, n + 1) { pp[i + 1] = (pp[i] * 10) % 2019; }
  fact[0] = 1;
  rep(i, 2020) { fact[i + 1] = fact[i] * (i + 1); }
  vi a(n + 1);
  rep(i, n) {
    a[i + 1] = a[i] * 10 + (s[i] - '0');
    a[i + 1] %= 2019;
  }
  rep(i, n + 1) {
    // cerr<<i<<' '<<a[i]<<' '<<pp[n-i]<<endl;
    kk[(a[i] * pp[n - i]) % 2019]++;
  }
  // cout << b.size();
  // return 0;
  ll ans = n + 1;
  rep(i, 2019) if (kk[i] >= 1) ans--;
  rep(i, 2019) {
    if (kk[i] >= 2)
      ans += ncr(kk[i], 2) - 1;
  }
  cout << ans << endl;

  // cout<<n+1 - b.size()<<endl;
  // rep(i,n+1) cout<<a[i]<<' ';
  // ll ans = 0;
  // rep(i, n+1) {
  //     ll p = 10;
  //     FOR(j, i+1, n+1) {
  //         if(ll(a[j] - a[i]* p) %2019==0) ans++;
  //         p *= 10;
  //     }
  // }
  // cout<<ans<<endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  string S;
  cin >> S;
  reverse(S.begin(), S.end());
  int N = S.length();
  int a[N];
  a[0] = int(S[0] - '0');
  int k = 10;
  rep(i, N - 1) {
    a[i + 1] = (a[i] + int(S[i + 1] - '0') * k) % 2019;
    k = (k * 10) % 2019;
  }
  int ans = 0;
  int w[2019] = {};
  rep(i, N) { w[a[i]]++; }
  rep(i, 2019) { ans += (w[i] * (w[i] - 1)) / 2; }
  rep(i, N) if (a[i] == 0) ans++;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  string S;
  cin >> S;
  reverse(S.begin(), S.end());
  int N = S.length();
  int a[N];
  a[0] = int(S[0] - '0');
  int k = 10;
  rep(i, N - 1) {
    a[i + 1] = (a[i] + int(S[i + 1] - '0') * k) % 2019;
    k = (k * 10) % 2019;
  }
  int ans = 0;
  rep(i, N) rep(j, N) {
    if (i == j)
      break;
    if (a[i] - a[j] == 0) {
      ans++;
    }
  }
  rep(i, N) if (a[i] == 0) ans++;
  // rep(i,N) cout << a[i] << endl;
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T> using vec = std::vector<T>;

int main() {
  string S;
  cin >> S;
  int N = S.size();
  vec<int> dp0(2019, 0);
  // vec<int> dp1(2019, 0);

  dp0[0] = 1;
  ll ans = 0;
  for (int i = 1; i <= N; ++i) {
    vec<int> dp1(2019, 0);
    int s = S[i - 1] - '0';
    dp1[s] += 1;
    rep(j, 2019) {
      if (dp0[j] >= 1 && (i - 1 != 0)) {
        dp1[(j * 10 + s) % 2019] += dp0[j];
      }
    }
    ans += dp1[0];
    copy(all(dp1), dp0.begin());
  }
  cout << ans << endl;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T> using vec = std::vector<T>;

int main() {
  string S;
  cin >> S;
  int N = S.size();
  vec<vec<int>> dp(N + 1, vec<int>(2019, 0));

  dp[0][0] = 1;
  ll ans = 0;
  for (int i = 1; i <= N; ++i) {
    int s = S[i - 1] - '0';
    dp[i][s] += 1;
    rep(j, 2019) {
      if (dp[i - 1][j] >= 1 && (i - 1 != 0)) {
        dp[i][(j * 10 + s) % 2019] += dp[i - 1][j];
      }
    }
  }
  for (int i = 1; i <= N; ++i)
    ans += dp[i][0];
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  string S;
  cin >> S;
  map<long long, long long> mp;
  mp[0] = 1;
  long long now = 0, ten = 1;
  for (int i = S.size() - 1; i >= 0; i--) {
    now = (now + (S[i] - '0') * ten) % 2019;
    ten *= 10;
    ten %= 2019;
    mp[now]++;
  }
  long long ans = 0;
  for (auto m : mp) {
    ans += m.second * (m.second - 1) / 2;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

int main() {
  string S;
  cin >> S;
  map<long long, long long> mp;
  mp[0] = 1;

  for (int i = 1; i <= S.size(); i++) {
    string s = S.substr(S.size() - i, i);
    // cout << s << endl;
    long long n = stoll(s);
    mp[n % 2019]++;
  }
  long long ans = 0;
  for (auto m : mp) {
    ans += m.second * (m.second - 1) / 2;
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;
const long long INF = 1LL << 60;

typedef pair<ll, ll> Pair;

int main() {

  string S;

  cin >> S;
  /*
  for(ll i = 0;i < S.size();++i){

      string subS = "";

      for(ll j = i;j < S.size();++j){

          subS = subS + S[j];
          ll num = atoi(subS.c_str());

          if(num%2019 == 0){

              ++ans;

          }

      }

  }

  */

  ll x = 1;
  ll tot = 0;
  vector<ll> cnt(2019);
  ll ans = 0;
  ll mod = 2019;

  cnt[0] = 1;

  reverse(S.begin(), S.end());

  for (ll i = 0; i < S.size(); ++i) {

    ll num = S[i] - '0';
    tot = (tot + (num * x)) % mod;
    cnt[tot] = cnt[tot] + 1;
    x = (10 * x) % mod;
  }

  for (ll n : cnt)
    ans += (n * (n - 1)) / 2;

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {

  string S;
  ll ans = 0;

  cin >> S;

  for (ll i = 0; i < S.size(); ++i) {

    string subS = "";

    for (ll j = i; j < S.size(); ++j) {

      subS = subS + S[j];

      ll num = atoi(subS.c_str());

      if (num % 2019 == 0)
        ++ans;
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 100000000;
#define rep1(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, k, n) for (ll i = k; i < (n); i++)

// 累積和的なものを考えると良いらしい
int main() {
  string s;
  cin >> s;
  ll n = s.size();
  vector<ll> a(2019, 0);
  ll fac = 1;
  ll cur = 0;
  a[cur]++;
  rep1(i, n) {
    ll add = s[n - 1 - i] - '0';
    cur = (cur + fac * add) % 2019;
    fac = (fac * 10) % 2019;
    a[cur]++;
  }
  ll ans = 0;
  rep1(i, a.size()) { ans += a[i] * (a[i] - 1) / 2; }
  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 100000000;
#define rep1(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, k, n) for (ll i = k; i < (n); i++)

// 累積和的なものを考えると良いらしい
int main() {
  string s;
  cin >> s;
  ll n = s.size();
  vector<ll> a(2019, 0);
  ll fac = 1;
  ll cur = 0;
  a[cur]++;
  rep1(i, n) {
    ll add = s[n - 1 - i] - '0';
    cur = (cur + fac * add) % 2019;
    fac = (fac * 10) % 2019;
    a[cur]++;
  }
  ll ans = 0;
  rep1(i, n) { ans += a[i] * (a[i] - 1) / 2; }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int pow_mod(int a, int n, int mod) {
  int res = 1;
  while (n > 0) {
    if (n & 1)
      res *= a;
    a = a * a;
    a %= mod;
    n >>= 1;
    res %= mod;
  }
  return res;
}

int main() {
  string S;
  cin >> S;
  int N = S.size();
  vector<int> mod(N + 1);
  mod[N] = 0;
  for (int s = N - 1; s >= 0; s--) {
    int a = S[s] - '0';
    mod[s] = a * pow_mod(10, (N - 1) - s, 2019) + mod[s + 1];
    mod[s] %= 2019;
  }
  map<int, int> cnt;
  for (auto m : mod) {
    cnt[m]++;
  }
  int ans = 0;
  for (auto c : cnt) {
    ans += c.second * (c.second - 1) / 2;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

ll pow_mod(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res *= a;
    a = a * a;
    a %= mod;
    n >>= 1;
    res %= mod;
  }
  return res;
}

int main() {
  string S;
  cin >> S;
  int N = S.size();
  vector<vector<ll>> table(9, vector<ll>(200000));
  for (ll i = 0; i < 9; i++) {
    for (ll j = 0; j < 200000; j++) {
      table[i][j] = (i + 1) * pow_mod(10, j, 2019);
      table[i][j] %= 2019;
    }
  }
  ll ans = 0;
  for (ll i = N - 1; i >= 0; i--) {
    int c = 0;
    int sum = 0;
    for (ll j = i; j >= 0; j--) {
      int s = S[j] - '0';
      sum += table[s - 1][c];
      sum %= 2019;
      if (sum == 0) {
        ans++;
      }
      c++;
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

ll cnt[2019];

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

  int now = 0;
  ll ans = 0, p = 1;
  cnt[0] = 1;

  for (int i = s.size() - 1; i >= 0; i--) {
    now = (now + (s[i] - '0') * p) % 2019;
    ans += cnt[now]++;
    p = p * 10 % 2019;
  }

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


Error: 0

Fixed submission:

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

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

  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 4; j <= n - i; j++) {
      ll t = stoll(s.substr(i, j));
      if (t % 2019 == 0)
        ans++;
    }
  }

  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;

  long long dp[2][2019];
  for (int i = 0; i < 2019; i++) {
    dp[0][i] = 0;
    dp[1][i] = 0;
  }

  long long count = 0;
  for (long long i = 0; i < s.length(); i++) {
    int d = s[i] - '0';
    for (int j = 0; j < 2019; j++)
      dp[0][j] = dp[1][j];
    for (int j = 0; j < 2019; j++)
      dp[1][(10 * j + d) % 2019] = dp[0][j];
    if (d != 0)
      dp[1][d]++;

    count += dp[1][0];
  }

  cout << count << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

  int count = 0;
  for (int i = 0; i < s.length(); i++) {
    int num = (s[i] - '0');
    for (int j = i + 1; j < s.length(); j++) {
      num = 10 * num + (s[j] - '0');
      if (num % 2019 == 0)
        count++;
      num %= 2019;
    }
  }

  cout << count << endl;

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

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

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

  reverse(S.begin(), S.end());
  int size = S.size();
  int cnt[2020] = {0};
  int m = 2019;
  int amari = 0;
  long result = 0;
  int x = 1;

  cnt[0]++;
  for (int i = 0; i < size; i++) {
    amari = amari + (S[i] - '0') * x;
    amari %= m;
    result += cnt[amari];
    cnt[amari]++;
    x = x * 10 % m;
  }

  cout << result << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

  int size = S.size();

  long cnt = 0;
  for (int i = 0; i < (size - 3); i++) {
    for (int j = 4; j <= (size - i); j++) {
      string a = S.substr(i, j);
      int num = atoi(a.c_str());
      if (0 == (num % 2019))
        cnt++;
    }
  }

  cout << cnt << endl;

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

/*
    STARK_BOY
*/
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repe(i, a, b) for (ll i = a; i <= b; i++)
#define bac(i, a, b) for (ll i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef long long ll;
typedef vector<ll> vi;
typedef vector<pair<ll, ll>> vpii;
typedef pair<ll, ll> pii;
#define all(c) c.begin(), c.end()
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define PI 3.14159265358979323846 /* pi */
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
#define N 1000005
// Variables Start
ll inf = 1e18, MOD = 1e9 + 7;
// Variables End

void solve() {
  string s;
  cin >> s;
  int n = s.length();
  reverse(all(s));
  vector<ll> val(2019, 0);
  vector<int> po(n, 0);
  po[0] = 1;
  for (int i = 1; i < n; i++)
    po[i] = (10 * po[i - 1]) % 2019;
  val[0]++;
  int mod = 0;
  for (int i = 0; i < n; i++) {
    int new_mod = (mod + po[i] * (s[i] - '0')) % 2019;
    val[new_mod]++;
    mod = new_mod;
  }
  ll ans = 0;
  for (int i = 0; i < 2019; i++) {
    // cout<<val[i]<<" ";
    ans += val[i] * (val[i] - 1) / 2;
  }

  cout << ans << endl;
}
int main() {
  ios int ti = 1;
  // cin>>ti;
  for (int i = 1; i <= ti; i++) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

/*
    STARK_BOY
*/
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repe(i, a, b) for (ll i = a; i <= b; i++)
#define bac(i, a, b) for (ll i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef long long ll;
typedef vector<ll> vi;
typedef vector<pair<ll, ll>> vpii;
typedef pair<ll, ll> pii;
#define all(c) c.begin(), c.end()
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define PI 3.14159265358979323846 /* pi */
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
#define N 1000005
// Variables Start
ll inf = 1e18, MOD = 1e9 + 7;
// Variables End

void solve() {
  string s;
  cin >> s;
  int n = s.length();
  vector<vector<ll>> dp(n + 1, vector<ll>(2019, 0));
  // dp[0][0]=1;

  for (int i = 0; i < n; i++) {
    dp[i + 1][s[i] - '0']++;
    for (int mod = 0; mod < 2019; mod++) {
      // cout<<i<<" "<<mod<<endl;
      int new_mod = (10 * mod + (s[i] - '0')) % 2019;
      dp[i + 1][new_mod] += dp[i][mod];
    }
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++)
    ans += dp[i][0];
  cout << ans << endl;
}
int main() {
  ios int ti = 1;
  // cin>>ti;
  for (int i = 1; i <= ti; i++) {
    solve();
  }
  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 < (int)(n); i++)

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

const ll mod =
    1000000007; // 出力は (ans % mod + mod) % mod  (負の剰余を正にする)
const int inf = 1e9;
const long long INF = 1LL << 60; // INF = 1152921504606846976

ll pow_k(ll x, ll n) { // xのn乗 (mod m)
  ll res = 1;
  if (n > 0) {
    res = pow(x, n / 2);
    if (n % 2 == 0) {
      res = (res * res);
    } else {
      res = (((res * res)) * x);
    }
  }
  return res;
}

int main() {
  string s;
  cin >> s;
  ll n = s.size();
  ll a[n];
  reverse(s.begin(), s.end());
  int tmp = 10;
  a[0] = s[0] - '0';
  rep(i, n - 1) {
    a[i + 1] = (s[i + 1] - '0') * tmp + a[i];
    a[i + 1] %= 2019;
    tmp *= 10;
    tmp %= 2019;
  }
  // rep(i,n) cout << a[i] << ' ';
  map<ll, ll> m;
  m[0] = 1;
  rep(i, n) { m[a[i]]++; }
  ll ans = 0;
  rep(i, m.size()) {
    int k = m[i];
    ans += (k * (k - 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 < (int)(n); i++)

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

const ll mod =
    1000000007; // 出力は (ans % mod + mod) % mod  (負の剰余を正にする)
const int inf = 1e9;
const long long INF = 1LL << 60; // INF = 1152921504606846976

ll pow_k(ll x, ll n) { // xのn乗 (mod m)
  ll res = 1;
  if (n > 0) {
    res = pow(x, n / 2);
    if (n % 2 == 0) {
      res = (res * res);
    } else {
      res = (((res * res)) * x);
    }
  }
  return res;
}

int main() {
  string s;
  cin >> s;
  ll n = s.size();
  ll a[n];
  reverse(s.begin(), s.end());
  a[0] = s[0] - '0';
  for (ll i = 0; i < n - 1; i++) {
    a[i + 1] = ((ll)(s[i + 1] - '0') * pow_k(10, i + 1) + (a[i])) % 2019;
    // a[i+1] = ((a[i]+ (s[i+1]-'0'))*pow(10,i)) % 2019);
  }
  map<ll, ll> mp;
  mp[0] = 1;
  rep(i, n) { mp[a[i]]++; }
  ll ans = 0;
  rep(i, mp.size()) {
    if (mp[i] > 1) {
      ll tmp = mp[i];
      ans += (tmp * (tmp - 1)) / 2;
    }
  }
  cout << ans << endl;
}