Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <fstream>
#include <iostream>
using namespace std;

long long sum(long long N, const long long base) {
  long long s = 0;
  while (N > 0) {
    s += N % base;
    N = N / base;
  }
  return s;
}

int solve_less(const long long N, const long long S) {
  long long sq = 1;
  for (; sq * sq <= N; sq++)
    ;

  for (long long base = 2; base <= sq; base++) {
    if (sum(N, base) == S)
      return (int)base;
  }
  return -1;
}

long long solve_greater(const long long N, const long long S) {
  long long sq = 1;
  for (; sq * sq < N; sq++)
    ;

  long long sol = (1LL << 62);
  // sq * sq > N;
  // N = x + b * y, b >= sq => y <= sq
  for (long long y = 0; y <= sq; y++) {
    // N = x + b * y;
    // x + y = S;
    long long x = S - y;
    if (x < 0)
      continue;
    long long M = N - x;
    // M = b * y;
    if (M < 0) {
      continue;
    }
    if (y == 0) {
      if (M == 0) {
        sol = min(sol, max(sq, x + 1));
      }
    } else {
      if (M % y == 0) {
        long long base = M / y;
        if (base >= sq && x < base && y < base) {
          sol = min(sol, base);
        }
      }
    }
  }
  if (sol == (1LL << 62))
    sol = -1;
  return sol;
}

long long solve(const long long N, const long long S) {
  int ret = solve_less(N, S);
  if (ret != -1)
    return ret;
  return solve_greater(N, S);
}

int main() {
  ios_base::sync_with_stdio(false);

  long long N, S;
  // ifstream cin("code.in");
  cin >> N >> S;
  long long sol = solve(N, S);
  if (sol < 0) {
    cout << "-1\n";
    return 0;
  }
  long long x = N % sol;
  long long y = N / sol;
  assert(x + y == S);
  cout << sol << '\n';
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <fstream>
#include <iostream>
using namespace std;

long long sum(long long N, const long long base) {
  long long s = 0;
  while (N > 0) {
    s += N % base;
    N = N / base;
  }
  return s;
}

int solve_less(const long long N, const long long S) {
  long long sq = 1;
  for (; sq * sq <= N; sq++)
    ;

  for (long long base = 2; base <= sq; base++) {
    if (sum(N, base) == S)
      return (int)base;
  }
  return -1;
}

long long solve_greater(const long long N, const long long S) {
  long long sq = 1;
  for (; sq * sq < N; sq++)
    ;

  long long sol = (1LL << 62);
  // sq * sq > N;
  // N = x + b * y, b >= sq => y <= sq
  for (long long y = 0; y <= sq; y++) {
    // N = x + b * y;
    // x + y = S;
    long long x = S - y;
    if (x < 0)
      continue;
    long long M = N - x;
    // M = b * y;
    if (M < 0) {
      continue;
    }
    if (y == 0) {
      if (M == 0) {
        sol = min(sol, max(sq, x + 1));
      }
    } else {
      if (M % y == 0) {
        long long base = M / y;
        if (base >= sq && x < base && y < base) {
          sol = min(sol, base);
        }
      }
    }
  }
  if (sol == (1LL << 62))
    sol = -1;
  return sol;
}

long long solve(const long long N, const long long S) {
  int ret = solve_less(N, S);
  if (ret != -1)
    return ret;
  return solve_greater(N, S);
}

int main() {
  ios_base::sync_with_stdio(false);

  long long N, S;
  // ifstream cin("code.in");
  cin >> N >> S;
  long long sol = solve(N, S);
  if (sol < 0) {
    cout << "-1\n";
    return 0;
  }
  // long long x = N % sol;
  // long long y = N / sol;
  // assert(x + y == S);
  cout << sol << '\n';
  return 0;
}