Problem p02702 (C++) — buggy submission, verdict: Runtime Error

/* Author - thiolhub */

#include <bits/stdc++.h>

using namespace std;

#define ll long long int
#define ld long double
#define db double
#define pi 3.1415926535897932384626
#define PI acos(-1.0)
#define endl "\\n"
#define mod 1000000007
#define mk make_pair
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define ps(x, y) fixed << setprecision(y) << x
#define w(t)                                                                   \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define sc(a) scanf("%lld", &a);
#define pr(a) printf("%lld\\n", a)
#define f(i, a, n) for (ll i = a; i < n; i++)
#define m(a) memset(a, 0, sizeof(a))
#define FastIO                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0), cout.tie(0)

int main() {
  FastIO;

  ll i, j, k = 0, n;
  string s;
  cin >> s;
  n = s.length();
  vector<ll> dp(2019, 0);
  dp[s[0] - '0'] += 1;
  for (i = 1; i < n; i++) {
    ll d = s[i] - '0';
    vector<ll> tdp(2019, 0);
    tdp[d] += 1;
    for (j = 0; j < 2019; j++) {
      int r = (j * 10 + d) % 2019;
      if (r == 0)
        k += dp[j];
      tdp[r] += dp[j];
    }
    dp = tdp;
  }
  cout << k;

  return 0;
}

Error: WA

Fixed submission:

/* Author - thiolhub */

#include <bits/stdc++.h>

using namespace std;

#define ll long long int
#define ld long double
#define db double
#define pi 3.1415926535897932384626
#define PI acos(-1.0)
#define endl "\\n"
#define mod 1000000007
#define mk make_pair
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define ps(x, y) fixed << setprecision(y) << x
#define w(t)                                                                   \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define sc(a) scanf("%lld", &a);
#define pr(a) printf("%lld\\n", a)
#define f(i, a, n) for (ll i = a; i < n; i++)
#define m(a) memset(a, 0, sizeof(a))
#define FastIO                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0), cout.tie(0)

int main() {
  FastIO;

  ll i, j, k = 0, n;
  string s;
  cin >> s;
  n = s.length();
  ll dp[n][2019];
  memset(dp, 0, sizeof(dp));
  dp[0][s[0] - '0'] = 1;
  for (i = 1; i < n; i++) {
    ll d = s[i] - '0';
    dp[i][d] += 1;
    for (j = 0; j < 2019; j++) {
      int r = (j * 10 + d) % 2019;
      if (r == 0)
        k += dp[i - 1][j];
      dp[i][r] += dp[i - 1][j];
    }
  }
  cout << k;

  return 0;
}