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

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

using VS = vector<string>;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
using VL = vector<LL>;
using VVL = vector<VL>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;
const LL LINF = 1e16;
const LL MOD = 1000000007;
const double PI = acos(-1.0);
int DX[8] = {0, 0, 1, -1, 1, 1, -1, -1};
int DY[8] = {1, -1, 0, 0, 1, -1, 1, -1};

/* -----  2017/11/06  Problem: NU_25 B / Link: __CONTEST_URL__  ----- */
/* ------問題------



-----問題ここまで----- */
/* -----解説等-----



----解説ここまで---- */

void solve(int N, int S, int W, int P) {

  int g = S;
  VI a(N);
  FOR(i, 0, N) {
    a[i] = (g / 7) % 10;
    if (g % 2 == 0)
      g = g / 2;
    else
      g = (g / 2) ^ W;
  }

  /*cout << "a : ";
  FOR(i, 0, N) {
          cout << a[i] << " ";
  }cout << endl;*/

  int ans = 0;
  VL cumR(N + 1);
  cumR[N] = 0;
  LL ten = 1;
  // a = VI(N, 1); 0じゃない
  FORR(i, N - 1, -1) {
    // debug(a[i]);
    cumR[i] = ((ten)*a[i] % P) % P + cumR[i + 1];
    cumR[i] %= P;
    ten *= 10;
    ten %= P;
  }
  /*cout << "cumR : ";
  FOR(i, 0, N + 1) {
          cout << cumR[i] << " ";
  }cout << endl;*/

  if (P == 2) {
    int x = 0;
    FOR(i, 0, N) {
      if (a[i] != 0)
        x++;
      if (a[i] % 2 == 0) {
        ans += x;
      }
      // debug(x);
    }
  } else if (P == 5) {
    int x = 0;
    FOR(i, 0, N) {
      if (a[i] != 0)
        x++;
      if (a[i] % 5 == 0) {
        ans += x;
      }
      // debug(x);
    }
  } else {
    map<LL, LL> cnt;
    FOR(i, 0, N) {
      if (a[i] != 0) {
        cnt[cumR[i]]++;
      }
      ans += cnt[cumR[i + 1]];
    }
  }
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int N, S, W, Q;
  while (cin >> N >> S >> W >> Q) {
    if (N == 0)
      break;
    solve(N, S, W, Q);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define LL long long
using namespace std;
int a[100001], ma[10000001];
int main() {
  int N, S, W, Q;
  while (scanf("%d %d %d %d", &N, &S, &W, &Q) && N && S && W && Q) {

    int g = S;
    for (int i = 0; i < N; i++) {
      a[i] = (g / 7) % 10;
      if (g % 2 == 0) {
        g = (g / 2);
      } else {
        g = (g / 2) ^ W;
      }
    }
    int sum = 0, ans = 0;
    memset(ma, 0, sizeof(ma));
    if (Q == 2 || Q == 5) {
      for (int i = 0; i < N; i++) {
        if (a[i])
          sum++;
        if (a[i] % Q == 0)
          ans += sum;
      }
    } else {
      int t = 1;
      ans = 0;
      for (int i = N - 1; i >= 0; i--) {
        sum = (a[i] * t + sum) % Q;
        if (a[i])
          ans += ma[sum];
        ma[sum]++;
        if (a[i] && sum == 0)
          ans++;
        t = (t * 10) % Q;
      }
    }
    printf("%d\\n", ans);
  }
}