Problem p04023 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define M 100010
#define fi first
#define sc second
#define pb push_back
#define LL long long
#define MOD 998244353
#define pii pair<int, int>
#define For(i, x, y) for (int i = (x); i < (y); i++)
#define rep(i, x, y) for (int i = (x); i <= (y); i++)
#define drep(i, x, y) for (int i = (x); i >= (y); i--)
inline int read() {
  char ch = getchar();
  int x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline LL llread() {
  char ch = getchar();
  LL x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline int Power(int x, int y, int mod) {
  int r = 1;
  while (y) {
    if (y & 1)
      r = 1ll * r * x % mod;
    x = 1ll * x * x % mod;
    y >>= 1;
  }
  return r;
}
LL a[M], f[M];
LL res[M];
int tt;
inline int Get(int l, int r, int C) {
  int ret = -1;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (a[mid] <= C)
      l = (ret = mid) + 1;
    else
      r = mid - 1;
  }
  return ret;
}
int main() {
  int n = read(), q = read();
  a[tt = 1] = n;
  rep(i, 1, q) {
    LL x = llread();
    while (tt && x <= a[tt])
      tt--;
    a[++tt] = x;
  }
  q = tt;
  f[q] = 1;
  // cout << tt << endl;
  drep(i, q, 1) {
    LL x = a[i];
    int t = i - 1;
    while ((t = Get(1, t, x)) > 0) {
      f[t] += (x / a[t]) * f[i];
      x %= a[t];
    }
    res[x] += f[i];
  }
  drep(i, n - 1, 1) res[i] += res[i + 1];
  rep(i, 1, n) printf("%lld\n", res[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define M 100010
#define fi first
#define sc second
#define pb push_back
#define LL long long
#define MOD 998244353
#define pii pair<int, int>
#define For(i, x, y) for (int i = (x); i < (y); i++)
#define rep(i, x, y) for (int i = (x); i <= (y); i++)
#define drep(i, x, y) for (int i = (x); i >= (y); i--)
inline int read() {
  char ch = getchar();
  int x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline LL llread() {
  char ch = getchar();
  LL x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline int Power(int x, int y, int mod) {
  int r = 1;
  while (y) {
    if (y & 1)
      r = 1ll * r * x % mod;
    x = 1ll * x * x % mod;
    y >>= 1;
  }
  return r;
}
LL a[M], f[M];
LL res[M];
int tt;
inline int Get(int l, int r, LL C) {
  int ret = -1;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (a[mid] <= C)
      l = (ret = mid) + 1;
    else
      r = mid - 1;
  }
  return ret;
}
int main() {
  int n = read(), q = read();
  a[tt = 1] = n;
  rep(i, 1, q) {
    LL x = llread();
    while (tt && x <= a[tt])
      tt--;
    a[++tt] = x;
  }
  q = tt;
  f[q] = 1;
  // cout << tt << endl;
  drep(i, q, 1) {
    LL x = a[i];
    int t = i - 1;
    while ((t = Get(1, t, x)) > 0) {
      f[t] += (x / a[t]) * f[i];
      x %= a[t];
    }
    res[x] += f[i];
  }
  drep(i, n - 1, 1) res[i] += res[i + 1];
  rep(i, 1, n) printf("%lld\n", res[i]);
  return 0;
}