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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(begin, i, end) for (int i = begin; i < (int)(end); i++)
#define all(v) v.begin(), v.end()
const int MOD = 1000000007;

int main() {
  string S;
  cin >> S;
  map<ll, ll> mp; // Xi,0 mod2019 を入れておく
  mp[0] = 1; // X0,0 mod2019 (=0) はfor文に出てこないので先に入れておく
  reverse(all(S)); // 後ろから数字を見るのであらかじめreverseしておくと良い
  ll now = 0, ten = 1;
  for (auto s : S) // Xi,0 mod2019を計算するループ
  {
    now = (now + (s - '0') * ten) % 2019; // Xi,0 mod2019の計算
    ten *= 10;
    ten %= 2019;
    mp[now]++;
  }

  ll ans = 0;
  for (auto p : mp) {
    ans += p.second * (p.second - 1) / 2;
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(begin, i, end) for (int i = begin; i < (int)(end); i++)
#define all(v) v.begin(), v.end()
const int MOD = 1000000007;

int main() {
  string s;
  cin >> s;
  int size = s.size();
  ll ans = 0;
  for (int i = 4; i <= size; i++) {
    for (int j = 0; j <= size - i; j++) {
      string sub = s.substr(j, i);
      ll a = stoll(sub);
      if (a % 2019 == 0) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const ll mod = 2019;
int main() {
  string S;
  cin >> S;
  ll N = S.size();

  ll val = 0;
  ll pm = 1;
  vector<int> m(2030, 0);
  m[0]++;
  ll res = 0;
  for (int i = N - 1; i >= 0; i--) {
    val = val + pm * (S[i] - '0');
    val %= mod;
    pm = (pm * 10) % mod;
    res += m[val];
    m[val]++;
  }

  cout << res << endl;
}

Error: TLE

Fixed submission:

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

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

  vector<ll> mT(200020, 0);
  vector<int> m(2030, 0);
  for (int i = N - 1; i >= 0; i--) {
    mT[i] = mT[i + 1];
    int calc = S[i] - '0';
    for (int j = 0; j < N - i - 1; j++) {
      calc *= 10;
      calc %= mod;
    }
    mT[i] += calc;
    mT[i] %= mod;
    m[mT[i] % mod]++;
  }

  ll res = m[0];
  for (int i = 1; i < 2019; i++) {
    res += (m[i] - 1) * m[i] / 2;
  }
  cout << res << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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;
}
const ll INF = 1LL << 60;
const ll M = 1000000007;
ll mpow(ll a, ll b) {
  ll res = 1;
  while (b != 0) {
    if (b & 1)
      res = res * a % M;
    a = a * a % M;
    b = b >> 1;
  }
  return res;
}
ll mcomb(ll n, ll k) {
  if (k > n / 2)
    return mcomb(n, n - k);
  ll res = 1;
  for (ll i = 0; i < k; i++) {
    res = res * (n - i) % M;
    res = res * mpow(k - i, M - 2) % M;
  }
  return res;
}
/*template*/

ll hpow(ll a, ll b) {
  ll res = 1;
  while (b != 0) {
    if (b & 1)
      res = res * a % 2019;
    a = a * a % 2019;
    b = b >> 1;
  }
  return res;
}

int main() {
  string s;
  cin >> s;
  ll n = s.length();
  ll a[2019] = {0};
  ll hoge = 0;
  for (ll i = 0; i < n; i++) {
    hoge = (hoge + (s[n - 1 - i] - '0') * hpow(10, i)) % 2019;
    a[hoge]++;
  }
  ll ans = 0;
  for (ll i = 0; i < 2019; i++) {
    ans += a[i] * (a[i] - 1) / 2;
  }
  cout << ans + a[0] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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;
}
const ll INF = 1LL << 60;
const ll M = 1000000007;
ll mpow(ll a, ll b) {
  ll res = 1;
  while (b != 0) {
    if (b & 1)
      res = res * a % M;
    a = a * a % M;
    b = b >> 1;
  }
  return res;
}
ll mcomb(ll n, ll k) {
  if (k > n / 2)
    return mcomb(n, n - k);
  ll res = 1;
  for (ll i = 0; i < k; i++) {
    res = res * (n - i) % M;
    res = res * mpow(k - i, M - 2) % M;
  }
  return res;
}
/*template*/

int main() {
  string s;
  cin >> s;
  ll n = s.length();
  ll ans = 0;
  for (ll i = 0; i < n - 4; i++) {
    for (ll j = 4; j <= n - i; j++) {
      if (stoll(s.substr(i, j)) % 2019 == 0)
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <string>
#include <vector>
std::vector<long long int> vec(200002, 0);
std::vector<long long int> sum(2022, 0);
int main() {
  std::string str;
  std::cin >> str;
  long long int s = 1;
  for (int i = 1; i <= str.length(); i++) {

    //      std::cout<<s<<std::endl;
    //      std::cout<<(str[str.length()-i]-'0')*s+vec[i-1]<<" ";
    vec[i] = ((str[str.length() - i] - '0') * s) % 2019 + vec[i - 1] % 2019;
    vec[i] = vec[i] % 2019;
    sum[vec[i]]++;

    s = 10 * s;
    s = s % 2019;
  }
  //   for(int i=1;i<=str.length();i++)
  //    std::cout<<vec[i]<<" mod:" <<vec[i]%2019<<std::endl;

  long long int count = 0;
  for (int i = 0; i < sum.size(); i++) {
    count += sum[i] * (sum[i] - 1) / 2;
  }
  count += sum[0];
  std::cout << count << std::endl;
  return 0;
}


Error: WA

Fixed submission:

#include <cmath>
#include <iostream>
#include <string>
#include <vector>
std::vector<long long int> vec(200002, 0);
std::vector<long long int> sum(2022, 0);
int main() {
  std::string str;
  std::cin >> str;
  long long int s;
  long long int b = 1;
  for (int i = 1; i <= str.length(); i++) {
    if (b > 6) {
      s = std::pow(10, b - 7);
      s = s % 2019;
      s = s * std::pow(10, 6);
      s = s % 2019;
      b = b % 6;
    } else
      s = std::pow(10, i - 1);

    //      std::cout<<s<<std::endl;
    //      std::cout<<(str[str.length()-i]-'0')*s+vec[i-1]<<" ";
    vec[i] = ((str[str.length() - i] - '0') * s) % 2019 + vec[i - 1] % 2019;
    vec[i] = vec[i] % 2019;
    sum[vec[i]]++;
    b++;
  }
  //   for(int i=1;i<=str.length();i++)
  //    std::cout<<vec[i]<<" mod:" <<vec[i]%2019<<std::endl;

  long long int count = 0;
  for (int i = 0; i < sum.size(); i++) {
    count += sum[i] * (sum[i] - 1) / 2;
  }
  count += sum[0];
  std::cout << count << std::endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int f(ll a, ll b) {
  if (b == 0) {
    return 1;
  }
  if (b % 2 == 0) {
    return (f(a * a % 2019, b / 2));
  }
  if (b % 2 == 1) {
    return (a * f(a, b - 1) % 2019);
  }
}
int main() {
  string s;
  cin >> s;
  ll N = s.size();
  ll ans = 0;
  vector<ll> sum(N + 1);
  sum.at(0) = 0;
  for (ll i = 1; i <= N; i++) {
    sum.at(i) = (sum.at(i - 1) + f(10, i - 1) * (int)(s.at(N - i) - '0'));
    sum.at(i) %= 2019;
  }
  vector<ll> vec(2019);
  for (ll i = 0; i <= N; i++) {
    vec.at(sum.at(i))++;
  }
  for (ll i = 0; i < 2019; i++) {
    if (vec.at(i) >= 2) {
      ans += vec.at(i) * (vec.at(i) - 1) / 2;
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  string s;
  cin >> s;
  ll N = s.size();
  ll ans = 0;
  vector<ll> sum(N + 1);
  sum.at(0) = 0;
  for (ll i = 1; i <= N; i++) {
    sum.at(i) =
        (sum.at(i - 1) + std::pow(10, i - 1) * (int)(s.at(N - i) - '0'));
    sum.at(i) %= 2019;
  }
  vector<int> ac(2019);
  for (ll i = 0; i < 2019; i++) {
    ac.at(i) = 1;
  }
  sort(sum.begin(), sum.end());
  for (auto v : sum) {
  }
  for (ll j = 1; j < N; j++) {
    if (sum.at(j) == sum.at(j - 1)) {
      ac.at(sum.at(j))++;
    }
  }
  for (ll i = 0; i < 2019; i++) {
    if (ac.at(i) >= 2) {
      ans += ac.at(i) * (ac.at(i) - 1) / 2;
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define SPEED                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
const int mod = 1e9 + 7;
void reverseS(string &s, int n) {
  for (int i = 0; i < n / 2; ++i) {
    char ch = s[i];
    s[i] = s[n - i - 1];
    s[n - i - 1] = ch;
  }
}
int main() {
  SPEED
  string s;
  cin >> s;
  int n = s.size();
  reverseS(s, n);

  int k = 2019;
  vector<int> dp(k, 0);

  int ans = 0;

  int tensmod2019[200001];
  tensmod2019[0] = 1;
  for (int i = 1; i <= 200000; ++i) {
    int num = tensmod2019[i - 1] * 10;
    tensmod2019[i] = num % k;
  }

  int prev = 0;
  for (int i = 0; i < n; ++i) {
    int digit = s[i] - '0';
    int remainder = (prev + (digit * tensmod2019[i])) % k;
    dp[remainder]++;
    prev = remainder;
  }

  ans += dp[0];

  for (int i = 0; i < k; ++i) {
    ans += (dp[i] * (dp[i] - 1)) / 2;
  }

  cout << ans;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define SPEED                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
const int mod = 1e9 + 7;
int main() {
  SPEED
  string s;
  cin >> s;

  int n = s.size();
  int ans = 0;
  int mod2019[n];

  mod2019[0] = (s[0] - '0') % 2019;

  for (int i = 1; i < n; ++i) {
    mod2019[i] = ((mod2019[i - 1] * 10) + (s[i] - '0')) % 2019;
  }

  int tensmod2019[200001];
  tensmod2019[0] = 1;
  for (int i = 1; i <= 200000; ++i) {
    int num = tensmod2019[i - 1] * 10;
    tensmod2019[i] = num % 2019;
  }

  /*for (int i = 0; i < 10; ++i)
  {
          cout << tensmod2019[i] << " ";
  }*/

  for (int l = 0; l < n; ++l) {
    for (int r = l; r < n; ++r) {
      if (l == 0) {
        if (mod2019[r] == 0) {
          ans++;
          // cout << l << " " << r << '\\n';
        }
      } else {
        if ((mod2019[r] - mod2019[l - 1] * tensmod2019[r - l + 1]) % 2019 ==
            0) {
          ans++;
          // cout << l << " " << r << '\\n';
        }
      }
    }
  }

  cout << ans;

  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'
using namespace std;
const int INF = 0x3f3f3f3f;
const double pi = acos(-1), eps = 1e-8;
const int maxn = 202000;
int a[maxn], b[maxn];
int main() {
  //	ios::sync_with_stdio(false);
  //	cin.tie(0);cout.tie(0);
  string s;
  ll sum = 0;
  cin >> s;
  int len = s.length();
  for (int i = 0; i < len; i++) {
    int tmp = s[i] - '0';
    b[tmp] = 1;
    for (int j = 0; j < 2019; j++) {
      b[(j * 10 + tmp) % 2019] += a[j];
    }
    for (int j = 0; j < 2019; j++) {
      a[j] = b[j];
      b[j] = 0;
    }
    sum += a[0];
  }

  cout << sum << endl;
  return 0;
}


Error: 0

Fixed submission:

#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'
using namespace std;
const int INF = 0x3f3f3f3f;
const double pi = acos(-1), eps = 1e-8;
const int maxn = 2020;
int a[maxn], b[maxn];
int main() {
  //	ios::sync_with_stdio(false);
  //	cin.tie(0);cout.tie(0);
  string s;
  cin >> s;
  int len = s.length();
  a[0] = 1;
  for (int i = 0; i < len; i++) {
    int tmp = s[i] - '0';
    b[tmp] = 1;
    for (int j = 0; j < 2019; j++) {
      b[(j * 10 + tmp) % 2019] += a[j];
    }
    for (int j = 0; j < 2019; j++) {
      a[j] = b[j];
      b[j] = 0;
    }
  }
  ll sum = 0;
  for (int i = 0; i < len; i++) {
    sum += a[i] * (a[i] + 1) / 2;
  }
  cout << sum << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define modulo 2019
#define mod(mod_x) ((((long long)mod_x + modulo)) % modulo)
#define Inf 1000000000000000001

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

  vector<vector<long long>> dp(2, vector<long long>(modulo, 0));
  long long ans = 0;
  for (int i = 0; i < S.size(); i++) {
    dp[1][S[i] - '0']++;
    for (int j = 0; j < modulo; j++) {
      dp[1][(j * 10 + S[i] - '0') % 2019] += dp[0][j];
      dp[0][j] = 0;
    }
    ans += dp[1][0];
    swap(dp[0], dp[1]);
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define modulo 2019
#define mod(mod_x) ((((long long)mod_x + modulo)) % modulo)
#define Inf 1000000000000000001

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

  vector<vector<long long>> dp(S.size() + 1, vector<long long>(modulo, 0));

  for (int i = 0; i < S.size(); i++) {
    dp[i + 1][S[i] - '0']++;
    for (int j = 0; j < modulo; j++) {
      dp[i + 1][(j * 10 + S[i] - '0') % 2019] += dp[i][j];
    }
  }

  long long ans = 0;

  for (int i = 1; i <= S.size(); i++) {
    ans += dp[i][0];
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define modulo P
#define mod(mod_x) ((((long long)mod_x + modulo)) % modulo)
#define Inf 1000000000000000001

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

  map<int, long long> mp;

  mp[0]++;

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

  int now = 0;
  long long x = 1;
  long long ans = 0;
  for (int i = 0; i < S.size(); i++) {
    now += x * (S[i] - '0');
    now %= 2019;
    ans += mp[now];
    mp[now]++;
    //	cout<<now<<endl;
    x *= 10;
    x %= 2019;
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define modulo P
#define mod(mod_x) ((((long long)mod_x + modulo)) % modulo)
#define Inf 1000000000000000001

int main() {
  string S;
  cin >> S;
  int P;
  P = 2019;

  vector<vector<long long>> dp(S.size() + 1, vector<long long>(P, 0));

  // dp[0][0] = 1;

  for (int i = 0; i < S.size(); i++) {
    for (int j = 0; j < P; j++) {
      dp[i + 1][mod(j * 10 + S[i] - '0')] += dp[i][j];
    }
    dp[i + 1][mod(S[i] - '0')]++;
  }

  long long ans = 0;

  for (int i = 1; i <= S.size(); i++) {
    ans += dp[i][0];
    // cout<<dp[i][0]<<endl;
  }
  cout << ans << endl;

  return 0;
}
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 INT;
double PI = 3.14159265;
using namespace std;
using Graph = vector<vector<int>>;
using Field = vector<string>;
using DGraph = vector<vector<pair<int, int>>>;

const long long INF = 1LL << 60;
//---//
int rem[2019][2] = {};

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

  int n, num = 0, pow10[200010], nnum, sum = 0;
  n = S.size();
  pow10[0] = 1;
  rep(i, n - 1) pow10[i + 1] = pow10[i] * 10 % 2019;

  rem[0][0] = 1;
  rep(i, n) {
    num = (num * 10 + (S[i] - '0')) % 2019;
    nnum = num * pow10[n - i - 1] % 2019;
    if (rem[nnum][0] > 0)
      rem[nnum][1] += rem[nnum][0];
    rem[nnum][0]++;
  }

  rep(i, 2019) if (rem[i][0] > 1) sum += rem[i][1];

  cout << sum << 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 INT;
double PI = 3.14159265;
using namespace std;
using Graph = vector<vector<int>>;
using Field = vector<string>;
using DGraph = vector<vector<pair<int, int>>>;

const long long INF = 1LL << 60;
//---//
int rem[2019][2] = {};

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

  int n, num = 0, pow10[2019], nnum, sum = 0;
  n = S.size();
  pow10[0] = 1;
  rep(i, 2018) pow10[i + 1] = pow10[i] * 10 % 2019;

  rem[0][0] = 1;
  rep(i, n) {
    num = (num * 10 + (S[i] - '0')) % 2019;
    nnum = num * pow10[2018 - i] % 2019;
    if (rem[nnum][0] > 0)
      rem[nnum][1] += rem[nnum][0];
    rem[nnum][0]++;
  }

  rep(i, 2019) if (rem[i][0] > 1) sum += rem[i][1];

  cout << sum << endl;

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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;

int main() {
  string S;
  cin >> S;
  LL total = 0;
  LL keta = 1;
  LL cnt[2020] = {};
  LL ans = 0;
  cnt[0] = 1;
  for (auto it = S.rbegin(); it != S.rend(); ++it) {
    LL v = *it - '0';
    total = (v * keta + total) % 2019;
    keta = (keta * 10) % 2019;

    ans += cnt[total];
    ++cnt[total];
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;

int main() {
  string S;
  cin >> S;
  int L = S.size();
  vector<vector<LL>> dp(L + 1, vector<LL>(2020, 0));
  int cnt = 0;
  for (int i = 0; i < 2020; ++i)
    dp[0][0] = 0;

  for (int i = 1; i <= L; ++i) {
    int x = S[i - 1] - '0';
    for (int j = 0; j < 2020; ++j) {
      int y = (j * 10 + x) % 2019;
      dp[i][y] += dp[i - 1][j];
    }
    ++dp[i][x];
    cnt += dp[i][0];
  }
  cout << cnt << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define INF 1000000000000000000

int main() {
  string s;
  cin >> s;
  int N = s.size();
  reverse(all(s));
  map<ll, ll> ma;
  vector<ll> sum(N + 1, 0);
  ll MOD = 2019;
  ll num = 0, keta = 1;
  rep(i, N + 1) {
    num += keta * (s[i] - '0');
    keta *= 10;
    keta %= MOD;
    num %= MOD;
    sum[i + 1] = num;
  }

  rep(i, N + 1) { ma[sum[i]]++; }

  ll res = 0;
  for (auto p : ma) {
    res += (p.second * (p.second - 1) / 2);
  }

  cout << res << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define INF 1000000000000000000

int main() {
  string s;
  cin >> s;
  int N = s.size();
  reverse(all(s));
  map<ll, ll> ma;
  ll MOD = 2019;
  vector<ll> sum(N + 1, 0);
  string str = "";
  rep(i, N) {
    str = s[i] + str;
    sum[i + 1] = stol(str);
  }

  rep(i, N + 1) {
    sum[i] %= MOD;
    ma[sum[i]]++;
  }

  ll res = 0;
  for (auto p : ma) {
    res += p.second * (p.second - 1) / 2;
  }

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

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

#define watch(x) cerr << (#x) << ": " << (x) << endl

typedef long long LL;
const int N = 200003;
int n;
string s;
int dp[2019], dp2[2019];
LL ans;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> s;
  n = s.length();

  for (int i = 0; i < n; ++i) {
    int dig = s[i] - '0';
    for (int num = 0; num < 2019; ++num) {
      int nxt = (num * 10 + dig) % 2019;
      dp2[nxt] += dp[num];
    }
    dp2[dig]++;
    ans += dp2[0];

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

  cout << ans << '\\n';

  return 0;
}


Error: MLE

Fixed submission:

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

#define watch(x) cerr << (#x) << ": " << (x) << endl

typedef long long LL;
const int N = 200003;
int n;
string s;
int dp[N][2019];
LL ans;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> s;
  n = s.length();

  for (int i = 0; i < n; ++i) {
    int dig = s[i] - '0';
    for (int num = 0; num < 2019; ++num) {
      int nxt = (num * 10 + dig) % 2019;
      dp[i + 1][nxt] += dp[i][num];
    }
    dp[i + 1][dig]++;
  }

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

  cout << ans << '\\n';

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

#include <algorithm>
#include <bits/stdint-uintn.h>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;

template <typename T> inline constexpr bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline constexpr bool chmax(T &a, T b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <typename ite>
void print_ite(ite start, ite end, char sp = ' ', char en = '\\n') {
  for (auto i = start; i != end; i++)
    cout << *i << sp;
  cout << en;
}
template <typename ite> void scan_ite(ite start, ite end) {
  for (auto i = start; i != end; i++)
    cin >> (*i);
}
inline void Yes(bool condition) { cout << (condition ? "Yes\\n" : "No\\n"); }

int main() {
  string st;
  cin >> st;
  int n = st.length();
  vector<uint16_t> s(n);
  for (int i = 0; i < n; i++) {
    s[i] = st[i] - '0';
  }
  vector<vector<uint16_t>> dp(n, vector<uint16_t>(2019, 0));
  for (int i = 0; i < n - 1; i++) {
    dp[i][s[i]] += 1;
    for (int j = 0; j < 2019; j++) {
      int next = 10 * j + s[i + 1];
      next %= 2019;
      dp[i + 1][next] += dp[i][j];
    }
  }
  dp[n - 1][s[n - 1]] += 1;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans += dp[i][0];
  }
  cout << ans << endl;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;

template <typename T> inline constexpr bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline constexpr bool chmax(T &a, T b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <typename ite>
void print_ite(ite start, ite end, char sp = ' ', char en = '\\n') {
  for (auto i = start; i != end; i++)
    cout << *i << sp;
  cout << en;
}
template <typename ite> void scan_ite(ite start, ite end) {
  for (auto i = start; i != end; i++)
    cin >> (*i);
}
inline void Yes(bool condition) { cout << (condition ? "Yes\\n" : "No\\n"); }

int main() {
  string st;
  cin >> st;
  int n = st.length();
  vector<int> s(n);
  for (int i = 0; i < n; i++) {
    s[i] = st[i] - '0';
  }
  vector<vector<int>> dp(n, vector<int>(2019, 0));
  for (int i = 0; i < n - 1; i++) {
    dp[i][s[i]] += 1;
    for (int j = 0; j < 2019; j++) {
      int next = 10 * j + s[i + 1];
      next %= 2019;
      dp[i + 1][next] += dp[i][j];
    }
  }
  dp[n - 1][s[n - 1]] += 1;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans += dp[i][0];
  }
  cout << ans << endl;
}