Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

// https://atcoder.jp/contests/abc164/tasks/abc164_d
#include <bits/stdc++.h>
#if LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif
using namespace std;
using ll = long long;
const ll LINF = 0x1fffffffffffffff;
#define FOR(i, a, b) for (ll i = (a); i < static_cast<ll>(b); ++i)
#define REP(i, n) FOR(i, 0, n)
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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
// https://atcoder.jp/contests/agc023/tasks/agc023_a
// zero-sum rangesにちかい
int main() {
#if LOCAL & 01
  // std::ifstream in("./test/sample-1.in");
  std::ifstream in("./input.txt");
  std::cin.rdbuf(in.rdbuf());
#else
  cin.tie(0);
  ios::sync_with_stdio(false);
#endif
  string S;
  cin >> S;
  reverse(S.begin(), S.end());
  dump(18171 % 2019);
  dump(S, 2019123 % 2019);

  // Aj*10^n,..,Ai+2*10^2,Ai+1*10,Ai
  ll sum = 0;
  unordered_map<ll, ll> tbl;
  ll rate = 1;
  tbl[sum]++;
  FOR(i, 0, S.size()) {
    ll digit = S.at(i) - '0';
    sum = sum + digit * rate;
    sum %= 2019;
    tbl[sum]++;
    rate = (rate * 10) % 2019;
  }
  dump(tbl);
  ll ans = 0;
  for (auto &p : tbl) {
    if (p.second >= 2) {
      ans += p.second * (p.second - 1) / 2;
    }
  }
  cout << ans << endl;

  return 0;
}

int main_() {
#if LOCAL & 01
  std::ifstream in("./test/sample-1.in");
  // std::ifstream in("./input.txt");
  std::cin.rdbuf(in.rdbuf());
#else
  cin.tie(0);
  ios::sync_with_stdio(false);
#endif
  string S;
  cin >> S;

  auto getval = [&](ll ii, ll jj) {
    ll v = 0;
    FOR(p, ii, jj + 1) {
      v *= 10;
      v += S.at(p) - '0';
    }
    return v;
  };

  ll ans = 0;
  FOR(i, 0, (ll)S.size()) {
    if (i + 2 >= (ll)S.size())
      break;
    ll v = getval(i, i + 2);
    FOR(j, i + 3, (ll)S.size()) {
      if (j - i >= 4 - 1) {
        v *= 10;
        v += S.at(j) - '0';
        //        dump(i,j,v);
        if (v % 2019 == 0) {
          ++ans;
          dump(v);
        }
      } else {
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

// https://atcoder.jp/contests/abc164/tasks/abc164_d
#include <bits/stdc++.h>
#if LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif
using namespace std;
using ll = long long;
const ll LINF = 0x1fffffffffffffff;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
#if LOCAL & 01
  std::ifstream in("./test/sample-1.in");
  // std::ifstream in("./input.txt");
  std::cin.rdbuf(in.rdbuf());
#else
  cin.tie(0);
  ios::sync_with_stdio(false);
#endif
  string S;
  cin >> S;

  auto getval = [&](ll ii, ll jj) {
    ll v = 0;
    FOR(p, ii, jj + 1) {
      v *= 10;
      v += S.at(p) - '0';
    }
    return v;
  };

  ll ans = 0;
  FOR(i, 0, (ll)S.size()) {
    FOR(j, i + 3, (ll)S.size()) {
      if (j - i >= 4 - 1) {
        ll v = getval(i, j);
        dump(i, j, v);
        if (v % 2019 == 0)
          ++ans;
      }
    }
  }
  cout << ans << endl;
  return 0;
}