prithivMLmods/Coder-Stat huggingface dataset:
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define For(i, l, r)                                                           \
  for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l)                                                       \
  for (register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)

using namespace std;

typedef long long ll;

template <typename T> inline bool chkmin(T &a, T b) {
  return b < a ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, T b) {
  return b > a ? a = b, 1 : 0;
}

void File() {
#ifdef zjp_shadow
  freopen("B.in", "r", stdin);
  freopen("B.out", "w", stdout);
#endif
}

ll n, S;

inline bool Check(int val) {
  ll tmp = n, res = 0;
  for (; tmp; tmp /= val)
    res += tmp % val;
  return res == S;
}

int main() {

  File();

  cin >> n >> S;

  int gap = sqrt(n + .5);
  For(i, 2, gap) if (Check(i)) return printf("%d\n", i), 0;

  for (ll i = gap + 1, Nexti; i <= n; i = Nexti + 1) {
    Nexti = n / (n / i);
    ll res = (S - n);
    if (res % (n / i))
      continue;
    res = res / (n / i);
    ll b = -res + 1;
    if (i <= b && b <= Nexti)
      return printf("%lld\n", b), 0;
  }

  if (n == S)
    return printf("%lld\n", n + 1);

  puts("-1");

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define For(i, l, r)                                                           \
  for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l)                                                       \
  for (register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)

using namespace std;

typedef long long ll;

template <typename T> inline bool chkmin(T &a, T b) {
  return b < a ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, T b) {
  return b > a ? a = b, 1 : 0;
}

void File() {
#ifdef zjp_shadow
  freopen("B.in", "r", stdin);
  freopen("B.out", "w", stdout);
#endif
}

ll n, S;

inline bool Check(int val) {
  ll tmp = n, res = 0;
  for (; tmp; tmp /= val)
    res += tmp % val;
  return res == S;
}

