Problem p04014 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000000
#define MOD 1000000007
#define all(v) v.begin(), v.end()
#define pb push_back
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int kaijo[200010];
struct edge {
  int to, cost;
};
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool prime(int a) {
  if (a == 1)
    return false;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
bool kosa(double ax, double ay, double bx, double by, double cx, double cy,
          double dx, double dy) {
  double ta = (cx - dx) * (ay - cy) + (cy - dy) * (cx - ax);
  double tb = (cx - dx) * (by - cy) + (cy - dy) * (cx - bx);
  double tc = (ax - bx) * (cy - ay) + (ay - by) * (ax - cx);
  double td = (ax - bx) * (dy - ay) + (ay - by) * (ax - dx);
  return tc * td < 0 && ta * tb < 0;
}
void init_fact(int n) {
  kaijo[0] = 1;
  for (int i = 1; i <= n; i++) {
    kaijo[i] = kaijo[i - 1] * i;
    kaijo[i] %= MOD;
  }
}
int modpow(int a, int b) {
  if (b == 0)
    return 1;
  if (b % 2)
    return modpow(a, b - 1) * a % MOD;
  int memo = modpow(a, b / 2);
  return memo * memo % MOD;
}
int comb(int a, int b) {
  if (b > a)
    return 0;
  if (a == b)
    return 1;
  return kaijo[a] * modpow(kaijo[a - b], MOD - 2) % MOD *
         modpow(kaijo[b], MOD - 2) % MOD;
}
int g(int x) {
  x = modpow(x, MOD - 2);
  return x;
}
int n, s, ans = 1000000000000;
bool ok(int a, int b) {
  int res = 0;
  while (a) {
    res += a % b;
    a -= a % b;
    a /= b;
  }
  return res == s;
}
signed main() {
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  for (int b = 2; b * b <= n; b++) {
    if (ok(n, b)) {
      cout << b << endl;
      return 0;
    }
  }
  for (int p = 1; p * p < n; p++) {
    if (n - s + p < 0)
      continue;
    if ((n - s + p) % p)
      continue;
    int b = (n - s + p) / p;
    if (ok(n, b))
      ans = min(ans, b);
  }
  if (ans == 1000000000000)
    puts("-1");
  else
    cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000000
#define MOD 1000000007
#define all(v) v.begin(), v.end()
#define pb push_back
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int kaijo[200010];
struct edge {
  int to, cost;
};
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool prime(int a) {
  if (a == 1)
    return false;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
bool kosa(double ax, double ay, double bx, double by, double cx, double cy,
          double dx, double dy) {
  double ta = (cx - dx) * (ay - cy) + (cy - dy) * (cx - ax);
  double tb = (cx - dx) * (by - cy) + (cy - dy) * (cx - bx);
  double tc = (ax - bx) * (cy - ay) + (ay - by) * (ax - cx);
  double td = (ax - bx) * (dy - ay) + (ay - by) * (ax - dx);
  return tc * td < 0 && ta * tb < 0;
}
void init_fact(int n) {
  kaijo[0] = 1;
  for (int i = 1; i <= n; i++) {
    kaijo[i] = kaijo[i - 1] * i;
    kaijo[i] %= MOD;
  }
}
int modpow(int a, int b) {
  if (b == 0)
    return 1;
  if (b % 2)
    return modpow(a, b - 1) * a % MOD;
  int memo = modpow(a, b / 2);
  return memo * memo % MOD;
}
int comb(int a, int b) {
  if (b > a)
    return 0;
  if (a == b)
    return 1;
  return kaijo[a] * modpow(kaijo[a - b], MOD - 2) % MOD *
         modpow(kaijo[b], MOD - 2) % MOD;
}
int g(int x) {
  x = modpow(x, MOD - 2);
  return x;
}
int n, s, ans = 1000000000000;
bool ok(int a, int b) {
  int res = 0;
  while (a) {
    res += a % b;
    a -= a % b;
    a /= b;
  }
  return res == s;
}
signed main() {
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  for (int b = 2; b * b <= n; b++) {
    if (ok(n, b)) {
      cout << b << endl;
      return 0;
    }
  }
  for (int p = 1; p * p < n; p++) {
    if (n - s + p <= 0)
      continue;
    if ((n - s + p) % p)
      continue;
    int b = (n - s + p) / p;
    if (ok(n, b))
      ans = min(ans, b);
  }
  if (ans == 1000000000000)
    puts("-1");
  else
    cout << ans << endl;
  return 0;
}