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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#define ll long long
using namespace std;
int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  ll n = s.length();
  ll now = 0;
  ll d = 1;
  map<ll, ll> rmap;
  rmap[0]++;
  for (ll i = 0; i < n; i++) {
    now = (now + (s[i] - '0') * d) % 2019;
    rmap[now]++;
    d *= 10;
    d %= 2019;
  }

  ll ans = 0;
  for (const auto &imap : rmap) {
    ans += imap.second * (imap.second - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#define ll long long
using namespace std;
int main() {
  string s;
  cin >> s;
  ll n = s.length();
  vector<ll> x(n + 1, 0);
  for (ll i = 0; i < n; i++) {
    for (ll j = i; j < n; j++) {
      x[i] += (s[j] - '0') * pow(10, n - j - 1);
    }
  }
  map<ll, ll> rmap;
  for (ll i = 0; i < n + 1; i++) {
    rmap[x[i] % 2019]++;
  }

  ll ans = 0;
  for (const auto &imap : rmap) {
    ans += imap.second * (imap.second - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

const int mod = 2019;

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

  // 1の位から累積和を求めていく
  int total = 0;
  int ten = 1; // 10の何乗
  long long ans = 0;
  std::vector<int> cnt(mod);
  for (int i = 0; i < s.size(); i++) {
    cnt[total]++;
    total += (s[i] - '0') * ten;
    total %= mod;
    ten = ten * 10 % mod;
    // 自分より位が小さい値を見る
    ans += cnt[total];
  }

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

Error: 0

Fixed submission:

#include <iostream>
#include <string>

int main() {
  std::string s;
  std::cin >> s;
  long long ans = 0;
  for (long long i = 0; i <= s.size() - 4; i++) {
    for (long long j = i + 3; j < s.size(); j++) {
      std::string tmp = s.substr(i, j - i + 1);
      long long num = std::stol(tmp);
      if (num % 2019 == 0) {
        ans++;
      }
    }
  }
  std::cout << ans << std::endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll zero = 0;
ll INF = 10000000000;

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

  vector<int> mod10(N + 1); // 10のi乗を2019で割ったあまり
  mod10.at(0) = 1;
  for (int i = 1; i < N + 1; i++) {
    mod10.at(i) = (mod10.at(i - 1) * 10) % 2019;
  }

  vector<int> mod(N + 1); // 下からi桁目までを2019で割ったあまり
  mod.at(0) = 0;
  int tmp;
  for (int i = 1; i < N + 1; i++) {
    tmp = S.at(N - i) - '0';
    mod.at(i) = (mod.at(i - 1) + tmp * mod10.at(i - 1)) % 2019;
  }

  vector<int> count(2019, 0); // 配列modで中身がiなものの個数
  for (int i = 0; i < N + 1; i++) {
    count.at(mod.at(i))++;
  }

  ll ans = 0;
  for (int i = 0; i < 2019; i++) {
    ans += count.at(i) * (count.at(i) - 1) / 2;
  }
  printf("%lld\\n", ans);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll zero = 0;
ll INF = 10000000000;

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

  vector<int> mod10(2020); // 10のi乗を2019で割ったあまり
  mod10.at(0) = 1;
  for (int i = 1; i < 2020; i++) {
    mod10.at(i) = (mod10.at(i - 1) * 10) % 2019;
  }

  vector<int> mod(N + 1); // 下からi桁目までを2019で割ったあまり
  mod.at(0) = 0;
  int tmp;
  for (int i = 1; i < N + 1; i++) {
    tmp = S.at(N - i) - '0';
    mod.at(i) = (mod.at(i - 1) + tmp * mod10.at(i - 1)) % 2019;
  }

  vector<int> count(2019, 0); // 配列modで中身がiなものの個数
  for (int i = 0; i < N + 1; i++) {
    count.at(mod.at(i))++;
  }

  ll ans = 0;
  for (int i = 0; i < 2019; i++) {
    ans += count.at(i) * (count.at(i) - 1) / 2;
  }
  printf("%lld\\n", ans);
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()

typedef long long lint;

using namespace std;
lint p = 2019;

int main() {
  string S, SR;
  cin >> S;
  SR = string(S.rbegin(), S.rend());
  int N = S.size();
  map<int, int> m;
  lint x = 0;
  lint pow10 = 1;
  m[0]++;
  for (auto c : SR) {
    x += ((c - '0') * pow10);
    x %= p;
    m[x]++;
    pow10 *= 10;
    pow10 %= p;
    // cerr<<x<<endl;
  }
  lint ans = 0;
  for (auto e : m) {
    // cerr<<e.first<<" "<<e.second<<endl;
    lint y = e.second;
    ans += y * (y - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()

typedef long long lint;

using namespace std;

int main() {
  string S;
  cin >> S;
  int N = S.size();
  vector<map<lint, int>> dp(N);
  dp[0][S[0] - '0']++;
  lint ans = 0;
  FOR(i, 1, N) {
    int p = S[i] - '0';
    dp[i][p]++;
    for (auto e : dp[i - 1]) {
      dp[i][(p + 10 * e.first) % 673] += e.second;
    }
    ans += dp[i][0];
  }
  // REP(i,N){
  //   REP(j,20)cerr<<dp[i][j]<<" ";
  //   cerr<<endl;
  // }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void solve() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  ll ans = 0, x = 0, mul = 1;
  map<ll, ll> m;
  m[0] = 1;
  for (ll i = 0; i < s.size(); i++) {
    x = (x + ((ll)(s[i] - '0') * mul)) % 2019;
    ans += m[x];
    m[x]++;
    mul = (mul * 10) % 2019;
  }
  cout << ans;
}

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

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

void solve() {
  string s, pattern;
  cin >> s;
  ll x = 2019, ans = 0;
  while (x <= INT_MAX) {
    pattern = to_string(x);
    ll found = s.find(pattern);
    while (found != string::npos) {
      ans++;
      found = s.find(pattern, found + 1);
    }
    x += 2019;
  }
  cout << ans;
}

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _DEBUG 1
#define _GLIBCXX_DEBUG

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

using ll = long long;

#ifdef _DEBUG
#define dump(x) cerr << #x << "=" << x << endl
#define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define dump3(x, y, z)                                                         \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define check(s) cerr << s << endl
#else
#define dump(x)
#define dump2(x, y)
#define dump3(x, y, z)
#define check(s)
#endif

#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, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

const ll LINF = 2e18;
const int INF = 1e9;

void solve(string S) {
  int N = S.size();
  map<int, int> mp;
  mp[0] = 1;

  int p = 0;
  int ten = 1;
  repr(i, N - 1) {
    p = (p + (S.at(i) - '0') * ten) % 2019;
    ten *= 10;
    ten %= 2019;
    mp[p]++;
  }
  int cnt = 0;
  for (auto v : mp) {
    cnt += v.second * (v.second - 1) / 2;
  }
  cout << cnt << endl;
}

int main() {
  string S;
  cin >> S;
  solve(S);
  return 0;
}


Error: TLE

Fixed submission:

#define _DEBUG 1
#define _GLIBCXX_DEBUG

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

using ll = long long;

#ifdef _DEBUG
#define dump(x) cerr << #x << "=" << x << endl
#define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define dump3(x, y, z)                                                         \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define check(s) cerr << s << endl
#else
#define dump(x)
#define dump2(x, y)
#define dump3(x, y, z)
#define check(s)
#endif

#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, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

const ll LINF = 2e18;
const int INF = 1e9;

void solve(string S) {
  int N = S.size();
  int cnt = 0;
  rep(i, N + 1) {
    if (S.substr(i, 4).empty()) {
      continue;
    }
    string sp = to_string(stoll(S.substr(i, 4)) % 2019);
    for (int j = i + 4; j < N + 1; j++) {
      string sb = sp + S.substr(j, 1);
      ll result = stoll(sb) % 2019;
      if (result == 0 && stoll(sb) != 0) {
        cnt++;
      }
      sp = to_string(result);
    }
  }

  cout << cnt << endl;
}

int main() {
  string S;
  cin >> S;
  solve(S);
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define pb push_back
#define F first
#define S second
#define pi pair<int, int>
#define all(v) v.begin(), v.end()
const int MOD = 1e9 + 7;
template <typename A> istream &operator>>(istream &input, vector<A> &x) {
  for (auto &i : x)
    input >> i;
  return input;
}
template <typename A> ostream &operator<<(ostream &output, vector<A> &x) {
  for (auto &i : x)
    output << i << ' ';
  return output;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  // cin>>T;
  while (T--) {
    string s;
    cin >> s;
    int ans = 0;
    vector<int> cnt(2020, 0);
    int x = 1;
    int n = s.size();
    cnt[0] = 1;
    int num = 0;
    for (int i = n - 1; i >= 0; i--) {
      num += (s[i] - '0') * x;
      num %= 2019;
      ans += cnt[num];
      cnt[num]++;
      x = (x * 10) % 2019;
    }
    cout << ans << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  string s;
  cin >> s;
  int sum = 0;
  int k = 673;
  int n = s.length();

  vector<unordered_map<int, int>> v(n);

  for (int i = 0; i < n; i++) {
    int rem = 0;
    int pref = 0;
    int sum2 = 0;
    for (int j = i; j < n; j++) {
      rem = (10 * rem + (s[j] - '0')) % k;
      sum2 += (s[j] - '0');
      if (v[j][rem])
        break;
      v[j][rem]++;
      if (rem == 0 && sum2 % 3 == 0) {
        pref++;
      }
    }
    sum += (pref * (pref + 1)) / 2;
  }
  cout << sum << "\\n";
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, p;
  // cin >> n >> p;
  string s;

  cin >> s;
  n = s.size();
  p = 2019;
  if (10 % p == 0) { // 2,5
    ll ans = 0;
    rep(r, n) {
      if ((s[r] - '0') % p == 0) {
        ans += r + 1;
      }
    }
    cout << ans << endl;
    return 0;
  }

  vector<int> d(n + 1);
  int ten = 1;
  for (int i = n - 1; i >= 0; --i) {
    int a = (s[i] - '0') * ten % p;
    d[i] = (d[i + 1] + a) % p;
    ten *= 10;
    ten %= p;
  }
  vector<int> cnt(p);
  ll ans = 0;
  for (int i = n; i >= 0; --i) {
    ans += cnt[d[i]];
    cnt[d[i]]++;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#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(n) begin(n), end(n)
#define sz(x) int(x.size())
using ll = long long;
const ll LLINF = numeric_limits<long long>::max();
const int mod = 1000000007;
const int INF = 1001001001;

int main() {
  string S;
  cin >> S;
  ll ans = 0;
  int l = S.size();
  for (int i = 0; i < l - 4; i++) {
    ll a = S[i] - '0';
    for (int j = i + 1; j < l; j++) {
      a *= 10;
      a += S[j] - '0';
      // cout << a <<endl;
      if (a % 2019 == 0) {
        // cout << i << " " << j <<endl;
        ans++;
      }
      a %= 2019;
    }
  }
  cout << ans;
  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 rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using ll = long long;
using VL = vector<ll>;
using VVL = vector<vector<ll>>;
using P = pair<ll, ll>;

// chmin, chmax関数
template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
  if (comp(xmax, x)) {
    xmax = x;
    return true;
  }
  return false;
}
template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
  if (comp(x, xmin)) {
    xmin = x;
    return true;
  }
  return false;
}

// Yes-Noを出力する問題で楽をする
void Ans(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

//---------------------------

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

  VL mods(2019, 0);
  ll rem = 0, tens = 1;

  rep(i, n) {
    ll d = s[n - 1 - i] - '0';
    rem = (rem + tens * d) % 2019;
    tens = (tens * 10) % 2019;
    mods[rem]++;
  }

  mods[0]++;

  ll ans = 0;
  rep(i, 2019) {
    ll m = mods[i];
    ans += m * (m - 1) / 2;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using ll = long long;
using VL = vector<ll>;
using VVL = vector<vector<ll>>;
using P = pair<ll, ll>;

// chmin, chmax関数
template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
  if (comp(xmax, x)) {
    xmax = x;
    return true;
  }
  return false;
}
template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
  if (comp(x, xmin)) {
    xmin = x;
    return true;
  }
  return false;
}

// Yes-Noを出力する問題で楽をする
void Ans(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

//---------------------------

int main() {
  string s;
  cin >> s;
  ll n = s.size();
  set<ll> mods;
  mods.insert(0);
  ll ans = 0;

  rep(i, n) {
    ll d = s[i] - '0';
    set<ll> tmp;
    tmp.insert(0);
    for (auto x : mods) {
      ll y = (10 * x + d) % 2019;
      if (y == 0)
        ans++;
      tmp.insert(y);
    }

    mods = tmp;
  }

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

#include <bits/stdc++.h>
#define INF 1e9
using namespace std;

#define REPR(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()
#define endl "\\n"

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;

void solve() {
  string S;
  cin >> S;
  int ans = 0;
  vector<int> ntm(2019, 0);
  for (const auto c : S) {
    vector<int> stm(2019, 0);
    REP(j, 2019) stm[(j * 10 + (c - '0')) % 2019] += ntm[j];
    stm[c - '0']++;
    ans += stm[0];
    ntm = stm;
  }
  cout << ans << endl;
}

int main() {
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 1e9
using namespace std;

#define REPR(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()
#define endl "\\n"

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;

vector<int> create_mod_tbl() {
  vector<int> md_tbl(200001);
  md_tbl[0] = 0;
  md_tbl[1] = 1;
  FOR(i, 2, 200001) { md_tbl[i] = (md_tbl[i - 1] * 10) % 2019; }
  return md_tbl;
}

void solve() {
  auto tbl = create_mod_tbl();
  string S;
  cin >> S;
  int count = 0;
  REPR(i, S.size() - 1) {
    int now = 0;
    for (auto j = i; j >= 0; j--) {
      int x = S[j] - '0';
      int length = i - j + 1;
      now += (x * tbl[length]) % 2019;
      //  cerr << "now: " << now << " i: "  <<  i << " j: " << j  << " x: " <<
      //  x;
      // cerr << " tbl: "<< tbl[length] << "length: " << length << endl;
      if (now % 2019 == 0)
        count++;
      now %= 2019;
    }
  }
  cout << count << endl;
}

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

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

int main() {
  int cnt = 0, rec[2020] = {0};
  string s;
  cin >> s;
  vector<int> a(s.size());
  int now = 1;
  for (int i = a.size() - 1; i >= 0; --i) {
    a[i] = now * (s[i] - '0') % 2019;
    now = now * 10 % 2019;
  }
  now = 0;
  rec[0] = 1;
  for (int i = a.size() - 1; i >= 0; --i) {
    now = (now + a[i]) % 2019;
    cnt += rec[now];
    rec[now]++;
  }
  cout << cnt << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
  int cnt = 0;
  string s;
  map<string, int> m;
  cin >> s;
  for (long long i = 1ll; i <= 10000000ll; ++i) {
    long long tmp = i * 2019;
    m[to_string(i * 2019)] = 1;
  }
  int i = s.size() - 1;
  while (i >= 0) {
    int j = i;
    long long now = 0;
    while (j >= 0) {
      string em = s.substr(j, i - j + 1);
      if (m.count(em)) {
        cnt++;
        break;
      }
      j--;
    }
    i = j;
  }
  cout << cnt << 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)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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);
  ll ans = 0;
  rep(i, n) {
    cnt[tot]++;
    tot += (s[i] - '0') * x;
    tot %= m;
    ans += cnt[tot];
    x = x * 10 % m;
    // printf("x:%d tot:%d\\n",x, tot);
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int main(void) {
  string s;
  cin >> s;
  ll ans = 0;
  vector<int> nums;
  for (char c : s) {
    int num = c - '0';
    nums.emplace_back(num);
  }

  for (ll i = 0; i < nums.size() - 3; i++) {
    ll target = nums[i] * 100 + nums[i + 1] * 10 + nums[i + 2];
    for (ll j = i + 3; j < nums.size(); j++) {
      target = target * 10 + nums[j];
      if (target % 2019 == 0)
        ans++;
      // cout << target << endl;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end));                                         \\
       i += 1 - 2 * ((begin) >                                                 \\
                     (end))) // rep(i, 1, 5) --> {1,2,3,4} // rep(i, 5, 1) -->
                             // {4,3,2,1} // rep(it, end(v), begin(v)) --> *it

using namespace std;

int main() {
  string s;
  cin >> s;
  ll n = s.size();
  vector<ll> dp(n);
  map<ll, ll> mp;
  dp[n - 1] = s[n - 1] - '0';
  ll ten = 10;
  mp[0] = 1;
  mp[dp[n - 1]]++;
  for (ll i = n - 2; i >= 0; i--) {
    dp[i] = (dp[i + 1] + (s[i] - '0') * ten) % 2019;
    ten = (ten * 10) % 2019;
    mp[dp[i]]++;
  }
  ll ans = 0;
  for (auto x : mp)
    ans += x.second * (x.second - 1) / 2;
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end));                                         \\
       i += 1 - 2 * ((begin) >                                                 \\
                     (end))) // rep(i, 1, 5) --> {1,2,3,4} // rep(i, 5, 1) -->
                             // {4,3,2,1} // rep(it, end(v), begin(v)) --> *it

using namespace std;

int mod(string num, int a) {
  // Initialize result
  int res = 0;

  // One by one process all digits of 'num'
  for (int i = 0; i < num.length(); i++)
    res = (res * 10 + (int)num[i] - '0') % a;

  return res;
}

int main() {
  string s;
  cin >> s;
  ll N = s.size();
  ll res = 0;
  rep(len, 4, N + 1) rep(i, 0, N - len + 1) {
    string a = s.substr(i, len);
    if (mod(a, 2019) == 0)
      res++;
  }
  cout << res;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long int ll;

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

  ll mod = 0;
  ll next = 1;
  vector<ll> memo(2019, 0);
  memo[0] = 1;
  for (int i = n - 1; i >= 0; i--) {
    mod = (((s[i] - '0') * next) + mod) % 2019;
    next = (next * 10) % 2019;
    memo[mod]++;
  }
  ll ans = 0;
  for (int i = 0; i < (int)memo.size(); i++) {
    if (memo[i] <= 1) {
      continue;
    }
    ans += ((memo[i] - 1) * (memo[i] - 1 + 1)) / 2;
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long int ll;

// const double PI=3.14159265358979323846;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  vector<vector<ll>> memo = vector<vector<ll>>(n, vector<ll>(2019, 0));
  int sch = s[0] - '0';
  memo[0][sch] = 1;
  for (int i = 1; i < n; i++) {
    int v = s[i] - '0';
    memo[i][v] += 1;
    for (int j = 0; j < 2019; j++) {
      if (memo[i - 1][j] == 0) {
        continue;
      }
      int k = j;
      int next = (k * 10 + v) % 2019;
      memo[i][next] += memo[i - 1][j];
    }
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans += memo[i][0];
  }
  cout << ans << endl;

  return 0;
}