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

#include "bits/stdc++.h"
using namespace std;
// #define MOD 1000000007
// #define MOD 998244353
#define MOD 2019
const double EPS = 1e-9;
#define INF (1LL << 60)
#define D double
#define fs first
#define sc second
#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) FOR(i, 0, (n))
#define RREP(i, n) RFOR(i, 0, (n))
#define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i, a, b) ((a) <= (i) && (i) < (b))
#define debug(x) cout << #x << " = " << (x) << endl;
#define SP << " " <<
typedef pair<int, int> P;
typedef vector<int> vec;
typedef vector<vector<int>> mat;

void add(int &a, int b) { a = ((a + b) % MOD + MOD) % MOD; }

inline int mul(int a, int b) {
  add(a, 0);
  add(b, 0);
#if !defined(_WIN32) || defined(_WIN64)
  return (int)((long long)a * b % MOD);
#endif
  unsigned long long x = (long long)a * b;
  unsigned xh = (unsigned)(x >> 32), xl = (unsigned)x, d, m;
  asm("divl %4; \\n\\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(MOD));
  return m;
}

inline int modpow(int a, long long b) {
  int res = 1;
  while (b > 0) {
    if (b & 1) {
      res = mul(res, a);
    }
    a = mul(a, a);
    b >>= 1;
  }
  return res;
}

inline int inv(int a) {
  a %= MOD;
  if (a < 0)
    a += MOD;
  int b = MOD, u = 0, v = 1;
  while (a) {
    int t = b / a;
    b -= t * a;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  assert(b == 1);
  if (u < 0)
    u += MOD;
  return u;
}

inline int modstoll(string s) {
  int ans = 0;
  RREP(i, (int)ceil(s.length() / 17.0)) {
    int t = min((int)s.length() - i * 17, 17ll);
    ans += stoll(s.substr(i * 17, t % MOD)) * modpow(10, i * t);
  }
  return ans;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);

  string s;
  cin >> s;

  vec a(2019, 0);
  a[0]++; // Tn mod 2019 == 0

  int t = 0;
  REP(i, s.length()) {
    const char c = s[s.length() - i - 1];
    t += atoll(&c) * modpow(10, i);
    a[t % 2019]++;
  }

  int ans = 0;
  REP(i, a.size()) { ans += a[i] * (a[i] - 1) / 2; }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
const double EPS = 1e-9;
#define INF (1LL << 60)
#define D double
#define fs first
#define sc second
#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) FOR(i, 0, (n))
#define RREP(i, n) RFOR(i, 0, (n))
#define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i, a, b) ((a) <= (i) && (i) < (b))
#define debug(x) cout << #x << " = " << (x) << endl;
#define SP << " " <<
typedef pair<int, int> P;
typedef vector<int> vec;
typedef vector<vector<int>> mat;

void add(int &a, int b) { a = ((a + b) % MOD + MOD) % MOD; }

inline int mul(int a, int b) {
  add(a, 0);
  add(b, 0);
#if !defined(_WIN32) || defined(_WIN64)
  return (int)((long long)a * b % MOD);
#endif
  unsigned long long x = (long long)a * b;
  unsigned xh = (unsigned)(x >> 32), xl = (unsigned)x, d, m;
  asm("divl %4; \\n\\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(MOD));
  return m;
}

inline int modpow(int a, long long b) {
  int res = 1;
  while (b > 0) {
    if (b & 1) {
      res = mul(res, a);
    }
    a = mul(a, a);
    b >>= 1;
  }
  return res;
}

inline int inv(int a) {
  a %= MOD;
  if (a < 0)
    a += MOD;
  int b = MOD, u = 0, v = 1;
  while (a) {
    int t = b / a;
    b -= t * a;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  assert(b == 1);
  if (u < 0)
    u += MOD;
  return u;
}

inline int modstoll(string s) {
  int ans = 0;
  RREP(i, (int)ceil(s.length() / 17.0)) {
    int t = min((int)s.length() - i * 17, 17ll);
    ans += stoll(s.substr(i * 17, t % MOD)) * modpow(10, i * t);
  }
  return ans;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);

  string s;
  cin >> s;

  vec a(2019, 0);
  a[0]++; // Tn mod 2019 == 0

  string t = "";
  REP(i, s.length()) { t += "0"; }
  REP(i, s.length()) {
    t[i] = s[i];
    a[modstoll(t) % 2019]++;
  }

  int ans = 0;
  REP(i, a.size()) { ans += a[i] * (a[i] - 1) / 2; }

  cout << ans << endl;

  return 0;
}