Problem p03561 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
// long long p = 998244353;
long long p = 1000000007;
#define int long long
#define vel vector<long long>
#define vvel vector<vel>
#define rep(i, n) for (int i = 0; i < n; i++)
#define sor(v) sort(v.begin(), v.end())
#define mmax(a, b) a = max(a, b)
#define mmin(a, b) a = min(a, b)
#define mkp make_pair
#define pin pair<int, int>
#define qin pair<int, pin>
#define V vector
#define Endl endl
#define veb vector<bool>
#define rev(s) reverse(s.begin(), s.end())
#define lower(h, val) lower_bound(h.begin(), h.end(), val) - h.begin()
#define upper(h, val) upper_bound(h.begin(), h.end(), val) - h.begin()
int max_kai = 150000;
vel kai(max_kai, 1);
vel inv_kai;
int rui(int a, int n) {
  if (n == 0) {
    return 1;
  }
  int x = rui(a, n / 2);
  x *= x;
  x %= p;
  if (n % 2 == 1) {
    x *= a;
    x %= p;
  }
  return x;
}
int inv(int a) { return rui(a, p - 2); }
void make_kai() {
  for (int i = 1; i < max_kai; i++) {
    kai[i] = kai[i - 1] * i;
    kai[i] %= p;
  }
  inv_kai = kai;
  rep(i, max_kai) { inv_kai[i] = inv(kai[i]); }
}
int nCr(int n, int r) {
  int a = kai[n] * inv_kai[r];
  a %= p;
  a *= inv_kai[n - r];
  a %= p;
  return a;
}
vel pa;
int root(int x) {
  if (pa[x] == -1) {
    return x;
  }
  int ans = root(pa[x]);
  pa[x] = ans;
  return ans;
}
void marge(int x, int y) {
  x = root(x);
  y = root(y);
  if (x != y) {
    pa[x] = y;
  }
}
int gcd(int x, int y) {
  if (x < y) {
    return gcd(y, x);
  }
  if (y == 0) {
    return x;
  }
  return gcd(y, x % y);
}
int solve(int x, int y) {
  int ans = rui(2, x) + rui(2, y) + p - 1;
  ans %= p;
  int g1 = gcd(x, y);
  ans += rui(2, g1) + p - 2;
  ans %= p;
  return ans;
}
void mi(vel &v, int k) {
  int n = v.size();
  int j = n - 1;
  while (v[j] == 0) {
    j--;
  }
  v[j]--;
  if (v[j] != 0) {
    j++;
    while (j < n) {
      v[j] = k;
      j++;
    }
  }
}
signed main() {
  int n, k;
  cin >> k >> n;
  if (k % 2 == 0) {
    int l = k / 2;
    cout << l;
    rep(i, n - 1) { cout << " " << k; }
    cout << endl;
  } else {
    vel ans(n, (k + 1) / 2);
    int pl = n - 1;
    while (pl > 0) {
      mi(ans, k);
      pl -= 2;
    }
    cout << ans[0];
    int ind = 1;
    while (ind < n) {
      if (ans[ind] == 0) {
        break;
      }
      cout << " " << ans[ind];
      ind++;
    }
    cout << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
// long long p = 998244353;
long long p = 1000000007;
#define int long long
#define vel vector<long long>
#define vvel vector<vel>
#define rep(i, n) for (int i = 0; i < n; i++)
#define sor(v) sort(v.begin(), v.end())
#define mmax(a, b) a = max(a, b)
#define mmin(a, b) a = min(a, b)
#define mkp make_pair
#define pin pair<int, int>
#define qin pair<int, pin>
#define V vector
#define Endl endl
#define veb vector<bool>
#define rev(s) reverse(s.begin(), s.end())
#define lower(h, val) lower_bound(h.begin(), h.end(), val) - h.begin()
#define upper(h, val) upper_bound(h.begin(), h.end(), val) - h.begin()
int max_kai = 150000;
vel kai(max_kai, 1);
vel inv_kai;
int rui(int a, int n) {
  if (n == 0) {
    return 1;
  }
  int x = rui(a, n / 2);
  x *= x;
  x %= p;
  if (n % 2 == 1) {
    x *= a;
    x %= p;
  }
  return x;
}
int inv(int a) { return rui(a, p - 2); }
void make_kai() {
  for (int i = 1; i < max_kai; i++) {
    kai[i] = kai[i - 1] * i;
    kai[i] %= p;
  }
  inv_kai = kai;
  rep(i, max_kai) { inv_kai[i] = inv(kai[i]); }
}
int nCr(int n, int r) {
  int a = kai[n] * inv_kai[r];
  a %= p;
  a *= inv_kai[n - r];
  a %= p;
  return a;
}
vel pa;
int root(int x) {
  if (pa[x] == -1) {
    return x;
  }
  int ans = root(pa[x]);
  pa[x] = ans;
  return ans;
}
void marge(int x, int y) {
  x = root(x);
  y = root(y);
  if (x != y) {
    pa[x] = y;
  }
}
int gcd(int x, int y) {
  if (x < y) {
    return gcd(y, x);
  }
  if (y == 0) {
    return x;
  }
  return gcd(y, x % y);
}
int solve(int x, int y) {
  int ans = rui(2, x) + rui(2, y) + p - 1;
  ans %= p;
  int g1 = gcd(x, y);
  ans += rui(2, g1) + p - 2;
  ans %= p;
  return ans;
}
void mi(vel &v, int k) {
  int n = v.size();
  int j = n - 1;
  while (v[j] == 0) {
    j--;
  }
  v[j]--;
  if (v[j] != 0) {
    j++;
    while (j < n) {
      v[j] = k;
      j++;
    }
  }
}
signed main() {
  int n, k;
  cin >> k >> n;
  if (k % 2 == 0) {
    int l = k / 2;
    cout << l;
    rep(i, n - 1) { cout << " " << k; }
    cout << endl;
  } else if (k == 1) {
    int m = (n + 1) / 2;
    rep(i, m) { cout << " " << 1; }
    cout << endl;
  } else {
    vel ans(n, (k + 1) / 2);
    int pl = n - 1;
    while (pl > 0) {
      mi(ans, k);
      pl -= 2;
    }
    cout << ans[0];
    int ind = 1;
    while (ind < n) {
      if (ans[ind] == 0) {
        break;
      }
      cout << " " << ans[ind];
      ind++;
    }
    cout << endl;
  }
  return 0;
}