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

#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

int main() {
  vector<long long int> um1, um2;
  string s;
  cin >> s;
  int l = s.size();
  um1 = vector<long long int>(2019, 0);
  um1[0]++;
  long long int res = 0;
  int c = 0;
  for (int i = 0; i < l; i++) {
    um2 = vector<long long int>(2019, 0);
    for (int j = 0; j < 2019; j++) {
      um2[j * 10 % 2019] += um1[j];
    }
    um1 = um2;
    c = c * 10 + s[i] - '0';
    c %= 2019;
    res += um1[c];
    um1[c]++;
  }
  cout << res << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

int main() {
  unordered_map<int, long long int> um1, um2;
  string s;
  cin >> s;
  int l = s.size();
  um1[0]++;
  long long int res = 0;
  int c = 0;
  for (int i = 0; i < l; i++) {
    um2.clear();
    for (auto p : um1) {
      um2[p.first * 10 % 2019] += p.second;
    }
    um1 = um2;
    c = c * 10 + s[i] - '0';
    c %= 2019;
    res += um1[c];
    um1[c]++;
  }
  cout << res << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
const int mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i < n; i++)
#define all(v) v.begin(), v.end()
#define nepe(v) next_permutation(all(v))
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;

typedef pair<int, int> P;
string s;
int k;
ll INF = 10000000000000000;

int main() {
  string S;
  cin >> S;
  int n = S.size();
  vi SS(n);
  rep(i, n) SS[i] = S[n - 1 - i] - '0';
  vi mod19(n);
  int p10 = 1;
  mod19[0] = SS[0];
  rep2(i, 1, n) {
    p10 = (p10 * 10) % 2019;
    mod19[i] = (mod19[i - 1] + p10 * SS[i]) % 2019;
  }
  vi count(2019, 0);
  count[0] = 1;
  rep(i, n) { count[mod19[i]]++; }
  ll ans = 0;
  rep(i, 2019) { ans += count[i] * (count[i] - 1) / 2; }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
const int mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i < n; i++)
#define all(v) v.begin(), v.end()
#define nepe(v) next_permutation(all(v))
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;

typedef pair<int, int> P;
string s;
int k;
ll INF = 10000000000000000;

ll a19 = 10000000000000000000 % 2019;

bool j2019(string S) {
  int n = S.size();
  int kai = (n - 1) / 19 + 1;
  int ans = 0;
  rep(i, kai - 1) {
    string temp = S.substr(n - 19 * (i + 1), 19);
    ll ntemp = atoi(temp.c_str());
    ntemp %= 2019;
    rep(j, i) { ntemp = (ntemp * a19) % 2019; }
    ans = (ans + ntemp) % 2019;
  }
  string temptemp = S.substr(0, n % 19);
  ll ntemptemp = atoi(temptemp.c_str());
  rep(i, kai) ntemptemp = (ntemptemp * a19) % 2019;
  ans = (ans + ntemptemp) % 2019;
  return (ans == 0);
}

int main() {
  string S;
  cin >> S;
  int ans = 0;
  int keta = S.size();
  rep(i, keta - 4) {
    rep2(j, 4, keta + 1 - i) {
      string temp = S.substr(i, j);
      if (j2019(temp))
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
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;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }

long long modpow(ll a, ll b, ll mod) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}

ll dp[220000];
map<ll, ll> p;
int main() {
  ll ans = 0;
  string s;
  cin >> s;
  long sl = s.length();
  char t = s[sl - 1];
  dp[0] = (t - '0') % 2019;
  rep(i, 1, sl) {
    ll y = s[sl - i - 1] - '0';
    ll z = y * modpow(10, i, 2019);
    dp[i] = (dp[i - 1] + z) % 2019;
  }
  rep(i, 0, sl) p[dp[i]]++;
  for (map<ll, ll>::iterator it = p.begin(); it != p.end(); it++) {
    ll ff = it->second;
    ans += ff * (ff - 1) / 2;
    if (it->first == 0)
      ans += ff;
  }
  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
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;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll x10(ll b, ll k) {
  // cout<<b<<endl;
  if (k >= 16) {
    b *= 1e16;
    b %= 2019;
    x10(b, k - 10);
  } else {
    b = ceil(powl(10, k)) * b;
    // cout<<b<<endl;
    b %= 2019;
    return b;
  }
}
ll dp[220000];
map<ll, ll> p;
int main() {
  ll ans = 0;
  string s;
  cin >> s;
  long sl = s.length();
  char t = s[sl - 1];
  dp[0] = (t - '0') % 2019;
  rep(i, 1, sl + 1) {
    ll y = s[sl - i - 1] - '0';
    dp[i] = (dp[i - 1] + x10(y, i)) % 2019;
  }
  rep(i, 0, sl + 1) p[dp[i]]++;
  for (map<ll, ll>::iterator it = p.begin(); it != p.end(); it++) {
    ll ff = it->second;
    ans += ff * (ff - 1) / 2;
    if (it->first == 0)
      ans += ff;
  }
  cout << ans;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <numeric>
#include <string>
#include <unordered_map>
#include <vector>
#define myfor(i, N) for (i = 0; i < N; i++)
#define myforFL(i, f, l) for (i = f; i < l; i++)

using namespace std;

int main(void) {
  string S;
  int i, j;
  long num = 0, num2, ans;
  cin >> S;
  long dp2[2019] = {};
  myfor(i, S.size()) {
    long dp[2019] = {};
    myfor(j, 2019) {
      num2 = (j * 10 + long(S[i] - '0')) % 2019;
      dp[num2] += dp2[j];
    }
    num = long(S[i] - '0') % 2019;
    dp[num]++;
    ans += dp[0];
    memcpy(dp2, dp, sizeof(dp));
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <string>
#include <unordered_map>
#include <vector>
#define myfor(i, N) for (i = 0; i < N; i++)
#define myforFL(i, f, l) for (i = f; i < l; i++)

using namespace std;

int main(void) {
  string S;
  int i, j, f, l;
  long num;
  cin >> S;

  long ans = 0;
  myfor(i, S.size()) {
    num = long(S[i] - '0');
    myforFL(j, i + 1, S.size() + 1) {
      num = num * 10 + long(S[j] - '0');
      if (num % 2019 == 0)
        ans++;
      num = num % 2019;
    }
  }

  cout << ans << endl;

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


#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define INF 1e9
#define LINF (ll) INF *INF
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

const double PI = acos(-1);
const double EPS = 1e-9; // うまくいかなかったらゆるめる

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef pair<int, pii> pip;
typedef vector<pii> vp;
typedef vector<vi> vvi;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }

signed main(void) {
  string s;
  cin >> s;
  int n = s.size();
  vvi v(2, vi(2019, 0));
  int ans = 0;
  rep(i, n) {
    ans += v[i % 2][0];
    v[i % 2][0]++;
    int t = s[i] - '0';
    rep(j, 2019) if (v[i % 2][j]) {
      v[(i + 1) % 2][(j * 10 + t) % 2019] += v[i % 2][j];
      v[i % 2][j] = 0;
    }
  }
  ans += v[n % 2][0];
  cout << ans << endl;
}


Error: MLE

Fixed submission:


#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define INF 1e9
#define LINF (ll) INF *INF
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

const double PI = acos(-1);
const double EPS = 1e-9; // うまくいかなかったらゆるめる

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef pair<int, pii> pip;
typedef vector<pii> vp;
typedef vector<vi> vvi;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }

char s[200005];
int v[200005][2019];
signed main(void) {
  scanf("%s", s);
  int n = strlen(s);
  rep(i, n) v[i][0] = 1;
  int ans = 0;
  rep(i, n) rep(j, 2019) if (v[i][j]) {
    if (j == 0)
      ans += v[i][j] - 1;
    int t = j * 10 + (s[i] - '0');
    v[i + 1][t % 2019] += v[i][j];
  }
  if (v[n][0])
    ans += v[n][0];
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  vector<long long> v(2020, 0);
  long long curr = 0;
  long long p = 1;
  v[0]++;
  reverse(s.begin(), s.end());
  for (auto &x : s) {
    curr = curr + (x - '0') * p;
    p = p * 10 % 2019;
    curr %= 2019;
    v[curr]++;
  }
  long long ans = 0;
  for (auto &x : v) {
    ans += x * (x - 1) / 2;
  }
  cout << ans << "\\n";
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, x, n) for (int i = x; i < (n); i++)
#define REP3(i, x, n) for (int i = x; i > (n); i--)
#define zero_pad(num) setfill('0') << std::right << setw(num)
#define space_pad(num) setfill(' ') << std::right << setw(num)
#define ALL(n) begin(n), end(n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
const long long INF = numeric_limits<long long>::max();
// 桁をvectorに入れる
vector<ll> GetDigit(ll num) {
  vector<ll> kakuketa;
  while (num != 0) {
    ll keta = num % 10;
    kakuketa.push_back(keta);
    num /= 10;
  }
  return kakuketa;
}
int main() {
  ll S;
  string A;
  ll cnt = 0;
  cin >> A;
  for (ll i = 0; i < A.size() - 3; i++) {
    for (ll j = 4; j < A.size() - i + 1; j++) {
      // ll num = std::stoll(A.substr(i,j));
      string subs = A.substr(i, j);
      istringstream ss;
      ss = istringstream(subs);
      ll num;
      ss >> num;
      if (num == 2019) {
        cnt += A.size() - i - j;
        i++;
      } else if (num % 2019 == 0) {
        cnt++;
        i++;
      }
    }
  }
  printf("%lld", cnt);
  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 PI 3.14159265359
#define INF 1000100100
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define P pair<ll, ll>
#define PP pair<P, P>
using namespace std;
int dp[2][2019];

int main() {
  string s;
  cin >> s;
  rep(i, 2019) dp[1][i] = 0;
  ll ans = 0;
  bool f = true;
  int now;
  rep(i, s.length()) {
    if (f)
      now = 0;
    else
      now = 1;
    f = f ^ 1;
    rep(j, 2019) dp[now][(j * 10 + (s[i] - '0')) % 2019] = dp[now ^ 1][j];
    dp[now][s[i] - '0']++;
    ans += dp[now][0];
  }
  cout << ans << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI 3.14159265359
#define INF 1000100100
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define P pair<ll, ll>
#define PP pair<P, P>
using namespace std;
int dp[200009][2019];

int main() {
  string s;
  cin >> s;
  rep(i, 2019) dp[0][i] = 0;
  ll ans = 0;
  rep(i, s.length()) {
    rep(j, 2019) dp[i + 1][(j * 10 + (s[i] - '0')) % 2019] = dp[i][j];
    dp[i + 1][s[i] - '0']++;
    ans += dp[i + 1][0];
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n, s) for (int i = (s); i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define rreps(i, n, s) for (int i = s; i >= n; i--)
#define MOD 1000000007
using ll = long long;
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;
}

const int m = 2019;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  // mod2019で0となる部分を求める
  // 10と2019は素であるため逆元が定義できる
  // よって累積和がs_i-s_j%2019==0となるi,jを求める
  //->s_i==s_j(mod2019)となる組を求めればよい

  reverse(s.begin(), s.end());
  vector<int> cnt(m, 0);
  int x = 1, tot = 0;
  ll ans = 0;
  rep(i, s.size()) {
    cnt[tot]++;
    tot += (s[i] - '0') * x;
    tot %= m;
    ans += cnt[tot]; // それまでに見つかっている同じ累積和の個数を足す
    x = (x * 10) % m;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n, s) for (int i = (s); i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define rreps(i, n, s) for (int i = s; i >= n; i--)
#define MOD 1000000007
using ll = long long;
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;
}
map<string, bool> memo;
bool check(string sub) {

  if (memo.count(sub)) {
    return memo[sub];
  }

  int m = sub.size();

  ll digit = 1;
  ll val = 0;
  int d = 0;
  rrep(i, m) {
    if (i == m - 1) {
      d = sub[i] - '0';
    }
    if (i < m - 1) {
      val += digit * (sub[i] - '0');
      digit *= 10;
    }
  }
  val *= 2009;

  return memo[sub] = (d - val) % 2019 == 0;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  ll n = s.size();
  ll ans = 0;
  string sub;
  rep(i, n) {
    reps(j, n + 1, i + 4) {
      sub = s.substr(i, j - i);
      if (check(sub)) {
        ans++;
      }
    }
  }
  cout << ans << endl;
  // system("pause");
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

//
//  multiple_of_2019.cpp
//  learningCplusplus
//
//  Created by Tran Duc Chinh on 2020/04/04.
//  Copyright © 2020 Tran Duc Chinh. All rights reserved.
//

#include <iostream>
#include <map>
using namespace std;
typedef long long ll;
ll mod = 1000000007;

int main(int argc, const char *argv[]) {

  std::string s;
  std::cin >> s;
  int n = s.size();
  std::map<int, int> ma;
  ma[0] = 1;

  int t[n + 1];
  t[n] = 0;

  int basemod = 1;
  for (int i = n - 1; i >= 0; i--) {
    t[i] = (t[i + 1] + (s[i] - '0') * basemod) % 2019;
    basemod = (basemod * 10) % 2019;
    ma[t[i]] += 1;
  }
  int sum = 0;
  for (int i = 0; i < 2019; i++) {
    if (ma.count(i)) {
      // std::cout << i << ": " << ma[i] << std::endl;
    }
    sum += ma[i] * (ma[i] - 1) / 2;
  }
  std::cout << sum << std::endl;

  return 0;
}


Error: TLE

Fixed submission:

//
//  multiple_of_2019.cpp
//  learningCplusplus
//
//  Created by Tran Duc Chinh on 2020/04/04.
//  Copyright © 2020 Tran Duc Chinh. All rights reserved.
//

#include <iostream>
#include <map>
using namespace std;
typedef long long ll;
ll mod = 1000000007;

int main(int argc, const char *argv[]) {

  std::string s;
  std::cin >> s;
  std::map<std::string, int> ma;

  int sum = 0;
  for (int i = 0; i < s.size(); i++) {
    int remaining =
        (s[i] - '0') * 100 + (s[i + 1] - '0') * 10 + (s[i + 2] - '0');
    for (int j = i + 3; j < s.size(); j++) {
      remaining = (remaining * 10 + (s[j] - '0')) % 2019;
      if (remaining == 0) {
        sum += 1;
        remaining =
            (s[j + 1] - '0') * 100 + (s[j + 2] - '0') * 10 + (s[j + 3] - '0');
        j += 3;
      } else if (remaining < 10) {
        remaining = remaining * 100 + (s[j + 1] - '0') * 10 + (s[j + 2] - '0');
        j += 2;
      } else if (remaining < 100) {
        remaining = remaining * 10 + (s[j + 1] - '0');
        j += 1;
      }
    }
  }
  std::cout << sum << std::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<int, int>;

const int MOD = 2019;

ll nums[MOD + 5];

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

Error: 0

Fixed submission:

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

const int MOD = 2019;

ll nums[MOD + 5];

int main() {
  string s;
  cin >> s;
  int n = s.size();
  nums[0] = 1;
  ll num = 0, t = 1;
  for (int i = n - 1; i >= 0; i--, t *= 10) {
    num = (num + (s[i] - '0') * t) % MOD;
    nums[num]++;
  }
  ll ans = 0;
  for (int i = 0; i < MOD; i++) {
    ans += nums[i] * (nums[i] - 1) / 2;
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define pi (double)acos(-1)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

int main() {
  string s;
  cin >> s;
  int now = 0, ans = 0, a = 1;
  reverse(all(s));
  map<int, int> mp;
  mp[0]++;
  rep(i, s.size()) {
    now = (now + (s[i] - '0') * a % 2019) % 2019;
    a = a * 10 % 2019;
    mp[now]++;
  }
  for (auto x : mp)
    ans += x.second * (x.second - 1) / 2;
  cout << ans << endl;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define pi (double)acos(-1)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

int main() {
  string s;
  cin >> s;
  vector<vector<int>> dp(s.size(), vector<int>(2019, 0));
  int now;
  dp[0][s[0] - '0']++;
  REP(i, s.size() - 1) {
    now = s[i] - '0';
    dp[i][now]++;
    rep(j, 2019) { dp[i][(j * 10 + now) % 2019] += dp[i - 1][j]; }
  }
  int ans = 0;
  rep(i, s.size()) ans += dp[i][0];
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll long long
#define endl "\\n"
#define print_array(name, n)                                                   \\
  for (ll o = 0; o < n; o++)                                                   \\
  cout << name[o] << " "
#define fr(i, f, t) for (ll i = (f); i < t; i++)
#define fb(i, f, t) for (ll i = (f); i >= t; i--)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define inf 2e18
bool sortbysecond(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.ss > b.ss);
}
template <typename T> string NumberTostring(T Number) {
  ostringstream ss;
  ss << Number;
  return ss.str();
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  string s;
  cin >> s;
  int n = s.size();
  // vector<vector<int>> vec(n,vector<int>(2019,0));
  vector<int> vec(2019, 0);
  vec[int(s[0]) - 48] = 1;
  int cnt = 0;
  for (int i = 1; i < n; i++) {
    vector<int> temp(2019, 0);
    for (int m = 0; m < 2019; m++) {
      if (vec[m] > 0) {
        int num = m * 10 + int(s[i]) - 48;
        temp[num % 2019] = vec[m];
      }
    }
    temp[int(s[i]) - 48]++;
    cnt += temp[0];
    vec = temp;
  }
  cout << cnt << endl;
  // int cnt = 0;
  // for(int i=0;i<n;i++){
  // 	cnt+=vec[i][0];
  // }
  // cout<<cnt<<endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll long long
#define endl "\\n"
#define print_array(name, n)                                                   \\
  for (ll o = 0; o < n; o++)                                                   \\
  cout << name[o] << " "
#define fr(i, f, t) for (ll i = (f); i < t; i++)
#define fb(i, f, t) for (ll i = (f); i >= t; i--)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define inf 2e18
bool sortbysecond(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.ss > b.ss);
}
template <typename T> string NumberTostring(T Number) {
  ostringstream ss;
  ss << Number;
  return ss.str();
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  string s;
  cin >> s;
  int n = s.size();
  vector<vector<int>> vec(n, vector<int>(2019, 0));
  vec[0][int(s[0]) - 48] = 1;

  for (int i = 1; i < n; i++) {
    for (int m = 0; m < 2019; m++) {
      if (vec[i - 1][m] > 0) {
        int num = m * 10 + int(s[i]) - 48;
        vec[i][num % 2019] = vec[i - 1][m];
      }
    }
    vec[i][int(s[i]) - 48]++;
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += vec[i][0];
  }
  cout << cnt << endl;
  return 0;
}