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

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef unsigned long long ull;
typedef signed long long ll;
#define INF (1LL << 62)

int main(int argc, char *argv[]) {
  for (;;) {
    ll n, s;
    cin >> n >> s;
    if (cin.fail())
      break;

    auto solve = [n, s]() -> ll {
      auto test = [n, s](ll b) {
        ll rr = 0;
        ll nn = n;
        while (nn) {
          rr += nn % b;
          nn /= b;
        }
        return rr == s;
      };

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

      ll sqrt_n = (ll)floor(sqrt(n));
      for (ll b = 2; b <= sqrt_n; b++) {
        if (test(b)) {
          return b;
        }
      }

      ll result = INF;
      for (ll p = 1; p <= sqrt_n; p++) {
        ll b = (n - s) / p + 1;
        if (b > 0 && test(b)) {
          result = min(result, b);
        }
      }

      return result == INF ? -1 : result;
    };

    printf("%lld\n", solve());
  }

  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef unsigned long long ull;
typedef signed long long ll;
#define INF (1LL << 62)

int main(int argc, char *argv[]) {
  for (;;) {
    ll n, s;
    cin >> n >> s;
    if (cin.fail())
      break;

    auto solve = [n, s]() -> ll {
      auto test = [n, s](ll b) {
        ll rr = 0;
        ll nn = n;
        while (nn) {
          rr += nn % b;
          nn /= b;
        }
        return rr == s;
      };

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

      ll sqrt_n = (ll)floor(sqrt(n));
      for (ll b = 2; b <= sqrt_n; b++) {
        if (test(b)) {
          return b;
        }
      }

      ll result = INF;
      for (ll p = 1; p <= sqrt_n; p++) {
        ll b = (n - s) / p + 1;
        if (b > 1 && test(b)) {
          result = min(result, b);
        }
      }

      return result == INF ? -1 : result;
    };

    printf("%lld\n", solve());
  }

  return 0;
}
Problem p04014 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long ll;
ll n, s, i, l, r, k;
ll f(ll b, ll n) {
  ll sum = 0;
  while (n) {
    sum += n % b;
    n /= b;
  }
  return sum;
}
void work1() {
  for (i = 2; i <= n; i++)
    if (f(i, n) == s) {
      printf("%lld", i);
      return;
    }
  printf("-1");
}
void work2() {
  l = (int)sqrt(n) + 10;
  for (i = 2; i < l; i++)
    if (f(i, n) == s) {
      printf("%lld", i);
      return;
    }
  for (; l <= n; l = r + 1) {
    r = n / (n / l);
    k = (n - s) / (n / l) + 1;
    if (f(k, n) == s) {
      printf("%lld", k);
      return;
    }
  }
  printf("-1");
}
int main() {
  scanf("%lld%lld", &n, &s);
  if (n == s) {
    printf("%lld", n + 1);
    return 0;
  }
  if (n < s) {
    printf("-1");
    return 0;
  }
  if (n <= 1000)
    work1();
  else
    work2();
  // while(1);
  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long ll;
ll n, s, i, l, r, k;
ll f(ll b, ll n) {
  ll sum = 0;
  while (n) {
    sum += n % b;
    n /= b;
  }
  return sum;
}
void work1() {
  for (i = 2; i <= n; i++)
    if (f(i, n) == s) {
      printf("%lld", i);
      return;
    }
  printf("-1");
}
void work2() {
  l = (int)sqrt(n) + 10;
  for (i = 2; i < l; i++)
    if (f(i, n) == s) {
      printf("%lld", i);
      return;
    }
  for (; l <= n; l = r + 1) {
    r = n / (n / l);
    k = (n - s) / (n / l) + 1;
    if (k > 1 && f(k, n) == s) {
      printf("%lld", k);
      return;
    }
  }
  printf("-1");
}
int main() {
  scanf("%lld%lld", &n, &s);
  if (n == s) {
    printf("%lld", n + 1);
    return 0;
  }
  if (n < s) {
    printf("-1");
    return 0;
  }
  if (n <= 1000)
    work1();
  else
    work2();
  // while(1);
  return 0;
}
Problem p04014 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <math.h>
#define ll long long
using namespace std;

ll n, s;

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

ll solve(ll n, ll s) {
  if (n == s)
    return n + 1;

  ll b = 2;
  for (; b * b <= n; b++) {
    if (f(b, n) == s)
      return b;
  }
  for (; b > 0; b--) {
    if ((n - s) % b == 0 && f((n - s) / b + 1, n) == s)
      return (n - s) / b + 1;
  }

  return -1;
}

int main() {
  cin >> n >> s;
  cout << solve(n, s) << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <math.h>
#define ll long long
using namespace std;

ll n, s;

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

ll solve(ll n, ll s) {
  if (n == s)
    return n + 1;
  if (n < s)
    return -1;

  ll b = 2;
  for (; b * b <= n; b++) {
    if (f(b, n) == s)
      return b;
  }
  for (; b > 0; b--) {
    if ((n - s) % b == 0 && f((n - s) / b + 1, n) == s)
      return (n - s) / b + 1;
  }

  return -1;
}

int main() {
  cin >> n >> s;
  cout << solve(n, s) << endl;
  return 0;
}
Problem p04014 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ALL(v) v.begin(), v.end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort((c).begin(), (c).end())
#define dbug(x) cout << #x << "=" << x << endl
#define m0(x) memset(x, 0, sizeof(x))
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define reps(i, m, n) for (ll i = m; i < n; i++)
#define reprs(i, m, n) for (ll i = n; i >= m; i--)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef long long ll;
typedef pair<ll, ll> pl;

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

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

int main() {

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

  ll x = floor(sqrt(n));

  reps(i, 2, x + 2) {
    if (fu(i, n) == s) {
      std::cout << i << '\n';
      return 0;
    }
  }

  reprs(j, 0, min(x, s)) {
    if ((n - s + j) % j == 0 && (n - s + j) / j > x && (n - s + j) / j < n) {
      std::cout << (n - s + j) / j << '\n';
      return 0;
    }
  }

  if (n == s) {
    std::cout << n + 1 << '\n';
    return 0;
  }

  std::cout << -1 << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ALL(v) v.begin(), v.end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort((c).begin(), (c).end())
#define dbug(x) cout << #x << "=" << x << endl
#define m0(x) memset(x, 0, sizeof(x))
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define reps(i, m, n) for (ll i = m; i < n; i++)
#define reprs(i, m, n) for (ll i = n; i >= m; i--)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef long long ll;
typedef pair<ll, ll> pl;

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

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

int main() {

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

  ll x = floor(sqrt(n));

  reps(i, 2, x + 2) {
    if (fu(i, n) == s) {
      std::cout << i << '\n';
      return 0;
    }
  }

  reprs(j, 1, min(x, s)) {
    if ((n - s + j) % j == 0 && (n - s + j) / j > x && (n - s + j) / j <= n) {
      if (fu((n - s + j) / j, n) == s) {
        std::cout << (n - s + j) / j << '\n';
        return 0;
      }
    }
  }

  if (n == s) {
    std::cout << n + 1 << '\n';
    return 0;
  }

  std::cout << -1 << '\n';

  return 0;
}
Problem p04014 (C++)

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

using namespace std;
typedef long long ll;
// typedef unsigned long long ll;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
// #define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) ll(x.size())
// typedef pair<int, int> P;
typedef pair<ll, ll> P;
// const double INF = 1e10;
// const ll INF = LONG_LONG_MAX;
const ll MINF = LONG_LONG_MIN;
const int INF = INT_MAX / 10;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)

bool contain(set<char> &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);

typedef priority_queue<ll, vector<ll>, greater<ll>> PQ_ASK;
const int mod = 1000000007;

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 (s == n) {
    cout << n + 1 << endl;
    return 0;
  }

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

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

  cout << -1 << endl;
}

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

using namespace std;
typedef long long ll;
// typedef unsigned long long ll;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
// #define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) ll(x.size())
// typedef pair<int, int> P;
typedef pair<ll, ll> P;
// const double INF = 1e10;
// const ll INF = LONG_LONG_MAX;
const ll MINF = LONG_LONG_MIN;
const int INF = INT_MAX / 10;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)

bool contain(set<char> &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);

typedef priority_queue<ll, vector<ll>, greater<ll>> PQ_ASK;
const int mod = 1000000007;

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 (s == n) {
    cout << n + 1 << endl;
    return 0;
  }

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

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

  cout << -1 << endl;
}
Problem p04014 (C++)

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

