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

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, i0, n) for (int(i) = (i0); (i) < (n); (i)++)
#define FORR(i, i0, n) for (int(i) = (n)-1; (i) >= (i0); (i)--)
#define SORT(x) sort(x.begin(), x.end())
#define SORTR(x) sort(x.begin(), x.end(), greater<int>())
#define rn return
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#include <time.h>

using namespace std;
using ll = long long;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main() {
  string s;
  getline(cin, s);
  int n = s.size();

  int f = s[0] - '0';

  vector<int> prev(2019, 0);
  vector<int> next(2019, 0);

  prev[f] = 1;

  int sum = 0;
  for (int i = 1; i < n; i++) {
    int c = s[i] - '0';
    next = vector<int>(2019, 0);
    next[c] = 1;
    for (int j = 0; j < 2019; j++) {

      int count = prev[j];

      if (j == 0) {
        sum += count;
      }

      int nt = (10 * j + c) % 2019;
      next[nt] += count;
    }
    prev = next;
  }
  // cout << n << endl;
  // cout << prev.size() << endl;
  sum += prev[0];
  cout << sum;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, i0, n) for (int(i) = (i0); (i) < (n); (i)++)
#define FORR(i, i0, n) for (int(i) = (n)-1; (i) >= (i0); (i)--)
#define SORT(x) sort(x.begin(), x.end())
#define SORTR(x) sort(x.begin(), x.end(), greater<int>())
#define rn return
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#include <time.h>

using namespace std;
using ll = long long;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main() {
  string s;
  getline(cin, s);
  int n = s.size();

  int f = s[0] - '0';

  unordered_map<int, int> prev;

  prev[f] = 1;

  int sum = 0;
  for (int i = 1; i < n; i++) {
    int c = s[i] - '0';
    unordered_map<int, int> next;
    next[c] = 1;
    for (auto e : prev) {

      int j = e.first;
      int count = e.second;

      if (j == 0) {
        sum += count;
      }

      int nt = (10 * j + c) % 2019;
      next[nt] += count;
    }
    prev = next;
  }
  // cout << n << endl;
  // cout << prev.size() << endl;
  for (auto e : prev) {
    int j = e.first;
    if (j == 0) {
      int count = e.second;
      sum += count;
      break;
    }
  }
  cout << sum;
}