int main() {

  File();

  cin >> n >> S;

  int gap = sqrt(n + .5);
  For(i, 2, gap) if (Check(i)) return printf("%d\n", i), 0;

  for (ll i = gap + 1, Nexti; i <= n; i = Nexti + 1) {
    Nexti = n / (n / i);
    ll res = (S - n);
    if (res % (n / i))
      continue;
    res = res / (n / i);
    ll b = -res + 1;
    if (i <= b && b <= Nexti)
      return printf("%lld\n", b), 0;
  }

  if (n == S)
    return printf("%lld\n", n + 1), 0;

  puts("-1");

  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
const ll INF = 1ll << 60;
using namespace std;

ll N, S;

ll function_f(ll b, ll n) {

  ll ans = 0;

  while (n >= b) {
    ans += n % b;
    n /= b;
  }

  return ans + n;
}

ll search() {
  if (N == S)
    return N + 1;
  ll ans = INF;

  for (int i = 2; i < sqrt(N) + 1; i++)
    if (function_f(i, N) == S)
      ans = min(ans, (ll)i);

  for (int i = 1; i < sqrt(N); i++) {
    ll B = (N - S) / i + 1;
    if (function_f(B, N) == S)
      ans = min(ans, B);
  }

  return (ans == INF ? -1 : ans);
}

int main() {

  cin >> N >> S;
  cout << search() << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
const ll INF = 1ll << 60;
using namespace std;

ll N, S;

ll function_f(ll b, ll n) {

  if (b < 2)
    return INF;

  ll ans = 0;

  while (n >= b) {
    ans += n % b;
    n /= b;
  }

  return ans + n;
}

ll search() {
  if (N == S)
    return N + 1;
  ll ans = INF;

  for (int i = 2; i < sqrt(N) + 1; i++)
    if (function_f(i, N) == S)
      ans = min(ans, (ll)i);

  for (int i = 1; i < sqrt(N); i++) {
    ll B = (N - S) / i + 1;
    if (function_f(B, N) == S)
      ans = min(ans, B);
  }

  return (ans == INF ? -1 : ans);
}

int main() {

  cin >> N >> S;
  cout << search() << endl;

  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

ll f(ll b, ll n) {
  ll sum = 0;

  while (n >= b) {
    sum += n % b;
    n /= b;
  }

  sum += n;

  return sum;
}

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

  ll ans = -1;

  if (n == s) {
    ans = n + 1;
  }

  for (ll i = 2; i * i <= n; ++i) {
    if (f(i, n) == s) {
      ans = i;
      break;
    }
  }

  for (ll i = 1; i * i < n; ++i) {
    ll b = (n - s) / i + 1;

    if (f(b, n) == s && (ans == -1 || ans > b)) {
      ans = b;
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

ll f(ll b, ll n) {
  ll sum = 0;

  while (n >= b) {
    sum += n % b;
    n /= b;
  }

  sum += n;

  return sum;
}

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

  ll ans = -1;

  if (n == s) {
    ans = n + 1;
  }

  for (ll i = 2; i * i <= n; ++i) {
    if (f(i, n) == s) {
      ans = i;
      break;
    }
  }

  for (ll i = 1; i * i < n; ++i) {
    ll b = (n - s) / i + 1;

    if (b < 2)
      continue;
    if (f(b, n) == s && (ans == -1 || ans > b)) {
      ans = b;
    }
  }

  cout << ans << endl;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
long long rec(long long b, long long n) {
  if (n < b)
    return n;
  return rec(b, n / b) + n % b;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, s;
  cin >> n >> s;
  if (s > n) {
    cout << -1 << endl;
    return 0;
  }
  if (s == n) {
    cout << n + 1 << endl;
    return 0;
  }
  for (long long b = 2; b * b <= n; b++) {
    if (s == rec(b, n)) {
      cout << b << endl;
      return 0;
    }
  }
  long long ans = 1LL << 60;
  for (long long p = 1; p * p <= n; p++) {
    long long b = (n - s) / p + 1;
    if (s == rec(b, n)) {
      ans = min(ans, b);
    }
  }
  cout << (ans == 1LL << 60 ? -1 : ans) << endl;
  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
long long rec(long long b, long long n) {
  if (n < b)
    return n;
  return rec(b, n / b) + n % b;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, s;
  cin >> n >> s;
  if (s > n) {
    cout << -1 << endl;
    return 0;
  }
  if (s == n) {
    cout << n + 1 << endl;
    return 0;
  }
  for (long long b = 2; b * b <= n; b++) {
    if (s == rec(b, n)) {
      cout << b << endl;
      return 0;
    }
  }
  long long ans = 1LL << 60;
  for (long long p = 1; p * p <= n; p++) {
    long long b = (n - s) / p + 1;
    if (b == 1)
      continue;
    if (s == rec(b, n)) {
      ans = min(ans, b);
    }
  }
  cout << (ans == 1LL << 60 ? -1 : ans) << endl;
  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  long long Sqt = 4e5;
  long long N, S;
  cin >> N >> S;

  // b smaller than sqrt(N)
  for (int b = 2; b < Sqt; b++) {
    long long num = N, sum = 0;
    while (num > 0) {
      sum += num % b;
      num /= b;
    }
    if (sum == S) {
      cout << b << '\n';
      return 0;
    }
  }

  // b between sqrtNn) and N
  long long ans = 5e16;
  for (int a = 0; a < Sqt && S - a > 0; a++) { // a = N % b
    long long b = (N - a) / (S - a);
    if (N / b + N % b == S && N / b / b == 0) {
      ans = min(ans, b);
    }
  }

  if (ans == 5e16 && S == N)
    ans = N + 1;
  if (ans == 5e16)
    ans = -1;
  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  long long Sqt = 4e5;
  long long N, S;
  cin >> N >> S;

  // b smaller than sqrt(N)
  for (int b = 2; b < Sqt; b++) {
    long long num = N, sum = 0;
    while (num > 0) {
      sum += num % b;
      num /= b;
    }
    if (sum == S) {
      cout << b << '\n';
      return 0;
    }
  }

  // b between sqrtNn) and N
  long long ans = 5e16;
  for (int x = 1; x < Sqt; x++) { // x = N / b
    long long b = (N - S + x) / x;
    if (b >= 2 && N / b + N % b == S && N / b / b == 0) {
      ans = min(ans, b);
    }
  }

  if (ans == 5e16 && S == N)
    ans = N + 1;
  if (ans == 5e16)
    ans = -1;
  cout << ans << '\n';
  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

constexpr ll INF = 1e18;

ll f(ll b, ll n) {
  if (n < b) {
    return n;
  } else {
    return f(b, n / b) + n % b;
  }
}

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

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

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

  ll ans = INF;
  for (ll c = 1; c * c <= n; c++) {
    if ((n - s) % c != 0) {
      continue;
    }
    ll b = (n - s) / c + 1;
    if (b * b <= n) {
      continue;
    }

    if (f(b, n) == s) {
      ans = min(ans, b);
    }
  }

  cout << (ans == INF ? -1 : ans) << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

constexpr ll INF = 1e18;

ll f(ll b, ll n) {
  if (n < b) {
    return n;
  } else {
    return f(b, n / b) + n % b;
  }
}

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

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

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

  ll ans = INF;
  for (ll c = 1; c * c <= n; c++) {
    if ((n - s) % c != 0) {
      continue;
    }
    ll b = (n - s) / c + 1;
    if (b <= sqrt(n)) {
      continue;
    }

    if (f(b, n) == s) {
      ans = min(ans, b);
    }
  }

  cout << (ans == INF ? -1 : ans) << endl;
  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

ll f(ll b, ll n) {
  if (n < b)
    return n;
  return f(b, n / b) + n % b;
}
int main() {
  ll n, s;
  scanf("%lld%lld", &n, &s);
  for (ll i = 2; i * i <= n; i++) { // nをi進表記
    if (f(i, n) == s) {
      printf("%lld\n", i);
      return 0;
    }
  }
  ll Min = LLONG_MAX;
  for (ll i = 1; i * i < n; i++) { // 上位桁を固定
    if (f((n - (s - i)) / i, n) == s)
      Min = min(Min, n - (s - i) / i);
  }
  printf("%lld\n", Min == LLONG_MAX ? -1 : Min);
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

ll f(ll b, ll n) {
  if (n < b)
    return n;
  return f(b, n / b) + n % b;
}
int main() {
  ll n, s;
  scanf("%lld%lld", &n, &s);
  for (ll i = 2; i * i <= n; i++) { // nをi進表記
    if (f(i, n) == s) {
      printf("%lld\n", i);
      return 0;
    }
  }
  ll Min = LLONG_MAX;
  for (ll i = 1; i * i < n; i++) { // 上位桁を固定
    ll b = (n - (s - i)) / i;
    if (b > 1 && f(b, n) == s)
      Min = min(Min, b);
  }
  if (Min == LLONG_MAX && n == s) {
    printf("%lld\n", n + 1);
    return 0;
  }
  printf("%lld\n", Min == LLONG_MAX ? -1 : Min);
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I, A, B) for (ll I = (A); I < (B); ++I)
#define N 100
const ll INF = 1e15 + 7;
ll n, s;

ll f(ll b, ll nn) {
  if (nn < b)
    return nn;
  return f(b, nn / b) + nn % b;
}

int main() {
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  FOR(i, 2, sqrt(n) + 1) {
    if (f(i, n) == s) {
      cout << i << endl;
      return 0;
    }
  }
  ll ans = INF;
  if (n - s <= 0) {
    cout << -1 << endl;
    return 0;
  }
  FOR(i, 1, sqrt(n) + 1) {
    if (f((n - s) / i + 1, n) == s) {
      ans = min((n - s) / i + 1, ans);
    }
  }
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I, A, B) for (ll I = (A); I < (B); ++I)
#define N 100
const ll INF = 1e15 + 7;
ll n, s;

ll f(ll b, ll nn) {
  if (nn < b)
    return nn;
  return f(b, nn / b) + nn % b;
}

int main() {
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  FOR(i, 2, sqrt(n) + 1) {
    if (f(i, n) == s) {
      cout << i << endl;
      return 0;
    }
  }
  ll ans = INF;
  if (n - s <= 1) {
    cout << -1 << endl;
    return 0;
  }
  FOR(i, 1, sqrt(n) + 1) {
    if (f((n - s) / i + 1, n) == s) {
      ans = min((n - s) / i + 1, ans);
    }
  }
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1e5 + 5, M = 6000, Z = 2501;
typedef long long ll;
ll sum(ll x, ll y) {
  ll res = 0;
  while (x) {
    res += x % y;
    x /= y;
  }
  return res;
}
int main() {
  ll N, S, b;
  scanf("%lld%lld", &N, &S);
  if (S > N) {
    puts("-1");
    return 0;
  }
  if (S == N) {
    printf("%lld\n", N + 1);
    return 0;
  }
  for (int i = 2; i * i <= N; i++) {
    if (sum(N, i) == S) {
      printf("%d\n", i);
      return 0;
    }
  }
  for (int i = sqrt(N - S); i >= 1; i--)
    if ((N - S) % i == 0) {
      b = (N - S) / i + 1;
      if (sum(N, b) == S) {
        printf("%lld\n", b);
        return 0;
      }
    }
  puts("-1");
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1e5 + 5, M = 6000, Z = 2501;
typedef long long ll;
ll sum(ll x, ll y) {
  ll res = 0;
  while (x) {
    res += x % y;
    x /= y;
  }
  return res;
}
int main() {
  ll N, S, b;
  scanf("%lld%lld", &N, &S);
  if (S > N) {
    puts("-1");
    return 0;
  }
  if (S == N) {
    printf("%lld\n", N + 1);
    return 0;
  }
  for (int i = 2; 1ll * i * i <= N; i++) {
    if (sum(N, i) == S) {
      printf("%d\n", i);
      return 0;
    }
  }
  for (int i = sqrt(N - S); i >= 1; i--)
    if ((N - S) % i == 0) {
      b = (N - S) / i + 1;
      if (sum(N, b) == S) {
        printf("%lld\n", b);
        return 0;
      }
    }
  puts("-1");
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int f(int b, int n) {
  int ret = 0;
  while (n) {
    ret += n % b;
    n /= b;
  }
  return ret;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, s;
  cin >> n >> s;
  if (s == n) {
    cout << n + 1 << endl;
    return 0;
  } else {
    rep(b, 2, 1 + sqrt(n + 1)) {
      if (f(b, n) == s) {
        cout << b << endl;
        return 0;
      }
    }
    int ans = INF;
    rep(p, 1, 1 + sqrt(n)) {
      if ((n - s) % p == 0) {
        int b = 1 + (n - s) / p;
        if (f(b, n) == s) {
          chmin(ans, b);
        }
      }
    }
    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int f(int b, int n) {
  int ret = 0;
  while (n) {
    ret += n % b;
    n /= b;
  }
  return ret;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, s;
  cin >> n >> s;
  if (s == n) {
    cout << n + 1 << endl;
    return 0;
  } else {
    rep(b, 2, 1 + sqrt(n + 1)) {
      if (f(b, n) == s) {
        cout << b << endl;
        return 0;
      }
    }
    int ans = INF;
    rep(p, 1, 1 + sqrt(n)) {
      if ((n - s) % p == 0) {
        int b = 1 + (n - s) / p;
        if (!(b >= 2))
          continue;
        if (f(b, n) == s) {
          chmin(ans, b);
        }
      }
    }
    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }

  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// typedef __int128_t LL;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define F first
#define S second
#define pb push_back
mt19937 rnd;

const ll inf = 1e18 + 10;

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  ll n, s;
  cin >> n >> s;
  ll ans = inf;
  for (ll b = 2; b * b <= n || b <= 100; b++) {
    if (f(b, n) == s) {
      ans = min(ans, b);
    }
  }
  if (n == s) {
    ans = min(ans, n + 1);
  }
  for (ll k = 1; k * k <= n; k++) {
    if ((n - s) % k == 0) {
      ll b = (n - s) / k + 1;
      if (f(b, n) == s) {
        ans = min(ans, b);
      }
    }
  }
  cout << (ans == inf ? -1 : ans) << "\n";

#ifdef LOCAL
  cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// typedef __int128_t LL;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define F first
#define S second
#define pb push_back
mt19937 rnd;

const ll inf = 1e18 + 10;

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  ll n, s;
  cin >> n >> s;
  ll ans = inf;
  for (ll b = 2; b * b <= n || b <= 100; b++) {
    if (f(b, n) == s) {
      ans = min(ans, b);
    }
  }
  if (n == s) {
    ans = min(ans, n + 1);
  }
  for (ll k = 1; k * k <= n && n > s; k++) {
    if ((n - s) % k == 0) {
      ll b = (n - s) / k + 1;
      if (f(b, n) == s) {
        ans = min(ans, b);
      }
    }
  }
  cout << (ans == inf ? -1 : ans) << "\n";

#ifdef LOCAL
  cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (n); ++i)
constexpr int mod = 1000000007;
constexpr int mod1 = 998244353;
vector<int> dx = {0, 1, 0, -1}, dy = {-1, 0, 1, 0};
bool inside(int y, int x, int h, int w) {
  if (y < h && y >= 0 && x < w && x >= 0)
    return true;
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  ll ans = -1;
  for (ll i = 2; i * i <= n; i++) {
    ll sum = 0, k = n;
    while (k > 0) {
      sum += k % i;
      k /= i;
    }
    if (sum == s) {
      ans = i;
      break;
    }
  }
  if (ans != -1) {
    cout << ans << endl;
    return 0;
  }
  for (ll i = pow(n, 0.5); i >= 1; i--) {
    if ((n - s) % i == 0) {
      ll sum = 0, k = n, m = (n - s) / i + 1;
      while (k > 0) {
        sum += k % m;
        k /= m;
      }
      if (sum == s) {
        ans = m;
        break;
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (n); ++i)
constexpr int mod = 1000000007;
constexpr int mod1 = 998244353;
vector<int> dx = {0, 1, 0, -1}, dy = {-1, 0, 1, 0};
bool inside(int y, int x, int h, int w) {
  if (y < h && y >= 0 && x < w && x >= 0)
    return true;
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  if (s > n) {
    cout << -1 << endl;
    return 0;
  }
  ll ans = -1;
  for (ll i = 2; i * i <= n; i++) {
    ll sum = 0, k = n;
    while (k > 0) {
      sum += k % i;
      k /= i;
    }
    if (sum == s) {
      ans = i;
      break;
    }
  }
  if (ans != -1) {
    cout << ans << endl;
    return 0;
  }
  for (ll i = pow(n, 0.5); i >= 1; i--) {
    if ((n - s) % i == 0) {
      ll sum = 0, k = n, m = (n - s) / i + 1;
      while (k > 0) {
        sum += k % m;
        k /= m;
      }
      if (sum == s) {
        ans = m;
        break;
      }
    }
  }
  cout << ans << endl;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll digit_sum(ll n, ll b) {
  if (n < b)
    return n;
  else
    return digit_sum(n / b, b) + (n % b);
}
vector<int> used[100005];
int main() {
  ll n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  if (n < s) {
    cout << -1 << endl;
    return 0;
  }
  for (ll i = 2; i * i <= n; i++) {
    if (digit_sum(n, i) == s) {
      cout << i << endl;
      return 0;
    }
  }
  ll ans = -1;
  for (ll p = 1; p * p <= n; p++) {
    ll b = ((n - s) / p) + 1;
    if (digit_sum(n, b) == s)
      ans = b;
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll digit_sum(ll n, ll b) {
  if (n < b)
    return n;
  else
    return digit_sum(n / b, b) + (n % b);
}
vector<int> used[100005];
int main() {
  ll n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  if (n < s) {
    cout << -1 << endl;
    return 0;
  }
  for (ll i = 2; i * i <= n; i++) {
    if (digit_sum(n, i) == s) {
      cout << i << endl;
      return 0;
    }
  }
  ll ans = -1;
  for (ll p = 1; p * p <= n; p++) {
    ll b = ((n - s) / p) + 1;
    if (b > 1) {
      if (digit_sum(n, b) == s)
        ans = b;
    }
  }
  cout << ans << endl;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
int f(ll b, ll n) {
  if (n < b)
    return n;
  else
    return f(b, n / b) + n % b;
}
int main() {
  ll n, s;
  cin >> n >> s;
  for (ll i = 2; i <= sqrt(n); i++)
    if (f(i, n) == s) {
      cout << i << "\n";
      return 0;
    }
  for (ll i = sqrt(n); i >= 1; i--) {
    ll a = f(n / i, n);
    if (a % i == s % i && 0 <= (s - a) / i &&
        (s - a) / i < n / i - n / (i + 1)) {
      cout << n / i - (s - a) / i << "\n";
      return 0;
    }
  }
  if (n == s) {
    cout << n + 1 << "\n";
    return 0;
  }
  cout << -1 << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
int f(ll b, ll n) {
  if (n < b)
    return n;
  else
    return f(b, n / b) + n % b;
}
int main() {
  ll n, s;
  cin >> n >> s;
  for (ll i = 2; i <= sqrt(n); i++)
    if (f(i, n) == s) {
      cout << i << "\n";
      return 0;
    }
  for (ll i = sqrt(n); i >= 1; i--) {
    if (n / i == 1)
      continue;
    ll a = f(n / i, n);
    if (a % i == s % i && 0 <= (s - a) / i &&
        (s - a) / i < n / i - n / (i + 1)) {
      cout << n / i - (s - a) / i << "\n";
      return 0;
    }
  }
  if (n == s) {
    cout << n + 1 << "\n";
    return 0;
  }
  cout << -1 << "\n";
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl

using namespace std;

typedef long long int lli;

template <typename T> vector<T> make_v(size_t a, T b) {
  return vector<T>(a, b);
}

template <typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

lli f(lli b, lli n) {
  if (n < b)
    return n;
  else
    return f(b, n / b) + n % b;
}

int main() {
  lli n, s;
  cin >> n >> s;
  const lli inf = 1LL << 60;
  lli ans = inf;
  if (n < s) {
    cout << -1 << endl;
    return 0;
  }
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  if ((n - s) % (n - 1) == 0 && f(n, n) == s) {
    ans = n;
  }
  for (lli b = 2; b * b <= n; b++) {
    if ((n - s) % (b - 1) == 0) {
      lli rest = (n - s) / (b - 1);
      if (f(b, n) == s) {
        ans = min(ans, b);
      }
      if (f(rest, n) == s) {
        ans = min(ans, b);
      }
    }
  }
  if (ans == inf) {
    ans = -1;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl

using namespace std;

typedef long long int lli;

template <typename T> vector<T> make_v(size_t a, T b) {
  return vector<T>(a, b);
}

template <typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

lli f(lli b, lli n) {
  if (n < b)
    return n;
  else
    return f(b, n / b) + n % b;
}

int main() {
  lli n, s;
  cin >> n >> s;
  const lli inf = 1LL << 60;
  lli ans = inf;
  if (n < s) {
    cout << -1 << endl;
    return 0;
  }
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  if ((n - s) % (n - 1) == 0 && f(n, n) == s) {
    ans = n;
  }
  for (lli b = 2; b * b <= n; b++) {
    if ((n - s) % (b - 1) == 0) {
      lli rest = (n - s) / (b - 1);
      if (f(b, n) == s) {
        ans = min(ans, b);
      }
      if (f(rest + 1, n) == s) {
        ans = min(ans, rest + 1);
      }
    }
  }
  if (ans == inf) {
    ans = -1;
  }
  cout << ans << endl;
  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair

using namespace std;
using ll = long long int;

ll func(ll B, ll N) {
  if (N < B) {
    return N;
  } else {
    return func(B, N / B) + N % B;
  }
  return 0;
}

int main() {
  ll n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  } else {
    ll ans = 1e11 + 1;
    for (ll i = 2; i <= sqrt(n); i++) {
      if (s == func(i, n)) {
        if (ans > i)
          ans = i;
      }
    }
    ll b;
    for (ll i = 1; i < sqrt(n); i++) {
      b = ((n - s) / i) + 1;
      if (b != 1) {
        if (s == func(b, n)) {
          if (ans > b)
            ans = b;
        }
      }
    }
    if (ans < 1e11 + 1) {
      cout << ans << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair

using namespace std;
using ll = long long int;

ll func(ll B, ll N) {
  if (N < B) {
    return N;
  } else {
    return func(B, N / B) + N % B;
  }
  return 0;
}

int main() {
  ll n, s;
  cin >> n >> s;
  if (n < s) {
  } else if (n == s) {
    cout << n + 1 << endl;
    return 0;
  } else {
    ll ans = 1e11 + 1;
    for (ll i = 2; i <= sqrt(n); i++) {
      if (s == func(i, n)) {
        if (ans > i)
          ans = i;
      }
    }
    ll b;
    for (ll i = 1; i < sqrt(n); i++) {
      b = ((n - s) / i) + 1;
      if (b != 1) {
        if (s == func(b, n)) {
          if (ans > b)
            ans = b;
        }
      }
    }
    if (ans < 1e11 + 1) {
      cout << ans << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

const double PI = 3.14159265358979323846;
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
// #define rep(i, n) for (ll i = 0; i < (n); ++i)
// typedef pair<ll, ll> P;
typedef pair<double, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

std::istream &operator>>(std::istream &in, set<int> &o) {
  ll a;
  in >> a;
  o.insert(a);
  return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
  ll a;
  in >> a;
  o.push(a);
  return in;
}

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

// ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
// ofstream outfile("log.txt");
// outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0')
// << rank << endl;
//  std::cout << std::bitset<8>(9);

// const ll mod = 1e10;
typedef priority_queue<string, vector<string>, greater<string>> PQ_ASK;

ll f(ll b, ll n) {
  if (n < b)
    return n;
  return f(b, n / b) + n % b;
}

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

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

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

  ll ans = INF;
  for (ll p = 1; p * p < n; p++) {
    ll b = (n - s) / p + 1;
    if (f(b, n) == s) {
      cmin(ans, b);
    }
  }

  cout << (ans == INF ? -1 : ans) << endl;
}


Fixed submission:
#include <bits/stdc++.h>

const double PI = 3.14159265358979323846;
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
// #define rep(i, n) for (ll i = 0; i < (n); ++i)
// typedef pair<ll, ll> P;
typedef pair<double, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

std::istream &operator>>(std::istream &in, set<int> &o) {
  ll a;
  in >> a;
  o.insert(a);
  return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
  ll a;
  in >> a;
  o.push(a);
  return in;
}

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

// ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
// ofstream outfile("log.txt");
// outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0')
// << rank << endl;
//  std::cout << std::bitset<8>(9);

// const ll mod = 1e10;
typedef priority_queue<string, vector<string>, greater<string>> PQ_ASK;

ll f(ll b, ll n) {
  if (n < b)
    return n;
  return f(b, n / b) + n % b;
}

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

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

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

  ll ans = INF;
  for (ll p = 1; p * p < n; p++) {
    ll b = (n - s) / p + 1;
    if (b < 2)
      continue;
    if (f(b, n) == s) {
      cmin(ans, b);
    }
  }

  cout << (ans == INF ? -1 : ans) << endl;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<ll, ll> P;
#define pb push_back
#define mp make_pair
#define fr(i, n) for (int i = 0; i < n; i++)
#define Fr(i, n) for (int i = 0; i++ < n;)
#define ifr(i, n) for (int i = n - 1; i >= 0; i--)
#define iFr(i, n) for (int i = n; i > 0; i--)

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

int main() {
  ll n, s, k, b;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  for (b = 2; b * b <= n; b++) {
    k = b;
    if (f(b, n) == s) {
      cout << b << endl;
      return 0;
    }
  }
  if (k * k == n)
    k--;
  for (ll a = k; a > 0; a--) {
    b = (n - s + a) / a;
    if (b < 2)
      continue;
    if (f(b, n) == s) {
      cout << b << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<ll, ll> P;
#define pb push_back
#define mp make_pair
#define fr(i, n) for (int i = 0; i < n; i++)
#define Fr(i, n) for (int i = 0; i++ < n;)
#define ifr(i, n) for (int i = n - 1; i >= 0; i--)
#define iFr(i, n) for (int i = n; i > 0; i--)

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

int main() {
  ll n, s, k = 2, b;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  for (b = 2; b * b <= n; b++) {
    k = b;
    if (f(b, n) == s) {
      cout << b << endl;
      return 0;
    }
  }
  if (k * k == n)
    k--;
  for (ll a = k; a > 0; a--) {
    b = (n - s + a) / a;
    if (b < 2)
      continue;
    if (f(b, n) == s) {
      cout << b << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}
Problem p04016 (C++)

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

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

ll sum(ll b, ll n) {
  if (b > n)
    return n;

  return sum(b, n / b) + n % b;
}

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

  ans = INF;

  ll sq = sqrt(n);
  if (sq * sq == n)
    sq--;

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

  for (ll i = 2; i <= sqrt(n); i++) {
    if (sum(i, n) == s) {
      ans = min(i, ans);
    }
  }

  for (ll i = 1; i <= sq; i++) {
    ll b = (n - s) / i + 1;
    if (b == 0)
      continue;
    if (sum(b, n) == s) {
      ans = min(b, ans);
    }
  }

  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}

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

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

ll sum(ll b, ll n) {
  if (b > n)
    return n;

  return sum(b, n / b) + n % b;
}

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

  ans = INF;

  ll sq = sqrt(n);
  if (sq * sq == n)
    sq--;

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

  for (ll i = 2; i <= sqrt(n); i++) {
    if (sum(i, n) == s) {
      ans = min(i, ans);
    }
  }

  for (ll i = 1; i <= sq; i++) {
    ll b = (n - s) / i + 1;
    if (b <= 1)
      continue;
    if (sum(b, n) == s) {
      ans = min(b, ans);
    }
  }

  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
/*بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم*/

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
ll n, s;
inline bool check(ll b) {
  if (b == 1)
    return false;
  ll sum = 0;
  ll m = n;
  while (m > 0) {
    sum += m % b;
    m /= b;
  }
  if (sum == s)
    return true;
  else
    return false;
}
int main() {
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  ll k = sqrt(n);
  ll ans = n + 1;
  for (ll i = 1; i <= k; i++) {
    if (check(i))
      ans = min(ans, i);
    if ((n - s) % i == 0) {
      ll t = (n - s) / i;
      if (check(t + 1))
        ans = min(ans, t + 1);
      if (i == k && check(k + 1))
        ans = min(ans, k + 1);
    }
  }
  if (ans == n + 1)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}


Fixed submission:
/*بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم*/

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
ll n, s;
inline bool check(ll b) {
  if (b < 2)
    return false;
  ll sum = 0;
  ll m = n;
  while (m > 0) {
    sum += m % b;
    m /= b;
  }
  if (sum == s)
    return true;
  else
    return false;
}
int main() {
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  ll k = sqrt(n);
  ll ans = n + 1;
  for (ll i = 1; i <= k; i++) {
    if (check(i))
      ans = min(ans, i);
    if ((n - s) % i == 0) {
      ll t = (n - s) / i;
      if (check(t + 1))
        ans = min(ans, t + 1);
      if (i == k && check(k + 1))
        ans = min(ans, k + 1);
    }
  }
  if (ans == n + 1)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}
Problem p04016 (C++)

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef pair<ll, ll> pll;
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
#define MOD 1000000007
#define ARRAY_MAX 100005

const ll INF = 1LL << 60;

ll calc(ll b, ll n) {

  if (n < b) {
    return n;
  } else {
    return calc(b, n / b) + n % b;
  }
}

int main() {

  ll n, s;
  cin >> n >> s;

  if (n < s) {
    cout << "-1" << endl;
    return 0;
  }
  if (n == s) {
    cout << s + 1 << endl;
    return 0;
  }
  ll mini = INF;

  for (ll i = 2; i * i <= n; i++) {
    if (calc(i, n) == s) {
      mini = min(mini, i);
    }
  }

  if (mini != INF) {
    cout << mini << endl;
    return 0;
  }
  // ここからは2 <= b <= sqrt(n)までに解がなかった場合

  for (ll p = 1; p * p < n; p++) {

    ll b = (n - s) / p + 1;
    if (b > 0 && calc(b, n) == s) {
      mini = min(mini, b);
    }
  }

  if (mini != INF) {
    cout << mini << endl;
  } else {
    cout << "-1" << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef pair<ll, ll> pll;
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
#define MOD 1000000007
#define ARRAY_MAX 100005

const ll INF = 1LL << 60;

ll calc(ll b, ll n) {

  if (n < b) {
    return n;
  } else {
    return calc(b, n / b) + n % b;
  }
}

int main() {

  ll n, s;
  cin >> n >> s;

  if (n < s) {
    cout << "-1" << endl;
    return 0;
  }
  if (n == s) {
    cout << s + 1 << endl;
    return 0;
  }
  ll mini = INF;

  for (ll i = 2; i * i <= n; i++) {
    if (calc(i, n) == s) {
      mini = min(mini, i);
    }
  }

  if (mini != INF) {
    cout << mini << endl;
    return 0;
  }
  // ここからは2 <= b <= sqrt(n)までに解がなかった場合

  for (ll p = 1; p * p < n; p++) {

    ll b = (n - s) / p + 1;
    if (b == 1) {
      continue;
    }
    if (calc(b, n) == s) {
      mini = min(mini, b);
    }
  }

  if (mini != INF) {
    cout << mini << endl;
  } else {
    cout << "-1" << endl;
  }

  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

typedef long long int64;

int64 f(int64 b, int64 n) {
  if (n < b)
    return (n);
  return (f(b, n / b) + n % b);
}

int64 Search(int64 N, int64 S) {
  if (N == S)
    return (N + 1);
  for (int64 i = 2; i * i <= N; i++) {
    if (f(i, N) == S)
      return (i);
  }
  for (int64 i = sqrt(N); i > 0; i--) {
    if ((N - S) % i == 0 && f((N - S) / i + 1, N) == S)
      return ((N - S) / i + 1);
  }
  return (-1);
}

int main() {
  int64 N, S;
  cin >> N >> S;
  cout << Search(N, S) << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long int64;

int64 f(int64 b, int64 n) {
  if (n < b)
    return (n);
  return (f(b, n / b) + n % b);
}

int64 Search(int64 N, int64 S) {
  if (N < S)
    return (-1);
  if (N == S)
    return (N + 1);
  for (int64 i = 2; i * i <= N; i++) {
    if (f(i, N) == S)
      return (i);
  }
  for (int64 i = sqrt(N); i > 0; i--) {
    if ((N - S) % i == 0 && f((N - S) / i + 1, N) == S)
      return ((N - S) / i + 1);
  }
  return (-1);
}

int main() {
  int64 N, S;
  cin >> N >> S;
  cout << Search(N, S) << endl;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()
// #pragma GCC optimize ("-O3")
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
ll f(ll b, ll n) {
  if (n < b)
    return n;
  return f(b, n / b) + n % b;
}

void _main() {
  ll n, s;
  cin >> n >> s;

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

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

  ll p = 0;
  while (p * p < n)
    ++p;
  --p;

  for (; p > 0; --p) {
    ll b = (n - s) / p + 1;
    if (f(b, n) == s) {
      cout << b << endl;
      return;
    }
  }

  cout << -1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()
// #pragma GCC optimize ("-O3")
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
ll f(ll b, ll n) {
  if (n < b)
    return n;
  return f(b, n / b) + n % b;
}

void _main() {
  ll n, s;
  cin >> n >> s;

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

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

  ll p = 0;
  while (p * p < n)
    ++p;
  --p;

  for (; p > 0; --p) {
    ll b = (n - s) / p + 1;
    if (b == 1)
      continue;
    if (f(b, n) == s) {
      cout << b << endl;
      return;
    }
  }

  cout << -1 << endl;
}