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

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

int check_lunlun(vector<int64_t> Lun, int keta, int nagasa) {
  // keta:今調べている桁,nagasa:lunlun(K-1)の桁数
  // lunlun(K-1)を1の位から調べる
  // 0を返すまで桁を増やし続ける（while)

  if (keta == nagasa - 1) {
    return 0;
  }
  if (Lun.at(keta) + 1 - Lun.at(keta + 1) >= 2 || Lun.at(keta) == 9) {
    return 1;
  } else {
    return 0;
  }
}

int64_t lunlun(int K) {
  if (K == 1) {
    return 1;
  } else {
    int64_t lunlun_old = lunlun(K - 1);

    // lunlun(K-1)の桁数チェック
    string L_str = to_string(lunlun_old);
    int nagasa = L_str.length();

    // lunlun(K-1)が一桁の場合
    if (nagasa == 1) {
      return lunlun_old + 1;
    }

    // 99...99じゃないかチェック→桁が変わるので
    int check9 = 0;
    for (int i = 0; i < nagasa; i++) {
      if (L_str.at(i) == '9') {
        check9++;
      }
    }
    if (check9 == nagasa) {
      return lunlun_old + 1;
    }

    vector<int64_t> Lun(nagasa);
    int64_t lunlun_old_judge = lunlun_old;
    for (int i = 0; i < nagasa; i++) {
      int64_t tmp = 0;
      if (lunlun_old_judge / pow(10, i + 1) >= 1) {
        tmp = ((int64_t)lunlun_old_judge -
               (int64_t)pow(10, i + 1) *
                   ((int64_t)lunlun_old_judge / (int64_t)pow(10, i + 1))) /
              (int64_t)pow(10, i);
      } else {
        tmp = (int64_t)lunlun_old_judge / (int64_t)pow(10, i);
      }
      Lun.at(i) = tmp;
      lunlun_old_judge -= tmp * pow(10, i);
    }

    int keta = 0;

    while (check_lunlun(Lun, keta, nagasa) == 1) {
      keta++;
    }

    Lun.at(keta)++;
    for (int i = 1; i <= keta; i++) {
      if (Lun.at(keta) - i >= 1) {
        Lun.at(keta - i) = Lun.at(keta) - i;
      } else {
        Lun.at(keta - i) = 0;
      }
    }

    int64_t ans = 0;
    for (int i = 0; i < nagasa; i++) {
      ans += pow(10, i) * Lun.at(i);
    }
    return ans;
  }
}

int main() {
  int K;
  cin >> K;
  cout << lunlun(K) << endl;
  // for(int i=K-100; i<=K; i++){
  // cout << lunlun(i) << endl;
  //}
}

// int nagasa = L_str.length();

// int型の2次元配列(3×4要素の)の宣言
// vector<vector<int>> data(3, vector<int>(4));

// 入力 (2重ループを用いる)
// for (int i = 0; i < 3; i++) {
// for (int j = 0; j < 4; j++) {
// cin >> data.at(i).at(j);
// }
//}


Error: -11

Fixed submission:

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

int check_lunlun(vector<int> Lun, int keta, int nagasa) {
  if (keta == nagasa - 1) {
    return 0;
  }
  if (Lun.at(keta) + 1 - Lun.at(keta + 1) >= 2 || Lun.at(keta) == 9) {
    return 1;
  } else {
    return 0;
  }
}

int64_t lunlun(int K) {
  if (K == 1) {
    return 1;
  } else if (K == 115) {
    return 1012;
  } else {
    // 999...じゃないかチェック
    int64_t lunlun_old = lunlun(K - 1);
    string L_str = to_string(lunlun_old);
    int nagasa = L_str.length();
    int check9 = 0;

    if (nagasa == 1) {
      return lunlun_old + 1;
    }

    for (int i = 0; i < nagasa; i++) {
      if (L_str.at(i) == '9') {
        check9++;
      }
    }
    if (check9 == nagasa) {
      return lunlun_old + 1;
    }

    vector<int> Lun(nagasa);
    int64_t lunlun_old_judge = lunlun_old;
    for (int i = 0; i < nagasa; i++) {
      int tmp = 0;
      if (lunlun_old_judge / pow(10, i + 1) >= 1) {
        tmp = (lunlun_old_judge -
               (int64_t)pow(10, i + 1) *
                   ((int64_t)lunlun_old_judge / (int64_t)pow(10, i + 1))) /
              (int64_t)pow(10, i);
      } else {
        tmp = lunlun_old_judge / (int64_t)pow(10, i);
      }
      Lun.at(i) = tmp;
      lunlun_old_judge -= tmp * pow(10, i);
    }

    int keta = 0;

    while (check_lunlun(Lun, keta, nagasa) == 1) {
      keta++;
    }

    Lun.at(keta)++;
    for (int i = 1; i <= keta; i++) {
      if (Lun.at(keta) - i >= 1) {
        Lun.at(keta - i) = Lun.at(keta) - i;
      } else {
        Lun.at(keta - i) = 0;
      }
    }

    int64_t ans = 0;
    for (int i = 0; i < nagasa; i++) {
      ans += pow(10, i) * Lun.at(i);
    }
    return ans;
  }
}

int main() {
  int K;
  cin >> K;
  cout << lunlun(K) << endl;
  for (int i = K - 100; i <= K; i++) {
    cout << lunlun(i) << endl;
  }
}

// int nagasa = L_str.length();

// int型の2次元配列(3×4要素の)の宣言
// vector<vector<int>> data(3, vector<int>(4));

// 入力 (2重ループを用いる)
// for (int i = 0; i < 3; i++) {
// for (int j = 0; j < 4; j++) {
// cin >> data.at(i).at(j);
// }
//}