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

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

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"

long long int powint(long long int a, long long int b) {
  assert(b >= 0);
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  long long int ans = 1;
  long long int aa = powint(a, b / 2);
  ans *= aa * aa;
  if (b % 2)
    ans *= a;
  return ans;
}
vector<long long int> memo(18);
long long int getans(const long long int num) {
  const int keta = to_string(num).size();
  if (keta >= 2) {
    return (num - powint(10, keta - 1) + 1) * keta + memo[keta - 1];
  } else {
    return num + 1;
  }
}
long long int getnum(const pair<long long int, int> &p) {
  if (p.first == 0)
    return p.second;
  return getans(p.first - 1) - p.second;
}
bool check(const int f, const long long int num, const string st) {
  string pre = st.substr(0, f);
  const string prenumst = to_string(num - 1);
  if (prenumst.substr(prenumst.size() - pre.size(), pre.size()) != pre)
    return false;
  else {
    int nf = f;
    long long int nnum = num;
    while (nf < st.size()) {
      string nnumst = to_string(nnum);
      string next = st.substr(nf, nnumst.size());
      bool same = (next == nnumst.substr(0, next.size()));
      if (!same) {
        return false;
      }
      nf += nnumst.size();
      nnum++;
    }
    return true;
  }
}
long long int check2(const string from, const string to, const int keta) {

  int f_size = from.size();
  int t_size = to.size();
  int kaburi = f_size + t_size - keta;
  if (all_of(from.begin() + kaburi, from.end(),
             [](const char &c) { return c == '9'; })) {
    bool ok = true;
    string kaburif = from.substr(0, kaburi);
    kaburif = kaburif.empty() ? "1" : to_string(stol(kaburif) + 1);
    if (kaburif.size() != kaburi) {
      string kaburit = to.substr(t_size - kaburi, kaburi);
      if (kaburif.substr(1) != kaburit)
        ok = false;
      if (ok) {
        string ans;
        ans += to_string(stol(to.substr(0, t_size - kaburi)) - 1);
        ans += from;
        return getnum(make_pair(stoll(ans), from.size() - keta));
      } else {
        return 3e18;
      }
    } else {
      string kaburit = to.substr(t_size - kaburi, kaburi);
      if (kaburif != kaburit)
        ok = false;
      if (ok) {
        string ans;
        ans += to.substr(0, t_size - kaburi);
        ans += from;
        return getnum(make_pair(stoll(ans), from.size() - keta));
      } else {
        return 3e18;
      }
    }

  } else {
    bool ok = true;
    string kaburif = from.substr(0, kaburi);
    string kaburit = to.substr(t_size - kaburi, kaburi);
    if (kaburif != kaburit)
      ok = false;
    if (ok) {
      string ans;
      ans += to.substr(0, t_size - kaburi);
      ans += from;
      return getnum(make_pair(stoll(ans), from.size() - keta));
    } else {
      return 3e18;
    }
  }
}

