Problem p04016 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <chrono>
#include <climits>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define debug(x) cerr << #x << ": " << x << ", "
#define debugln(x) cerr << #x << ": " << x << '\n'

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using namespace std;
using ll = long long;
using P = pair<int, int>;

constexpr ll mod = 1e9 + 7;
const double PI = acos(-1.0);
mt19937_64 engine(chrono::steady_clock::now().time_since_epoch().count());

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  ll n, s;
  cin >> n >> s;
  if (n < s) {
    cout << -1 << endl;
    return 0;
  }
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }

  for (ll b = 2; b * b <= n; b++) {
    ll cnt = 0;
    auto tmp = n;
    while (tmp) {
      cnt += tmp % b;
      tmp /= b;
    }
    if (cnt == s) {
      cout << b << endl;
      return 0;
    }
  }

  ll ans = LLONG_MAX;
  for (ll p = 1; p * p < n; p++) {
    ll b = (n - s) / p + 1;
    ll cnt = 0;
    auto tmp = n;
    while (tmp) {
      cnt += tmp % b;
      tmp /= b;
    }
    if (cnt == s)
      chmin(ans, b);
  }

  if (ans == LLONG_MAX) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <chrono>
#include <climits>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define debug(x) cerr << #x << ": " << x << ", "
#define debugln(x) cerr << #x << ": " << x << '\n'

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using namespace std;
using ll = long long;
using P = pair<int, int>;

constexpr ll mod = 1e9 + 7;
const double PI = acos(-1.0);
mt19937_64 engine(chrono::steady_clock::now().time_since_epoch().count());

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  ll n, s;
  cin >> n >> s;
  if (n < s) {
    cout << -1 << endl;
    return 0;
  }
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }

  for (ll b = 2; b * b <= n; b++) {
    ll cnt = 0;
    auto tmp = n;
    while (tmp) {
      cnt += tmp % b;
      tmp /= b;
    }
    if (cnt == s) {
      cout << b << endl;
      return 0;
    }
  }

  ll ans = LLONG_MAX;
  for (ll p = 1; p * p < n; p++) {
    ll b = (n - s) / p + 1;
    if (b <= n / b)
      continue;
    ll cnt = 0;
    auto tmp = n;
    while (tmp) {
      cnt += tmp % b;
      tmp /= b;
    }
    if (cnt == s)
      chmin(ans, b);
  }

  if (ans == LLONG_MAX) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}