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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const ll mod = 1e9 + 7;

vector<string> g;

int main() {

  string s;
  cin >> s;

  reverse(s.begin(), s.end());
  vector<ll> m(2019);

  ll cur = 1;
  ll prev = 0;
  m[0]++;
  for (int i = 0; i < s.size(); ++i) {
    ll num = s[i] - '0';
    m[(cur * num + prev) % 2019]++;
    prev = cur * num + prev;
    cur *= 10;
    cur %= 2019;
  }

  ll ret = 0;
  for (int i = 0; i < 2019; ++i) {
    if (m[i] != 0) {
      ret += (m[i] * (m[i] - 1)) / 2;
    }
  }

  cout << ret << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const ll mod = 1e9 + 7;

vector<string> g;

int main() {

  string s;
  cin >> s;

  vector<vector<ll>> dp(s.size(), vector<ll>(2019));

  ll ret = 0;
  for (int i = 0; i < s.size(); ++i) {
    ll num = s[i] - '0';
    if (i == 0) {
      dp[0][num] = 1;
      continue;
    }
    dp[i][num]++;
    for (int j = 0; j < 2019; ++j) {
      dp[i][(j * 10 + num) % 2019] += dp[i - 1][j];
    }
    ret += dp[i][0];
  }

  cout << ret << endl;

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

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

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

  map<ll, ll> p;
  ll n = 0;
  ll keta = 1;
  p[0] = 1;
  reverse(S.begin(), S.end());
  for (int i = 0; i < S.size(); i++) {

    n = (n + (S[i] - '0') * keta) % 2019;
    p[n % 2019] += 1;
    keta *= 10;
    keta %= 2019;
  }
  ll ans = 0;
  for (int i = 0; i < 2019; i++) {
    if (p[i] > 0)
      ans += (p[i] * (p[i] - 1)) / 2;
  }

  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

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

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

  ll sum = 0;
  for (int i = 0; i <= S.size() - 3; i++) {
    for (int j = i + 3; j < S.size(); j++) {
      istringstream iss1(S.substr(i, j - i + 1));
      int t;
      iss1 >> t;
      if (t % 2019 == 0)
        sum++;
    }
  }

  cout << sum;

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

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

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

  vector<int> mop10(s.size());
  mop10[0] = 1;
  for (int i = 1; i < s.size(); i++) {
    mop10[i] = mop10[i - 1] * 10 % 2019;
  }

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

  vector<int> sum(s.size());
  vector<int> cnt(2019, 0);

  sum[0] = (int)(s[0] - '0');
  cnt[sum[0]]++;
  ll ans = 0LL;

  for (int i = 1; i < s.size(); i++) {
    sum[i] = sum[i - 1] + mop10[i] * (int)(s[i] - '0');
    sum[i] %= 2019;
    if (sum[i] == 0) {
      ans++;
    }
    cnt[sum[i]]++;
  }

  for (int i = 0; i < 2019; i++) {
    ll num = cnt[i] * (cnt[i] - 1) / 2;
    ans += num;
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

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

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

  if (s.size() <= 3) {
    cout << 0 << endl;
  } else {
    vector<string> v(99);
    for (ll i = 0; i < 99; i++) {
      ll num = 2019 * (i + 1);
      v[i] = to_string(num);
    }
    ll cnt = 0;
    ll n = stoll(s);
    for (ll i = 0; i < 99; i++) {
      ll m = stoll(v[i]);
      if (n < m) {
        break;
      }
      ll k = s.size() - v[i].size() + 1;
      for (ll j = 0; j < k; j++) {
        string res = s.substr(j, v[i].size());
        if (res == v[i]) {
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define INF 2e9
#define MOD 1000000007
using namespace std;
typedef long long llong;
typedef long double ldouble;

using Graph = vector<vector<int>>;

int ctoi(const char c) {
  if ('0' <= c && c <= '9')
    return (c - '0');
  return -1;
}

// int gcd(int a, int b){
//   int ma = max(a,b);
//   int mi = min(a,b);
//   if(mi == 0) return ma;

//   return gcd(mi, ma%mi);
// }

int main() {
  string S;
  cin >> S;
  vector<llong> r(2019);
  int rem = 0;
  int previous = 0;
  for (int i = 0; i < S.length(); ++i) {
    if (previous == 0) {
      rem = rem + ctoi(S[S.length() - 1 - i]);
      previous = 1;
    } else {
      rem = rem + previous * 10 * ctoi(S[S.length() - 1 - i]);
      previous = previous * 10 % 2019;
      if (previous < 0)
        previous += 2019;
    }
    rem %= 2019;
    if (rem < 0)
      rem += 2019;
    ++r[rem];
  }
  llong ans = r[0];
  for (int i = 0; i < 2019; ++i) {
    ans += r[i] * (r[i] - 1) / 2;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 2e9
#define MOD 1000000007
using namespace std;
typedef long long llong;
typedef long double ldouble;

using Graph = vector<vector<int>>;

int ctoi(const char c) {
  if ('0' <= c && c <= '9')
    return (c - '0');
  return -1;
}

// int gcd(int a, int b){
//   int ma = max(a,b);
//   int mi = min(a,b);
//   if(mi == 0) return ma;

//   return gcd(mi, ma%mi);
// }

int remain(int num, int keta) {
  if (keta == 1)
    return num;
  else {
    int hoge = remain(num, keta - 1) * 10;
    return hoge % 2019;
  }
}

int main() {
  string S;
  cin >> S;
  vector<double> r(2019);
  int rem = 0;
  for (int i = 0; i < S.length(); ++i) {
    rem = rem + remain(ctoi(S[S.length() - 1 - i]), i + 1);
    rem %= 2019;
    if (rem < 0)
      rem += 2019;
    ++r[rem];
  }
  llong ans = r[0];
  for (int i = 1; i < 2019; ++i) {
    ans += r[i] * (r[i] - 1) / 2;
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

using ll = long long;
using P = pair<int, int>;
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rep0(i, n) for (int i = 0; i < n; i++)
#define zarray(x, n)                                                           \\
  x[n + 1];                                                                    \\
  rep1(i, n) { x[i] = 0; }

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

  int aa[2020] = {0};

  aa[0] = 1;
  int a = 0;
  int c = 1;
  for (int j = (int)s.length() - 1; j >= 0; j--) {
    a += (s[j] - '0') * c;
    a %= 2019;
    aa[a]++;
    c *= 10;
    c %= 2019;
  }

  ll ans = 0;
  for (int mod : aa) {
    if (mod == 0)
      continue;
    ans += mod * (mod - 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>;
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rep0(i, n) for (int i = 0; i < n; i++)
#define zarray(x, n)                                                           \\
  x[n + 1];                                                                    \\
  rep1(i, n) { x[i] = 0; }

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

  ll ans = 0;
  for (size_t i = 0; i < s.length() - 3; i++) {
    int a = s[i] - '0';
    for (size_t j = i + 1; j < s.length(); j++) {
      a *= 10;
      a += (s[j] - '0');
      a %= 2019;
      if (a == 0) {
        ans++;
        // i = j+1;
      }
    }
  }

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

#include <bits/stdc++.h>
#include <time.h>
#define F(i, a, b) for (int i = a; i <= (b); ++i)
#define dF(i, a, b) for (int i = a; i >= (b); --i)
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3f
#define pb push_back
#define maxnkp make_pair
#define fi first
#define se second
#define eps 1e-10
using namespace std;
typedef long long ll;
const int mods = 1e9 + 7;
const int maxn = 2e5 + 10;
const int N = 1e5 + 10;
const int E = 2e5 + 10;
int n, k;
string s;
// int pre[maxn];
int f[2][2100];
int top;
int main() {
  top = 2019;
  cin >> s;
  int t = 0;
  f[0][s[0] - '0'] = 1;
  t++;
  ll ans = 0;
  F(i, 1, s.size() - 1) {
    F(j, 0, top - 1) {
      f[t % 2][(j * 10 + (s[i] - '0')) % top] = f[(t + 1) % 2][j];
    }
    f[t % 2][s[i] - '0']++;
    ans += f[t % 2][0];
    t++;
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#include <time.h>
#define F(i, a, b) for (int i = a; i <= (b); ++i)
#define dF(i, a, b) for (int i = a; i >= (b); --i)
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3f
#define pb push_back
#define maxnkp make_pair
#define fi first
#define se second
#define eps 1e-10
using namespace std;
typedef long long ll;
const int mods = 1e9 + 7;
const int maxn = 2e5 + 10;
const int N = 1e5 + 10;
const int E = 2e5 + 10;
int n, k;
string s;
// int pre[maxn];
int f[maxn][2100];
int top;
int main() {
  top = 2019;
  cin >> s;
  f[0][s[0] - '0'] = 1;
  F(i, 1, s.size() - 1) {
    f[i][s[i] - '0']++;
    F(j, 0, top - 1) { f[i][(j * 10 + (s[i] - '0')) % top] += f[i - 1][j]; }
  }
  ll ans = 0;
  F(i, 0, s.size() - 1) { ans += f[i][0]; }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <vector>
#define ll long long
#define MOD = 1e9 + 7

using namespace std;

int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());

  map<int, int> cnt;
  cnt[0]++;
  int sum = 0, ans = 0, base = 1;
  for (int i = 0; i < s.size(); i++) {
    sum += base * (s[i] - '0');
    sum %= 2019;
    ans += cnt[sum];
    cnt[sum]++;
    base *= 10;
    base %= 2019;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <vector>
#define ll long long
#define MOD = 1e9 + 7

using namespace std;

int main() {
  string S;
  cin >> S;
  vector<ll> ans;
  for (int i = 4; i < S.size(); i++) {
    int n = 0;
    while (n + i <= S.size()) {
      ll num = stoll(S.substr(n, n + i));
      if (num % 2019 == 0)
        ans.push_back(num);
      n++;
    }
  }
  cout << ans.size() << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  string s;
  cin >> s;
  unordered_map<ll, ll> mapp;
  ll pow = 1;
  ll sum = 0;
  for (int i = s.size() - 1; ~i; i--) {
    sum = ((s[i] - '0') * pow + (sum)) % 2019;
    pow = (pow * 10) % 2019;
    mapp[sum]++;
  }
  mapp[0]++;
  ll ans = 0;
  for (auto it : mapp) {
    ans += (it.second) * (it.second - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  string s;
  cin >> s;
  ll k = 2019;
  ll numMultiples = 0;
  //     = array of n sets, all initially empty
  set<ll> seenRemainders[s.size()];
  for (int i = 0; i < s.size(); i++) {
    ll remainder = 0;
    ll prefixesFound = 0;
    for (int j = i; j < s.size(); j++) {
      remainder = (10 * remainder + (s[j] - '0')) % k;
      if (seenRemainders[j].find(remainder) != seenRemainders[j].end()) {
        break;
      }
      seenRemainders[j].insert(remainder);
      if (remainder == 0) {
        prefixesFound++;
      }
    }
    numMultiples += prefixesFound * (prefixesFound + 1) / 2;
  }
  cout << numMultiples << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;

int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());

  int m = 2019;
  vector<int> count(m);

  int t = 0, x = 1;
  long long ans = 0;

  for (int i = 0; i < s.size(); i++) {
    count[t]++;
    t += (s[i] - '0') * x;
    t %= m;
    ans += count[t];
    x = x * 10 % m;
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;

int main() {
  fast;
  int x = 0, counter = 0;
  string s, temp;
  cin >> s;
  int n = s.length();
  for (int j = 3; j < n; j++) {
    for (int i = 0; i < n - j; i++) {
      temp = s.substr(i, j + 1);
      stringstream conv(temp);
      conv >> x;
      if (x % 2019 == 0) {
        counter++;
      }
    }
  }

  cout << counter;

  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++)
#define PI 3.14159265358979323846
#define int long long
constexpr long long INF = numeric_limits<long long>::max() / 2;
constexpr int MOD = 1000000007;
using Graph = vector<vector<int>>;

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  string S;
  cin >> S;
  int N = S.size();
  map<int, int> m;
  int x = S[N - 1] - '0';
  m[0]++;
  m[x]++;
  int cou = 10;
  for (int i = N - 2; i >= 0; i--) {
    int y = S[i] - '0';
    y *= cou;
    y %= 2019;
    x = x + y;
    x %= 2019;
    m[x]++;
    cou *= 10;
    cou %= 2019;
  }
  int ans = 0;
  for (auto p : m) {
    ans += (p.second * (p.second - 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++)
#define PI 3.14159265358979323846
#define int long long
constexpr long long INF = numeric_limits<long long>::max() / 2;
constexpr int MOD = 1000000007;
using Graph = vector<vector<int>>;

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  string S;
  cin >> S;
  int N = S.size();
  map<int, int> m;
  int x = S[N - 1] - '0';
  m[0]++;
  m[x]++;
  int cou = 1;
  for (int i = N - 2; i >= 0; i--) {
    int y = S[i] - '0';
    rep(i, cou) {
      y *= 10;
      y %= 2019;
    }
    x = x + y;
    x %= 2019;
    m[x]++;
    cou++;
  }
  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>
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define int long long
const int inf = 0x3f3f3f3f3f3f3f3LL;
const int mod = 2019;
using namespace std;
#if LOCAL
#include "../../tr.h"
#else
#define tr(...)
#define endl '\\n'
#endif
template <class T> void smin(T &a, T val) {
  if (a > val)
    a = val;
}
template <class T> void smax(T &a, T val) {
  if (a < val)
    a = val;
}
const int N = 5 * (int)1e5 + 10;

void solve() {
  string s;

  cin >> s;
  int n = (int)(s.size());

  vector<int> freq(mod, 0);
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int curr = s[i] - '0';
    vector<int> X(mod, 0);
    for (int j = 0; j < mod; ++j) {
      if (freq[j] == 0) {
        continue;
      }
      int num = j;
      num *= 10;
      num %= 2019;
      num += curr;
      num %= 2019;
      X[num] = freq[j];
    }
    X[curr]++;
    ans += X[0];
    swap(freq, X);
  }

  cout << ans;
}

int32_t main() {
  _

      int t = 1;
  // cin >> t;

  while (t--) {
    solve();
    cout << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define int long long
const int inf = 0x3f3f3f3f3f3f3f3LL;
const int mod = (int)1e9 + 7;
using namespace std;
#if LOCAL
#include "../../tr.h"
#else
#define tr(...)
#define endl '\\n'
#endif
template <class T> void smin(T &a, T val) {
  if (a > val)
    a = val;
}
template <class T> void smax(T &a, T val) {
  if (a < val)
    a = val;
}
const int N = 5 * (int)1e5 + 10;

void solve() {
  string s;

  cin >> s;
  int n = (int)(s.size());

  map<int, int> M;

  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int curr = s[i] - '0';
    map<int, int> X;
    for (auto it : M) {
      int num = it.first;
      num *= 10;
      num %= 2019;
      num += curr;
      num %= 2019;
      X[num] = it.second;
    }
    X[curr]++;
    ans += X[0];
    swap(M, X);
  }

  cout << ans;
}

int32_t main() {
  _

      int t = 1;
  // cin >> t;

  while (t--) {
    solve();
    cout << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;
typedef long long ll;
const long long MOD = 1e09 + 7;

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define pb push_back

int factorial(int n) {
  if (n == 0) {
    return 1;
  }
  if (n == 1) {
    return 10;
  }
  if (n % 2 == 0) {
    return (factorial(n / 2) * factorial(n / 2)) % 2019;
  }
  if (n % 2 == 1) {
    return (factorial(n / 2) * factorial(n / 2) * 10) % 2019;
  }
}

int main() {

  string s;
  cin >> s;

  ll n = int(s.size());

  vector<ll> data(n + 1);

  data.at(n) = 0;
  data.at(n - 1) = (s.at(n - 1) - '0');

  for (int i = n - 2; i >= 0; i--) {
    data.at(i) =
        (data.at(i + 1) + (factorial(n - 1 - i) * (s.at(i) - '0'))) % 2019;
  }

  map<ll, ll> cnt;
  rep(i, n + 1) {
    if (cnt.count(data.at(i))) {
      cnt.at(data.at(i))++;
    } else {
      cnt[data.at(i)] = 1;
    }
  }

  ll answer = 0;
  for (auto mypair : cnt) {
    answer += (mypair.second) * (mypair.second - 1) / 2;
  }

  cout << answer << endl;
}


Error: 0

Fixed submission:

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

int main() {

  string s;
  cin >> s;

  int n;
  n = int(s.size());

  vector<int> sum(n);

  sum.at(0) = int(s.at(0) - '0') % 3;

  for (int i = 1; i < n; i++) {
    sum.at(i) = (sum.at(i - 1) + int(s.at(i) - '0')) % 3;
  }

  int answer = 0;

  ///////0桁目からj(>=3)桁目までの数字で2019で割り切れるものを調べる

  ////0桁目から3桁目までをxとおく

  int x = (1000 * (int(s.at(0) - '0')) + 100 * int(s.at(1) - '0') +
           10 * int(s.at(2) - '0') + int(s.at(3) - '0')) %
          2019;

  for (int j = 3; j < n; j++) {
    if (sum.at(j) % 3 == 0) {
      if (x == 0) {
        answer += 1;
      }
    }

    if (j != n - 1) {
      x = (10 * x + int(s.at(j + 1) - '0')) % 2019;
    }
  }

  for (int i = 1; i < n - 3; i++) {

    int y = (1000 * (int(s.at(i) - '0')) + 100 * int(s.at(i + 1) - '0') +
             10 * int(s.at(i + 2) - '0') + int(s.at(i + 3) - '0')) %
            2019;
    for (int j = i + 3; j < n; j++) {
      if ((sum.at(j) - sum.at(i - 1)) % 3 == 0) {
        if (y == 0) {
          answer += 1;
        }
      }

      if (j != n - 1) {
        y = (10 * y + int(s.at(j + 1) - '0')) % 2019;
      }
    }
  }

  cout << answer << endl;

  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 < (n); i++)

typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
// 定数
#define INF 1000000000000 // 10^12:極めて大きい値,∞
// 略記
#define PB push_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first      // pairの一つ目の要素
#define S second     // pairの二つ目の要素

int main() {
  int mod = 2019;
  string S;
  cin >> S;
  int n;
  n = S.size();
  reverse(S.begin(), S.end());
  map<int, int> M;
  int mul = 1;
  int pre = 0;
  M[0]++;
  rep(i, n) {
    int t = pre + (S[i] - '0') * mul;
    t %= mod;
    M[t]++;
    mul = (mul * 10) % mod;
    pre = t;
    //       cout<<t<<" ";
  }
  //    cout<<endl;
  ll ans = 0;
  for (pair<int, int> p : M) {
    ans += p.S * (p.S - 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 < (n); i++)

typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
// 定数
#define INF 1000000000000 // 10^12:極めて大きい値,∞
// 略記
#define PB push_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first      // pairの一つ目の要素
#define S second     // pairの二つ目の要素

int main() {
  int mod = 2019;
  string S;
  cin >> S;
  int n;
  n = S.size();
  reverse(S.begin(), S.end());
  vector<int> T(n + 1);
  int mul = 1;
  T[0] = 0;
  rep(i, n + 1) {
    T[i + 1] = T[i] + (S[i] - '0') * mul;
    T[i] %= mod;
    mul = (mul * 10) % mod;
    //        cout<<T[i]<<" ";
  }
  //    cout<<endl;
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    rep(j, i) {
      if (T[i] == T[j])
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for (int(i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for (int(i) = (n); (i) < (m); ++(i))

constexpr int INF = 1e9;
// constexpr ll INF = 1LL<<61;
//  constexpr ll mod = 1e9+7;
constexpr int mod = 2019;

int main() {
  string S;
  cin >> S;
  int N = S.size();
  // cout << "N:" << N << endl;
  vector<int> cnt(2020, 0);
  int sum = 0;
  int beki = 1;
  ll ans = 0;
  REPR(i, N - 1) {
    sum = sum + (S[i] - '0') * beki;
    sum %= mod;
    beki *= 10;
    beki %= mod;
    ans += cnt[sum];
    cnt[sum]++;
    // cout << S.substr(i, N-i) << " " << sum << endl;
    if (sum == 0) {
      ans++;
    }
  }
  cout << ans << endl;
  // cout << 18171 % mod << endl;
  // cout << 81712114LL % mod << endl;
  // cout << 1817181712114LL % mod << endl;
  // cout << 1817100000000LL % mod << endl;
  return 0;
}


Error: MLE

Fixed submission:

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

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for (int(i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for (int(i) = (n); (i) < (m); ++(i))

constexpr int INF = 1e9;
// constexpr ll INF = 1LL<<61;
//  constexpr ll mod = 1e9+7;
constexpr int mod = 2019;

int main() {
  string S;
  cin >> S;
  int N = S.size();
  // cout << "N:" << N << endl;
  vector<vector<int>> cnt(N + 1, vector<int>(2020, 0));
  int sum = 0;
  int beki = 1;
  ll ans = 0;
  REPR(i, N - 1) {
    sum = sum + (S[i] - '0') * beki;
    sum %= mod;
    beki *= 10;
    beki %= mod;
    cnt[i][sum]++;
    if (N - i >= 4) {
      ans += cnt[i + 4][sum];
    }
    // cout << S.substr(i, N-i) << " " << sum << endl;
    if (sum == 0) {
      ans++;
    }
    REP(j, 2019) { cnt[i][j] += cnt[i + 1][j]; }
  }
  cout << ans << endl;
  // cout << 18171 % mod << endl;
  // cout << 81712114LL % mod << endl;
  // cout << 1817181712114LL % mod << endl;
  // cout << 1817100000000LL % mod << endl;
  return 0;
}