Problem p01295 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);

int numdigits(ll n) {
  int ret = 0;
  while (n) {
    n /= 10;
    ret++;
  }
  return ret;
}

int main() {
  ll n, k;
  while (cin >> n >> k, n | k) {
    int a = -1;
    ll pos = 0;
    REP(i, 9) {
      if (pos + pow(10LL, i) * 9 * (i + 1) > n) {
        a = i - 1;
        break;
      }
      pos += pow(10LL, i) * 9 * (i + 1);
    }
    ll num = pow(10LL, a + 1) - 1;
    int b = a + 1;
    while (pos < n && b >= 0) {
      if (pos + pow(10LL, b) * (a + 2) <= n) {
        pos += pow(10LL, b) * (a + 2);
        num += pow(10LL, b);
      } else {
        b--;
      }
    }

    if (pos < n) {
      num++;
      pos += numdigits(num);
    }

    cerr << numdigits(num) - 1 - (pos - n) << "th digit of " << num << " "
         << endl;

    ostringstream oss;
    REP(i, 100) { oss << num + i; }
    string s = oss.str();
    REP(i, k) { cout << s[numdigits(num) - 1 - (pos - n) + i]; }
    cout << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);

int numdigits(ll n) {
  int ret = 0;
  while (n) {
    n /= 10;
    ret++;
  }
  return ret;
}

int main() {
  ll n, k;
  while (cin >> n >> k, n | k) {
    int a = -1;
    ll pos = 0;
    REP(i, 9) {
      if (pos + pow(10LL, i) * 9 * (i + 1) > n) {
        a = i - 1;
        break;
      }
      pos += pow(10LL, i) * 9 * (i + 1);
    }
    ll num = pow(10LL, a + 1) - 1;
    int b = a + 1;
    while (pos < n && b >= 0) {
      if (pos + pow(10LL, b) * (a + 2) <= n) {
        pos += pow(10LL, b) * (a + 2);
        num += pow(10LL, b);
      } else {
        b--;
      }
    }

    if (pos < n) {
      num++;
      pos += numdigits(num);
    }

    // cerr<<numdigits(num)-1-(pos-n)<<"th digit of " <<num<<" "<<endl;

    ostringstream oss;
    REP(i, 100) { oss << num + i; }
    string s = oss.str();
    REP(i, k) { cout << s[numdigits(num) - 1 - (pos - n) + i]; }
    cout << endl;
  }
}