Problem p01295 (C++) — buggy submission, verdict: Runtime Error

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 58;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  while (true) {
    ll n, k;
    cin >> n >> k;
    if (n == 0 and k == 0)
      break;

    ll x, y;
    rep(i, 1, inf) {
      ll tmp = i * (pow(10, i) - pow(10, i - 1));
      if (n - tmp > 0)
        n -= tmp;
      else {
        x = pow(10, i - 1);
        y = i;
        break;
      }
    }
    dump(x);
    dump(y);
    dump(n);

    ll z = 0;
    rep(i, 1, 11) {
      if (n - y * x > 0)
        n -= y * x;
      else {
        z = i;
        break;
      }
    }
    dump(z);
    dump(n);
    ll w = 0;
    rep(i, 1, 11) {
      if (n - y * x / 10 > 0)
        n -= y * x / 10;
      else {
        w = i - 1;
        break;
      }
    }
    dump(w);
    dump(n);
    ll hoge = 0;
    rep(i, 1, 11) {
      if (n - y * x / 100 > 0)
        n -= y * x / 100;
      else {
        hoge = i - 1;
        break;
      }
    }
    dump(hoge);
    ll piyo = 0;
    rep(i, 1, 11) {
      if (n - y * x / 1000 > 0)
        n -= y * x / 1000;
      else {
        piyo = i - 1;
        break;
      }
    }
    dump(piyo);
    dump(n);
    stringstream ss;
    rep(i, 0, 100000) {
      ss << x * z + x * w / 10 + x * hoge / 100 + x * piyo / 1000 + i;
      // dump(ss.str().size());
      if (ss.str().size() > n + k)
        break;
    }
    cout << ss.str().substr(n - 1, k) << endl;
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cout << fixed << setprecision(8);
  solve();
  return 0;
}

Error: 0

Fixed submission:

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 58;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  while (true) {
    ll n, k;
    cin >> n >> k;
    if (n == 0 and k == 0)
      break;

    ll x, y;
    rep(i, 1, inf) {
      ll tmp = i * (pow(10, i) - pow(10, i - 1));
      if (n - tmp > 0)
        n -= tmp;
      else {
        x = pow(10, i - 1);
        y = i;
        break;
      }
    }
    dump(x);
    dump(y);
    dump(n);

    ll z;
    rep(i, 1, 10) {
      if (n - y * x > 0)
        n -= y * x;
      else {
        z = i;
        break;
      }
    }
    dump(z);
    dump(n);
    stringstream ss;
    rep(i, 0, 100) {
      ss << x * z + i;
      if (ss.str().size() > n + k)
        break;
    }
    cout << ss.str().substr(n - 1, k) << endl;
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cout << fixed << setprecision(8);
  solve();
  return 0;
}