Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define DBG cerr << '!' << endl;
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \
  {                                                                            \
    for (int W = 0; W < (n); W++)                                              \
      cerr << v[W] << ' ';                                                     \
    cerr << endl << endl;                                                      \
  }
#define SHOW2d(v, i, j)                                                        \
  {                                                                            \
    for (int aaa = 0; aaa < i; aaa++) {                                        \
      for (int bbb = 0; bbb < j; bbb++)                                        \
        cerr << v[aaa][bbb] << ' ';                                            \
      cerr << endl;                                                            \
    }                                                                          \
    cerr << endl;                                                              \
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(10)

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

ll func(ll b, ll n) {
  if (n == 0)
    return 0;
  return func(b, n / b) + (n % b);
}

int main() {
  ll n, s;
  cin >> n >> s;

  if (s == n) {
    cout << n + 1 << endl;
    return 0;
  }

  for (ll i = 2; i * i <= n; i++) {
    if (func(i, n) == s) {
      cout << i << endl;
      return 0;
    }
  }
  ll ans = 100000000000000LL;
  for (ll i = 1; i * i <= n; i++) {
    if ((n - s) % i == 0 && (n - s) / i != 0) {
      ll tmp = (n - s) / i + 1;
      if (func(tmp, n) == s)
        ans = min(ans, tmp);
    }
  }

  if (ans == 100000000000000LL) {
    cout << -1 << endl;
    return 0;
  } else {
    cout << ans << endl;
    return 0;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define DBG cerr << '!' << endl;
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \
  {                                                                            \
    for (int W = 0; W < (n); W++)                                              \
      cerr << v[W] << ' ';                                                     \
    cerr << endl << endl;                                                      \
  }
#define SHOW2d(v, i, j)                                                        \
  {                                                                            \
    for (int aaa = 0; aaa < i; aaa++) {                                        \
      for (int bbb = 0; bbb < j; bbb++)                                        \
        cerr << v[aaa][bbb] << ' ';                                            \
      cerr << endl;                                                            \
    }                                                                          \
    cerr << endl;                                                              \
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(10)

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

ll func(ll b, ll n) {
  if (n == 0)
    return 0;
  return func(b, n / b) + (n % b);
}

int main() {
  ll n, s;
  cin >> n >> s;

  if (s > n) {
    cout << -1 << endl;
    return 0;
  }
  if (s == n) {
    cout << n + 1 << endl;
    return 0;
  }

  for (ll i = 2; i * i <= n; i++) {
    if (func(i, n) == s) {
      cout << i << endl;
      return 0;
    }
  }
  ll ans = 100000000000000LL;
  for (ll i = 1; i * i <= n; i++) {
    if ((n - s) % i == 0 && (n - s) / i != 0) {
      ll tmp = (n - s) / i + 1;
      if (func(tmp, n) == s)
        ans = min(ans, tmp);
    }
  }

  if (ans == 100000000000000LL) {
    cout << -1 << endl;
    return 0;
  } else {
    cout << ans << endl;
    return 0;
  }

  return 0;
}