long long int solve(string st) {
  if (st == "0")
    return 11;
  if (all_of(st.begin(), st.end(), [](const char &ch) { return ch == '0'; })) {
    return getnum(make_pair(stoll('1' + st), -1));
  }
  long long int ans(1e18);
  for (int keta = 1; keta <= min(17, int(st.size())); ++keta) {
    for (int f = 0; f < min(keta, int(st.size())); ++f) {
      string ast = st.substr(f, keta);
      if (ast.size() < keta) {
        string from = st.substr(0, f);
        string to = st.substr(f, st.size() - f);
        if (to[0] != '0') {
          ans = min(ans, check2(from, to, keta));
        }
      } else {
        if (ast[0] != '0') {

          long long int num = stoll(ast);
          if (check(f, num, st)) {
            ans = min(ans, getnum(make_pair(num, f)));
          }
        }
      }
    }
  }
  return ans;
}
int main() {
  memo[1] = 10;
  memo[2] = 190;
  for (int i = 2; i < 18; ++i) {
    memo[i] = powint(10, i - 1) * (i)*9 + memo[i - 1];
  }
  /*for (int i = 1; i <1001; ++i) {
          cout << i<<":"<<getans(i) << endl;
  }*/
  while (1) {

    string st;
    cin >> st;
    if (st != "#") {

      long long int ans = solve(st);
      cout << ans << endl;
    } else {
      break;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

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

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"

long long int powint(long long int a, long long int b) {
  assert(b >= 0);
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  long long int ans = 1;
  long long int aa = powint(a, b / 2);
  ans *= aa * aa;
  if (b % 2)
    ans *= a;
  return ans;
}
vector<long long int> memo(18);
long long int getans(const long long int num) {
  const int keta = to_string(num).size();
  if (keta >= 2) {
    return (num - powint(10, keta - 1) + 1) * keta + memo[keta - 1];
  } else {
    return num + 1;
  }
}
long long int getnum(const pair<long long int, int> &p) {
  if (p.first == 0)
    return p.second;
  return getans(p.first - 1) - p.second;
}
bool check(const int f, const long long int num, const string st) {
  string pre = st.substr(0, f);
  const string prenumst = to_string(num - 1);
  if (prenumst.substr(prenumst.size() - pre.size(), pre.size()) != pre)
    return false;
  else {
    int nf = f;
    long long int nnum = num;
    while (nf < st.size()) {
      string nnumst = to_string(nnum);
      string next = st.substr(nf, nnumst.size());
      bool same = (next == nnumst.substr(0, next.size()));
      if (!same) {
        return false;
      }
      nf += nnumst.size();
      nnum++;
    }
    return true;
  }
}
long long int check2(const string from, const string to, const int keta) {

  int f_size = from.size();
  int t_size = to.size();
  int kaburi = f_size + t_size - keta;
  if (all_of(from.begin() + kaburi, from.end(),
             [](const char &c) { return c == '9'; })) {
    bool ok = true;
    string kaburif = from.substr(0, kaburi);
    kaburif = kaburif.empty() ? "1" : to_string(stol(kaburif) + 1);
    if (kaburif.size() != kaburi) {
      string kaburit = to.substr(t_size - kaburi, kaburi);
      if (kaburif.substr(1) != kaburit)
        ok = false;
      if (ok) {
        string ans;
        ans += to_string(stol(to.substr(0, t_size - kaburi)) - 1);
        ans += from;
        return getnum(make_pair(stoll(ans), from.size() - keta));
      } else {
        return 3e18;
      }
    } else {
      string kaburit = to.substr(t_size - kaburi, kaburi);
      if (kaburif != kaburit)
        ok = false;
      if (ok) {
        string ans;
        ans += to.substr(0, t_size - kaburi);
        ans += from;
        return getnum(make_pair(stoll(ans), from.size() - keta));
      } else {
        return 3e18;
      }
    }

  } else {
    bool ok = true;
    string kaburif = from.substr(0, kaburi);
    string kaburit = to.substr(t_size - kaburi, kaburi);
    if (kaburif != kaburit)
      ok = false;
    if (ok) {
      string ans;
      ans += to.substr(0, t_size - kaburi);
      ans += from;
      return getnum(make_pair(stoll(ans), from.size() - keta));
    } else {
      return 3e18;
    }
  }
}

long long int solve(string st) {
  if (st == "0")
    return 0;
  if (all_of(st.begin(), st.end(), [](const char &ch) { return ch == '0'; })) {
    return getnum(make_pair(stoll('1' + st), -1));
  }
  long long int ans(1e18);
  for (int keta = 1; keta <= st.size(); ++keta) {
    for (int f = 0; f < min(keta, int(st.size())); ++f) {
      string ast = st.substr(f, keta);
      if (ast.size() < keta) {
        string from = st.substr(0, f);
        string to = st.substr(f, st.size() - f);
        if (to[0] != '0') {
          ans = min(ans, check2(from, to, keta));
        }
      } else {
        if (ast.size() == 1 || ast[0] != '0') {

          long long int num = stoll(ast);
          if (check(f, num, st)) {
            ans = min(ans, getnum(make_pair(num, f)));
          }
        }
      }
    }
  }
  return ans;
}
int main() {
  memo[1] = 10;
  memo[2] = 190;
  for (int i = 2; i < 18; ++i) {
    memo[i] = powint(10, i - 1) * (i)*9 + memo[i - 1];
  }
  /*for (int i = 1; i <1001; ++i) {
          cout << i<<":"<<getans(i) << endl;
  }*/
  while (1) {

    string st;
    cin >> st;
    if (st != "#") {

      long long int ans = solve(st);
      cout << ans << endl;
    } else {
      break;
    }
  }
  return 0;
}