Problem p03320 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define endl '\n'

// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")

#define double long double

using namespace std;
template <class T, class T2> inline int chkmax(T &x, const T2 &y) {
  return x < y ? x = y, 1 : 0;
}
template <class T, class T2> inline int chkmin(T &x, const T2 &y) {
  return x > y ? x = y, 1 : 0;
}
const int MAXN = (1 << 20);

int k;

void read() { cin >> k; }

void solve() {
  int64_t val = (int64_t)1e15 - 1, dwn = (int64_t)1e11,
          cntr = (int64_t)1e14 - 1;
  vector<int64_t> li;

  double last = 1e9;
  while (val) {
    int64_t cc = val, c = 0;
    while (cc) {
      c += cc % 10;
      cc /= 10;
    }

    double curr = val / (double)c;

    // cout << val << " -> " << last << " " << curr << endl;

    if (curr <= last) {
      last = curr;
      li.push_back(val);
    }

    val -= dwn;
    if (val == cntr)
      dwn /= 10, cntr /= 10;
    if (!dwn)
      dwn = 1;
  }

  sort(li.begin(), li.end());
  for (int i = 0; i < k; i++)
    cout << li[i] << endl;

  /*int c = 0, dgs = 0;
  while(k--)
  {
          c++;
          int x = c, S = c;
          cout << c;
          for(int i = 0; i < dgs; i++) x = x * 10 + 9, S += 9, cout << 9;
          cout << endl;

          //cout << "-> " << setprecision(9) << fixed << x / (double)S << endl;

          if(c == 9)
          {
                  dgs++;
                  c = 0;
                  if(dgs >= 3 && k)
                  {
                          cout << 90;
                          for(int d = 0; d < dgs - 1; d++)
                                  cout << 9;
                          cout << endl;
                          k--;
                  }
          }
  }*/
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  read();
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define endl '\n'

// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")

#define double long double

using namespace std;
template <class T, class T2> inline int chkmax(T &x, const T2 &y) {
  return x < y ? x = y, 1 : 0;
}
template <class T, class T2> inline int chkmin(T &x, const T2 &y) {
  return x > y ? x = y, 1 : 0;
}
const int MAXN = (1 << 20);

int k;

void read() { cin >> k; }

void solve() {
  int64_t val = (int64_t)1e15 - 1, dwn = (int64_t)1e11,
          cntr = (int64_t)1e14 - 1;
  vector<int64_t> li;

  double last = 1e19;
  while (val) {
    int64_t cc = val, c = 0;
    while (cc) {
      c += cc % 10;
      cc /= 10;
    }

    double curr = val / (double)c;

    // cout << val << " -> " << last << " " << curr << endl;

    if (curr <= last) {
      last = curr;
      li.push_back(val);
    }

    val -= dwn;
    if (val == cntr)
      dwn /= 10, cntr /= 10;
    if (!dwn)
      dwn = 1;
  }

  sort(li.begin(), li.end());
  for (int i = 0; i < k; i++)
    cout << li[i] << endl;

  /*int c = 0, dgs = 0;
  while(k--)
  {
          c++;
          int x = c, S = c;
          cout << c;
          for(int i = 0; i < dgs; i++) x = x * 10 + 9, S += 9, cout << 9;
          cout << endl;

          //cout << "-> " << setprecision(9) << fixed << x / (double)S << endl;

          if(c == 9)
          {
                  dgs++;
                  c = 0;
                  if(dgs >= 3 && k)
                  {
                          cout << 90;
                          for(int d = 0; d < dgs - 1; d++)
                                  cout << 9;
                          cout << endl;
                          k--;
                  }
          }
  }*/
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  read();
  solve();
  return 0;
}