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

#include <bits/stdc++.h>
using namespace std;
#define loop(i, a, n) for (ll i = a; i < n; i++)
#define loopm(i, a, n) for (ll i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define Graph(n) vector<vector<ll>>(n, vector<ll>(0, 0))
#define tr(container, it)                                                      \\
  for (auto it = container.begin(); it != container.end(); it++)
#define SZ(x) ((int)(x).size())
typedef vector<long long> VI;
typedef long long ll;
typedef vector<vector<ll>> VII;
typedef pair<ll, ll> PII;
typedef double db;
const ll m = 2019;
ll binpow(ll a, ll b) {
  // b=b%(m-1);//remove if m is not a prime
  a = a % m;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll ans = 0;
  vector<ll> v(2019, 0);
  string s = "";
  cin >> s;
  ll curr = 0;
  v[0]++;
  ll n = s.size();
  loopm(i, 0, s.size()) {
    ll thistime = binpow(10, n - i - 1) * (s[i] - '0') + curr;
    //   cout<<binpow(10,n-i-1)<<" "<<s[i]-'0'<<"\\n";
    thistime %= m;
    //  cout<<thistime<<"\\n";
    v[thistime] += 1;
    curr = thistime;
  }
  loop(i, 0, 2019) {
    // cout<<v[i]<<" ";
    ans += (v[i] * (v[i] - 1)) / 2;
  }
  cout << ans << "\\n";
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define loop(i, a, n) for (ll i = a; i < n; i++)
#define loopm(i, a, n) for (ll i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define Graph(n) vector<vector<ll>>(n, vector<ll>(0, 0))
#define tr(container, it)                                                      \\
  for (auto it = container.begin(); it != container.end(); it++)
#define SZ(x) ((int)(x).size())
typedef vector<long long> VI;
typedef long long ll;
typedef vector<vector<ll>> VII;
typedef pair<ll, ll> PII;
typedef double db;
ll ans = 0;
void computeLPSArray(string pt, ll m, ll *lps) {
  lps[0] = 0;
  ll len = 0;
  ll i = 1;
  while (i < m) {
    if (pt[len] == pt[i]) {
      len++;
      pt[i] = len;
      i++;
    } else {
      if (len != 0) // ajeeb if else hn bhai!!
      {
        len = lps[len - 1];
      } else
        lps[i++] = 0;
    }
  }
}
void KMPSearch(string pat, string txt) {
  ll m = pat.size();
  ll n = txt.size();
  ll lps[m];
  computeLPSArray(pat, m, lps);
  ll i = 0, j = 0;
  while (i < n) {
    if (txt[i] == pat[j]) {
      i++;
      j++;
    }
    if (j == m) {
      ans++;
      j = lps[j - 1];
    } else if (i < n && pat[j] != txt[i]) {
      if (j != 0) // ajeed if else hn bhai!!
        j = lps[j - 1];
      else
        i++;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  string s = "";
  cin >> s;
  for (int i = 1; i < 1000; i++) {
    string txt = to_string(2019 * i);
    ll flag = 1;
    loop(i, 0, txt.size()) {
      if (txt[i] == '0') {
        flag = 0;
        break;
      }
    }
    if (flag == 1) {
      KMPSearch(s, txt);
    }
  }
  cout << ans << "\\n";
}