Problem p03561 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using pii = pair<int, int>;
using ll = long long;
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define repeat(i, j, k) for (int i = (j); i < (int)(k); i++)
#define all(v) v.begin(), v.end()
#define debug(x) cerr << #x << " : " << x << endl

template <class T> bool set_min(T &a, const T &b) {
  return a > b ? a = b, true : false;
}
template <class T> bool set_max(T &a, const T &b) {
  return a < b ? a = b, true : false;
}
// vector
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &a : v)
    is >> a;
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &t : v)
    os << "\t" << t;
  return os << endl;
}
// pair
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &v) {
  return os << "<" << v.first << ", " << v.second << ">";
}

const int INF = 1 << 30;
const ll INFL = 1LL << 60;

class Solver {
public:
  bool solve() {
    int K, N;
    cin >> K >> N;
    if (K % 2 == 0) {
      cout << K / 2;
      rep(i, N - 1) cout << " " << K;
      cout << endl;
    } else {
      int dec = (N - 1 + 1) / 2; // + 1 ?
      vector<int> decs(N);
      decs[0] = dec;
      rep(i, N - 1) decs[i + 1] = decs[i] / (K + 1);
      rep(i, N) assert(0 <= decs[i] and decs[i] % (K + 1) < K / 2 + 1);
      rep(i, N) decs[i] %= (K + 1);
      // debug(dec);
      // rep(i, N) cerr << calc(i) << " ";
      // cerr << endl;
      rep(i, N) cout << (i == 0 ? "" : " ") << K / 2 + 1 - decs[N - i - 1];
      cout << endl;
    }
    return 0;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  Solver s;
  s.solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using pii = pair<int, int>;
using ll = long long;
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define repeat(i, j, k) for (int i = (j); i < (int)(k); i++)
#define all(v) v.begin(), v.end()
#define debug(x) cerr << #x << " : " << x << endl

template <class T> bool set_min(T &a, const T &b) {
  return a > b ? a = b, true : false;
}
template <class T> bool set_max(T &a, const T &b) {
  return a < b ? a = b, true : false;
}
// vector
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &a : v)
    is >> a;
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &t : v)
    os << "\t" << t;
  return os << endl;
}
// pair
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &v) {
  return os << "<" << v.first << ", " << v.second << ">";
}

const int INF = 1 << 30;
const ll INFL = 1LL << 60;

class Solver {
public:
  bool solve() {
    int K, N;
    cin >> K >> N;
    if (K % 2 == 0) {
      cout << K / 2;
      rep(i, N - 1) cout << " " << K;
      cout << endl;
    } else {
      int left = (N - 1 + 1) / 2;
      vector<int> ans(N);
      rep(i, N) ans[i] = K / 2 + 1;
      rep(i, left) {
        if (ans.back() == 1) {
          ans.pop_back();
        } else {
          ans.back()--;
          while (ans.size() != N)
            ans.push_back(K);
        }
      }
      rep(i, ans.size()) cout << (i == 0 ? "" : " ") << ans[i];
      cout << endl;
    }
    return 0;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  Solver s;
  s.solve();
  return 0;
}