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

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef vector<ll> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<ll, ll> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef map<ll, ll> mii;
typedef vector<char> vc;
typedef vector<bool> vb;
#define endl '\\n'
#define forn(i, n) for (ll i = 0; i < n; i++)
#define rforn(i, n) for (ll i = n - 1; i >= 0; i--)
#define forne(i, n) for (ll i = 1; i <= n; i++)
#define rforne(i, n) for (ll i = n; i >= 1; i--)
#define forse(i, s, e) for (ll i = s; i < e; i++)
#define rforse(i, s, e) for (ll i = e - 1; i >= s; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll) x.size()
#define MOD 1000000007
#define F first
#define S second
#define pb push_back
#define in insert
#define mp(a, b) make_pair(a, b)
#define fill(a, x) memset(a, x, sizeof a);
#define trav(a, x) for (auto &a : x)
#define min3(a, b, c) min(min(a, b), c)
#define min4(a, b, c, d) min(min(a, b), min(c, d))
#define max3(a, b, c) max(max(a, b), c)
#define max4(a, b, c, d) max(max(a, b), max(c, d))
#define INF ll_MAX
#ifdef KARAN
#include "../../trace.h"
#else
#define trace(args...)
#endif

void solve() {
  string s;
  cin >> s;
  ll n = s.size();

  ll dp[2][2019];
  forn(i, 2) forn(j, 2019) dp[i][j] = 0;

  ll output = 0;
  dp[0][s[0] - '0']++;
  forne(i, n - 1) {
    forn(j, 2019) dp[i % 2][j] = 0;
    ll curr = s[i] - '0';
    dp[i % 2][curr]++;
    forn(j, 2019) dp[i % 2][(j * 10 + curr) % 2019] += dp[1 - (i % 2)][j];
    output = output + dp[i % 2][0];
  }
  cout << output << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef KARAN
  freopen("inp.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  freopen("err.txt", "w", stderr);
#endif
  ll t = 1;
  // cin>>t;
  while (t--)
    solve();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef vector<ll> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<ll, ll> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef map<ll, ll> mii;
typedef vector<char> vc;
typedef vector<bool> vb;
#define endl '\\n'
#define forn(i, n) for (ll i = 0; i < n; i++)
#define rforn(i, n) for (ll i = n - 1; i >= 0; i--)
#define forne(i, n) for (ll i = 1; i <= n; i++)
#define rforne(i, n) for (ll i = n; i >= 1; i--)
#define forse(i, s, e) for (ll i = s; i < e; i++)
#define rforse(i, s, e) for (ll i = e - 1; i >= s; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll) x.size()
#define MOD 1000000007
#define F first
#define S second
#define pb push_back
#define in insert
#define mp(a, b) make_pair(a, b)
#define fill(a, x) memset(a, x, sizeof a);
#define trav(a, x) for (auto &a : x)
#define min3(a, b, c) min(min(a, b), c)
#define min4(a, b, c, d) min(min(a, b), min(c, d))
#define max3(a, b, c) max(max(a, b), c)
#define max4(a, b, c, d) max(max(a, b), max(c, d))
#define INF ll_MAX
#ifdef KARAN
#include "../../trace.h"
#else
#define trace(args...)
#endif

void solve() {
  string s;
  cin >> s;
  ll n = s.size();

  ll dp[n][2019];
  forn(i, n) forn(j, 2019) dp[i][j] = 0;

  ll output = 0;
  dp[0][s[0] - '0']++;
  forne(i, n - 1) {
    ll curr = s[i] - '0';
    dp[i][curr]++;
    forn(j, 2019) dp[i][(j * 10 + curr) % 2019] += dp[i - 1][j];
    output = output + dp[i][0];
  }
  cout << output << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef KARAN
  freopen("inp.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  freopen("err.txt", "w", stderr);
#endif
  ll t = 1;
  // cin>>t;
  while (t--)
    solve();
}