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

#include <algorithm>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <queue>
#include <set>
#include <vector>
// #include <bits/stdc++.h>
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;
#define ALL(X) X.begin(), X.end()
using ll = long long int;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
const int MOD = 1000000007;
// const int MOD=998244353;
const int INTMAX = 2147483647;
const ll LLMAX = 9223372036854775807;

ll modadd(ll a, ll b, ll mod = MOD) { return (a % mod + b % mod) % mod; }

ll modsub(ll a, ll b, ll mod = MOD) {
  a %= mod;
  b %= mod;
  if (a >= b)
    return a - b;
  else
    return mod - b + a;
}

ll modmul(ll a, ll b, ll mod = MOD) { return ((a % mod) * (b % mod)) % mod; }

ll modpow(ll a, ll n, ll mod = MOD) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll moddiv(ll a, ll b, ll mod = MOD) { return modmul(a, modpow(b, mod - 2)); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  string S;
  cin >> S;
  ll N = S.size();

  vll T(N + 1);
  T[N] = 0;
  ll pow10 = 1;
  for (ll i = N - 1; i >= 0; i--) {
    T[i] = modadd(modmul(S[i] - '0', pow10, 2019), T[i + 1], 2019);
    pow10 = modmul(10, pow10, 2019);
    // printf("T[%d]=%d\\n",i,T[i]);
  }

  vll count(2019);
  for (ll i = 0; i < N; i++) {
    count[T[i]]++;
  }

  ll ans = count[0];
  for (ll i = 0; i < 2019; i++) {
    ans += count[i] * (count[i] - 1) / 2;
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <queue>
#include <set>
#include <vector>
// #include <bits/stdc++.h>

template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using namespace std;
#define ALL(X) X.begin(), X.end()
using ll = long long int;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
const int MOD = 1000000007;
// const int MOD=998244353;
const int INTMAX = 2147483647;
const ll LLMAX = 9223372036854775807;

ll modadd(ll a, ll b, ll mod = MOD) { return (a % mod + b % mod) % mod; }

ll modsub(ll a, ll b, ll mod = MOD) {
  a %= mod;
  b %= mod;
  if (a >= b)
    return a - b;
  else
    return mod - b + a;
}

ll modmul(ll a, ll b, ll mod = MOD) { return ((a % mod) * (b % mod)) % mod; }

ll modpow(ll a, ll n, ll mod = MOD) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll moddiv(ll a, ll b, ll mod = MOD) { return modmul(a, modpow(b, mod - 2)); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  string S;
  cin >> S;

  if (S.size() <= 3) {
    cout << 0 << endl;
    return 0;
  }

  vvvll dp(S.size(), vvll(2019, vll(2, 0)));
  dp[0][S[0] - '0'][1] = 1;

  for (ll i = 0; i < S.size() - 1; i++) {
    int X = S[i + 1] - '0';
    dp[i + 1][X][1] = 1;
    for (ll j = 0; j < 2019; j++) {
      dp[i + 1][(j * 10 + X) % 2019][1] += dp[i][j][1];
      dp[i + 1][j][0] = dp[i][j][0] + dp[i][j][1];
    }
  }
  cout << dp[S.size() - 1][0][0] + dp[S.size() - 1][0][1] << endl;

  return 0;
}