Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <time.h>
#include <vector>
#define ll long long
using namespace std;

ll f(ll b, ll n) {

  ll answer = 0;
  while (n >= b) {
    answer += n % b;
    n /= b;
  }
  return answer + n;
}

ll SolveSmall(ll n, ll s) {
  vector<ll> divs;
  for (ll j = 2; j * j <= n; j++) {
    if (n % j == 0) {
      ll m = n;
      while (m % j == 0) {
        m /= j;
      }

      if (m == s && j >= s + 1) {
        return j;
      }
    }
  }

  if (n > 1)
    return n;
  else
    return -1;
}

ll SolveRev(ll n, ll s) {
  // 1, s-1
  // s-1 <= b-1

  // b + s - 1 = n

  // b = n-s+1 >= s
  if (n - s + 1 >= s) {
    return n - s + 1;
  } else
    return -1;
}

ll SolveSmall2(ll n, ll s) {
  // 1,b,..,b^(L-1)

  // d(1) * b + d(0) = n
  // d(1)  + d(0) = s

  // d(1) = (n-s) / (b-1) <= b-1
  // d(0) = s - d(1) <= b-1

  vector<ll> divs;
  for (ll x = 2; x * x <= n; x++) {
    if ((n - s) % x == 0) {
      divs.push_back(x);
      divs.push_back((n - s) / x);
    }
  }

  sort(divs.begin(), divs.end());

  for (int j = 0; j < divs.size(); j++) {
    ll x = divs[j];
    ll b = x + 1;
    ll d1 = (n - s) / (b - 1);
    ll d0 = s - d1;

    if (d1 >= 0 && d1 <= b - 1 && d0 >= 0 && d0 <= b - 1) {
      return b;
    }
  }

  return -1;
}

ll Solve(ll n, ll s) {

  ll prev = -1;
  int time_since_last_increase = 0;
  ll low = 50000000 + 1;
  for (int j = 2; j <= 50000000; j++) {
    ll curr = f(j, n);
    if (curr == s) {
      return j;
    }
    prev = curr;
  }
  ll high = 1000000000000000000ll;

  while (low < high) {
    ll mid = (low + high) / 2;

    if (f(mid, n) >= s) {
      high = mid;
    } else {
      low = mid + 1;
    }
  }

  vector<ll> answers;
  answers.push_back(f(low, n) == s ? low : -1);

  answers.push_back(SolveSmall(n, s));

  answers.push_back(SolveRev(n, s));

  answers.push_back(SolveSmall2(n, s));

  sort(answers.begin(), answers.end());
  for (int j = 0; j < answers.size(); j++) {
    if (answers[j] >= 2 && f(answers[j], n) == s)
      return answers[j];
  }

  return -1;
}

int main() {
  ll n, s;

  cin >> n >> s;

  cout << Solve(n, s) << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <time.h>
#include <vector>
#define ll long long
using namespace std;

ll f(ll b, ll n) {

  ll answer = 0;
  while (n >= b) {
    answer += n % b;
    n /= b;
  }
  return answer + n;
}

ll SolveSmall(ll n, ll s) {
  vector<ll> divs;
  for (ll j = 2; j * j <= n; j++) {
    if (n % j == 0) {
      ll m = n;
      while (m % j == 0) {
        m /= j;
      }

      if (m == s && j >= s + 1) {
        return j;
      }
    }
  }

  if (n > 1)
    return n;
  else
    return -1;
}

ll SolveRev(ll n, ll s) {
  // 1, s-1
  // s-1 <= b-1

  // b + s - 1 = n

  // b = n-s+1 >= s
  if (n - s + 1 >= s) {
    return n - s + 1;
  } else
    return -1;
}

ll SolveSmall2(ll n, ll s) {
  if (n == s)
    return n + 1;
  // 1,b,..,b^(L-1)

  // d(1) * b + d(0) = n
  // d(1)  + d(0) = s

  // d(1) = (n-s) / (b-1) <= b-1
  // d(0) = s - d(1) <= b-1

  vector<ll> divs;
  for (ll x = 2; x * x <= n; x++) {
    if ((n - s) % x == 0) {
      divs.push_back(x);
      divs.push_back((n - s) / x);
    }
  }

  sort(divs.begin(), divs.end());

  for (int j = 0; j < divs.size(); j++) {
    ll x = divs[j];
    ll b = x + 1;
    ll d1 = (n - s) / (b - 1);
    ll d0 = s - d1;

    if (d1 >= 0 && d1 <= b - 1 && d0 >= 0 && d0 <= b - 1) {
      return b;
    }
  }

  return -1;
}

ll Solve(ll n, ll s) {

  ll prev = -1;
  int time_since_last_increase = 0;
  ll low = 50000000 + 1;
  for (int j = 2; j <= 50000000; j++) {
    ll curr = f(j, n);
    if (curr == s) {
      return j;
    }
    prev = curr;
  }
  ll high = 1000000000000000000ll;

  while (low < high) {
    ll mid = (low + high) / 2;

    if (f(mid, n) >= s) {
      high = mid;
    } else {
      low = mid + 1;
    }
  }

  vector<ll> answers;
  answers.push_back(f(low, n) == s ? low : -1);

  answers.push_back(SolveSmall(n, s));

  answers.push_back(SolveRev(n, s));

  answers.push_back(SolveSmall2(n, s));

  sort(answers.begin(), answers.end());
  for (int j = 0; j < answers.size(); j++) {
    if (answers[j] >= 2 && f(answers[j], n) == s)
      return answers[j];
  }

  return -1;
}

int main() {
  ll n, s;

  cin >> n >> s;

  cout << Solve(n, s) << endl;

  return 0;
}