Problem p04023 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

typedef long long int int64;

const int N = (int)1e5 + 100;
long long answer[N];

int main(int, char **) {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

  int n, q;
  scanf("%d%d", &n, &q);

  vector<long long> v;
  v.push_back(1);
  v.push_back(n);

  bool has1 = false;
  for (int i = 0; i < q; i++) {
    long long x;
    scanf("%lld", &x);
    if (x == 1)
      has1 = true;
    while (v.empty() && x <= v.back())
      v.pop_back();
    v.push_back(x);
  }

  if (has1) {
    printf("%lld\n", v.back());
    for (int i = 2; i <= n; i++)
      printf("0\n");
    return 0;
  }

  vector<long long> dp;
  dp.resize(v.size(), 0);
  dp.back() = 1;

  for (int i = (int)v.size() - 1; i > 0; i--) {
    long long x = v[i];
    // eprintf("i = %d, x = %lld\n", i, x);
    long long curdp = dp[i];
    while (x > 0) {
      int it = lower_bound(v.begin(), v.end(), x) - v.begin();
      if (it > 0 && v[it] >= x)
        it--;
      long long y = v[it];
      long long k = x / y;
      eprintf("x = %lld, y = %lld, curdp = %lld\n", x, y, curdp);

      if (it == 0)
        answer[x] += curdp;
      else
        dp[it] += curdp * k;

      x = x % y;
    }
  }

  for (int i = n; i >= 1; i--)
    answer[i - 1] += answer[i];

  for (int i = 1; i <= n; i++)
    printf("%lld\n", answer[i]);

  // eprintf("sz = %d\n", sz);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

typedef long long int int64;

const int N = (int)1e5 + 100;
long long answer[N];

int main(int, char **) {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

  int n, q;
  scanf("%d%d", &n, &q);

  vector<long long> v;
  v.push_back(1);
  v.push_back(n);

  bool has1 = false;
  for (int i = 0; i < q; i++) {
    long long x;
    scanf("%lld", &x);
    if (x == 1)
      has1 = true;
    while (!v.empty() && x <= v.back())
      v.pop_back();
    v.push_back(x);
  }

  if (has1) {
    printf("%lld\n", v.back());
    for (int i = 2; i <= n; i++)
      printf("0\n");
    return 0;
  }

  vector<long long> dp;
  dp.resize(v.size(), 0);
  dp.back() = 1;

  for (int i = (int)v.size() - 1; i > 0; i--) {
    long long x = v[i];
    // eprintf("i = %d, x = %lld\n", i, x);
    long long curdp = dp[i];
    while (x > 0) {
      int it = lower_bound(v.begin(), v.end(), x) - v.begin();
      if (it > 0 && v[it] >= x)
        it--;
      long long y = v[it];
      long long k = x / y;
      eprintf("x = %lld, y = %lld, curdp = %lld\n", x, y, curdp);

      if (it == 0)
        answer[x] += curdp;
      else
        dp[it] += curdp * k;

      x = x % y;
    }
  }

  for (int i = n; i >= 1; i--)
    answer[i - 1] += answer[i];

  for (int i = 1; i <= n; i++)
    printf("%lld\n", answer[i]);

  // eprintf("sz = %d\n", sz);

  return 0;
}