Problem p04014 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define INF 1100000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007
#define Decimal fixed << setpresicion(20)
#define SHOWP(x) cerr << "(" << (x).FI << ", " << (x).SE << ")" << endl;
#define SHOWX(x) cerr << #x << ": " << x << endl;
#define SHOWVEC(v, e)                                                          \
  REP(i, e) cerr << (v[i]) << ' ';                                             \
  cerr << endl;
#define SHOW2D(a, h, w)                                                        \
  REP(i, h) {                                                                  \
    REP(j, w) cerr << setw(3) << (a[i][j]) << ' ';                             \
    cerr << endl;                                                              \
  }

typedef long long ll;
typedef pair<ll, ll> P;

ll f(ll n, ll b) {
  ll ret = 0;
  while (n) {
    ret += n % b;
    n /= b;
  }
  return ret;
}

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

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

  for (ll b = 2; b * b <= n; b++) {
    if (s == f(n, b)) {
      cout << b << endl;
      return 0;
    }
  }

  ll ans = LLINF;
  for (ll u = 1; u * u <= n; u++) {
    ll b = (n - s) / u + 1;
    if (s == f(n, b))
      ans = min(ans, b);
  }
  if (ans == LLINF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define INF 1100000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007
#define Decimal fixed << setpresicion(20)
#define SHOWP(x) cerr << "(" << (x).FI << ", " << (x).SE << ")" << endl;
#define SHOWX(x) cerr << #x << ": " << x << endl;
#define SHOWVEC(v, e)                                                          \
  REP(i, e) cerr << (v[i]) << ' ';                                             \
  cerr << endl;
#define SHOW2D(a, h, w)                                                        \
  REP(i, h) {                                                                  \
    REP(j, w) cerr << setw(3) << (a[i][j]) << ' ';                             \
    cerr << endl;                                                              \
  }

typedef long long ll;
typedef pair<ll, ll> P;

ll f(ll n, ll b) {
  ll ret = 0;
  while (n) {
    ret += n % b;
    n /= b;
  }
  return ret;
}

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

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

  for (ll b = 2; b * b <= n; b++) {
    if (s == f(n, b)) {
      cout << b << endl;
      return 0;
    }
  }

  ll ans = LLINF;
  for (ll u = 1; u * u <= n; u++) {
    ll b = (n - s) / u + 1;
    if (b <= 1)
      continue;
    if (s == f(n, b))
      ans = min(ans, b);
  }
  if (ans == LLINF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}