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

#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unistd.h>
#include <vector>

#ifndef ONLINE_JUDGE
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#else
#define DEBUG(x)                                                               \\
  do {                                                                         \\
  } while (0);
#endif

#define pb push_back
#define mp make_pair
#define ALL(x) (x).begin(), (x).end()
#define PER(i, N) for (int(i) = (N)-1; (i) >= 0; (i)--)
#define REP(i, N) for (int(i) = 0; (i) < (int)(N); (i)++)

using namespace std;
typedef long long ll;
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }

template <typename T, typename U>
std::istream &operator>>(std::istream &i, pair<T, U> &p) {
  i >> p.x >> p.y;
  return i;
}
template <typename T> std::istream &operator>>(std::istream &i, vector<T> &t) {
  for (auto &v : t) {
    i >> v;
  }
  return i;
}
template <typename T, typename U>
std::ostream &operator<<(std::ostream &o, const pair<T, U> &p) {
  o << p.x << ' ' << p.y;
  return o;
}
template <typename T>
std::ostream &operator<<(std::ostream &o, const vector<T> &t) {
  if (t.empty())
    o << '\\n';
  for (size_t i = 0; i < t.size(); ++i) {
    o << t[i] << " \\n"[i == t.size() - 1];
  }
  return o;
}
template <typename T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using maxheap = priority_queue<T, vector<T>, less<T>>;
template <typename T> T gcd(T a, T b) {
  if (a < b)
    swap(a, b);
  return b ? gcd(b, a % b) : a;
}
int C[2666];
int nC[2666];
void solve(istream &cin, ostream &cout) {
  string S;
  cin >> S;
  int n = S.size();
  int cmod = 0;
  int tenmod = 1;
  int posledni = n - 1;
  int sum = 0;
  ll ans = 0;
  for (int prvni = posledni; prvni >= 0; prvni--) {
    sum = (sum + ((tenmod * (S[prvni] - '0')) % 2019)) % 2019;
    C[sum]++;
    tenmod = (10 * tenmod) % 2019;
    //		cout << sum << endl;
  }
  ans += C[0];
  //	cout << "Adding " << cnt[0] <<endl;
  for (int k = 0; k < n - 1; k++) {
    REP(i, 2019) nC[i] = 0;
    int sub = (-(S[n - k - 1] - '0') + 2019) % 2019;
    REP(i, 2019) {
      int newZb = (i + sub) % 2019;
      newZb = (newZb * 202) % 2019;
      nC[newZb] += C[i];
    }
    REP(i, 2019) C[i] = nC[i];
    C[0]--;
    ans += C[0];
    //		cout << "Adding " << cnt[0] <<endl;
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  solve(cin, cout);
  /*
  {
          istringstream instr("2\\n2\\n14");
          solve(instr, cout);
  }
*/
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unistd.h>
#include <vector>

#ifndef ONLINE_JUDGE
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#else
#define DEBUG(x)                                                               \\
  do {                                                                         \\
  } while (0);
#endif

#define pb push_back
#define mp make_pair
#define ALL(x) (x).begin(), (x).end()
#define PER(i, N) for (int(i) = (N)-1; (i) >= 0; (i)--)
#define REP(i, N) for (int(i) = 0; (i) < (int)(N); (i)++)

using namespace std;
typedef long long ll;
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }

template <typename T, typename U>
std::istream &operator>>(std::istream &i, pair<T, U> &p) {
  i >> p.x >> p.y;
  return i;
}
template <typename T> std::istream &operator>>(std::istream &i, vector<T> &t) {
  for (auto &v : t) {
    i >> v;
  }
  return i;
}
template <typename T, typename U>
std::ostream &operator<<(std::ostream &o, const pair<T, U> &p) {
  o << p.x << ' ' << p.y;
  return o;
}
template <typename T>
std::ostream &operator<<(std::ostream &o, const vector<T> &t) {
  if (t.empty())
    o << '\\n';
  for (size_t i = 0; i < t.size(); ++i) {
    o << t[i] << " \\n"[i == t.size() - 1];
  }
  return o;
}
template <typename T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using maxheap = priority_queue<T, vector<T>, less<T>>;
template <typename T> T gcd(T a, T b) {
  if (a < b)
    swap(a, b);
  return b ? gcd(b, a % b) : a;
}
int C[2666];
int nC[2666];
void solve(istream &cin, ostream &cout) {
  string S;
  cin >> S;
  int n = S.size();
  vector<int> sufmods(S.size() + 1, 0);
  int cmod = 0;
  int tenmod = 1;
  int posledni = n - 1;
  int sum = 0;
  ll ans = 0;
  for (int prvni = posledni; prvni >= 0; prvni--) {
    sum = (sum + ((tenmod * (S[prvni] - '0')) % 2019)) % 2019;
    C[sum]++;
    tenmod = (10 * tenmod) % 2019;
    //		cout << sum << endl;
  }
  ans += C[0];
  //	cout << "Adding " << cnt[0] <<endl;
  for (int k = 0; k < n - 1; k++) {
    REP(i, 2019) nC[i] = 0;
    REP(i, 2019) {
      int oldZb = i;
      int newZb = (2019 + (i - (S[n - k - 1] - '0')) % 2019) % 2019;
      newZb = (newZb * 202) % 2019;
      nC[newZb] += C[i];
    }
    REP(i, 2019) C[i] = nC[i];
    C[0]--;
    ans += C[0];
    //		cout << "Adding " << cnt[0] <<endl;
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  solve(cin, cout);
  /*
  {
          istringstream instr("2\\n2\\n14");
          solve(instr, cout);
  }
*/
  return 0;
}