Problem p04023 (C++)

Buggy submission (status: Runtime Error):
// Relive your past life.
// Face your demons.
// The past is never dead,it is not even past.
// The memories are not only the key to the past but...also to the future.
// coded in Rusty Lake
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <streambuf>
#include <string.h>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define ll long long
#define pb push_back
#define mp make_pair
#define orz 1000000007
using namespace std;
int n, m, q;
ll a[100005], t[100005], k, ans[100005];
int main() {
  scanf("%d%d", &n, &m);
  a[q = 1] = n;
  for (int i = 1; i <= m; ++i) {
    ll x;
    scanf("%lld", &x);
    while (a[q] < x && q)
      --q;
    a[++q] = x;
  }
  t[q] = 1;
  for (int i = q; i > 1; --i) {
    t[i - 1] += t[i] * (a[i] / a[i - 1]);
    k = a[i] % a[i - 1];
    int o;
    do {
      o = 0;
      for (int j = 16; j >= 0; --j) {
        int _ = o + (1 << j);
        if (_ < i && a[_] <= k)
          o = _;
      }
      if (!o)
        ans[k] += t[i];
      else
        t[o] += (k / a[o]) * t[i], k %= a[o];
    } while (o);
  }
  ans[a[1]] += t[1];
  for (int i = n - 1; i; --i)
    ans[i] += ans[i + 1];
  for (int i = 1; i <= n; ++i)
    printf("%lld\n", ans[i]);
  // system("pause");
  return 0;
}


Fixed submission:
// Relive your past life.
// Face your demons.
// The past is never dead,it is not even past.
// The memories are not only the key to the past but...also to the future.
// coded in Rusty Lake
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <streambuf>
#include <string.h>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define ll long long
#define pb push_back
#define mp make_pair
#define orz 1000000007
using namespace std;
int n, m, q;
ll a[100005], t[100005], k, ans[100005];
int main() {
  scanf("%d%d", &n, &m);
  a[q = 1] = n;
  for (int i = 1; i <= m; ++i) {
    ll x;
    scanf("%lld", &x);
    while (a[q] >= x && q)
      --q;
    a[++q] = x;
  }
  t[q] = 1;
  for (int i = q; i > 1; --i) {
    t[i - 1] += t[i] * (a[i] / a[i - 1]);
    k = a[i] % a[i - 1];
    int o;
    do {
      o = 0;
      for (int j = 16; j >= 0; --j) {
        int _ = o + (1 << j);
        if (_ < i && a[_] <= k)
          o = _;
      }
      if (!o)
        ans[k] += t[i];
      else
        t[o] += (k / a[o]) * t[i], k %= a[o];
    } while (o);
  }
  ans[a[1]] += t[1];
  for (int i = n - 1; i; --i)
    ans[i] += ans[i + 1];
  for (int i = 1; i <= n; ++i)
    printf("%lld\n", ans[i]);
  // system("pause");
  return 0;
}