Problem p03561 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define For(i, j, k) for (int i = (j), i##_end_ = (k); i <= i##_end_; ++i)
#define Fordown(i, j, k) for (int i = (j), i##_end_ = (k); i >= i##_end_; --i)
#define Set(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#ifdef hany01
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
using namespace std;
typedef long long LL;

inline void file() {
#ifdef hany01
  freopen("a.in", "r", stdin);
  freopen("a.out", "w", stdout);
#endif
}

template <typename T> inline bool chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmin(T &a, T b) {
  return b < a ? a = b, 1 : 0;
}

char c_;
int _, __;
inline int read() {
  for (_ = 0, __ = 1, c_ = getchar(); !isdigit(c_); c_ = getchar())
    if (c_ == '-')
      __ = -1;
  for (; isdigit(c_); c_ = getchar())
    _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
  return _ * __;
}

const int maxn = 300010;

int n, k, a[maxn];

inline void Solve1() {
  int p = n / 2;
  printf("%d ", p);
  For(i, 2, k) printf("%d ", n);
}

inline void Solve2() {
  int p = (n >> 1) + 1, t;
  For(i, 1, k) a[i] = p;
  For(i, 1, k / 2) {
    --a[t = k];
    while (!a[t]) {
      if (i >= k / 2)
        break;
      ++i;
      For(j, t, k) a[j] = n;
      --a[--t];
    }
  }
  For(i, 1, k) {
    if (!a[i])
      return;
    printf("%d ", a[i]);
  }
}

int main() {
  file();
  n = read();
  k = read();
  if (n % 2 == 0)
    Solve1();
  else
    Solve2();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define For(i, j, k) for (int i = (j), i##_end_ = (k); i <= i##_end_; ++i)
#define Fordown(i, j, k) for (int i = (j), i##_end_ = (k); i >= i##_end_; --i)
#define Set(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#ifdef hany01
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
using namespace std;
typedef long long LL;

inline void file() {
#ifdef hany01
  freopen("a.in", "r", stdin);
  freopen("a.out", "w", stdout);
#endif
}

template <typename T> inline bool chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmin(T &a, T b) {
  return b < a ? a = b, 1 : 0;
}

char c_;
int _, __;
inline int read() {
  for (_ = 0, __ = 1, c_ = getchar(); !isdigit(c_); c_ = getchar())
    if (c_ == '-')
      __ = -1;
  for (; isdigit(c_); c_ = getchar())
    _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
  return _ * __;
}

const int maxn = 300010;

int n, k, a[maxn];

inline void Solve1() {
  int p = n / 2;
  printf("%d ", p);
  For(i, 2, k) printf("%d ", n);
}

inline void Solve2() {
  if (n == 1) {
    For(i, 1, (k + 1) / 2) printf("1 ");
    return;
  }
  int p = (n >> 1) + 1, t;
  For(i, 1, k) a[i] = p;
  For(i, 1, k / 2) {
    --a[t = k];
    while (!a[t]) {
      if (i >= k / 2)
        break;
      ++i;
      For(j, t, k) a[j] = n;
      --a[--t];
    }
  }
  For(i, 1, k) {
    if (!a[i])
      return;
    printf("%d ", a[i]);
  }
}

int main() {
  file();
  n = read();
  k = read();
  if (n % 2 == 0)
    Solve1();
  else
    Solve2();
  return 0;
}