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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

ll sum;
ll floor(ll b_, ll n_) {
  if (n_ < b_)
    return n_;
  else {
    return floor(b_, n_ / b_) + n_ % b_;
  }
}

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

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

  for (ll i = 2; i <= sqrt(n) + 1; i++) {
    if (s == floor(i, n)) {
      cout << i << endl;
      return 0;
    }
  }

  if (n - s > 0) {
    for (ll i = sqrt(n); i > 0; i--) {
      if (s == floor((n - s) / i + 1, n)) {
        cout << (n - s) / i + 1 << endl;
        return 0;
      }
    }
  }

  cout << "-1\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

ll sum;
ll floor(ll b_, ll n_) {
  if (n_ < b_)
    return n_;
  else {
    return floor(b_, n_ / b_) + n_ % b_;
  }
}

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

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

  for (ll i = 2; i <= sqrt(n) + 1; i++) {
    if (s == floor(i, n)) {
      cout << i << endl;
      return 0;
    }
  }

  if (n - s > 1) {
    for (ll i = sqrt(n); i > 0; i--) {
      if (s == floor((n - s) / i + 1, n)) {
        cout << (n - s) / i + 1 << endl;
        return 0;
      }
    }
  }

  cout << "-1\n";
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>

using namespace std;
using ll = long long;
int main() { // 答え見た後
  ll n, s;
  cin >> n >> s;
  if (n == s) {
    cout << (n + 1);
    return 0;
  }
  ll sqrtn = (ll)sqrt((double)n);
  for (ll b = 2; b <= sqrtn; ++b) {
    ll val = 0;
    ll m = n;
    while (m != 0) {
      val += m % b;
      m = m / b;
    }
    if (val == s) {
      cout << b;
      return 0;
    }
  }
  for (ll p = sqrtn; p >= 1; --p) {
    if ((n - s) % p != 0)
      continue;
    ll b = (n - s) / p + 1;

    ll val = 0;
    ll m = n;
    while (m != 0) {
      val += m % b;
      m = m / b;
    }
    if (val == s) {
      cout << b;
      return 0;
    }
  }
  if (s == 1)
    cout << n;
  else
    cout << "-1";
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>

using namespace std;
using ll = long long;
int main() { // 答え見た後
  ll n, s;
  cin >> n >> s;
  if (n == s) {
    cout << (n + 1);
    return 0;
  } else if (n < s) {
    cout << "-1";
    return 0;
  }
  ll sqrtn = (ll)sqrt((double)n);
  for (ll b = 2; b <= sqrtn; ++b) {
    ll val = 0;
    ll m = n;
    while (m != 0) {
      val += m % b;
      m = m / b;
    }
    if (val == s) {
      cout << b;
      return 0;
    }
  }
  for (ll p = sqrtn; p >= 1; --p) {
    if ((n - s) % p != 0)
      continue;
    ll b = (n - s) / p + 1;

    ll val = 0;
    ll m = n;
    while (m != 0) {
      val += m % b;
      m = m / b;
    }
    if (val == s) {
      cout << b;
      return 0;
    }
  }
  if (s == 1)
    cout << n;
  else
    cout << "-1";
  return 0;
}
Problem p04016 (C++)

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

#define REP(b, n) for (int b = 0; b < (n); ++b)
#define REP1(b, n) for (int b = 1; b <= (n); ++b)
#define RREP(b, n) for (int b = (n)-1; b >= 0; --b)
#define ALL(a) begin(a), end(a)
#define MP(a, b) make_pair((a), (b))
#define ROUNDUP(a, b) ((a) / (b) + ((a) % (b) == 0 ? 0 : 1))
#define X first
#define Y second
#define DEBUG(a) cout << #a << ": " << a << "\n"
// #define DEBUG(a) {}

using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;

constexpr int MOD = 1e9 + 7;
constexpr int INF = 1e9 + 1;
constexpr ll LLINF = 4 * 1e18 + 1;
// constexpr int INF = 2147483647; // 2 * 1e9
// constexpr ll LLINF = 9223372036854775807; // 9 * 1e18

int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0};

template <typename T> T debug(T x) {
  DEBUG(x);
  return x;
}

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << "\n";
    return 0;
  }
  ll rn = sqrt(n);
  for (ll b = 2; b <= rn; ++b) {
    if (f(b, n) == s) {
      cout << b << "\n";
      return 0;
    }
  }
  for (ll k = rn; k >= 1; --k) {
    ll b = (n - s) / k + 1;
    if (f(b, n) == s) {
      cout << b << "\n";
      return 0;
    }
  }
  cout << "-1\n";
  return 0;
}


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

#define REP(b, n) for (int b = 0; b < (n); ++b)
#define REP1(b, n) for (int b = 1; b <= (n); ++b)
#define RREP(b, n) for (int b = (n)-1; b >= 0; --b)
#define ALL(a) begin(a), end(a)
#define MP(a, b) make_pair((a), (b))
#define ROUNDUP(a, b) ((a) / (b) + ((a) % (b) == 0 ? 0 : 1))
#define X first
#define Y second
#define DEBUG(a) cout << #a << ": " << a << "\n"
// #define DEBUG(a) {}

using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;

constexpr int MOD = 1e9 + 7;
constexpr int INF = 1e9 + 1;
constexpr ll LLINF = 4 * 1e18 + 1;
// constexpr int INF = 2147483647; // 2 * 1e9
// constexpr ll LLINF = 9223372036854775807; // 9 * 1e18

