Problem p02702 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
//**デバッグ準備**//
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head h, Tail... t) {
  cerr << " " << h;
  if (sizeof...(t) > 0)
    cerr << " :";
  debug_out(t...);
}
template <typename T> ostream &operator<<(ostream &os, vector<T> vec) {
  for (size_t i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}
ll ctoi(char c) {
  switch (c) {
  case '0':
    return 0;
  case '1':
    return 1;
  case '2':
    return 2;
  case '3':
    return 3;
  case '4':
    return 4;
  case '5':
    return 5;
  case '6':
    return 6;
  case '7':
    return 7;
  case '8':
    return 8;
  case '9':
    return 9;
  default:
    return 0;
  }
}
bool pairCompare(const pll firstElof, pll secondElof) {
  return firstElof.first > secondElof.first;
}
//**Snippetリスト**//
// rep, vin, all, iteLoop//
// bitSearch, bitList, gcdlcm, isPrime, primeFactorize, Npow, divisor, modinv,
// dfs, bfs, eratos// dijkstra, WarshallFloyd, BellmanFord, UnionFind, COM,
// digitDP ,RMQ//
ll i, j, k, l;
ll N, M, K, H, W, L, X, Y, Z, R;
ll MOD = 1000000007;
ll INF = 1LL << 62;
ll ans = 0;
ll z = 0, o = 1;
vl flag, color, D;
vll path;
ll Npow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return Npow(x * x % 2019, n / 2);
  else
    return x * Npow(x, n - 1) % 2019;
}
int main() {
  string S;
  cin >> S;
  N = S.length();
  for (i = 5; i <= 100; i++) {
    // ll key = 0;
    for (j = 0; j <= N - i; j++) {
      ll key = 0;
      ll p = i - 1;
      for (k = j; k < j + i; k++) {
        ll Q = Npow(10, p);
        key = (key + ctoi(S[k]) * Q) % 2019;
        p--;
        // debug(i,j,k,key);
      }
      if (key % 2019 == 0)
        ans++;
    }
  }
  cout << ans;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
//**デバッグ準備**//
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head h, Tail... t) {
  cerr << " " << h;
  if (sizeof...(t) > 0)
    cerr << " :";
  debug_out(t...);
}
template <typename T> ostream &operator<<(ostream &os, vector<T> vec) {
  for (size_t i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}
ll ctoi(char c) {
  switch (c) {
  case '0':
    return 0;
  case '1':
    return 1;
  case '2':
    return 2;
  case '3':
    return 3;
  case '4':
    return 4;
  case '5':
    return 5;
  case '6':
    return 6;
  case '7':
    return 7;
  case '8':
    return 8;
  case '9':
    return 9;
  default:
    return 0;
  }
}
bool pairCompare(const pll firstElof, pll secondElof) {
  return firstElof.first > secondElof.first;
}
//**Snippetリスト**//
// rep, vin, all, iteLoop//
// bitSearch, bitList, gcdlcm, isPrime, primeFactorize, Npow, divisor, modinv,
// dfs, bfs, eratos// dijkstra, WarshallFloyd, BellmanFord, UnionFind, COM,
// digitDP ,RMQ//
ll i, j, k, l;
ll N, M, K, H, W, L, X, Y, Z, R;
ll MOD = 1000000007;
ll INF = 1LL << 62;
ll ans = 0;
ll z = 0, o = 1;
vl flag, color, D;
vll path;
ll Npow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return Npow(x * x % 2019, n / 2);
  else
    return x * Npow(x, n - 1) % 2019;
}
int main() {
  string S;
  cin >> S;
  N = S.length();
  vl T(N + 1);
  vl P(2019);
  T[N] = 0;
  P[T[N]]++;
  ll c = 0;
  for (i = N - 1; i >= 0; i--) {
    T[i] = (T[i + 1] + ctoi(S[i]) * Npow(10, c)) % 2019;
    P[T[i]]++;
    c++;
    // debug(i, T[i]);
  }
  for (i = 0; i < 2019; i++) {
    ans += P[i] * (P[i] - 1) / 2;
  }
  cout << ans;
}