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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

int N, K;
LL num[11];

LL pow10(LL x) {
  if (x == 0)
    return 1;
  return 10LL * pow10(x - 1);
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  /*
  string check;
  for(int i=0;i<=1000;++i){
        stringstream ss; ss << i;
        string s; ss >> s;
        check += s;
  }
  */
  for (LL d = 1; d <= 10; ++d) {
    num[d] = pow10(d);
    REP(i, d) num[d] -= num[i];
  }

  while (cin >> N >> K, !(N == 0 && K == 0)) {
    // string ans = check.substr(N,K);

    LL acc = 0, d;
    for (d = 1;; ++d) {
      acc += num[d] * d;
      if (acc > N)
        break;
    }
    acc -= num[d] * d;

    N -= acc;
    LL x = pow10(d - 1);
    if (d == 1)
      x = 0;
    x += N / d;
    N %= d;

    // cout<<N<<","<<K<<","<<x<<endl;
    string res;
    for (; SZ(res) <= N + K; ++x) {
      stringstream ss;
      ss << x;
      string s;
      ss >> s;
      res += s;
    }
    cout << res.substr(N, K) << endl;
    // cout << ans << endl<<endl;
  }

  return 0;
}

Error: 0

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

int N, K;
int num[10];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  /*
  string check;
  for(int i=0;i<=1000;++i){
        stringstream ss; ss << i;
        string s; ss >> s;
        check += s;
  }
  */
  for (int d = 1; d < 10; ++d)
    num[d] = pow(10, d) - num[d - 1];

  while (cin >> N >> K, !(N == 0 && K == 0)) {
    // string ans = check.substr(N,K);

    int acc = 0, d;
    for (d = 1;; ++d) {
      acc += num[d] * d;
      if (acc > N)
        break;
    }
    acc -= num[d] * d;

    N -= acc;
    int x = pow(10, d - 1);
    if (d == 1)
      x = 0;
    x += N / d;
    N %= d;
    string res;
    for (; SZ(res) <= N + K; ++x) {
      stringstream ss;
      ss << x;
      string s;
      ss >> s;
      res += s;
    }
    cout << res.substr(N, K) << endl;
    // cout << ans << endl<<endl;
  }

  return 0;
}

// 0123456789 10
//