using namespace std;
typedef long long ll;

int main(void) {
  ll n, s, buf, sum;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << "\n";
    return 0;
  }
  for (ll i = 2; i <= sqrt(n); i++) {
    sum = 0;
    buf = n;
    while (buf > 0) {
      sum += buf % i;
      buf /= i;
    }
    if (sum == s) {
      cout << i << "\n";
      return 0;
    }
  }
  sum = n << 1;
  for (ll i = 1; i < sqrt(n); i++) {
    buf = (n - s) / i + 1;
    if (n % buf + i == s)
      sum = min(sum, buf);
  }
  if (sum == n << 1)
    printf("-1\n");
  else
    cout << sum << "\n";
  return 0;
}

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

using namespace std;
typedef long long ll;

int main(void) {
  ll n, s, buf, sum;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << "\n";
    return 0;
  }
  for (ll i = 2; i <= sqrt(n); i++) {
    sum = 0;
    buf = n;
    while (buf > 0) {
      sum += buf % i;
      buf /= i;
    }
    if (sum == s) {
      cout << i << "\n";
      return 0;
    }
  }
  sum = n << 1;
  for (ll i = 1; i < sqrt(n); i++) {
    buf = (n - s) / i + 1;
    if (buf > 0 && n % buf + i == s)
      sum = min(sum, buf);
  }
  if (sum == n << 1)
    printf("-1\n");
  else
    cout << sum << "\n";
  return 0;
}
Problem p04014 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define pb push_back

