Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, N) repv(i, 0, N)
#define reps(i, N) repvs(i, 0, N)
#define repv(i, a, b) for (int i = a; i < b; i++)
#define repvs(i, a, b) for (int i = a + 1; i <= b; i++)
#define repr(i, N) for (int i = N - 1; i >= 0; i--)
#define reprs(i, N) for (int i = N; i > 0; i--)

#define d(a)                                                                   \
  if (isDebugMode)                                                             \
  cout << #a << "=" << a << ","
#define d2(a, b)                                                               \
  if (isDebugMode)                                                             \
  cout << #a << "=" << a << "," << #b << "=" << b
#define d3(a, b, c)                                                            \
  if (isDebugMode)                                                             \
  cout << #a << "=" << a << "," << #b << "=" << b << "," << #c << "=" << c
#define d4(a, b, c, d)                                                         \
  if (isDebugMode)                                                             \
  cout << #a << "=" << a << "," << #b << "=" << b << "," << #c << "=" << c     \
       << "," << #d << "=" << d
#define da(arr, N)                                                             \
  {                                                                            \
    if (isDebugMode) {                                                         \
      rep(i, N + 1) {                                                          \
        if (arr[i] == INF)                                                     \
          printf("INF ");                                                      \
        else                                                                   \
          cout << arr[i] << " ";                                               \
      }                                                                        \
      cout << endl;                                                            \
    }                                                                          \
  }
#define da2(arr, N, M)                                                         \
  {                                                                            \
    if (isDebugMode)                                                           \
      rep(i, N + 1) {                                                          \
        rep(j, M + 1) {                                                        \
          if (arr[i][j] == INF)                                                \
            printf(" INF");                                                    \
          else                                                                 \
            printf(" %3d", arr[i][j]);                                         \
        }                                                                      \
        cout << endl;                                                          \
      }                                                                        \
  }
#define pb push_back
#define pob pop_back
#define s(a) cout << a << endl;

auto chmax = [](int &a, int b) { a = max(a, b); };
auto chmin = [](int &a, int b) { a = min(a, b); };

typedef long long ll;
typedef pair<int, int> P;
const int INF = 100000000;
const int MOD = 100000007;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};

bool isDebugMode = 1;
//--------------------------------------//
ll n;
ll s;

ll f(ll b, ll n) {
  if (n < b)
    return n;
  return f(b, n / b) + n % b;
}

void solve() {
  if (s == n) {
    cout << n + 1 << endl;
    return;
  }
  // if(s==1) {cout<<n<<endl;return;}
  ll sp = sqrt(n) + 1;
  repv(i, 2, sp) {
    if (f(i, n) == s) {
      cout << i << endl;
      return;
    }
  }
  ll ans = 1e18 + 1;
  for (ll i = 1; i < sp; i++) {
    ll b = (n - s) / i + 1;
    if ((n - s) % i == 0 && f(b, n) == s) {
      ans = min(b, ans);
    }
  }
  if (ans == 1e18 + 1)
    cout << -1 << endl;
  else
    cout << ans << endl;
}

int main() {
  cin >> n >> s;
  solve();
  return 0;
}


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

#define rep(i, N) repv(i, 0, N)
#define reps(i, N) repvs(i, 0, N)
#define repv(i, a, b) for (int i = a; i < b; i++)
#define repvs(i, a, b) for (int i = a + 1; i <= b; i++)
#define repr(i, N) for (int i = N - 1; i >= 0; i--)
#define reprs(i, N) for (int i = N; i > 0; i--)

#define d(a)                                                                   \
  if (isDebugMode)                                                             \
  cout << #a << "=" << a << ","
#define d2(a, b)                                                               \
  if (isDebugMode)                                                             \
  cout << #a << "=" << a << "," << #b << "=" << b
#define d3(a, b, c)                                                            \
  if (isDebugMode)                                                             \
  cout << #a << "=" << a << "," << #b << "=" << b << "," << #c << "=" << c
#define d4(a, b, c, d)                                                         \
  if (isDebugMode)                                                             \
  cout << #a << "=" << a << "," << #b << "=" << b << "," << #c << "=" << c     \
       << "," << #d << "=" << d
#define da(arr, N)                                                             \
  {                                                                            \
    if (isDebugMode) {                                                         \
      rep(i, N + 1) {                                                          \
        if (arr[i] == INF)                                                     \
          printf("INF ");                                                      \
        else                                                                   \
          cout << arr[i] << " ";                                               \
      }                                                                        \
      cout << endl;                                                            \
    }                                                                          \
  }
#define da2(arr, N, M)                                                         \
  {                                                                            \
    if (isDebugMode)                                                           \
      rep(i, N + 1) {                                                          \
        rep(j, M + 1) {                                                        \
          if (arr[i][j] == INF)                                                \
            printf(" INF");                                                    \
          else                                                                 \
            printf(" %3d", arr[i][j]);                                         \
        }                                                                      \
        cout << endl;                                                          \
      }                                                                        \
  }
#define pb push_back
#define pob pop_back
#define s(a) cout << a << endl;

auto chmax = [](int &a, int b) { a = max(a, b); };
auto chmin = [](int &a, int b) { a = min(a, b); };

typedef long long ll;
typedef pair<int, int> P;
const int INF = 100000000;
const int MOD = 100000007;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};

bool isDebugMode = 1;
//--------------------------------------//
ll n;
ll s;

ll f(ll b, ll n) {
  if (n < b)
    return n;
  return f(b, n / b) + n % b;
}

void solve() {
  if (s == n) {
    cout << n + 1 << endl;
    return;
  }
  if (s > n) {
    cout << -1 << endl;
    return;
  }
  // if(s==1) {cout<<n<<endl;return;}
  ll sp = sqrt(n) + 1;
  repv(i, 2, sp) {
    if (f(i, n) == s) {
      cout << i << endl;
      return;
    }
  }
  ll ans = 1e18 + 1;
  for (ll i = 1; i < sp; i++) {
    ll b = (n - s) / i + 1;
    if ((n - s) % i == 0 && f(b, n) == s) {
      ans = min(b, ans);
    }
  }
  if (ans == 1e18 + 1)
    cout << -1 << endl;
  else
    cout << ans << endl;
}

int main() {
  cin >> n >> s;
  solve();
  return 0;
}