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

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &p) {
  return out << "(" << p.first << ", " << p.second << ")";
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}

const int N = 2019;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  string S;
  cin >> S;
  int n = S.size();
  // map<int, int> m;
  // m[0]++;
  // vector<int> s(n + 1, 0);
  // LL ans = 0;
  // for (int i = 0 ; i < n; i++) {
  //     s[i + 1] = s[i] * 10 + (S[i] - '0');
  //     s[i + 1] %= 2019;
  //     cout << s[i + 1] << endl;
  //     if (m.count(s[i + 1])) ans += m[s[i + 1]];
  //     m[s[i + 1]]++;
  // }
  // cout << ans << endl;
  // memset(dp, 0, sizeof(dp));
  LL ans = 0;
  int m[N];
  memset(m, 0, sizeof(m));
  for (int i = 0; i < n; i++) {
    int cur[N];
    memset(cur, 0, sizeof(cur));
    int a = S[i] - '0';
    cur[a % N]++;
    // dp[i + 1][a % N]++;
    for (int j = 0; j < N; j++) {
      cur[(a + j * 10) % N] += m[j];
    }
    // for (int j = 0; j < N; j++) dp[i + 1][(a + j * 10) % N] += dp[i][j];
    // ans += dp[i + 1][0];
    ans += cur[0];
    for (int j = 0; j < N; j++)
      m[j] = cur[j];
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &p) {
  return out << "(" << p.first << ", " << p.second << ")";
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}

const int N = 2019;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  string S;
  cin >> S;
  int n = S.size();
  // map<int, int> m;
  // m[0]++;
  // vector<int> s(n + 1, 0);
  // LL ans = 0;
  // for (int i = 0 ; i < n; i++) {
  //     s[i + 1] = s[i] * 10 + (S[i] - '0');
  //     s[i + 1] %= 2019;
  //     cout << s[i + 1] << endl;
  //     if (m.count(s[i + 1])) ans += m[s[i + 1]];
  //     m[s[i + 1]]++;
  // }
  // cout << ans << endl;
  // memset(dp, 0, sizeof(dp));
  LL ans = 0;
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    map<int, int> cur;
    int a = S[i] - '0';
    cur[a % N]++;
    // dp[i + 1][a % N]++;
    for (auto &p : m) {
      int j = p.first, time = p.second;
      cur[(a + j * 10) % N] += time;
    }
    // for (int j = 0; j < N; j++) dp[i + 1][(a + j * 10) % N] += dp[i][j];
    // ans += dp[i + 1][0];
    ans += cur[0];
    m = cur;
  }
  cout << ans << endl;

  return 0;
}