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

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define itn long long

typedef long long ll;
typedef pair<ll, ll> p;

vector<ll> checklist;

string zero(string ume, ll keta) {
  while (ume.length() < keta) {
    ume = "0" + ume;
  }
  return ume;
}

ll checktolist(ll hoge) {
  for (int i = 0; i < checklist.size(); i++) {
    if (checklist[i] == hoge)
      return i;
  }
  return -1;
}

int main() {
  while (1) {
    string a;
    ll b;
    ll count = 0;

    cin >> a >> b;

    checklist = {stoi(a)};
    if (a == "0" && b == 0)
      exit(0);

    // 0埋め
    string kazu = zero(a, b);

    while (1) {
      count++;

      // v: ソート用配列
      vector<char> v;

      // 文字列をvectorに
      for (int i = 0; i < b; i++)
        v.push_back(kazu[i]);

      // sortした
      sort(all(v));

      // 小さい数
      string nekazu;
      for (int i = 0; i < b; i++)
        nekazu += v[i];

      // reverse
      sort(rall(v));

      // 大きい数
      string rekazu;
      for (int i = 0; i < b; i++)
        rekazu += v[i];

      ll hoge = stoi(rekazu) - stoi(nekazu);

      //      cout << stoi(rekazu) << " - " << stoi(nekazu) << " = " << hoge <<
      //      endl;

      if (checktolist(hoge) != -1) {
        if (a == "0")
          cout << 0 << " ";
        else
          cout << checktolist(hoge) << " ";
        cout << hoge << " ";
        cout << count - checktolist(hoge) << endl;
        break;
      }

      checklist.push_back(hoge);

      kazu = zero(to_string(hoge), b);
    }
  }
}


Error: -6
terminate called after throwing an instance of 'std::invalid_argument'
  what():  stoi


Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define itn long long

typedef long long ll;
typedef pair<ll, ll> p;

vector<ll> checklist(1, -1);

string zero(string ume, ll keta) {
  while (ume.length() < keta) {
    ume = "0" + ume;
  }
  return ume;
}

ll checktolist(ll hoge) {
  for (int i = 0; i < checklist.size(); i++) {
    if (checklist[i] == hoge)
      return i;
  }
  return -1;
}

int main() {
  while (1) {
    string a;
    ll b;
    ll count = 0;

    cin >> a >> b;

    if (a == "0" && b == 0)
      exit(0);

    // 0埋め
    string kazu = zero(a, b);

    while (1) {
      count++;

      // v: ソート用配列
      vector<char> v;
      v.push_back(stoi(a));

      // 文字列をvectorに
      for (int i = 0; i < b; i++)
        v.push_back(kazu[i]);

      // sortした
      sort(all(v));

      // 小さい数
      string nekazu;
      for (int i = 0; i < b; i++)
        nekazu += v[i];

      // reverse
      sort(rall(v));

      // 大きい数
      string rekazu;
      for (int i = 0; i < b; i++)
        rekazu += v[i];

      ll hoge = stoi(rekazu) - stoi(nekazu);

      cout << stoi(rekazu) << " - " << stoi(nekazu) << " = " << hoge << endl;

      if (checktolist(hoge) != -1) {
        if (a == "0")
          cout << 0 << " ";
        else
          cout << checktolist(hoge) << " ";
        cout << hoge << " ";
        cout << count - checktolist(hoge) << endl;
        break;
      }

      checklist.push_back(hoge);

      kazu = zero(to_string(hoge), b);
    }
  }
}