using namespace std;

typedef long long ll;
const int mod = 1e9 + 7;
const int INF = 1 << 30;

ll f(ll b, ll n) { // nをb進数表記した時の各桁の和
  ll res = 0;
  if (n < b)
    return n;
  else
    res = n % b + f(b, n / b);
  return res;
}

int main() {
  ll n, s;
  cin >> n >> s;
  if (s == n) {
    cout << n + 1 << endl;
    return 0;
  }
  for (ll b = 2; b * b <= n; b++) {
    ll temp = f(b, n);
    if (temp == s) {
      cout << b << endl;
      return 0;
    }
  }
  ll ans = -1;
  for (ll p = 1; p * p <= n; p++) {
    ll q = s - p;
    if (q < 0)
      continue;
    if ((n - q) % p)
      continue;
    ll b = (n - q) / p;
    if (f(b, n) == s) {
      if (ans == -1)
        ans = b;
      else
        ans = min(ans, b);
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define pb push_back

using namespace std;

typedef long long ll;
const int mod = 1e9 + 7;
const int INF = 1 << 30;

ll f(ll b, ll n) { // nをb進数表記した時の各桁の和
  ll res = 0;
  if (n < b)
    return n;
  else
    res = n % b + f(b, n / b);
  return res;
}

int main() {
  ll n, s;
  cin >> n >> s;
  if (s == n) {
    cout << n + 1 << endl;
    return 0;
  }
  if (s > (ll)((2 * n - 1) / 2LL)) {
    cout << "-1" << endl;
    return 0;
  }
  for (ll b = 2; b * b <= n; b++) {
    ll temp = f(b, n);
    if (temp == s) {
      cout << b << endl;
      return 0;
    }
  }
  ll ans = -1;
  for (ll p = 1; p * p <= n; p++) {
    ll q = s - p;
    if (q < 0)
      continue;
    if ((n - q) % p)
      continue;
    ll b = (n - q) / p;
    if (f(b, n) == s) {
      if (ans == -1)
        ans = b;
      else
        ans = min(ans, b);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p04014 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define ll long long int

ll f(ll n, ll a) {
  if (n > a)
    return a;
  return f(n, a / n) + a % n;
}

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


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define ll long long int

ll f(ll n, ll a) {
  if (n > a)
    return a;
  return f(n, a / n) + a % n;
}

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

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

using ll = long long;

const ll INF = (1LL << 60);

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 (s == n) {
    cout << n + 1 << endl;
  } else {
    for (ll b = 2; b * b <= n; b++) {
      if (f(b, n) == s) {
        cout << b << endl;
        return 0;
      }
    }

    ll ans = INF;
    for (ll p = 1; p * p < n; p++) {
      if ((n - s) % p != 0)
        continue;
      ll b = (n - s) / p + 1;
      if (f(b, n) == s) {
        ans = min(ans, b);
      }
    }
    if (ans >= INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}


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

using ll = long long;

const ll INF = (1LL << 60);

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 (s == n) {
    cout << n + 1 << endl;
  } else {
    for (ll b = 2; b * b <= n; b++) {
      if (f(b, n) == s) {
        cout << b << endl;
        return 0;
      }
    }

    ll ans = INF;
    for (ll p = 1; p * p < n; p++) {
      if ((n - s) % p != 0)
        continue;
      ll b = (n - s) / p + 1;
      if (b >= 2 && f(b, n) == s) {
        ans = min(ans, b);
      }
    }
    if (ans >= INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
Problem p04014 (C++)

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

using namespace std;
typedef long long ll;

ll n, s;

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

void solve() {
  if (n == s) {
    cout << n + 1 << endl;
    return;
  }
  for (int i = 2; i <= sqrt(n); i++) {
    if (f(i, n) == s) {
      cout << i << endl;
      return;
    }
  }
  for (int i = sqrt(n); i > 0; i--) {
    if ((n - s) % i != 0) {
      continue;
    }
    if (f((n - s) / i + 1, n) == s) {
      cout << (n - s) / i + 1 << endl;
      return;
    }
  }
  cout << "-1" << endl;
}

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


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

using namespace std;
typedef long long ll;

ll n, s;

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

void solve() {
  if (n == s) {
    cout << n + 1 << endl;
    return;
  } else if (s > n) {
    cout << -1 << endl;
    return;
  }
  for (int i = 2; i <= sqrt(n); i++) {
    if (f(i, n) == s) {
      cout << i << endl;
      return;
    }
  }
  for (int i = sqrt(n); i > 0; i--) {
    if ((n - s) % i != 0) {
      continue;
    }
    if (f((n - s) / i + 1, n) == s) {
      cout << (n - s) / i + 1 << endl;
      return;
    }
  }
  cout << "-1" << endl;
}

int main() {
  cin >> n >> s;
  solve();
}
Problem p04014 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define SZ(x) ((int)(x.size()))
#define FOR(i, s, n) for (ll i = (s); (i) < (n); ++i)
#define FORD(i, s, l) for (ll i = (s); (i) >= l; --i)
#define F first
#define S second
#define TC                                                                     \
  int __tc;                                                                    \
  cin >> __tc;                                                                 \
  FOR(case_num, 1, __tc + 1)
#define TEST(x, i) ((x) & (1ll << (i)))
#define SET(x, i) ((x) | (1ll << (i)))
#define FLIP(x, i) ((x) ^ (1ll << (i)))
#define CLEAR(x, i) ((x) & ~(1ll << (i)))

const double pi = 4 * atan(1);
using namespace std;
typedef long long ll;

const ll MOD = 1000000007;
const int MAXN = 200005;

ll sum(ll x, ll b) {
  ll r = 0;
  while (x >= b) {
    r += x % b;
    x /= b;
  }
  return r + x;
}

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

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

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

  for (ll i = 2; i * i <= n; i++) {
    if (sum(n, i) == s) {
      cout << i << "\n";
      return 0;
    }
  }

  for (ll i = (ll)sqrtl(n); i >= 1; i--) {
    if ((n - s) % i == 0 && sum(n, (n - s) / i + 1) == s) {
      cout << ((n - s) / i + 1) << "\n";
      return 0;
    }
  }

  cout << "-1\n";
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define SZ(x) ((int)(x.size()))
#define FOR(i, s, n) for (ll i = (s); (i) < (n); ++i)
#define FORD(i, s, l) for (ll i = (s); (i) >= l; --i)
#define F first
#define S second
#define TC                                                                     \
  int __tc;                                                                    \
  cin >> __tc;                                                                 \
  FOR(case_num, 1, __tc + 1)
#define TEST(x, i) ((x) & (1ll << (i)))
#define SET(x, i) ((x) | (1ll << (i)))
#define FLIP(x, i) ((x) ^ (1ll << (i)))
#define CLEAR(x, i) ((x) & ~(1ll << (i)))

const double pi = 4 * atan(1);
using namespace std;
typedef long long ll;

const ll MOD = 1000000007;
const int MAXN = 200005;

ll sum(ll x, ll b) {
  ll r = 0;
  while (x >= b) {
    r += x % b;
    x /= b;
  }
  return r + x;
}

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

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

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

  for (ll i = 2; i * i <= n; i++) {
    if (sum(n, i) == s) {
      cout << i << "\n";
      return 0;
    }
  }

  for (ll i = (ll)sqrtl(n - s); i >= 1; i--) {
    if ((n - s) % i == 0 && sum(n, (n - s) / i + 1) == s) {
      cout << ((n - s) / i + 1) << "\n";
      return 0;
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;
ll MOD = 1e9 + 7;
// LLONG_MIN

ll shin(ll n, ll b) {
  ll sum = 0;
  while (n >= b) {
    sum += n % b;
    n /= b;
  }
  sum += n;
  return sum;
}

int main(void) {
  ll n, s, small = 1e13;
  scanf("%lld%lld", &n, &s);
  if (n - s < 0) {
    printf("-1\n");
    return 0;
  }
  reg(b, 2, sqrt(n)) { // 始まりを1にすると無限ループ
    if (shin(n, b) == s && small > b) {
      small = b;
      break;
    }
  }
  if (small == 1e13) {
    ireg(p, 1, sqrt(n)) {
      if ((n - s) % p != 0)
        continue;
      ll b = (n - s) / p + 1;
      if (shin(n, b) == s && small > b) {
        small = b;
        break;
      }
    }
  }
  if (small == 1e13)
    small = -1;
  printf("%lld\n", small);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;
ll MOD = 1e9 + 7;
// LLONG_MIN

ll shin(ll n, ll b) {
  ll sum = 0;
  while (n >= b) {
    sum += n % b;
    n /= b;
  }
  sum += n;
  return sum;
}

int main(void) {
  ll n, s, small = 1e13;
  scanf("%lld%lld", &n, &s);
  if (n - s < 0) {
    printf("-1\n");
    return 0;
  }
  if (n == s) {
    printf("%lld\n", n + 1);
    return 0;
  }
  reg(b, 2, sqrt(n)) { // 始まりを1にすると無限ループ
    if (shin(n, b) == s && small > b) {
      small = b;
      break;
    }
  }
  if (small == 1e13) {
    ireg(p, 1, sqrt(n)) {
      if ((n - s) % p != 0)
        continue;
      ll b = (n - s) / p + 1;
      if (shin(n, b) == s && small > b) {
        small = b;
        break;
      }
    }
  }
  if (small == 1e13)
    small = -1;
  printf("%lld\n", small);
  return 0;
}
Problem p04014 (C++)

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

using namespace std;
// ascending order
#define vsort(v) sort(v.begin(), v.end())
// descending order
#define vsort_r(v) sort(v.begin(), v.end(), greater<int>())
#define vunique(v) v.erase(unique(v.begin(), v.end()), v.end())
#define mp make_pair
#define ts(x) to_string(x)
#define rep(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for (int i = (int)a; i > (int)b; i--)
#define bit(a) bitset<8>(a)
#define des_priority_queue priority_queue<int, vector<int>, greater<int>>
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e18;

#define MAX_V 1000000

bool check(ll b, ll n, ll s) {
  ll res = 0;
  ll tmp_n = n;
  while (tmp_n > 0) {
    res += tmp_n % b;
    tmp_n /= b;
  }

  return res == s;
}
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;
  }
  for (int b = 2; b <= sqrt(n); b++) {
    if (check(b, n, s)) {
      cout << b << endl;
      return 0;
    }
  }

  ll res = INF;
  for (int p = 1; p < sqrt(n); p++) {
    ll b = (n - s) / p + 1;
    if (check(b, n, s)) {
      res = min(res, b);
    }
  }

  if (res != INF)
    cout << res << endl;
  else
    cout << -1 << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
// ascending order
#define vsort(v) sort(v.begin(), v.end())
// descending order
#define vsort_r(v) sort(v.begin(), v.end(), greater<int>())
#define vunique(v) v.erase(unique(v.begin(), v.end()), v.end())
#define mp make_pair
#define ts(x) to_string(x)
#define rep(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for (int i = (int)a; i > (int)b; i--)
#define bit(a) bitset<8>(a)
#define des_priority_queue priority_queue<int, vector<int>, greater<int>>
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e18;

#define MAX_V 1000000

bool check(ll b, ll n, ll s) {
  ll res = 0;
  ll tmp_n = n;
  while (tmp_n > 0) {
    res += tmp_n % b;
    tmp_n /= b;
  }

  return res == s;
}
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;
  }
  for (int b = 2; b <= sqrt(n); b++) {
    if (check(b, n, s)) {
      cout << b << endl;
      return 0;
    }
  }

  ll res = INF;
  for (int p = 1; p < sqrt(n); p++) {
    ll b = (n - s) / p + 1;
    if (b <= sqrt(n))
      continue;
    if (check(b, n, s)) {
      res = min(res, b);
    }
  }

  if (res != INF)
    cout << res << endl;
  else
    cout << -1 << endl;
}
Problem p04014 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>
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(void) {
  LL n, s;
  cin >> n >> s;

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

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

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

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

  cout << -1 << endl;

  return 0;
}


Fixed submission:
#include <cmath>
#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>
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(void) {
  LL n, s;
  cin >> n >> s;

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

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

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

  for (LL i = sqrt(n); i > 0; i--) {
    LL b = (n - s) / i + 1;
    if (b <= 1)
      continue;

    if (f(b, n) == s) {
      cout << b << endl;
      return 0;
    }
  }

  cout << -1 << endl;

  return 0;
}
Problem p04014 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

/*
nをb進表記したときに桁の総和がsとなるようなbが存在するかどうか
f(b,n)を満たすbが存在するか

s=n -> n+1が答え

2<=b<=sqrt(n) で全探索

sqrt(n) < b のときnは2桁になる
n = pb+q
p+q = s とb > sqrtn
より n = pb+q > pb > p^2 となり p < sqrtn
なので上位桁について全探索
*/
ll f(ll b, ll n) {
  ll s = 0;
  while (n > 0) {
    s += n % b;
    n /= b;
  }
  return s;
}

int main() {
  ll n, s;
  cin >> n >> s;
  if (s == n) {
    cout << n + 1 << endl;
    return 0;
  }
  if (s > n) {
    cout << -1 << endl;
    return 0;
  }
  for (ll b = 2; b <= sqrt(n); b++) {
    if (f(b, n) == s) {
      cout << b << endl;
      return 0;
    }
  }
  ll res = 1e18;
  for (ll p = 1; p < sqrt(n); p++) {
    ll b = (n - s) / p + 1;
    if (f(b, n) == s) {
      chmin(res, b);
    }
  }
  if (res != 1e18)
    cout << res << endl;
  else
    cout << -1 << endl;
}


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

using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

/*
nをb進表記したときに桁の総和がsとなるようなbが存在するかどうか
f(b,n)を満たすbが存在するか

s=n -> n+1が答え

2<=b<=sqrt(n) で全探索

sqrt(n) < b のときnは2桁になる
n = pb+q
p+q = s とb > sqrtn
より n = pb+q > pb > p^2 となり p < sqrtn
なので上位桁について全探索
*/
ll f(ll b, ll n) {
  ll s = 0;
  while (n > 0) {
    s += n % b;
    n /= b;
  }
  return s;
}

int main() {
  ll n, s;
  cin >> n >> s;
  if (s == n) {
    cout << n + 1 << endl;
    return 0;
  }
  if (s > n) {
    cout << -1 << endl;
    return 0;
  }
  for (ll b = 2; b <= sqrt(n); b++) {
    if (f(b, n) == s) {
      cout << b << endl;
      return 0;
    }
  }
  ll res = 1e18;
  for (ll p = 1; p < sqrt(n); p++) {
    ll b = (n - s) / p + 1;
    if ((p + n % b) == s) {
      chmin(res, b);
    }
  }
  if (res != 1e18)
    cout << res << endl;
  else
    cout << -1 << endl;
}
Problem p04014 (C++)

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

#define DEBUG1 false
#define INF 1LL << 60

typedef long long ll;

ll f(const ll B, const ll N) {
  if (N < B) {
    return N;
  }
  ll a = f(B, N / B);
  ll c = N % B;
  return a + c;
}
ll solve(const ll N, const ll S) {
#if DEBUG1
  std::cout << "DEBUG1: N: " << N << " S: " << S << std::endl;
#endif
  if (N == S) {
    return N + 1;
  }
  ll a = INF;
#if DEBUG1
  std::cout << "DEBUG1: sqrt(N): " << sqrt(N) << std::endl;
#endif
  for (int B = 2; B <= sqrt(N); B++) {
    if (S == f(B, N)) {
      a = std::min(a, (ll)B);
    }
  }
#if DEBUG1
  std::cout << "DEBUG1: sqrt(N): " << sqrt(N) << std::endl;
#endif
  for (int P = 1; P < sqrt(N); P++) {
    ll B = (N - S) / P + 1;
    if (S == f(B, N)) {
      a = std::min(a, B);
    }
  }
  if (INF == a) {
    return -1;
  }
  return a;
}
signed main(void) {
  ll N, S;
  std::cin >> N >> S;
  std::cout << solve(N, S) << std::endl;
  return 0;
}

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

#define DEBUG1 false
#define INF 1LL << 60

typedef long long ll;

ll f(const ll B, const ll N) {
  if (B < 2) {
    return INF;
  }
  if (N < B) {
    return N;
  }
  ll a = f(B, N / B);
  ll c = N % B;
  return a + c;
}
ll solve(const ll N, const ll S) {
#if DEBUG1
  std::cout << "DEBUG1: N: " << N << " S: " << S << std::endl;
#endif
  if (N == S) {
    return N + 1;
  }
  ll a = INF;
#if DEBUG1
  std::cout << "DEBUG1: sqrt(N): " << sqrt(N) << std::endl;
#endif
  for (int B = 2; B <= sqrt(N); B++) {
    if (S == f(B, N)) {
      a = std::min(a, (ll)B);
    }
  }
#if DEBUG1
  std::cout << "DEBUG1: sqrt(N): " << sqrt(N) << std::endl;
#endif
  for (int P = 1; P < sqrt(N); P++) {
    ll B = (N - S) / P + 1;
    if (S == f(B, N)) {
      a = std::min(a, B);
    }
  }
  if (INF == a) {
    return -1;
  }
  return a;
}
signed main(void) {
  ll N, S;
  std::cin >> N >> S;
  std::cout << solve(N, S) << std::endl;
  return 0;
}
Problem p04014 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define fastio                                                                 \
  cin.sync_with_stdio(false);                                                  \
  cin.tie(NULL)
using namespace std;
ll findf(ll n, ll b) {
  ll re = 0;
  while (n > 0) {
    re += n % b;
    n /= b;
  }
  return re;
}
int main() {
  fastio;
  ll n, f;
  cin >> n >> f;
  ll ans = -1;
  if (n <= f) {
    if (n == f)
      ans = n + 1;
    if (ans == -1)
      for (ll d = 2; d * d <= n; d++) {
        if (findf(n, d) == f) {
          ans = d;
          break;
        }
      }
    if (ans == -1) {
      for (ll p = 1; p * p < n; p++) {
        if ((n - f) % p)
          continue;
        ll b = (n - f) / p + 1;
        if (n / b / b == 0 && f - p == n % b)
          ans = b;
      }
    }
  }
  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define fastio                                                                 \
  cin.sync_with_stdio(false);                                                  \
  cin.tie(NULL)
using namespace std;
ll findf(ll n, ll b) {
  ll re = 0;
  while (n > 0) {
    re += n % b;
    n /= b;
  }
  return re;
}
int main() {
  fastio;
  ll n, f;
  cin >> n >> f;
  ll ans = -1;
  if (n >= f) {
    if (n == f)
      ans = n + 1;
    if (ans == -1)
      for (ll d = 2; d * d <= n; d++) {
        if (findf(n, d) == f) {
          ans = d;
          break;
        }
      }
    if (ans == -1) {
      for (ll p = 1; p * p < n; p++) {
        if ((n - f) % p)
          continue;
        ll b = (n - f) / p + 1;
        if (n / b / b == 0 && f - p == n % b)
          ans = b;
      }
    }
  }
  cout << ans << '\n';
  return 0;
}
Problem p04014 (C++)

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

using namespace std;
typedef long long ll;
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) {
  ll res = 0, k = n;
  do {
    res += k % b;
    k /= b;
  } while (k);
  return res;
}

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

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

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

  const ll INF = 1e18;
  ll ans = INF;
  for (ll p = 1; p < sqrt(n); ++p) {
    ll b = (n - s) / p + 1;
    if (f(b, n) == s)
      chmin(ans, b);
  }

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

  return 0;
}


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

using namespace std;
typedef long long ll;
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) {
  ll res = 0, k = n;
  do {
    res += k % b;
    k /= b;
  } while (k);
  return res;
}

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

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

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

  const ll INF = 1e18;
  ll ans = INF;
  for (ll p = 1; p < sqrt(n); ++p) {
    ll b = (n - s) / p + 1;
    if (b > 1 && f(b, n) == s)
      chmin(ans, b);
  }

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

  return 0;
}
Problem p04014 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define each(a, x) for (auto a : (x))
#define all(a) (a).begin(), (a).end()
#define chmin(a, b) ((a) = min((a), (b)))
#define chmax(a, b) ((a) = max((a), (b)))
#define in_range(x, l, r) ((l) <= (x) && (x) < (r))
#define printvec(a) rep(i, a) cout << a[i] << " \n"[i + 1 == (a).size()];
#define fs first
#define sc second
#define em emplace
#define eb emplace_back
#define sz size()
#define MP make_pair
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;

const ll INF = 1e8;
const D EPS = 1e-8;
const ll MOD = 1e9 + 7;

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

ll solve(ll n, ll s) {
  if (n < s) {
    return -1;
  }
  if (n == s) {
    return n + 1;
  }

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

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

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

  cout << solve(n, s) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define each(a, x) for (auto a : (x))
#define all(a) (a).begin(), (a).end()
#define chmin(a, b) ((a) = min((a), (b)))
#define chmax(a, b) ((a) = max((a), (b)))
#define in_range(x, l, r) ((l) <= (x) && (x) < (r))
#define printvec(a) rep(i, a) cout << a[i] << " \n"[i + 1 == (a).size()];
#define fs first
#define sc second
#define em emplace
#define eb emplace_back
#define sz size()
#define MP make_pair
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;

const ll INF = 1e8;
const D EPS = 1e-8;
const ll MOD = 1e9 + 7;

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

ll solve(ll n, ll s) {
  if (n < s) {
    return -1;
  }
  if (n == s) {
    return n + 1;
  }

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

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

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

  cout << solve(n, s) << endl;
}
Problem p04014 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
typedef long long ll;

const ll INF = 1LL << 40;

int main() {
  ll N, S;
  cin >> N >> S;
  if (N == S) {
    cout << N + 1 << endl;
    return 0;
  }
  // 2からsqrt(N)までのbを探索
  for (ll b = 2; b * b <= N; b++) {
    ll ans = 0, tmp = N;
    while (tmp > 0) {
      ans += tmp % b;
      tmp /= b;
    }
    if (ans == S) {
      cout << b << endl;
      return 0;
    }
  }
  // sqrt(N)からNまでのbを探索
  ll min_b = INF;
  for (ll p = 1; p * p <= N; p++) {
    if ((N - S) % p != 0)
      continue;
    ll b = 1 + (N - S) / p, tmp = N, ans = 0;
    while (tmp > 0) {
      ans += tmp % b;
      tmp /= b;
    }
    if (ans == S)
      min_b = min(min_b, b);
  }
  if (min_b != INF)
    cout << min_b << endl;
  else
    cout << -1 << endl;
  return 0;
}

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

const ll INF = 1LL << 40;

int main() {
  ll N, S;
  cin >> N >> S;
  if (N == S) {
    cout << N + 1 << endl;
    return 0;
  }
  // 2からsqrt(N)までのbを探索
  for (ll b = 2; b * b <= N; b++) {
    ll ans = 0, tmp = N;
    while (tmp > 0) {
      ans += tmp % b;
      tmp /= b;
    }
    if (ans == S) {
      cout << b << endl;
      return 0;
    }
  }
  // sqrt(N)からNまでのbを探索
  ll min_b = INF;
  for (ll p = 1; p * p <= N; p++) {
    if ((N - S) % p != 0)
      continue;
    ll b = 1 + (N - S) / p, tmp = N, ans = 0;
    if (b == 0)
      continue;
    while (tmp > 0) {
      ans += tmp % b;
      tmp /= b;
    }
    if (ans == S)
      min_b = min(min_b, b);
  }
  if (min_b != INF)
    cout << min_b << endl;
  else
    cout << -1 << endl;
  return 0;
}
Problem p04014 (C++)

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

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<LL, LL> PLL;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

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

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

  LL N, S;
  cin >> N >> S;
  if (N < S) {
    cout << -1 << endl;
    return 0;
  }
  if (N == S) {
    cout << N + 1 << endl;
    return 0;
  }

  LL ans = 1e15, b;
  for (b = 2; b * b <= N; ++b) {
    if (f(N, b) == S)
      ans = min(ans, b);
  }
  for (LL p = 1; p < b; ++p) {
    LL q = S - p;
    LL b_ = (N - q) / p;
    if (f(N, b_) == S)
      ans = min(ans, b_);
  }

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

  return 0;
}


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

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<LL, LL> PLL;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

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

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

  LL N, S;
  cin >> N >> S;
  if (N < S) {
    cout << -1 << endl;
    return 0;
  }
  if (N == S) {
    cout << N + 1 << endl;
    return 0;
  }

  LL ans = 1e15, b;
  for (b = 2; b * b <= N; ++b) {
    if (f(N, b) == S)
      ans = min(ans, b);
  }
  for (LL p = 1; p < b; ++p) {
    LL q = S - p;
    LL b_ = (N - q) / p;
    if (b <= b_ && f(N, b_) == S)
      ans = min(ans, b_);
  }

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

  return 0;
}
Problem p04014 (C++)

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

#define MP make_pair
#define PB push_back
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP1(i, n) for (int i = 1; i < (n); i++)
#define REP2(i, d, n) for (int i = (d); i < (n); i++)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define CLR(a) memset((a), 0, sizeof(a))
#define MCLR(a) memset((a), -1, sizeof(a))
#define RANGE(x, y, maxX, maxY)                                                \
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<LL> VLL;
typedef pair<int, int> PII;

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;

const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

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;
  } else if (s > n) {
    cout << -1 << endl;
    return 0;
  }

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

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

  cout << -1 << endl;
}


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

#define MP make_pair
#define PB push_back
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP1(i, n) for (int i = 1; i < (n); i++)
#define REP2(i, d, n) for (int i = (d); i < (n); i++)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define CLR(a) memset((a), 0, sizeof(a))
#define MCLR(a) memset((a), -1, sizeof(a))
#define RANGE(x, y, maxX, maxY)                                                \
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<LL> VLL;
typedef pair<int, int> PII;

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;

const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

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;
  } else if (s > n) {
    cout << -1 << endl;
    return 0;
  }

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

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

  cout << -1 << endl;
}