int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0};

template <typename T> T debug(T x) {
  DEBUG(x);
  return x;
}

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << "\n";
    return 0;
  }
  ll rn = sqrt(n);
  for (ll b = 2; b <= rn; ++b) {
    if (f(b, n) == s) {
      cout << b << "\n";
      return 0;
    }
  }
  for (ll k = rn; k >= 1; --k) {
    ll b = (n - s) / k + 1;
    if (f(b, n) == s) {
      cout << b << "\n";
      return 0;
    }
  }
  cout << "-1\n";
  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;

long long f(long long b, long long n) {
  return b > n ? n : f(b, n / b) + n % b;
}

int main() {
  long long n, s, m;
  scanf("%lld%lld", &n, &s);
  if (n == s) {
    printf("%lld\n", n + 1);
    return 0;
  }
  m = sqrt(n);
  for (long long b = 2; b <= m; b++)
    if (f(b, n) == s) {
      printf("%lld\n", b);
      return 0;
    }
  for (long long p = m - 1, b = (p >= 1 ? (n - s) / p + 1 : 0); p >= 1;
       p--, b = (p >= 1 ? (n - s) / p + 1 : 0))
    if ((n - s) % p == 0 && n / b == p && p <= s) {
      printf("%lld\n", b);
      return 0;
    }
  if (s == 1) {
    printf("%lld\n", n);
    return 0;
  }
  puts("-1");

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;

long long f(long long b, long long n) {
  return b > n ? n : f(b, n / b) + n % b;
}

int main() {
  long long n, s, m;
  scanf("%lld%lld", &n, &s);
  if (n == s) {
    printf("%lld\n", n + 1);
    return 0;
  }
  if (n < s) {
    puts("-1");
    return 0;
  }
  m = sqrt(n);
  for (long long b = 2; b <= m; b++)
    if (f(b, n) == s) {
      printf("%lld\n", b);
      return 0;
    }
  for (long long p = m - 1, b = (p >= 1 ? (n - s) / p + 1 : 0); p >= 1;
       p--, b = (p >= 1 ? (n - s) / p + 1 : 0))
    if ((n - s) % p == 0 && n / b == p && p <= s) {
      printf("%lld\n", b);
      return 0;
    }
  if (s == 1) {
    printf("%lld\n", n);
    return 0;
  }
  puts("-1");

  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
template <typename T> inline void read(T &x) {
  x = 0;
  static int p;
  p = 1;
  static char c;
  c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - '0');
    c = getchar();
  }
  x *= p;
}
long long n, s;
long long f(long long b, long long j) {
  if (j < b)
    return j;
  return f(b, j / b) + j % b;
}
int main() {
  read(n);
  read(s);
  if (s == n)
    return printf("%lld\n", n + 1);
  long long S = sqrt(n) + 1;
  for (int i = 2; i <= S; i++)
    if (f(i, n) == s)
      return printf("%d\n", i), 0;
  long long ans = 1e11 + 1;
  for (long long i = 1; i * i <= n - s; i++)
    if ((n - s) % i == 0 && f((n - s) / i + 1, n) == s)
      ans = min(ans, (n - s) / i + 1);
  if (ans == 1e11 + 1)
    puts("-1");
  else
    printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <typename T> inline void read(T &x) {
  x = 0;
  static int p;
  p = 1;
  static char c;
  c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - '0');
    c = getchar();
  }
  x *= p;
}
long long n, s;
long long f(long long b, long long j) {
  if (j < b)
    return j;
  return f(b, j / b) + j % b;
}
int main() {
  read(n);
  read(s);
  if (s == n)
    return printf("%lld\n", n + 1), 0;
  long long S = sqrt(n) + 1;
  for (int i = 2; i <= S; i++)
    if (f(i, n) == s)
      return printf("%d\n", i), 0;
  long long ans = 1e11 + 1;
  for (long long i = 1; i * i <= n - s; i++)
    if ((n - s) % i == 0 && f((n - s) / i + 1, n) == s)
      ans = min(ans, (n - s) / i + 1);
  if (ans == 1e11 + 1)
    puts("-1");
  else
    printf("%lld\n", ans);
  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int MOD = 1000000007;
const int INF = 1000000000;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> P;
ll n, s;
ll minans = 1e12;

ll anydecimalB(ll n, ll decimal) {
  ll ret = 0;
  // string expre="";
  while (n >= decimal) {
    ret += (n % decimal);
    // expre=expre+to_string(n%decimal);
    n /= decimal;
  }
  if (n > 0) {
    ret += n;
    // expre=expre+to_string(n);
  }
  // reverse(expre.begin(),expre.end());
  // cout << ret << endl;
  // cout << expre << endl;
  return ret;
}
int main(void) {
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
  } else {
    for (ll i = 2; i <= sqrt(n); i++) {
      if (anydecimalB(n, i) == s) {
        cout << i << endl;
        return 0;
      }
    }
    for (ll p = 1; p < sqrt(n); p++) {
      ll b = (n - s) / p + 1;
      if (anydecimalB(n, b) == s) {
        minans = b;
      }
    }
    if (minans == 1e12) {
      cout << -1 << endl;
    } else {
      cout << minans << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
const int MOD = 1000000007;
const int INF = 1000000000;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> P;
ll n, s;
ll minans = 1e12;

ll anydecimalB(ll n, ll decimal) {
  ll ret = 0;
  // string expre="";
  while (n >= decimal) {
    ret += (n % decimal);
    // expre=expre+to_string(n%decimal);
    n /= decimal;
  }
  if (n > 0) {
    ret += n;
    // expre=expre+to_string(n);
  }
  // reverse(expre.begin(),expre.end());
  // cout << ret << endl;
  // cout << expre << endl;
  return ret;
}
int main(void) {
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
  } else {
    for (ll i = 2; i <= sqrt(n); i++) {
      if (anydecimalB(n, i) == s) {
        cout << i << endl;
        return 0;
      }
    }
    for (ll p = 1; p < sqrt(n); p++) {
      if ((n - s) > 0) {
        ll b = (n - s) / p + 1;
        // cout << b << endl;
        if (b == 1 && n == s) {
          minans = min(minans, n);
        } else if (b == 1 && n != s)
          continue;
        else if (anydecimalB(n, b) == s) {
          minans = b;
        }
      }
    }
    if (minans == 1e12) {
      cout << -1 << endl;
    } else {
      cout << minans << endl;
    }
  }
  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const ll kInf = 1LL << 62;
ll n, s, sol = kInf;

inline void Check(ll base) {
  ll sum = 0;
  for (ll x = n; x; x /= base)
    sum += x % base;
  if (sum == s)
    sol = min(sol, base);
}

ll Solve() {
  if (n < s)
    return -1;
  if (n == s)
    return n + 1;
  ll diff = n - s;
  for (ll i = 1; i * i <= diff; ++i) {
    if (diff % i)
      continue;
    Check(i + 1);
    Check(diff / i + 1);
  }
  return sol == kInf ? -1 : sol;
}

int main() {
  scanf("%lld%lld", &n, &s);
  return printf("%lld\n", Solve());
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const ll kInf = 1LL << 62;
ll n, s, sol = kInf;

inline void Check(ll base) {
  ll sum = 0;
  for (ll x = n; x; x /= base)
    sum += x % base;
  if (sum == s)
    sol = min(sol, base);
}

ll Solve() {
  if (n < s)
    return -1;
  if (n == s)
    return n + 1;
  ll diff = n - s;
  for (ll i = 1; i * i <= diff; ++i) {
    if (diff % i)
      continue;
    Check(i + 1);
    Check(diff / i + 1);
  }
  return sol == kInf ? -1 : sol;
}

int main() {
  scanf("%lld%lld", &n, &s);
  return printf("%lld\n", Solve()), 0;
}
Problem p04016 (C++)

Buggy submission (status: Memory Limit Exceeded):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

int main() {
  long long n, s;
  cin >> n >> s;
  if (n == s) {
    cout << (n + 1) << endl;
    return 0;
  }

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

  // n == x * b + y
  // s == x + y
  long long ans = -1;
  for (long long x = 1; x * x < n; ++x) {
    long long b = (n - s) / x + 1;
    if (f(b, n) == s)
      ans = b;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

int main() {
  long long n, s;
  cin >> n >> s;
  if (n == s) {
    cout << (n + 1) << endl;
    return 0;
  }

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

  // n == x * b + y
  // s == x + y
  long long ans = -1;
  for (long long x = 1; x * x < n; ++x) {
    long long b = (n - s) / x + 1;
    if (b >= 2 && f(b, n) == s)
      ans = b;
  }
  cout << ans << endl;

  return 0;
}
Problem p04016 (C++)

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

#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#define ALL(a) (a).begin(), (a).end()
#define Yes(hoge) cout << ((hoge) ? "Yes" : "No") << endl;
#define YES(hoge) cout << ((hoge) ? "YES" : "NO") << endl;
typedef struct aaaaa {
  int x, y, t;
} Grid;
using namespace std;
ll calc(ll b, ll n) {
  ll ret = 0;
  while (n >= b) {
    ret += n % b;
    n /= b;
  }
  return ret + n;
}
int main() {
  ll n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  for (ll i = 2; i <= sqrt(n); i++) {
    if (s == calc(i, n)) {
      cout << i << endl;
      return 0;
    }
  }
  ll ans = llINF;
  for (ll i = 1; i < sqrt(n); i++) {
    if ((n - s) % i != 0)
      continue;
    ll b = (n - s) / i + 1;
    if (s == calc(b, n)) {
      ans = min(ans, b);
    }
  }
  if (ans == llINF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}


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

#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#define ALL(a) (a).begin(), (a).end()
#define Yes(hoge) cout << ((hoge) ? "Yes" : "No") << endl;
#define YES(hoge) cout << ((hoge) ? "YES" : "NO") << endl;
typedef struct aaaaa {
  int x, y, t;
} Grid;
using namespace std;
ll calc(ll b, ll n) {
  ll ret = 0;
  while (n >= b) {
    ret += n % b;
    n /= b;
  }
  return ret + n;
}
int main() {
  ll n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  for (ll i = 2; i <= sqrt(n); i++) {
    if (s == calc(i, n)) {
      cout << i << endl;
      return 0;
    }
  }
  ll ans = llINF;
  for (ll i = 1; i < sqrt(n); i++) {
    if ((n - s) % i != 0)
      continue;
    ll b = (n - s) / i + 1;
    if (b > 0) {
      if (s == calc(b, n)) {
        ans = min(ans, b);
      }
    }
  }
  if (ans == llINF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
long long n, a, cha, ko[111111], cnt, ts, cur, pp;
int main() {
  cin >> n >> a;
  cha = n - a;
  if (cha < 0)
    cout << -1 << endl;
  else if (!cha)
    cout << n + 1 << endl;
  else {
    cnt = 1;
    for (int i = 1; i <= sqrt(cha); i++) {
      if (cha % i == 0) {
        ko[++cnt] = i;
        cur = 0;
        ts = n;
        while (ts) {
          cur += (ts % (i + 1));
          ts /= (i + 1);
        }
        if (cur == a) {
          cout << i + 1 << endl;
          return 0;
        }
      }
    }
    for (int i = cnt; i >= 1; i--) {
      pp = cha / ko[i];
      cur = 0;
      ts = n;
      while (ts) {
        cur += (ts % (pp + 1));
        ts /= (pp + 1);
      }
      if (cur == a) {
        cout << pp + 1 << endl;
        return 0;
      }
    }
    cout << -1 << endl;
  }
  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
long long n, a, cha, ko[111111], cnt, ts, cur, pp;
int main() {
  cin >> n >> a;
  cha = n - a;
  if (cha < 0)
    cout << -1 << endl;
  else if (!cha)
    cout << n + 1 << endl;
  else {
    for (int i = 1; i <= sqrt(cha); i++) {
      if (cha % i == 0) {
        ko[++cnt] = i;
        cur = 0;
        ts = n;
        while (ts) {
          cur += (ts % (i + 1));
          ts /= (i + 1);
        }
        if (cur == a) {
          cout << i + 1 << endl;
          return 0;
        }
      }
    }
    for (int i = cnt; i >= 1; i--) {
      pp = cha / ko[i];
      cur = 0;
      ts = n;
      while (ts) {
        cur += (ts % (pp + 1));
        ts /= (pp + 1);
      }
      if (cur == a) {
        cout << pp + 1 << endl;
        return 0;
      }
    }
    cout << -1 << endl;
  }
  return 0;
}
Problem p04016 (C++)

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

#define MaxN 100005

using namespace std;

long long S, N;
int main() {
  cin >> N >> S;
  for (long long i = 2; i * i <= N; i++) {
    long long X = N, sum = 0;
    while (X) {
      sum += X % i;
      X = X / i;
    }
    if (S == sum) {
      cout << i;
      return 0;
    }
  }
  long long d;
  for (d = 1; d * d <= (N - S); d++) {
    long long X = N, sum = 0;
    while (X) {
      sum += X % d;
      X = X / d;
    }
    if (S == sum) {
      cout << d + 1;
      return 0;
    }
  }
  d--;
  while (d) {
    long long X = N, sum = 0, i = (N - S) / d + 1;
    while (X) {
      sum += X % i;
      X = X / i;
    }
    if (S == sum) {
      cout << i;
      return 0;
    }
    d--;
  }
  if (N == S)
    cout << N + 1;
  else
    cout << -1;
  return 0;
}


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

#define MaxN 100005

using namespace std;

long long S, N;
int main() {
  cin >> N >> S;
  for (long long i = 2; i * i <= N; i++) {
    long long X = N, sum = 0;
    while (X) {
      sum += X % i;
      X = X / i;
    }
    if (S == sum) {
      cout << i;
      return 0;
    }
  }
  long long d;
  for (d = 1; d * d <= (N - S); d++) {
    long long X = N, sum = 0;
    while (X) {
      sum += X % (d + 1);
      X = X / (d + 1);
    }
    if (S == sum) {
      cout << d + 1;
      return 0;
    }
  }
  d--;
  while (d) {
    long long X = N, sum = 0, i = (N - S) / d + 1;
    while (X) {
      sum += X % i;
      X = X / i;
    }
    if (S == sum) {
      cout << i;
      return 0;
    }
    d--;
  }
  if (N == S)
    cout << N + 1;
  else
    cout << -1;
  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;
const int INF = sizeof(int) == sizeof(long long) ? LLONG_MAX : INT_MAX;
const int MOD = 1000000007;
const long double EPS = 1e-8;

int fun(int b, int n) { return n < b ? n : fun(b, n / b) + n % b; }

signed main() {
  int n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  int ans = INF;
  range(b, 2, sqrt(n) + 1) {
    int tmp = fun(b, n);
    if (s == tmp)
      ans = min(ans, b);
  }
  range(p, 1, sqrt(n)) {
    int b = (n - s) / p + 1;
    if (fun(b, n) == s)
      ans = min(ans, b);
  }
  cout << (ans == INF ? -1 : ans) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;
const int INF = sizeof(int) == sizeof(long long) ? LLONG_MAX : INT_MAX;
const int MOD = 1000000007;
const long double EPS = 1e-8;

int fun(int b, int n) {
  return b < 2 ? INF : n < b ? n : fun(b, n / b) + n % b;
}

signed main() {
  int n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  int ans = INF;
  range(b, 2, sqrt(n) + 1) {
    int tmp = fun(b, n);
    if (s == tmp)
      ans = min(ans, b);
  }
  range(p, 1, sqrt(n)) {
    int b = (n - s) / p + 1;
    if (fun(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>
using namespace std;

long long n, s;

bool valid(long long num) {
  long long sum = 0;
  long long tmp = n;
  while (tmp) {
    sum += tmp % num;
    tmp /= num;
  }
  return (s == sum);
}

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

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

  for (long long i = 2; i * i <= n; i++) {
    long long tmp = n;
    long long sum = 0;
    while (tmp) {
      sum += tmp % i;
      tmp /= i;
    }
    if (sum == s) {
      cout << i << endl;
      return 0;
    }
  }

  // cout << "debug0" << endl;

  long long ans = LONG_MAX;

  for (long long x = 1; x * x <= n; x++) {
    long long y = s - x;
    long long b = (n - y) / x;
    if (valid(b))
      ans = min(ans, b);
  }

  if (ans == LONG_MAX) {
    cout << -1 << endl;
    return 0;
  }
  cout << ans << endl;
}

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

long long n, s;

bool valid(long long num) {
  if (num < 2)
    return false;
  long long sum = 0;
  long long tmp = n;
  while (tmp) {
    sum += tmp % num;
    tmp /= num;
  }
  return (s == sum);
}

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

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

  for (long long i = 2; i * i <= n; i++) {
    long long tmp = n;
    long long sum = 0;
    while (tmp) {
      sum += tmp % i;
      tmp /= i;
    }
    if (sum == s) {
      cout << i << endl;
      return 0;
    }
  }

  // cout << "debug0" << endl;

  long long ans = LONG_MAX;

  for (long long x = 1; x * x <= n; x++) {
    long long y = s - x;
    long long b = (n - y) / x;
    if (valid(b))
      ans = min(ans, b);
  }

  if (ans == LONG_MAX) {
    cout << -1 << endl;
    return 0;
  }
  cout << ans << endl;
}
Problem p04016 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
#define ll long long
using namespace std;

ll n, s;

ll check(ll n, ll b) {
  ll r = 0;
  for (; n; n /= b)
    r += n % b;
  return r;
}

ll f(ll n, ll s) {

  if (n == s)
    return n + 1;
  if (n < s)
    return -1;

  for (ll i = 2; i <= sqrt(n); i++) {
    if (check(n, i) == s)
      return i;
  }

  n -= s;

  for (ll i = sqrt(n + s) + 1; i; i--) {
    if (check(n + s, n / i + 1) == s)
      return n / i + 1;
  }

  return -1;
}

int main() {

  scanf("%lld%lld", &n, &s);
  printf("%lld\n", f(n, s));

  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#define ll long long
using namespace std;

ll n, s;

ll check(ll n, ll b) {
  ll r = 0;
  for (; n; n /= b)
    r += n % b;
  return r;
}

ll f(ll n, ll s) {

  if (n == s)
    return n + 1;
  if (n < s)
    return -1;

  for (ll i = 2; i <= sqrt(n); i++) {
    if (check(n, i) == s)
      return i;
  }

  n -= s;

  for (ll i = sqrt(n + s) + 1; i; i--) {
    if (n % i == 0 && n / i > 0 && check(n + s, n / i + 1) == s)
      return n / i + 1;
  }

  return -1;
}

int main() {

  scanf("%lld%lld", &n, &s);
  printf("%lld\n", f(n, s));

  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#define INT unsigned long long

INT n, s, b, ans = -1;

inline INT f(INT x, INT y) {
  INT ret = 0;
  if (y == 1)
    return x;
  while (x) {
    ret += x % y;
    x /= y;
  }
  return ret;
}

int main() {
  scanf("%lld%lld", &n, &s);
  if (n == s) {
    printf("%lld\n", n + 1);
    return 0;
  }
  for (INT i = 2; i * i <= n; i++) {
    if (f(n, i) == s) {
      printf("%lld\n", i);
      return 0;
    }
  }
  for (INT i = 1; i * i <= n; i++) {
    if (f(n, (n - s) / i + 1) == s)
      ans = (n - s) / i + 1;
  }
  printf("%lld\n", ans);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#define INT unsigned long long

INT n, s, b, ans = -1;

inline INT f(INT x, INT y) {
  INT ret = 0;
  if (y == 1)
    return x;
  while (x) {
    ret += x % y;
    x /= y;
  }
  return ret;
}

int main() {
  scanf("%lld%lld", &n, &s);
  if (n < s) {
    puts("-1");
    return 0;
  }
  if (n == s) {
    printf("%lld\n", n + 1);
    return 0;
  }
  for (INT i = 2; i * i <= n; i++) {
    if (f(n, i) == s) {
      printf("%lld\n", i);
      return 0;
    }
  }
  for (INT i = 1; i * i <= n; i++) {
    if (f(n, (n - s) / i + 1) == s)
      ans = (n - s) / i + 1;
  }
  printf("%lld\n", ans);
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include "algorithm"
#include "bitset"
#include "cmath"
#include "functional"
#include "iostream"
#include "map"
#include "queue"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "vector"

#define lp(n) for (int i = 0; i < n; i++)
#define LP(n, i) for (int i = 0; i < n; i++)
#define mod 1000000007
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define nyan "(=^･ω･^=)"
typedef long long ll;
using namespace std;

ll n, s;

ll f(ll x, ll y) {
  if (x == 0)
    return 0;
  return x % y + f(x / y, y);
}

int main() {
  cin >> n >> s;
  if (s == n) {
    cout << n + 1 << endl;
    return 0;
  }
  for (ll i = 2; i * i <= n; i++) {
    if (f(n, i) == s) {
      cout << i << endl;
      return 0;
    }
  }
  for (ll i = sqrt(n) + 1; i >= 1; i--) {
    if ((n - s) / i + 1 != 1 && f(n, (n - s) / i + 1) == s) {
      cout << (n - s) / i + 1 << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}


Fixed submission:
#include "algorithm"
#include "bitset"
#include "cmath"
#include "functional"
#include "iostream"
#include "map"
#include "queue"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "vector"

#define lp(n) for (int i = 0; i < n; i++)
#define LP(n, i) for (int i = 0; i < n; i++)
#define mod 1000000007
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define nyan "(=^･ω･^=)"
typedef long long ll;
using namespace std;

ll n, s;

ll f(ll x, ll y) {
  if (x == 0)
    return 0;
  return x % y + f(x / y, y);
}

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

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define all(x) (x).begin(), (x).end()
#define foreach(u, v) for (auto(u) : (v))
#define pb push_back
#define mp make_pair
#define mt make_tuple

#define int long long

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;

const int inf = 1e9;
const ll linf = 1LL << 60;
const ll mod = 1e9 + 7;
const double eps = 1e-9;

/*
 */

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

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

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

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

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

  int ans = linf;
  for (int i = 1; i * i <= n; i++) {
    int b = (n - s) / i + 1;
    if (f(b, n) == s)
      ans = min(ans, b);
  }

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

  return 0;
}


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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define all(x) (x).begin(), (x).end()
#define foreach(u, v) for (auto(u) : (v))
#define pb push_back
#define mp make_pair
#define mt make_tuple

#define int long long

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;

const int inf = 1e9;
const ll linf = 1LL << 60;
const ll mod = 1e9 + 7;
const double eps = 1e-9;

/*
 */

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

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

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

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

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

  int ans = linf;
  for (int i = 1; i * i <= n; i++) {
    int b = (n - s) / i + 1;
    if (b < 2)
      continue;
    if (f(b, n) == s)
      ans = min(ans, b);
  }

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

  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define forx(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

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

  for (ll b = 2; b <= (ll)floor(sqrtl(n)); b++) {
    ll sub = n, res = 0;
    while (sub > 0) {
      res += sub % b;
      sub /= b;
    }
    if (s == res) {
      cout << b << endl;
      return 0;
    }
  }
  vector<ll> a;
  a.push_back(1e12);

  for (ll p = 1; p <= (ll)floor(sqrtl(n)); p++) {
    ll b = (n - s) / p + 1;
    ll sub = n, res = 0;
    while (sub > 0) {
      res += sub % b;
      sub /= b;
    }
    if (s == res) {
      a.push_back(b);
    }
  }
  sort(a.begin(), a.end());
  if (a[0] == 1e12)
    cout << -1 << endl;
  else
    cout << a[0] << endl;

  return 0;
}


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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

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

  for (ll b = 2; b <= (ll)floor(sqrtl(n)); b++) {
    ll sub = n, res = 0;
    while (sub > 0) {
      res += sub % b;
      sub /= b;
    }
    if (s == res) {
      cout << b << endl;
      return 0;
    }
  }
  vector<ll> a;
  a.push_back(1e12);

  for (ll p = 1; p <= (ll)floor(sqrtl(n)); p++) {
    ll b = (n - s) / p + 1;
    ll sub = n, res = 0;
    while (sub > 0) {
      if (b == 1)
        break;
      res += sub % b;
      sub /= b;
    }
    if (s == res) {
      a.push_back(b);
    }
  }
  sort(a.begin(), a.end());
  if (a[0] == 1e12)
    cout << -1 << endl;
  else
    cout << a[0] << endl;

  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

long long int f(const long long int b, const long long int n) {
  return n < b ? n : f(b, n / b) + n % b;
}
int main() {
  long long int n, s;
  std::cin >> n >> s;
  if (n < s) {
    std::cout << "-1\n";
    return 0;
  }
  long long int b = 2LL;
  for (; b * b <= n; ++b) {
    if (f(b, n) == s) {
      std::cout << b << std::endl;
      return 0;
    }
  }
  for (; b > 0; --b) {
    const auto p = (n - s) / b + 1;
    if (f(p, n) == s) {
      std::cout << p << std::endl;
      return 0;
    }
  }
  if (n == s) {
    std::cout << n + 1 << std::endl;
    return 0;
  }
  std::cout << "-1\n";
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

long long int f(const long long int b, const long long int n) {
  return n < b ? n : f(b, n / b) + n % b;
}
int main() {
  long long int n, s;
  std::cin >> n >> s;
  if (n < s) {
    std::cout << "-1\n";
    return 0;
  }
  long long int b = 2LL;
  for (; b * b <= n; ++b) {
    if (f(b, n) == s) {
      std::cout << b << std::endl;
      return 0;
    }
  }
  for (; b > 0; --b) {
    const auto p = (n - s) / b + 1;
    if (p > 1 && f(p, n) == s) {
      std::cout << p << std::endl;
      return 0;
    }
  }
  if (n == s) {
    std::cout << n + 1 << std::endl;
    return 0;
  }
  std::cout << "-1\n";
}
Problem p04016 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
using namespace std;

typedef long long ll;

int main() {
  ll n, s, ans;
  cin >> n >> s;
  for (ll i = 2; i * i <= n; i++) {
    ans = i;
    ll x = log2(n) / log2(i), tmp = 0, tmpN = n;
    while (x >= 0) {
      ll ml = tmpN / pow(i, x);
      tmp += ml;
      tmpN = tmpN - ml * pow(i, x--);
    }
    if (tmp == s) {
      cout << i << endl;
      return 0;
    }
  }
  ll start = n / ans + 1;
  for (ll i = start; i > 0; i++) {
    if (s - i < 0)
      continue;
    ll b = (n - (s - i)) / i;
    if (b > s - i && b * i + (s - i) == n && b >= 2 && b > i) {
      cout << b << endl;
      return 0;
    }
  }
  cout << (n == s ? n + 1 : -1) << endl;

  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;

typedef long long ll;

int main() {
  ll n, s, ans;
  cin >> n >> s;
  for (ll i = 2; i * i <= n; i++) {
    ans = i;
    ll x = log2(n) / log2(i), tmp = 0, tmpN = n;
    while (x >= 0) {
      ll ml = tmpN / pow(i, x);
      tmp += ml;
      tmpN = tmpN - ml * pow(i, x--);
    }
    if (tmp == s) {
      cout << i << endl;
      return 0;
    }
  }
  ll start = n / ans + 1;
  for (ll i = start; i > 0; i--) {
    if (s - i < 0)
      continue;
    ll b = (n - (s - i)) / i;
    if (b > s - i && b * i + (s - i) == n && b >= 2 && b > i) {
      cout << b << endl;
      return 0;
    }
  }
  cout << (n == s ? n + 1 : -1) << endl;

  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long LL;
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;

LL dig(LL x, LL n) {
  if (n < x)
    return n;
  else
    return dig(x, n / x) + n % x;
}
int main(void) {
  LL n, s;
  cin >> n >> s;
  vector<LL> f;
  for (LL i = 1; i * i <= n; ++i) {
    if ((n - s) % i)
      continue;
    f.push_back(i + 1);
    f.push_back((n - s) / i + 1);
  }
  vector<LL> as;
  for (LL x : f) {
    if (dig(x, n) == s)
      as.push_back(x);
  }
  sort(as.begin(), as.end());
  as.push_back(-1ll);
  cout << as[0] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long LL;
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;

LL dig(LL x, LL n) {
  if (n < x)
    return n;
  else
    return dig(x, n / x) + n % x;
}
int main(void) {
  LL n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  if (n < s) {
    cout << -1 << endl;
    return 0;
  }
  vector<LL> f;
  for (LL i = 1; i * i <= n; ++i) {
    if ((n - s) % i)
      continue;
    f.push_back(i + 1);
    f.push_back((n - s) / i + 1);
  }
  vector<LL> as;
  for (LL x : f) {
    if (dig(x, n) == s)
      as.push_back(x);
  }
  sort(as.begin(), as.end());
  as.push_back(-1ll);
  cout << as[0] << endl;
  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

long long f(long long x, long long b) {
  long long ret = 0;
  while (x > 0) {
    ret += x % b;
    x /= b;
  }
  return ret;
}

/*
p + q = s;
p *b + q = x
*/
int main() {
  long long n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  if (s > n) {
    cout << -1 << endl;
    return 0;
  }
  for (long long i = 2; i * i <= n; i++) {
    if (s == f(n, i)) {
      cout << i << endl;
      return 0;
    }
  }
  long long ans = 100000000000;
  for (long long i = 1; i * i <= n && i <= s; i++) {
    long long j = s - i;
    if ((n - j) / i) {
      if (f(n, ((n - j) / i)) == s)
        ans = min(ans, (n - j) / i);
    }
  }
  if (ans >= 100000000000)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

long long f(long long x, long long b) {
  long long ret = 0;
  if (b <= 1)
    return -1;
  while (x > 0) {
    ret += x % b;
    x /= b;
  }
  return ret;
}

/*
p + q = s;
p *b + q = x
*/
int main() {
  long long n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  if (s > n) {
    cout << -1 << endl;
    return 0;
  }
  for (long long i = 2; i * i <= n; i++) {
    if (s == f(n, i)) {
      cout << i << endl;
      return 0;
    }
  }
  long long ans = 100000000000;
  for (long long i = 1; i * i <= n && i <= s; i++) {
    long long j = s - i;
    if ((n - j) / i) {
      if (f(n, ((n - j) / i)) == s)
        ans = min(ans, (n - j) / i);
    }
  }
  if (ans >= 100000000000)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll 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;
  if (N == s) {
    cout << N + 1 << endl;
    return 0;
  }

  for (ll i = 2; i * i <= N; i++) {
    if (f(i, N) == s) {
      cout << i << endl;
      return 0;
    }
  }
  for (ll i = sqrt(N) + 1; i >= 1; i--) {
    ll b = (N - s) / i + 1;
    if (b >= sqrt(N) && f(b, N) == s) {
      cout << b << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll f(ll b, ll n) {
  if (b == 1)
    return -1;
  if (n < b)
    return n;
  else {
    return f(b, n / b) + n % b;
  }
}
int main() {
  ll N, s;
  cin >> N >> s;
  if (N == s) {
    cout << N + 1 << endl;
    return 0;
  }

  for (ll i = 2; i * i <= N; i++) {
    if (f(i, N) == s) {
      cout << i << endl;
      return 0;
    }
  }
  for (ll i = sqrt(N) + 1; i >= 1; i--) {
    ll b = (N - s) / i + 1;
    if (b >= sqrt(N) && f(b, N) == s) {
      cout << b << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}
Problem p04016 (C++)

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

long long n, s, b;
inline long long f(long long b, long long n) {
  if (n < b)
    return n;
  return f(b, n / b) + n % b;
}
int main() {
  scanf("%lld%lld", &n, &s);
  for (long long b = 2; b * b <= n; ++b) {
    long long sum = f(b, n);
    if (sum == s) {
      printf("%lld", b);
      return 0;
    }
  }
  long long d;
  long long m = n - s;
  for (d = 1; d * d <= m; ++d) {
    if (m % d == 0 && f(d + 1, n) == s) {
      printf("%lld", d + 1);
      return 0;
    }
  }
  for (; d; --d) {
    if (m % d == 0 && f(m / d + 1, n) == s) {
      printf("%lld", m / d + 1);
      return 0;
    }
  }
  if (n == s)
    printf("%lld", n + 1);
  else
    printf("-1");
  return 0;
}


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

long long n, s, b;
inline long long f(long long b, long long n) {
  if (n < b)
    return n;
  return f(b, n / b) + n % b;
}
int main() {
  scanf("%lld%lld", &n, &s);
  for (long long b = 2; b * b <= n; ++b) {
    long long sum = f(b, n);
    if (sum == s) {
      printf("%lld", b);
      return 0;
    }
  }
  long long d;
  long long m = n - s;
  for (d = 1; d * d <= m; ++d) {
    if (m % d == 0 && f(d + 1, n) == s) {
      printf("%lld", d + 1);
      return 0;
    }
  }
  --d;
  for (; d; --d) {
    if (m % d == 0 && f(m / d + 1, n) == s) {
      printf("%lld", m / d + 1);
      return 0;
    }
  }
  if (n == s)
    printf("%lld", n + 1);
  else
    printf("-1");
  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i, s, n) for (int i = (s); i < (n); i++)
#define repe(i, s, n) for (int i = (s); i <= (n); i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
const ll MOD = 1000000007, INF = 1e18;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};

int n, s;

int f(int b, int n) {
  ll ans = 0;
  while (n > 0) {
    if (n < b) {
      ans += n;
      break;
    }
    ans += (n % b);
    n /= b;
  }
  return ans;
}

signed main() {
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  int ans = INF;
  repe(i, 2, sqrt(n)) {
    if (f(i, n) == s)
      ans = min(ans, i);
  }
  rep(i, 1, sqrt(n)) {
    if (n <= s)
      continue;
    int b = (n - s) / i + 1;
    if (f(b, n) == s)
      ans = min(ans, b);
  }
  if (ans != INF)
    cout << ans << endl;
  else if (s == 1)
    cout << n << endl;
  else
    cout << "-1" << endl;
  return 0;
}

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

#define int long long

#define rep(i, s, n) for (int i = (s); i < (n); i++)
#define repe(i, s, n) for (int i = (s); i <= (n); i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
const ll MOD = 1000000007, INF = 1e18;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};

int n, s;

int f(int b, int n) {
  ll ans = 0;
  while (n > 0) {
    if (n < b) {
      ans += n;
      break;
    }
    ans += (n % b);
    n /= b;
  }
  return ans;
}

signed main() {
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  int ans = INF;
  repe(i, 2, sqrt(n)) {
    if (f(i, n) == s)
      ans = min(ans, i);
  }
  rep(i, 1, sqrt(n)) {
    if (n <= s)
      continue;
    int b = (n - s) / i + 1;
    if (b == 1)
      continue;
    if (f(b, n) == s)
      ans = min(ans, b);
  }
  if (ans != INF)
    cout << ans << endl;
  else if (s == 1)
    cout << n << endl;
  else
    cout << "-1" << endl;
  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>

long long f(long long b, long long n) {
  if (b > n)
    return n;

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

long long CheckForB(long long n, long long s) {
  long long max_sroot = 316228;

  if (n < s) {
    return -1;
  }

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

  long long b = 1;

  if (s <
      2 * max_sroot) // it could be possible that there are 3 or more b-digits
  {
    while (++b < max_sroot) {
      if (f(b, n) == s) {
        return b;
      }
    }
  }

  // now we left with at most 2 digits
  // and the first is at most max_sroot
  long long firstDigit = std::min(s, max_sroot) + 1,
            secondDigit = s - firstDigit;

  while ((++secondDigit, --firstDigit) > 0) {
    b = (n - secondDigit) / firstDigit;
    if (f(b, n) == s) {
      return b;
    }
  }

  return -1;
}

int main(int, char **) {
  long long n, s;

  std::cin >> n >> s;

  std::cout << CheckForB(n, s) << std::endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>

long long f(long long b, long long n) {
  if (b > n)
    return n;

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

long long CheckForB(long long n, long long s) {
  long long max_sroot = 316228;

  if (n < s) {
    return -1;
  }

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

  long long b = 1;

  if (s <
      2 * max_sroot) // it could be possible that there are 3 or more b-digits
  {
    while (++b < max_sroot) {
      if (f(b, n) == s) {
        return b;
      }
    }
  }

  // now we left with at most 2 digits
  // and the first is at most max_sroot
  long long firstDigit = std::min(s, max_sroot) + 1,
            secondDigit = s - firstDigit;

  while ((++secondDigit, --firstDigit) > 0) {
    b = (n - secondDigit) / firstDigit;
    if (b > 1 && f(b, n) == s) {
      return b;
    }
  }

  return -1;
}

int main(int, char **) {
  long long n, s;

  std::cin >> n >> s;

  std::cout << CheckForB(n, s) << std::endl;

  return 0;
}