Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
typedef long long int ll;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EREP(i, n) for (int i = (n - 1); i >= 0; --i)
#define D(n, retu)                                                             \
  REP(i, n) { cin >> retu[i]; }
#define mod 1000000007
#define MIN -93193111451418101
#define INF 931931114518101
using namespace std;
typedef pair<ll, ll> P;
template <typename T> void fill_all(T &arr, const T &v) { arr = v; }
template <typename T, typename ARR> void fill_all(ARR &arr, const T &v) {
  for (auto &i : arr) {
    fill_all(i, v);
  }
}
#define MAX_NUM 50
long long comb[MAX_NUM + 1][MAX_NUM + 1];

ll par[100000], depth[100000];
static void calc_comb() {
  for (uint32_t i = 0; i <= MAX_NUM; i++) {
    for (uint32_t j = 0; j <= i; j++) {
      if ((j == 0) || (i == j)) {
        comb[i][j] = 1;
      } else {
        comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j];
      }
    }
  }
}
#define yo 100001
//------------------変数-----------------------//
ll n, s;
//-------------------関数----------------------//

ll ketawa(ll b, ll nn) {
  if (nn < b) {
    return nn;
  }
  return ketawa(b, nn / b) + nn % b;
}
int main() {
  cin >> n >> s;
  ll hoge = n / 2 + 1;
  // if (s == 1) { cout << n << endl; return 0; }
  if (s > hoge) {
    cout << -1 << endl;
    return 0;
  }
  ll heihoukon = sqrt(n);
  REP(i, heihoukon + 1) {
    if (i <= 1) {
      continue;
    }
    if (ketawa(i, n) == s) {
      cout << i << endl;
      return 0;
    }
  }
  ll izryt = -1;
  ll kisuu = n & 1;
  REP(i, heihoukon + 1) {
    if (i) {
      if ((s - ketawa(n / i, n)) % i == 0 && ketawa(n / i, n) <= s &&
          s <= ketawa(n / (i + 1) + 1, n)) {
        izryt = (n / i) - (s - ketawa(n / i, n)) / i;
      }
    }
  }
  cout << izryt << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
typedef long long int ll;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EREP(i, n) for (int i = (n - 1); i >= 0; --i)
#define D(n, retu)                                                             \
  REP(i, n) { cin >> retu[i]; }
#define mod 1000000007
#define MIN -93193111451418101
#define INF 931931114518101
using namespace std;
typedef pair<ll, ll> P;
template <typename T> void fill_all(T &arr, const T &v) { arr = v; }
template <typename T, typename ARR> void fill_all(ARR &arr, const T &v) {
  for (auto &i : arr) {
    fill_all(i, v);
  }
}
#define MAX_NUM 50
long long comb[MAX_NUM + 1][MAX_NUM + 1];

ll par[100000], depth[100000];
static void calc_comb() {
  for (uint32_t i = 0; i <= MAX_NUM; i++) {
    for (uint32_t j = 0; j <= i; j++) {
      if ((j == 0) || (i == j)) {
        comb[i][j] = 1;
      } else {
        comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j];
      }
    }
  }
}
#define yo 100001
//------------------変数-----------------------//
ll n, s;
//-------------------関数----------------------//

ll ketawa(ll b, ll nn) {
  if (nn < b) {
    return nn;
  }
  return ketawa(b, nn / b) + nn % b;
}
int main() {
  cin >> n >> s;
  ll hoge = n / 2 + 1;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  // if (s == 1) { cout << n << endl; return 0; }
  if (s > hoge) {
    cout << -1 << endl;
    return 0;
  }
  ll heihoukon = sqrt(n);
  REP(i, heihoukon + 1) {
    if (i <= 1) {
      continue;
    }
    if (ketawa(i, n) == s) {
      cout << i << endl;
      return 0;
    }
  }
  ll izryt = -1;
  ll kisuu = n & 1;
  REP(i, heihoukon + 1) {
    if (i) {
      if ((s - ketawa(n / i, n)) % i == 0 && ketawa(n / i, n) <= s &&
          s <= ketawa(n / (i + 1) + 1, n)) {
        izryt = (n / i) - (s - ketawa(n / i, n)) / i;
      }
    }
  }
  cout << izryt << endl;
  return 0;
}