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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>
#include <bitset>
#include <complex>
#include <cstdio>
#include <list>

//< in.txt > out.txt
using namespace std;
// std::ios::sync_with_stdio(false);
// std::cin.tie(0);
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
typedef long long LL;
typedef long double LD;
typedef pair<LL, LL> PLL;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned long long ULL;
// typedef boost::multiprecision::cpp_int bigint;

LL modpow(LL base, LL p) {
  if (p == 0)
    return 1;
  if (p == 1)
    return base;
  LL ans = modpow(base, p >> 1);
  ans *= ans;
  if (p & 1)
    ans *= base;
  return ans % 2019;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  string sori;
  cin >> sori;
  VLL S(sori.size() + 2, 0);
  LL N = sori.size();
  S[N] = sori.back() - '0';
  for (LL n = N - 1; n > 0; n--) {
    S[n] = (S[n + 1] + modpow(10, N - n) * (sori[n - 1] - '0')) % 2019;
  }
  VLL count(2019, 0);
  count[0] = 1;
  LL ans = 0;
  for (LL n = N; n >= 1; n--) {
    ans += count[S[n]];
    count[S[n]]++;
  }
  cout << ans << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>
#include <bitset>
#include <complex>
#include <cstdio>
#include <list>

//< in.txt > out.txt
using namespace std;
// std::ios::sync_with_stdio(false);
// std::cin.tie(0);
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
typedef long long LL;
typedef long double LD;
typedef pair<LL, LL> PLL;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned long long ULL;
// typedef boost::multiprecision::cpp_int bigint;

LL modpow(LL base, LL p) {
  if (p == 0)
    return 1;
  if (p == 1)
    return base;
  LL ans = modpow(base, p >> 1);
  ans *= ans;
  if (p & 1)
    ans *= base;
  return ans % 2019;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  string sori;
  cin >> sori;
  VLL S(sori.size() + 2, 0);
  LL N = sori.size();
  S[N] = sori.back() - '0';
  for (LL n = N - 1; n > 0; n--) {
    S[n] = (S[n + 1] + modpow(10, N - n) * (sori[n - 1] - '0')) % 2019;
  }
  VVLL count(2019, VLL(N + 2, 0));
  for (LL n = 1; n <= N + 1; n++) {
    count[S[n]][n]++;
  }
  for (LL mod = 0; mod < 2019; mod++) {
    for (LL n = 2; n <= N + 1; n++) {
      count[mod][n] += count[mod][n - 1];
    }
  }
  LL ans = 0;
  for (LL n = N; n >= 1; n--) {
    ans += count[S[n]][N + 1] - count[S[n]][n];
  }
  cout << ans << "\\n";
  return 0;
}