Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define forr(i, p, n) for (ll i = p; i < n; i++)
#define tam 100347
#define offset 50000
using namespace std;
typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ll, ii> iii;
const ll INF = 1e11;
ll go(ll b, ll n) {
  if (b == 1)
    return 0;
  if (b > n)
    return n;
  return go(b, n / b) + n % b;
}
int main() {
  // ios::sync_with_stdio(false);cin.tie(0);
  ll n, s, ans = -1;
  cin >> n >> s;
  if (n <= 100) {
    forr(i, 2, n + 100) {
      // cout<<go(i,n)<<" "<<i<<endl;
      if (go(i, n) == s && ans == -1)
        ans = i;
    }
    cout << ans << endl;
  } else {
    // cout<<go((n/3),n)<<endl;
    set<ll> s2;
    s2.insert(s);
    ll posult;
    for (ll i = 1; i * i <= max(n, s); i++) {
      if (s % i == 0)
        s2.insert(i), s2.insert(s / i);
      if (n % i == 0)
        s2.insert(i), s2.insert(n / i);
      if ((n - s) % i == 0)
        s2.insert(i + 1), s2.insert(((n - s) / i) + 1);
      posult = i;
    }
    /*forr(i,posult,posult+100)
    cout<<go(i,n)<<" ";
    cout<<endl;*/
    for (ll i = 2; i * i <= max(n, s); i++) {
      if (go(i, n) == s && ans == -1)
        ans = i;
    }
    //     cout<<s2.size()<<endl;
    for (set<ll>::iterator ite = s2.begin(); ite != s2.end(); ite++) {
      if (go(*ite, n) == s && ans == -1)
        ans = *ite;
    }
    if (ans == -1 && go(n, n) == s)
      ans = n;
    if (ans == -1 && go(n + 1, n) == s)
      ans = n + 1;
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define forr(i, p, n) for (ll i = p; i < n; i++)
#define tam 100347
#define offset 50000
using namespace std;
typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ll, ii> iii;
const ll INF = 1e11;
ll go(ll b, ll n) {
  if (b == 1)
    return 0;
  if (b > n)
    return n;
  return go(b, n / b) + n % b;
}
int main() {
  // ios::sync_with_stdio(false);cin.tie(0);
  ll n, s, ans = -1;
  cin >> n >> s;
  if (n <= 100) {
    forr(i, 2, n + 100) {
      // cout<<go(i,n)<<" "<<i<<endl;
      if (go(i, n) == s && ans == -1)
        ans = i;
    }
    cout << ans << endl;
  } else {
    // cout<<go((n/3),n)<<endl;
    if (s > n) {
      cout << -1 << endl;
      return 0;
    }
    set<ll> s2;
    s2.insert(s);
    ll posult;
    for (ll i = 1; i * i <= max(n, s); i++) {
      if (s % i == 0)
        s2.insert(i), s2.insert(s / i);
      if (n % i == 0)
        s2.insert(i), s2.insert(n / i);
      if ((n - s) % i == 0)
        s2.insert(i + 1), s2.insert(((n - s) / i) + 1);
      posult = i;
    }
    /*forr(i,posult,posult+100)
    cout<<go(i,n)<<" ";
    cout<<endl;*/
    for (ll i = 2; i * i <= max(n, s); i++) {
      if (go(i, n) == s && ans == -1)
        ans = i;
    }
    //     cout<<s2.size()<<endl;
    for (set<ll>::iterator ite = s2.begin(); ite != s2.end(); ite++) {
      if (go(*ite, n) == s && ans == -1)
        ans = *ite;
    }
    if (ans == -1 && go(n, n) == s)
      ans = n;
    if (ans == -1 && go(n + 1, n) == s)
      ans = n + 1;
    cout << ans << endl;
  }
}