Problem p04023 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef double db;
typedef long long ll;
#define w1 first
#define w2 second
#define ls (x << 1)
#define rs (x << 1 | 1)
#define pb push_back
#define mid ((l + r) >> 1)
#define SZ(x) ((x).size())
#define All(x) (x).begin(), (x).end()
#define rep(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define rep2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define per(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)
#define Rep(p, x) for (int(p) = head[(x)]; (p); (p) = nxt[(p)])
template <class T> void read(T &num) {
  num = 0;
  T f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    num = num * 10 + ch - '0', ch = getchar();
  num *= f;
}
const int maxn = 1e5 + 5;
const ll inf = 1e18 + 1;
vector<ll> A;
int n, q;
ll a[maxn], cnt[maxn], ans[maxn];
typedef vector<ll>::iterator It;
void calc1(ll x, ll num) {
  It it = lower_bound(A.begin(), A.end(), x);
  if (it == A.begin()) {
    ans[x] += num;
    return;
  }
  it--;
  cnt[it - A.begin()] += num * (x / (*it));
  x %= (*it);
  calc1(x, num);
}
int main() {
  read(n);
  read(q);
  if (!q) {
    rep(i, 1, n) puts("1");
    return 0;
  }
  rep(i, 1, q) read(a[i]);
  ll mini = inf;
  per(i, q, 1) {
    if (a[i] < mini)
      A.pb(a[i]);
    mini = min(mini, a[i]);
  }
  if (A.back() > n)
    A.pb(n);
  reverse(A.begin(), A.end());
  cnt[SZ(A) - 1] = 1;
  per(i, SZ(A) - 1, 1) if (cnt[i]) {
    cnt[i - 1] += cnt[i] * (A[i] / A[i - 1]);
    calc1(A[i] % A[i - 1], cnt[i]);
  }
  ans[A[0]] += cnt[0];
  per(i, n, 1) ans[i] += ans[i + 1];
  rep(i, 1, n) printf("%lld\n", ans[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef double db;
typedef long long ll;
#define w1 first
#define w2 second
#define ls (x << 1)
#define rs (x << 1 | 1)
#define pb push_back
#define mid ((l + r) >> 1)
#define SZ(x) ((x).size())
#define All(x) (x).begin(), (x).end()
#define rep(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define rep2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define per(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)
#define Rep(p, x) for (int(p) = head[(x)]; (p); (p) = nxt[(p)])
template <class T> void read(T &num) {
  num = 0;
  T f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    num = num * 10 + ch - '0', ch = getchar();
  num *= f;
}
const int maxn = 1e5 + 5;
const ll inf = 1e18 + 100;
vector<ll> A;
int n, q;
ll a[maxn], cnt[maxn], ans[maxn];
typedef vector<ll>::iterator It;
void calc1(ll x, ll num) {
  It it = lower_bound(A.begin(), A.end(), x);
  if (it == A.begin()) {
    ans[x] += num;
    return;
  }
  it--;
  cnt[it - A.begin()] += num * (x / (*it));
  x %= (*it);
  calc1(x, num);
}
int main() {
  read(n);
  read(q);
  if (!q) {
    rep(i, 1, n) puts("1");
    return 0;
  }
  rep(i, 1, q) read(a[i]);
  ll mini = inf;
  per(i, q, 1) {
    if (a[i] < mini)
      A.pb(a[i]);
    mini = min(mini, a[i]);
  }
  if (A.back() > n)
    A.pb(n);
  reverse(A.begin(), A.end());
  cnt[SZ(A) - 1] = 1;
  per(i, SZ(A) - 1, 1) if (cnt[i]) {
    cnt[i - 1] += cnt[i] * (A[i] / A[i - 1]);
    calc1(A[i] % A[i - 1], cnt[i]);
  }
  ans[A[0]] += cnt[0];
  per(i, n, 1) ans[i] += ans[i + 1];
  rep(i, 1, n) printf("%lld\n", ans[i]);
  return 0;
}