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

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

#define ll long long
#define vi vector<int>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
// pll pp; -> (pp.first,pp.second)
// vector<vl> pp(n,vl(2)); sort(pp.begin(),pp.end(),[](vl a,vl b){return
// a[0]<b[0];});

#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define RREP(i, n) for (ll(i) = (n)-1; (i) >= 0; (i)--)
#define FOR(i, a, b) for (ll(i) = (a); (i) < (b); (i)++)
#define FORR(i, a, b) for (ll(i) = (b)-1; (i) >= (a); (i)--)
#define DEBUG(x) cout << #x << ": " << x << endl
#define CHMAX(a, b) a = max((a), (b))
#define CHMIN(a, b) a = min((a), (b))
vl modinv;
void fillModInv(ll n, ll mod) {
  modinv = vl(n + 1);
  modinv[1] = 1;
  FOR(i, 2, n + 1) { modinv[i] = modinv[mod % i] * (mod - mod / i) % mod; }
}
/*}}}*/
int main() {
  string s;
  cin >> s;

  ll n = s.length();
  // cout << s << endl;
  vector<vl> md(2, vl(2019, 0));
  ll ans = 0;
  FOR(i, 1, n + 1) {
    char c = s[i - 1];
    ll num = (ll)(c - '0');
    REP(j, 2019) { md[i & 1][(j * 10 + num) % 2019] += md[(i - 1) & 1][j]; }
    REP(j, 2019) { md[(i - 1) & 1][j] = 0; }
    md[i & 1][num] += 1;
    ans += md[i & 1][0];
  }

  // cout << fixed << setprecision(10);
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

#define ll long long
#define vi vector<int>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
// pll pp; -> (pp.first,pp.second)
// vector<vl> pp(n,vl(2)); sort(pp.begin(),pp.end(),[](vl a,vl b){return
// a[0]<b[0];});

#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define RREP(i, n) for (ll(i) = (n)-1; (i) >= 0; (i)--)
#define FOR(i, a, b) for (ll(i) = (a); (i) < (b); (i)++)
#define FORR(i, a, b) for (ll(i) = (b)-1; (i) >= (a); (i)--)
#define DEBUG(x) cout << #x << ": " << x << endl
#define CHMAX(a, b) a = max((a), (b))
#define CHMIN(a, b) a = min((a), (b))
vl modinv;
void fillModInv(ll n, ll mod) {
  modinv = vl(n + 1);
  modinv[1] = 1;
  FOR(i, 2, n + 1) { modinv[i] = modinv[mod % i] * (mod - mod / i) % mod; }
}
/*}}}*/
int main() {
  string s;
  cin >> s;

  ll n = s.length();
  // cout << s << endl;
  vector<vl> md(n + 1, vl(2019, 0));
  ll ans = 0;
  FOR(i, 1, n + 1) {
    char c = s[i - 1];
    ll num = (ll)(c - '0');
    REP(j, 2019) {
      // if(md[i-1][j]>0){
      //   cout << (j*10+num)%2019 << ": +=" << md[i-1][j] << endl;
      // }
      md[i][(j * 10 + num) % 2019] += md[i - 1][j];
    }
    md[i][num] += 1;
    ans += md[i][0];
  }

  // cout << fixed << setprecision(10);
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

ll ten(ll x) {
  if (x == 0) {
    return 1;
  } else {
    x %= 672;
    ll res = 1;
    for (int i = 0; i < x; i++) {
      res *= 10;
      res %= 2019;
    }
    return res;
  }
}

int main() {
  string S;
  cin >> S;
  ll lon = (ll)(S.length());
  ll Am[2019] = {0};
  Am[0]++;
  ll back = 0;
  ll ans = 0;
  for (ll i = lon - 1; i >= 0; i--) {
    ll X = S[i] - 48;
    ll ag = ((X * ten(lon - 1 - i)) % 2019 + back) % 2019;
    Am[ag]++;
    back = ag;
  }
  for (int i = 0; i < 2019; i++) {
    if (Am[i] >= 2) {
      ans += (Am[i] * (Am[i] - 1)) / 2;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

ll ten(ll x) {
  if (x == 0) {
    return 1;
  } else {
    ll res = 1;
    for (int i = 0; i < x; i++) {
      res *= 10;
      res %= 2019;
    }
    return res;
  }
}

int main() {
  string S;
  cin >> S;
  ll lon = (ll)(S.length());
  ll Am[2019] = {0};
  Am[0]++;
  ll back = 0;
  ll ans = 0;
  for (ll i = lon - 1; i >= 0; i--) {
    ll X = S[i] - 48;
    ll ag = ((X * ten(lon - 1 - i)) % 2019 + back) % 2019;
    Am[ag]++;
    back = ag;
  }
  for (int i = 0; i < 2019; i++) {
    if (Am[i] >= 2) {
      ans += (Am[i] * (Am[i] - 1)) / 2;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define mod 1e+7
#define int long long
#define garou                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
using namespace std;
int dp[2019];
signed main() {
  garou;
  string s;
  cin >> s;
  int n = s.length();
  dp[0] = 1;
  int c = 1;
  int l = 0;
  for (int i = n - 1; i >= 0; i--) {
    l += ((s[i] - '0') * c);
    l %= 2019;
    c = (c * 10) % 2019;
    dp[l]++;
  }
  int ans = 0;
  for (int i = 0; i < 2019; i++) {
    ans += (dp[i] * (dp[i] - 1)) / 2;
  }
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define mod 1e+7
#define int long long
#define garou                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
using namespace std;
int dp[2019];
signed main() {
  garou;
  string s;
  cin >> s;
  int n = s.length();
  dp[0] = 1;
  int c = 1;
  int l = 0;
  for (int i = n - 1; i >= 0; i--) {
    l += ((s[i] - '0') * c);
    l %= 2019;
    c *= 10;
    dp[l]++;
  }
  int ans = 0;
  for (int i = 0; i < 2019; i++) {
    ans += (dp[i] * (dp[i] - 1)) / 2;
  }
  cout << ans;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define repr(i, from, to) for (int(i) = (from); (i) < (to); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
// const bool debug=true;
const bool debug = false;
#define DEBUG if (debug == true)
#define all(x) begin((x)), end((x))
#define vprint(x)                                                              \\
  for (auto a : (x))                                                           \\
    cout << x << endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  ll n = s.size();
  vector<vector<ll>> dp(2, vector<ll>(2019, 0));
  ll res = 0;
  rep(i, n) {
    ll pls = s[i] - '0';
    rep(j, 2019) { dp[(i + 1) % 2][(j * 10 + pls) % 2019] = dp[i % 2][j]; }
    dp[(i + 1) % 2][pls]++;
    res += dp[(i + 1) % 2][0];
  }
  // repr(i,1,n+1) {
  //     res += dp[i][0];
  // }
  cout << res << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define repr(i, from, to) for (int(i) = (from); (i) < (to); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
// const bool debug=true;
const bool debug = false;
#define DEBUG if (debug == true)
#define all(x) begin((x)), end((x))
#define vprint(x)                                                              \\
  for (auto a : (x))                                                           \\
    cout << x << endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  ll n = s.size();
  vector<vector<ll>> dp(n + 1, vector<ll>(2019, 0));
  rep(i, n) {
    ll pls = s[i] - '0';
    dp[i + 1][pls] += 1;
    rep(j, 2019) { dp[i + 1][(j * 10 + pls) % 2019] += dp[i][j]; }
  }
  ll res = 0;
  repr(i, 1, n + 1) { res += dp[i][0]; }
  cout << res << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fi first
#define se second
using namespace std;
const ll INF = 1LL << 60;
const int inf = 1 << 30;
inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
const int maxn = 2e5 + 5;
char s[maxn];
unsigned short f[maxn][2020];
int main() {
  fastio();
  cin >> s + 1;
  int n = strlen(s + 1);
  for (int i = 1; i <= n; i++)
    s[i] = s[i] - '0';
  for (int i = 1; i <= n; i++)
    f[i][s[i]] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2019; j++) {
      f[i + 1][(10 * j + s[i + 1]) % 2019] += f[i][j];
    }
  }
  ll ans = 0;
  for (int i = 4; i <= n; i++)
    ans += f[i][0];
  cout << ans << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fi first
#define se second
using namespace std;
const ll INF = 1LL << 60;
const int inf = 1 << 30;
inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
const int maxn = 2e5 + 5;
char s[maxn];
int f[maxn][2020];
int main() {
  fastio();
  cin >> s + 1;
  int n = strlen(s + 1);
  for (int i = 1; i <= n; i++)
    s[i] = s[i] - '0';
  for (int i = 1; i <= n; i++)
    f[i][s[i]] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2019; j++) {
      f[i + 1][(10 * j + s[i + 1]) % 2019] += f[i][j];
    }
  }
  ll ans = 0;
  for (int i = 4; i <= n; i++)
    ans += f[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;
  long long result = 0;
  long long k = 0;
  cin >> S;
  long long N = S.length();
  vector<long long> A(N);
  vector<string> T(N);
  for (long long a = 0; a < N; a++)
    T.at(a) = S[a];

  A.at(0) = stoi(T.at(N - 1));
  k = 10;
  for (long long a = 1; a < N; a++) {

    A.at(a) = (A.at(a - 1) + stoi(T.at(N - a - 1)) * k) % 2019;
    if (A.at(a) == 0)
      result++;
    k = (k * 10) % 2019;
  }
  sort(A.begin(), A.end());
  k = 0;
  for (long long a = 1; a < N; a++) {

    if (A.at(a - 1) == A.at(a)) {
      k++;
      result += k;

    } else {
      k = 0;
    }
  }

  cout << result << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  string S;
  int result = 0;
  int k = 0;
  cin >> S;
  int N = S.length();
  vector<int> A(N);
  vector<string> T(N);
  for (int a = 0; a < N; a++)
    T.at(a) = S[a];

  A.at(0) = stoi(T.at(N - 1));
  k = 10;
  for (int a = 1; a < N; a++) {

    A.at(a) = (A.at(a - 1) + stoi(T.at(N - a - 1)) * k) % 2019;
    if (A.at(a) == 0)
      result++;
    k = (k * 10) % 2019;
  }
  for (int a = 0; a < N; a++) {
    for (int b = 1; b <= a; b++) {
      if ((A.at(b - 1) - A.at(a)) % 2019 == 0)
        result++;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const long long INF = 1LL << 60;
const int mod = 1e9 + 7;

const int m = 2019;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  reverse(s.begin(), s.end());
  int x = 1, tot = 0;
  vector<int> cnt(m); // 累積和のあまりがmになるものの数を保存してる
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    cnt[tot]++;
    tot += (s[i] - '0') * x; // 一桁ずつ数字をとり、桁数分10倍して足してる
    tot %= m;                // 累積和のあまりをとってる
    ans += cnt[tot];
    x = x * 10 % m;
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const long long INF = 1LL << 60;
const int mod = 1e9 + 7;

int main() {
  string s;
  cin >> s;
  const ll base = 2019;
  ll ans = 0;

  for (ll i = 4; i < (ll)((int)s.size() + 1); i++) {
    for (ll j = i - 1; j < (ll)s.size(); j++) {
      string str;
      ll t;
      str = s.substr(j - i + 1, i);
      t = stoll(str);

      if (t % base == 0) {
        ans++;
      }
    }
  }

  cout << ans << endl;
  return 0;
}
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++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
typedef long long ll;
typedef pair<int, int> pii;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  vector<ll> vec(n);
  vector<ll> ls(2020, 0);
  vec[n - 1] = s[n - 1] - '0';
  ls[s[n - 1] - '0'] += 1;
  ll t = 1;
  for (int i = n - 2; i >= 0; i--) {
    t = t * 10 % 2019;
    vec[i] = (t * (s[i] - '0') + vec[i + 1]) % 2019;
    ls[(t * (s[i] - '0') + vec[i + 1]) % 2019] += 1;
  }
  ll ans = 0;
  ls[0]++;
  rep(i, 2019) { ans += ls[i] * (ls[i] - 1) / 2; }
  cout << ans << 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++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
typedef long long ll;
typedef pair<int, int> pii;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  vector<ll> vec(n);
  vector<ll> ls(2020, 0);
  vec[n - 1] = s[n - 1] - '0';
  ls[n - 1] += 1;
  ll t = 1;
  for (int i = n - 2; i >= 0; i--) {
    t = t * 10 % 2019;
    vec[i] = (t * (s[i] - '0') + vec[i + 1]) % 2019;
    ls[(t * (s[i] - '0') + vec[i + 1]) % 2019] += 1;
  }
  ll ans = 0;
  ls[0]++;
  rep(i, 2019) { ans += ls[i] * (ls[i] - 1) / 2; }
  cout << ans << endl;

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

#include <bits/stdc++.h>

using namespace std;

signed main() {
  string s;
  cin >> s;
  int m[2019];
  memset(m, 0, sizeof m);
  int c = s[0] - '0';
  m[c]++;
  int res = 0;
  for (int i = 1; i < s.size(); i++) {
    int x = s[i] - '0';
    int t[2019];
    memset(t, 0, sizeof t);
    t[x] = 1;
    for (int z = 0; z < 2019; z++) {
      if (m[z] > 0) {
        int cc = (z * 10 + x) % 2019;
        if (cc % 2019 == 0)
          res += m[z];
        t[cc % 2019] += m[z];
      }
    }
    memset(m, 0, sizeof m);
    for (int i = 0; i < 2019; i++)
      m[i] = t[i];
  }
  cout << res;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

signed main() {
  string s;
  cin >> s;
  map<int, int> m;
  int c = s[0] - '0';
  m[c]++;
  int res = 0;
  for (int i = 1; i < s.size(); i++) {
    int x = s[i] - '0';
    map<int, int> t;
    t[x] = 1;
    for (int z = 0; z < 2019; z++) {
      if (m[z]) {
        int cc = (z * 10 + x) % 2019;
        if (cc % 2019 == 0)
          res += m[z];
        t[cc % 2019] += m[z];
      }
    }
    m.clear();
    for (auto it : t)
      m[it.first] = it.second;
  }
  cout << res;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

// const int MAX = 200000;
// int a[MAX];
// int b[2019] = {0};

int modpow(int x, int n, int p) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return modpow(x * x % p, n / 2, p);
  else
    return modpow(x * x % p, n / 2, p) * x % p;
}

// int assignMod(int n, std::string s, int k) {
//   if (k == n) {
//     b[a[k]]++;
//     return a[k] = 0;
//   }
//   if (a[k] != -1) {
//     b[a[k]]++;
//     return a[k];
//   }
//   int p = modpow(10, n - k - 1, 2019);
//   a[k] = (assignMod(n, s, k + 1) + ((p * (s[k] - '0')) % 2019)) % 2019;
//   b[a[k]]++;
//   return a[k];
// }

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

  int n = s.length();
  int a[n + 1], b[2019] = {0};
  // rep(i, n) a[i] = -1;
  // assignMod(n, s, 0);

  a[n] = 0;
  b[0]++;
  for (int i = n - 1; i >= 0; --i) {
    // std::cout << "i: " << i << '\\n';

    a[i] =
        (a[i + 1] + modpow(10, n - i - 1, 2019) * (s[i] - '0') % 2019) % 2019;
    b[a[i]]++;
  }

  // rep(i, n + 1) { std::cout << a[i] << ' '; }
  // std::cout << '\\n';

  // rep(i, 2019) {
  //   if (b[i] >= 1) printf("b[%d] = %d\\n", i, b[i]);
  // }

  int sum = 0;
  rep(i, 2019) {
    int m = b[i];
    sum += m * (m - 1) / 2;
  }

  std::cout << sum << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

const int MAX = 200000;
int a[MAX];
int b[2019] = {0};

int modpow(int x, int n, int p) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return modpow(x * x % p, n / 2, p);
  else
    return modpow(x * x % p, n / 2, p) * x % p;
}

int assignMod(int n, std::string s, int k) {
  if (k == n) {
    b[a[k]]++;
    return a[k] = 0;
  }
  if (a[k] != -1) {
    b[a[k]]++;
    return a[k];
  }
  int p = modpow(10, n - k - 1, 2019);
  a[k] = (assignMod(n, s, k + 1) + ((p * (s[k] - '0')) % 2019)) % 2019;
  b[a[k]]++;
  return a[k];
}

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

  int n = s.length();
  rep(i, n) a[i] = -1;
  assignMod(n, s, 0);

  int sum = 0;
  rep(i, 2019) {
    int m = b[i];
    sum += m * (m - 1) / 2;
  }

  std::cout << sum << '\\n';

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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;

long long int calcModPow(long long int a, long long int n, long long int m) {
  if (n == 0) {
    return 1 % m;
  }
  if (n == 1) {
    return a % m;
  }

  if (n % 2 == 1) {
    return (a * calcModPow(a, n - 1, m)) % m;
  }

  long long int t = calcModPow(a, n / 2, m);
  return (t * t) % m;
}

int main() {
  string S;
  int mod = 0, res = 0;
  vector<int> modEnd, n;

  cin >> S;

  for (int i = 0; i < S.length(); i++) {
    int tmp = S[S.length() - 1 - i] - '0';
    mod = (mod + calcModPow(10, i, 2019) * tmp % 2019) % 2019;

    modEnd.push_back(mod);
  }

  for (int i = 0; i < 2020; i++) {
    n.push_back(0);
  }

  for (int i = 0; i < S.length(); i++) {
    n[modEnd[i]]++;
  }

  res += (n[0] + 1) * n[0] / 2;

  for (int i = 1; i < 2020; i++) {
    res += n[i] * (n[i] - 1) / 2;
  }

  cout << res;

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;

string S;
int n[2020];
int mod;
int tmp = 0;

int calc(int start) {
  long long res = 0;
  mod = (S[start] - '0') % 2019;
  for (int j = start + 1; j < start + 7; j++) {
    mod = (S[j] - '0' + n[mod]) % 2019;
    if (mod == 0) {
      tmp++;
      res += calc(j + 1);
    }
  }
  return res;
}

long long int kaijou(int n) {
  if (n == 1) {
    return 1;
  }
  return n * kaijou(n - 1);
}

int main() {

  long long count = 0;

  cin >> S;
  for (int i = 0; i < 2020; i++) {
    n[i] = i * 10 % 2019;
  }

  for (int i = 0; i < S.length(); i++) {
    calc(i);
    if (tmp != 0) {
      count += kaijou(tmp);
      tmp = 0;
    }
  }

  cout << count;

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

// verma_ankit484

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

#define int long long
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define pb push_back
const int mod = (int)1e9 + 7;

using namespace std;
// using namespace __gnu_pbds;

// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>

int32_t main() {
  IOS
#ifdef AV
      freopen("input.txt", "r", stdin);
#endif
  string s;
  cin >> s;
  map<int, int> mp;
  mp[0]++;
  int c = 0;
  int n = s.length();
  int p = 1, pr = 0;
  for (int i = n - 1; i >= 0; i--) {
    int pp = p * (s[i] - '0') + pr;
    pp %= 2019;
    mp[pp]++;
    pr = pp;
    p *= 10;
    p %= 2019;
  }
  int ans = 0;
  for (auto i : mp) {
    c = i.second;
    c = c * (c - 1) / 2;
    ans += c;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

// verma_ankit484

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

// #define int long long
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define pb push_back
const int mod = (int)1e9 + 7;

using namespace std;
// using namespace __gnu_pbds;

// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>

int dp[200001][2020];

int n;

string s;

int rec(int idx, int modd) {
  if (idx >= n)
    return 0;
  modd *= 10;
  modd += (s[idx] - '0');
  modd %= 2019;
  int &ans = dp[idx][modd];
  if (ans != -1)
    return ans;
  ans = (modd == 0 ? 1 : 0);
  ans += rec(idx + 1, modd);
  return ans;
}

int32_t main() {
  IOS
#ifdef AV
      freopen("input.txt", "r", stdin);
#endif
  cin >> s;
  n = (int)s.length();
  memset(dp, -1, sizeof(dp));
  int ans = 0;
  for (int i = 0; i < n - 3; i++) {
    ans += rec(i, 0);
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
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 (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
int main() {
  string s;
  cin >> s;
  int n = s.size();
  vector<vector<ll>> dp(2, vector<ll>(2019, 0));
  ll res = 0;
  rep(i, n) {
    int x = s[i] - '0';
    rep(j, 2019) { dp[1][(j * 10 + x) % 2019] += dp[0][j]; }
    res += dp[1][0];
    dp[1][x]++;
    dp[0] = dp[1];
    dp[1].assign(2019, 0);
  }

  cout << res << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
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 (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
int main() {
  string s;
  cin >> s;
  int n = s.size();
  map<int, ll> mp1;
  ll res = 0;
  rep(i, n) {
    int x = s[i] - '0';
    map<int, ll> mp2;
    for (auto m : mp1) {
      mp2[(m.first * 10 + x) % 2019] += m.second;
    }
    mp2[x]++;
    res += mp2[0];
    mp1 = mp2;
  }

  cout << res << "\\n";

  return 0;
}