Problem p04023 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 1e-5
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define seg_size 262144 * 4
#define REP(a, b) for (long long a = 0; a < b; ++a)
long long gogo[200000];
long long geko[200000];
long long cnt[200000];
int main() {
#define int long long
  int n, query;
  cin >> n >> query;
  vector<int> pre;
  REP(i, query) {
    int a;
    cin >> a;
    pre.push_back(a);
  }
  vector<int> inputs;
  inputs.push_back(pre[query - 1]);
  for (int i = query - 2; i >= 0; --i) {
    if (inputs[inputs.size() - 1] > pre[i]) {
      inputs.push_back(pre[i]);
    }
  }
  reverse(inputs.begin(), inputs.end());
  geko[inputs.size() - 1] = 1;
  for (int q = inputs.size() - 1; q >= 0; --q) {
    long long now_k = inputs[q];
    while (now_k >= inputs[0] && q != 0) {
      long long bot = 0;
      long long top = q;
      while (abs(bot - top) > 1) {
        long long mid = (top + bot) / 2;
        if (now_k >= inputs[mid]) {
          bot = mid;
        } else {
          top = mid;
        }
      }
      geko[bot] += geko[q] * (now_k / inputs[bot]);
      now_k %= inputs[bot];
    }
    long long cnter = now_k / n;
    gogo[1] += (geko[q] * cnter);
    gogo[n + 1] -= geko[q] * cnter;
    gogo[1] += geko[q];
    gogo[now_k % n + 1] -= geko[q];
  }
  for (long long i = 1; i < 200000; ++i) {
    gogo[i] += gogo[i - 1];
  }
  for (long long i = 1; i < 200000; ++i) {
    cnt[(i - 1) % n + 1] += gogo[i];
  }
  REP(i, n) { cout << cnt[i + 1] << endl; }
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 1e-5
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define seg_size 262144 * 4
#define REP(a, b) for (long long a = 0; a < b; ++a)
long long gogo[200000];
long long geko[200000];
long long cnt[200000];
int main() {
#define int long long
  int n, query;
  cin >> n >> query;
  if (query == 0) {
    REP(q, n) { cout << 1 << endl; }
    return 0;
  }
  vector<int> pre;
  REP(i, query) {
    int a;
    cin >> a;
    pre.push_back(a);
  }
  vector<int> inputs;
  inputs.push_back(pre[query - 1]);
  for (int i = query - 2; i >= 0; --i) {
    if (inputs[inputs.size() - 1] > pre[i]) {
      inputs.push_back(pre[i]);
    }
  }
  reverse(inputs.begin(), inputs.end());
  geko[inputs.size() - 1] = 1;
  for (int q = inputs.size() - 1; q >= 0; --q) {
    long long now_k = inputs[q];
    while (now_k >= inputs[0] && q != 0) {
      long long bot = 0;
      long long top = q;
      while (abs(bot - top) > 1) {
        long long mid = (top + bot) / 2;
        if (now_k >= inputs[mid]) {
          bot = mid;
        } else {
          top = mid;
        }
      }
      geko[bot] += geko[q] * (now_k / inputs[bot]);
      now_k %= inputs[bot];
    }
    long long cnter = now_k / n;
    gogo[1] += (geko[q] * cnter);
    gogo[n + 1] -= geko[q] * cnter;
    gogo[1] += geko[q];
    gogo[now_k % n + 1] -= geko[q];
  }
  for (long long i = 1; i < 200000; ++i) {
    gogo[i] += gogo[i - 1];
  }
  for (long long i = 1; i < 200000; ++i) {
    cnt[(i - 1) % n + 1] += gogo[i];
  }
  REP(i, n) { cout << cnt[i + 1] << endl; }
}