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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define vii vector<pair<int, int>>
#define IO                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define IOfiles                                                                \\
  freopen("input.txt", "r+", stdin);                                           \\
  freopen("output.txt", "w+", stdout)
#define PI 3.14159265
const long long mod = 1e9 + 7LL;
typedef long long ll;
typedef pair<int, int> ii;
typedef double db;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll pwr(ll x, ll n) {
  ll ans = 1;
  while (n) {
    ans *= x;
    n--;
  }
  return ans;
}

int main() {
  IO;
  string s;
  cin >> s;
  int n = s.length();
  // int dp[n][2019];
  // memset(dp,0,sizeof dp);
  int arr[2][2019];
  memset(arr, 0, sizeof arr);
  arr[0][s[0] - '0']++;
  // dp[0][s[0]-'0']++;
  int ans = 0;
  for (int i = 1; i < n; i++) {
    int v = s[i] - '0';

    int p = (i + 1) % 2;
    for (int j = 0; j < 2019; j++)
      arr[i % 2][j] = 0;
    arr[i % 2][v]++;
    for (int j = 0; j < 2019; j++) {
      arr[i % 2][(j * 10 + v) % 2019] += arr[p][j];
    }
    // if(arr[i%2][0])
    // cout<<arr[i%2][0]<<endl;
    ans += arr[i % 2][0];
  }
  /*for(int i=0;i<n;i++){
      for(int j=0;j<2019;j++){
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }*/
  /*cout<<dp[8][0]<<endl;
  cout<<dp[n-1][0]<<endl;*/
  // int ans=0;
  /*for(int i=0;i<n;i++)
      ans+=dp[i][0];*/
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define vii vector<pair<int, int>>
#define IO                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define IOfiles                                                                \\
  freopen("input.txt", "r+", stdin);                                           \\
  freopen("output.txt", "w+", stdout)
#define PI 3.14159265
const long long mod = 1e9 + 7LL;
typedef long long ll;
typedef pair<int, int> ii;
typedef double db;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll pwr(ll x, ll n) {
  ll ans = 1;
  while (n) {
    ans *= x;
    n--;
  }
  return ans;
}

int main() {
  IO;
  string s;
  cin >> s;
  int n = s.length();
  int dp[n][2019];
  memset(dp, 0, sizeof dp);
  dp[0][s[0] - '0']++;
  for (int i = 1; i < n; i++) {
    int v = s[i] - '0';
    dp[i][v]++;
    for (int j = 0; j < 2019; j++) {
      // dp[i][j]=dp[i-1][j];
      dp[i][(j * 10 + v) % 2019] += dp[i - 1][j];
    }
  }
  /*for(int i=0;i<n;i++){
      for(int j=0;j<2019;j++){
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }*/
  /*cout<<dp[8][0]<<endl;
  cout<<dp[n-1][0]<<endl;*/
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans += dp[i][0];
  cout << ans << endl;
}