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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define show(a, n)                                                             \\
  rep(i, n) { cout << a[i] << ' '; }                                           \\
  cout << endl
#define show2(a, n, m)                                                         \\
  rep(i, n) {                                                                  \\
    rep(j, m) { cout << a[i][j] << ' '; }                                      \\
    cout << endl;                                                              \\
  }
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<long long, long long> pll;
typedef long double ld;

const int INF = 1 << 30;
const long long INFL = 1LL << 60;
const int MAX = 100000;
const int N = 100010;

const int MOD = 2019;
struct mint {
  ll x;
  mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

int main() {
  //	cout << fixed << setprecision(15);
  string s;
  cin >> s;
  map<ll, int> mp;
  ll ans = 0;
  mint base = 1;
  mint comp = 0;
  mp[comp.x]++;
  rep(i, s.size()) {
    mint x = (s[i] - '0');
    comp -= x * base;
    base *= mint(202);
    ans += mp[comp.x];
    mp[comp.x]++;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define show(a, n)                                                             \\
  rep(i, n) { cout << a[i] << ' '; }                                           \\
  cout << endl
#define show2(a, n, m)                                                         \\
  rep(i, n) {                                                                  \\
    rep(j, m) { cout << a[i][j] << ' '; }                                      \\
    cout << endl;                                                              \\
  }
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<long long, long long> pll;
typedef long double ld;

const int INF = 1 << 30;
const long long INFL = 1LL << 60;
const int MOD = 1000000007;
const int MAX = 100000;
const int N = 100010;

int main() {
  //	cout << fixed << setprecision(15);
  string s;
  cin >> s;
  map<int, int> mp[2];
  ll ans = 0;
  rep(i, s.size()) {
    int prev = i % 2;
    int next = 1 - prev;
    mp[next].clear();
    int x = s[i] - '0';
    for (auto i = mp[prev].begin(); i != mp[prev].end(); i++) {
      int idx = (i->first * 10 + x) % 2019;
      mp[next][idx] = i->second;
    }
    mp[next][x]++;
    ans += mp[next][0];
  }
  cout << ans << endl;
}