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

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, x, n) for (int i = x; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define MP(a, b) make_pair(a, b)
typedef long long LL;
typedef pair<int, int> PI;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
const LL MOD = 1000000007LL;
LL dp[50][51][2501];
int x[50];
int main() {
  int N, A;
  cin >> N >> A;
  rep(i, N) cin >> x[i];
  dp[0][0][0] = dp[0][1][x[0]] = 1;
  REP(i, 1, N) {
    rep(j, 51) {
      rep(k, 2501) {
        dp[i][j][k] = dp[i - 1][j][k];
        if (k >= x[i])
          dp[i][j][k] += dp[i - 1][j - 1][k - x[i]];
      }
    }
  }
  LL ans = 0;
  REP(i, 1, N + 1) { ans += dp[N - 1][i][i * A]; }
  cout << ans << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, x, n) for (int i = x; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define MP(a, b) make_pair(a, b)
typedef long long LL;
typedef pair<int, int> PI;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
const LL MOD = 1000000007LL;
LL dp[50][51][2501];
int x[50];
int main() {
  int N, A;
  cin >> N >> A;
  rep(i, N) cin >> x[i];
  dp[0][0][0] = dp[0][1][x[0]] = 1;
  REP(i, 1, N) {
    rep(j, 51) {
      rep(k, 2501) {
        dp[i][j][k] = dp[i - 1][j][k];
        if (j > 0 && k >= x[i])
          dp[i][j][k] += dp[i - 1][j - 1][k - x[i]];
      }
    }
  }
  LL ans = 0;
  REP(i, 1, N + 1) { ans += dp[N - 1][i][i * A]; }
  cout << ans << endl;
}
Problem p04015 (C++)

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

using namespace std;

void ARC060C() {
  int N, A;
  cin >> N >> A;
  vector<int> vals(N);
  for (auto &x : vals) {
    cin >> x;
  }

  vector<vector<vector<long long>>> dp(
      N + 1, vector<vector<long long>>(N + 1, vector<long long>(2501)));
  dp[1][1][vals.front()] = 1;
  for (int i = 1; i < N; ++i) {
    dp[i][0][0] = 1;
    for (int j = 0; j <= i; ++j) {
      for (int k = 1; k <= 2500; ++k) {
        int res = k - vals[i];
        dp[i + 1][j][k] = dp[i][j][k];
        if (res >= 0 && res < 2501)
          dp[i + 1][j][k] += dp[i][j - 1][res];
      }
    }
  }
  long long sum = 0;
  for (int i = 1; i <= N; ++i) {
    sum += dp[N][i][A * i];
  }
  cout << sum << endl;
}
int main() {
  ARC060C();
  return 0;
}


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

using namespace std;

void ARC060C() {
  int N, A;
  cin >> N >> A;
  vector<int> vals(N);
  for (auto &x : vals) {
    cin >> x;
  }

  vector<vector<vector<long long>>> dp(
      N + 1, vector<vector<long long>>(N + 1, vector<long long>(2501)));
  dp[1][1][vals.front()] = 1;
  for (int i = 1; i < N; ++i) {
    dp[i][0][0] = 1;
    for (int j = 1; j <= i + 1; ++j) {
      for (int k = 1; k <= 2500; ++k) {
        int res = k - vals[i];
        dp[i + 1][j][k] = dp[i][j][k];
        if (res >= 0 && res < 2501)
          dp[i + 1][j][k] += dp[i][j - 1][res];
      }
    }
  }
  long long sum = 0;
  for (int i = 1; i <= N; ++i) {
    sum += dp[N][i][A * i];
  }
  cout << sum << endl;
}
int main() {
  ARC060C();
  return 0;
}
Problem p04015 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <cstring>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#pragma warning(disable : 4996)

typedef long long ll;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
// const long long MOD = 998244353;
using namespace std;

ll dp[51][4005];

int main(int argc, char *argv[]) {
  int n, A;
  scanf("%d%d", &n, &A);
  vector<int> a(n);
  int i, j;
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    a[i] -= A;
  }

  dp[0][2000] = 1;
  for (i = 0; i < n; i++) {
    for (j = 0; j <= 4000; j++) {
      dp[i + 1][j] += dp[i][j];
      dp[i + 1][j + a[i]] += dp[i][j];
    }
  }

  printf("%lld\n", dp[n][2000] - 1);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cstring>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#pragma warning(disable : 4996)

typedef long long ll;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
// const long long MOD = 998244353;
using namespace std;

ll dp[51][4005];

int main(int argc, char *argv[]) {
  int n, A;
  scanf("%d%d", &n, &A);
  vector<int> a(n);
  int i, j;
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    a[i] -= A;
  }

  dp[0][2000] = 1;
  for (i = 0; i < n; i++) {
    for (j = 0; j <= 4000; j++) {
      dp[i + 1][j] += dp[i][j];
      if (j + a[i] >= 0 && j + a[i] <= 4000)
        dp[i + 1][j + a[i]] += dp[i][j];
    }
  }

  printf("%lld\n", dp[n][2000] - 1);

  return 0;
}
Problem p04015 (C++)

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

using namespace std;

typedef long long ll;

ll s[2510][60][2];

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, a;
  cin >> n >> a;
  s[0][0][0] = 1;
  s[0][0][1] = 1;
  ll ans = 0;
  int x;
  for (int i = 0; i < n; ++i) {
    cin >> x;
    for (int j = 0; j <= 2500; ++j) {
      for (int k = 0; k <= i; ++k) {
        s[j + x][k + 1][1] += s[j][k][0];
      }
    }
    for (int j = 0; j <= 2500; ++j) {
      for (int k = 0; k <= n; ++k) {
        s[j][k][0] = s[j][k][1];
      }
    }
  }
  for (int k = 1; k <= n; ++k) {
    ans += s[a * k][k][1];
  }
  cout << ans;
  return 0;
}

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

using namespace std;

typedef long long ll;

ll s[3000][60][2];

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, a;
  cin >> n >> a;
  s[0][0][0] = 1;
  s[0][0][1] = 1;
  ll ans = 0;
  int x;
  for (int i = 0; i < n; ++i) {
    cin >> x;
    for (int j = 0; j <= 2500; ++j) {
      for (int k = 0; k <= i; ++k) {
        s[j + x][k + 1][1] += s[j][k][0];
      }
    }
    for (int j = 0; j <= 2500; ++j) {
      for (int k = 0; k <= n; ++k) {
        s[j][k][0] = s[j][k][1];
      }
    }
  }
  for (int k = 1; k <= n; ++k) {
    ans += s[a * k][k][1];
  }
  cout << ans;
  return 0;
}
Problem p04015 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
long long int knapsack[50][3000] = {};
long long int ans = 0;
int main() {
  int n, a, t;
  knapsack[0][0] = 1;
  scanf("%d %d", &n, &a);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &t);
    for (int k = i - 1; k >= 0; k--) {
      for (int j = 2500; j >= t; j--) {
        knapsack[k + 1][j] += knapsack[k][j - t];
      }
    }
  }
  for (int i = 1; i <= n; i++)
    ans += knapsack[i][a * i];
  printf("%lld", ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
long long int knapsack[51][3000] = {};
long long int ans = 0;
int main() {
  int n, a, t;
  knapsack[0][0] = 1;
  scanf("%d %d", &n, &a);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &t);
    for (int k = i - 1; k >= 0; k--) {
      for (int j = 2500; j >= t; j--) {
        knapsack[k + 1][j] += knapsack[k][j - t];
      }
    }
  }
  for (int i = 1; i <= n; i++)
    ans += knapsack[i][a * i];
  printf("%lld", ans);
}
Problem p04015 (C++)

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

#define rep(i, a, n) for (int(i) = (a); (i) < (int)(n); (i)++)
#define foreach(c, itr)                                                        \
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
#define SZ(x) ((int)(x).size())
typedef long long ll;

int x[60];
ll dp[60][60][3000];
int main() {
  int n, a, mmax = 0;
  cin >> n >> a;
  rep(i, 0, n) {
    cin >> x[i];
    mmax = max(mmax, x[i]);
  }
  memset(dp, 0, sizeof(dp));
  dp[0][0][0] = 1;
  int len = n * mmax + 1;
  rep(i, 1, n + 1) rep(j, 0, n + 1) rep(k, 0, len) {
    if (k < x[i - 1])
      dp[i][j][k] = dp[i - 1][j][k];
    else if (j >= 1)
      dp[i][j][k] = dp[i - 1][j][k] + dp[i - 1][j - 1][k - x[i - 1]];
  }
  ll ans = 0;
  rep(i, 1, n * mmax + 1) ans += dp[n][i][i * a];
  cout << ans << endl;
  return 0;
}

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

#define rep(i, a, n) for (int(i) = (a); (i) < (int)(n); (i)++)
#define foreach(c, itr)                                                        \
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
#define SZ(x) ((int)(x).size())
typedef long long ll;

int x[60];
ll dp[60][60][3000];
int main() {
  int n, a, mmax = 0;
  cin >> n >> a;
  rep(i, 0, n) {
    cin >> x[i];
    mmax = max(mmax, x[i]);
  }
  memset(dp, 0, sizeof(dp));
  dp[0][0][0] = 1;
  int len = n * mmax + 1;
  rep(i, 1, n + 1) rep(j, 0, n + 1) rep(k, 0, len) {
    if (k < x[i - 1])
      dp[i][j][k] = dp[i - 1][j][k];
    else if (j >= 1)
      dp[i][j][k] = dp[i - 1][j][k] + dp[i - 1][j - 1][k - x[i - 1]];
  }
  ll ans = 0;
  rep(i, 1, n + 1) ans += dp[n][i][i * a];
  cout << ans << endl;
  return 0;
}
Problem p04015 (C++)

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

long long dp[51][51][51];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, a;
  cin >> n >> a;
  vector<int> x(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> x[i];
  for (int i = 0; i <= n; i++)
    dp[i][0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      for (int k = 1; k <= n * a; k++) {
        if (k < x[i])
          dp[i][j][k] = dp[i - 1][j][k];
        else
          dp[i][j][k] = dp[i - 1][j][k] + dp[i - 1][j - 1][k - x[i]];
      }
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    ans += dp[n][i][i * a];
  cout << ans << "\n";
  return 0;
}

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

long long dp[51][51][2501];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, a;
  cin >> n >> a;
  vector<int> x(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> x[i];
  for (int i = 0; i <= n; i++)
    dp[i][0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      for (int k = 1; k <= n * a; k++) {
        if (k < x[i])
          dp[i][j][k] = dp[i - 1][j][k];
        else
          dp[i][j][k] = dp[i - 1][j][k] + dp[i - 1][j - 1][k - x[i]];
      }
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    ans += dp[n][i][i * a];
  cout << ans << "\n";
  return 0;
}
Problem p04015 (C++)

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

typedef long long ll;
const int N = 55;

int n, A, a[N];
ll mm[N][N][N * N];

ll dp(int i, int cnt, int sum) {
  if (i == n) {
    if (cnt == 0)
      return 0;
    if ((sum / cnt == A) && (sum % cnt == 0))
      return 1;
    return 0;
  }

  ll ret = mm[i][cnt][sum];
  if (ret != -1)
    return ret;
  return ret = dp(i + 1, cnt, sum) + dp(i + 1, cnt + 1, sum + a[i]);
}

int main() {
  cin >> n >> A;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  memset(mm, -1, sizeof mm);
  cout << dp(0, 0, 0) << endl;

  return 0;
}

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

typedef long long ll;
const int N = 55;

int n, A, a[N];
ll mm[N][N][N * N];

ll dp(int i, int cnt, int sum) {
  if (i == n) {
    if (cnt == 0)
      return 0;
    if ((sum / cnt == A) && (sum % cnt == 0))
      return 1;
    return 0;
  }

  ll &ret = mm[i][cnt][sum];
  if (ret != -1)
    return ret;
  return ret = dp(i + 1, cnt, sum) + dp(i + 1, cnt + 1, sum + a[i]);
}

int main() {
  cin >> n >> A;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  memset(mm, -1, sizeof mm);
  cout << dp(0, 0, 0) << endl;

  return 0;
}
Problem p04015 (C++)

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

#define FOR(i, l, r) for (int i = (l); i < (r); i++)
#define PB push_back
#define MP make_pair
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;

int N, A;
vector<ll> V;
ll dp[51][2501][51];

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

  cin >> N >> A;
  V.resize(N);
  FOR(i, 0, N) { cin >> V[i]; }

  dp[0][0][0] = 1;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= N * 50; j++) {
      for (int k = 0; k < N; k++) {
        dp[i + 1][j][k] += dp[i][j][k];
        dp[i + 1][j + V[i]][k + 1] += dp[i][j][k];
      }
    }
  }

  ll ans = 0;
  for (int i = 1; i <= N; i++) {
    ans += dp[N][i * A][i];
  }

  cout << ans << endl;

  return 0;
}


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

#define FOR(i, l, r) for (int i = (l); i < (r); i++)
#define PB push_back
#define MP make_pair
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;

int N, A;
vector<ll> V;
ll dp[51][2501][51];

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

  cin >> N >> A;
  V.resize(N);
  FOR(i, 0, N) { cin >> V[i]; }

  dp[0][0][0] = 1;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= N * 50; j++) {
      for (int k = 0; k < N; k++) {
        dp[i + 1][j][k] += dp[i][j][k];
        if (j + V[i] <= N * 50) {
          dp[i + 1][j + V[i]][k + 1] += dp[i][j][k];
        }
      }
    }
  }

  ll ans = 0;
  for (int i = 1; i <= N; i++) {
    ans += dp[N][i * A][i];
  }

  cout << ans << endl;

  return 0;
}
Problem p04015 (C++)

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

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iomanip>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
// typedef vector<ll> vec;
// typedef vector<vec> mat;

const ll mod = 1000000007ll;
const ll inf = 1000000000000000ll;

const ll MAX_N = 50, MAX_A = 50;

ll N, A, x[MAX_N], dp[MAX_N][MAX_N][MAX_A * MAX_N];

int main() {
  cin >> N >> A;
  for (ll i = 1; i <= N; i++) {
    cin >> x[i];
  }
  dp[0][0][0] = 1;
  for (ll i = 0; i <= N; i++) {
    for (ll j = 0; j <= N; j++) {
      for (ll k = 0; k <= MAX_A * MAX_N; k++) {
        if (i - 1 >= 0 && j - 1 >= 0 && k - x[i] >= 0) {
          dp[i][j][k] = dp[i - 1][j][k] + dp[i - 1][j - 1][k - x[i]];
        } else if (i - 1 >= 0 && k - x[i] < 0) {
          dp[i][j][k] = dp[i - 1][j][k];
        } else if (i == 0 && j == 0 && k == 0) {
          dp[i][j][k] = 1;
        } else {
          dp[i][j][k] = 0;
        }
      }
    }
  }
  ll ans = 0;
  for (ll i = 1; i <= N; i++) {
    ans += dp[N][i][i * A];
  }
  /*
    for(ll i=0;i<=N;i++){
      for(ll j=0;j<=33;j++){
        cout<<dp[N][i][j]<<" ";
      }
      cout<<endl;
    }
  */
  cout << ans << endl;
  return 0;
}


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

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iomanip>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
// typedef vector<ll> vec;
// typedef vector<vec> mat;

const ll mod = 1000000007ll;
const ll inf = 1000000000000000ll;

const ll MAX_N = 60, MAX_A = 60;

ll N, A, x[MAX_N], dp[MAX_N][MAX_N][MAX_A * MAX_N];

int main() {
  cin >> N >> A;
  for (ll i = 1; i <= N; i++) {
    cin >> x[i];
  }
  dp[0][0][0] = 1;
  for (ll i = 0; i <= N; i++) {
    for (ll j = 0; j <= N; j++) {
      for (ll k = 0; k <= MAX_A * MAX_N; k++) {
        if (i - 1 >= 0 && j - 1 >= 0 && k - x[i] >= 0) {
          dp[i][j][k] = dp[i - 1][j][k] + dp[i - 1][j - 1][k - x[i]];
        } else if (i - 1 >= 0 && k - x[i] < 0) {
          dp[i][j][k] = dp[i - 1][j][k];
        } else if (i == 0 && j == 0 && k == 0) {
          dp[i][j][k] = 1;
        } else {
          dp[i][j][k] = 0;
        }
      }
    }
  }
  ll ans = 0;
  for (ll i = 1; i <= N; i++) {
    ans += dp[N][i][i * A];
  }
  /*
    for(ll i=0;i<=N;i++){
      for(ll j=0;j<=33;j++){
        cout<<dp[N][i][j]<<" ";
      }
      cout<<endl;
    }
  */
  cout << ans << endl;
  return 0;
}
Problem p04015 (C++)

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

ll dp[61][61][5501]; // i個目までの選択肢でｊ個を選んで合計がｋの個数
int main() {
  int n, a;
  cin >> n >> a;
  vector<ll> x(n);
  for (int i = 0; i < n; i++)
    cin >> x[i];
  dp[0][0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < 2501; k++) {
        if (dp[i][j][k]) {
          dp[i + 1][j][k] += dp[i][j][k];
          dp[i + 1][j + 1][k + x[i]] += dp[i][j][k];
        }
      }
    }
  }
  ll cnt = 0;

  for (int i = 1; i * a <= (2500); i++) {
    cnt += dp[n][i][a * i];
  }

  cout << cnt << endl;
}


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

ll dp[61][61][5501]; // i個目までの選択肢でｊ個を選んで合計がｋの個数
int main() {
  int n, a;
  cin >> n >> a;
  vector<ll> x(n);
  for (int i = 0; i < n; i++)
    cin >> x[i];
  dp[0][0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < 2501; k++) {
        if (dp[i][j][k]) {
          dp[i + 1][j][k] += dp[i][j][k];
          dp[i + 1][j + 1][k + x[i]] += dp[i][j][k];
        }
      }
    }
  }
  ll cnt = 0;

  for (int i = 1; i * a <= (2500) && i <= 60; i++) {
    cnt += dp[n][i][a * i];
  }

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

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

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef set<int> si;
typedef map<int, int> mii;

#define FOR(i, a, b) for (int i = a; i < b; i++)
#define F0R(i, a) for (int i = 0; i < a; i++)
#define FORd(i, a, b) for (int i = b - 1; i >= a; i--)
#define F0Rd(i, a) for (int i = a - 1; i >= 0; i--)

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

const int MOD = 1000000007;
double PI = 4 * atan(1);

ll n, s;

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> s;
  if (s > n) {
    cout << -1;
    return 0;
  }
  ll i = 2;
  while (i * i <= n) {
    if (f(i, n) == s) {
      cout << i;
      return 0;
    }
    i++;
  }
  for (ll j = s; j > 0; --j) {
    if ((n - (s - j)) % j == 0) {
      ll k = (n - (s - j)) / j;
      if (k > j && k > (s - j)) {
        cout << k;
        return 0;
      }
    }
  }
  if (n == s) {
    cout << (n + 1);
    return 0;
  }
  cout << -1;
}

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

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef set<int> si;
typedef map<int, int> mii;

#define FOR(i, a, b) for (int i = a; i < b; i++)
#define F0R(i, a) for (int i = 0; i < a; i++)
#define FORd(i, a, b) for (int i = b - 1; i >= a; i--)
#define F0Rd(i, a) for (int i = a - 1; i >= 0; i--)

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

const int MOD = 1000000007;
double PI = 4 * atan(1);

ll n, s;

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> s;
  if (s > n) {
    cout << -1;
    return 0;
  }
  ll i = 2;
  while (i * i <= n) {
    if (f(i, n) == s) {
      cout << i;
      return 0;
    }
    i++;
  }
  for (ll j = min(s, i); j > 0; --j) {
    if ((n - (s - j)) % j == 0) {
      ll k = (n - (s - j)) / j;
      if (k > j && k > (s - j)) {
        cout << k;
        return 0;
      }
    }
  }
  if (n == s) {
    cout << (n + 1);
    return 0;
  }
  cout << -1;
}
Problem p04016 (C++)

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

int n, s;
const int INF = 1e14;
int ans = INF;
signed main() {
  cin >> n >> s;

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

  for (int i = 2; i * i <= n; i++) {
    int tmp = n;
    int sum = 0;
    while (tmp > 0) {
      sum += tmp % i;
      tmp /= i;
    }
    if (sum == s) {
      cout << i << endl;
      return 0;
    }
  }
  for (int i = 1; i * i < n; i++) {
    int b = (n - s) / i + 1;
    int tmp = n;
    int sum = 0;
    while (tmp > 0) {
      sum += tmp % b;
      tmp /= b;
    }
    if (sum == s) {
      ans = min(ans, b);
    }
  }

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


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

int n, s;
const int INF = 1e14;
int ans = INF;
signed main() {
  cin >> n >> s;

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

  for (int i = 2; i * i <= n; i++) {
    int tmp = n;
    int sum = 0;
    while (tmp > 0) {
      sum += tmp % i;
      tmp /= i;
    }
    if (sum == s) {
      cout << i << endl;
      return 0;
    }
  }
  for (int i = 1; i * i < n; i++) {
    int b = (n - s) / i + 1;
    int tmp = n;
    int sum = 0;
    if (b < 2)
      continue;
    while (tmp > 0) {
      sum += tmp % b;
      tmp /= b;
    }
    if (sum == s) {
      ans = min(ans, b);
    }
  }

  if (ans == INF)
    cout << "-1" << endl;
  else
    cout << ans << endl;
}
Problem p04016 (C++)

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

int main() {
  ios::sync_with_stdio(false);
  // n/b+n%b=s (n/b)*b+n%b=n (n/b)*(b-1)=n-s
  ll n, s;
  cin >> n >> s;
  for (ll i = 2; i <= 890328; i++) {
    ll b = n, c = 0;
    while (b)
      c += b % i, b /= i;
    if (c == s) {
      cout << i;
      return 0;
    }
  }
  if (n == s) {
    cout << n + 1;
    return 0;
  }
  for (int i = 890328; i; i--)
    if ((n - s) % i == 0) {
      ll j = (n - s) / i + 1, b = n, c = 0;
      while (b)
        c += b % j, b /= j;
      if (c == s) {
        cout << j;
        return 0;
      }
    }
  if (s == 1)
    cout << n;
  else
    cout << -1;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  // n/b+n%b=s (n/b)*b+n%b=n (n/b)*(b-1)=n-s
  ll n, s;
  cin >> n >> s;
  for (ll i = 2; i <= 890328; i++) {
    ll b = n, c = 0;
    while (b)
      c += b % i, b /= i;
    if (c == s) {
      cout << i;
      return 0;
    }
  }
  if (n == s) {
    cout << n + 1;
    return 0;
  }
  if (n < s) {
    cout << -1;
    return 0;
  }
  for (int i = 890328; i; i--)
    if ((n - s) % i == 0) {
      ll j = (n - s) / i + 1, b = n, c = 0;
      while (b)
        c += b % j, b /= j;
      if (c == s) {
        cout << j;
        return 0;
      }
    }
  if (s == 1)
    cout << n;
  else
    cout << -1;
}
Problem p04016 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstdlib>
#include <stdint.h>

void check(int64_t N, int64_t S, int64_t B) {
  int64_t sum = 0;
  for (; N; N /= B) {
    sum += N % B;
  }
  if (sum == S) {
    printf("%ld\n", B);
    exit(0);
  }
}

int main() {
  int64_t N, S;
  scanf("%ld %ld", &N, &S);
  if (S > N) {
    printf("-1\n");
    return 0;
  }
  if (S == N) {
    printf("%ld\n", N + 1);
  }
  int64_t i = 1;
  for (; i * i <= (N - S); i++) {
    if ((N - S) % i == 0)
      check(N, S, i + 1);
  }
  for (; i > 0; i--) {
    if ((N - S) % i == 0)
      check(N, S, (N - S) / i + 1);
  }
  printf("-1\n");
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <stdint.h>

void check(int64_t N, int64_t S, int64_t B) {
  int64_t sum = 0;
  for (; N; N /= B) {
    sum += N % B;
  }
  if (sum == S) {
    printf("%ld\n", B);
    exit(0);
  }
}

int main() {
  int64_t N, S;
  scanf("%ld %ld", &N, &S);
  if (S > N) {
    printf("-1\n");
    return 0;
  }
  if (S == N) {
    printf("%ld\n", N + 1);
    return 0;
  }
  int64_t i = 1;
  for (; i * i <= (N - S); i++) {
    if ((N - S) % i == 0)
      check(N, S, i + 1);
  }
  for (; i > 0; i--) {
    if ((N - S) % i == 0)
      check(N, S, (N - S) / i + 1);
  }
  printf("-1\n");
  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using pii = std::pair<int, int>;
using namespace std;

int n, s;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << "\n";
    return 0;
  }
  int ans = 2e18;
  for (int i = 2; i * i <= n; i++) {
    int cursum = 0, x = n;
    while (x > 0) {
      cursum += x % i;
      x /= i;
    }
    if (cursum == s)
      ans = min(ans, i);
  }
  for (int i = 1; i * i < n; i++)
    if ((n - s) % i == 0) {
      int b = (n - s) / i + 1;
      int cursum = 0, x = n;
      while (x > 0) {
        cursum += x % b;
        x /= b;
      }
      if (cursum == s)
        ans = min(ans, b);
    }
  if (ans < 1e18)
    cout << ans << "\n";
  else
    cout << "-1\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using pii = std::pair<int, int>;
using namespace std;

int n, s;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << "\n";
    return 0;
  }
  int ans = 2e18;
  for (int i = 2; i * i <= n; i++) {
    int cursum = 0, x = n;
    while (x > 0) {
      cursum += x % i;
      x /= i;
    }
    if (cursum == s)
      ans = min(ans, i);
  }
  for (int i = 1; i * i < n; i++)
    if ((n - s) % i == 0) {
      int b = (n - s) / i + 1;
      if (b < 2)
        continue;
      int cursum = 0, x = n;
      while (x > 0) {
        cursum += x % b;
        x /= b;
      }
      if (cursum == s)
        ans = min(ans, b);
    }
  if (ans < 1e18)
    cout << ans << "\n";
  else
    cout << "-1\n";
  return 0;
}
Problem p04016 (C++)

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

typedef long long ll;
ll n, s;
vector<ll> v;

bool func(ll b) {
  ll m = n, t = 0;
  while (m) {
    t += m % b;
    m /= b;
  }
  return t == s;
}

int main() {
  //	cin.tie(0);
  //	ios::sync_with_stdio(false);
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << "\n";
    return 0;
  }
  for (int b = 2; (ll)b * b <= n; ++b) {
    if (func(b)) {
      cout << b << "\n";
      return 0;
    }
  }
  for (int p = 1; (ll)p * p < n; ++p)
    if ((n - s) % p == 0)
      v.push_back((n - s) / p + 1);
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); ++i) {
    if (func(v[i])) {
      cout << v[i] << "\n";
      return 0;
    }
  }
  cout << -1 << "\n";
}

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

typedef long long ll;
ll n, s;
vector<ll> v;

bool func(ll b) {
  ll m = n, t = 0;
  while (m) {
    t += m % b;
    m /= b;
  }
  return t == s;
}

int main() {
  //	cin.tie(0);
  //	ios::sync_with_stdio(false);
  cin >> n >> s;
  if (n < s) {
    cout << -1 << "\n";
    return 0;
  }
  if (n == s) {
    cout << n + 1 << "\n";
    return 0;
  }
  for (int b = 2; (ll)b * b <= n; ++b) {
    if (func(b)) {
      cout << b << "\n";
      return 0;
    }
  }
  for (int p = 1; (ll)p * p < n; ++p)
    if ((n - s) % p == 0)
      v.push_back((n - s) / p + 1);
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); ++i) {
    if (func(v[i])) {
      cout << v[i] << "\n";
      return 0;
    }
  }
  cout << -1 << "\n";
}
Problem p04016 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long int lnt;
lnt n, s, b, root;

lnt aq(lnt n, lnt i) {
  if (n == 0)
    return 0;
  return n % i + aq(n / i, i);
}

int main() {
  cin >> n >> s;
  if (n < s) {
    cout << -1 << endl;
    return 0;
  }
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  root = floor(sqrt(n)) + 2;
  for (lnt i = 2; i < root; i++) {
    if (aq(n, i) == s) {
      cout << i << endl;
      return 0;
    }
  }
  const lnt INF = 0xfffffffffff;
  lnt ans = INF;
  for (lnt i = 1; i < root; i++) {
    b = (n - s) / i + 1;
    if (aq(n, b) == s)
      ans = min(ans, b);
  }
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long int lnt;
lnt n, s, b, root;

lnt aq(lnt n, lnt i) {
  if (n == 0)
    return 0;
  return n % i + aq(n / i, i);
}

int main() {
  cin >> n >> s;
  if (n < s) {
    cout << -1 << endl;
    return 0;
  }
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  root = floor(sqrt(n)) + 2;
  for (lnt i = 2; i < root; i++) {
    if (aq(n, i) == s) {
      cout << i << endl;
      return 0;
    }
  }
  const lnt INF = 0xfffffffffff;
  lnt ans = INF;
  for (lnt i = 1; i < root; i++) {
    if ((n - s) % i == 0) {
      b = (n - s) / i + 1;
      if (aq(n, b) == s)
        ans = min(ans, b);
    }
  }
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define reset(a, b) memset(a, b, sizeof a);
#define ALL(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << (x) << endl
#define sz(x) (int)(x.size())
#define lt(x) (int)(x.length())
#define MOD 1000000007
#define mx 100003

long long sum(long long x, long long b) {
  if (x == 0)
    return 0;
  return sum(x / b, b) + (x % b);
}
long long n, s;
int main() {
  cin >> n >> s;
  for (int i = 2; i <= 1e6; i++) {
    if (sum(n, i) == s) {
      cout << i << endl;
      return 0;
    }
  }
  if (s > n) {
    cout << -1 << endl;
    return 0;
  }
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  long long selisih = n - s;
  set<long long> faktor;
  for (long long i = 1; i * i <= selisih; i++) {
    if (selisih % i == 0) {
      faktor.insert(i);
      faktor.insert(selisih / i);
    }
  }
  long long jaw = 1e18;
  for (auto i : faktor) {
    long long bit = i + 1;
    // debug(bit);
    if ((bit - 1) * (n / bit) == selisih) {
      assert(sum(n, bit) == s);
      jaw = min(jaw, bit);
    }
  }
  if (jaw == 1e18)
    cout << -1 << endl;
  else
    cout << jaw << endl;
}

// 2.000.002, selisih = bit * (n / bit) - (n / bit);
//(bit - 1) * (n / bit)

// 31415926535
// 1


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define reset(a, b) memset(a, b, sizeof a);
#define ALL(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << (x) << endl
#define sz(x) (int)(x.size())
#define lt(x) (int)(x.length())
#define MOD 1000000007
#define mx 100003

long long sum(long long x, long long b) {
  if (x == 0)
    return 0;
  return sum(x / b, b) + (x % b);
}
long long n, s;
int main() {
  cin >> n >> s;
  for (int i = 2; i <= 1e6; i++) {
    if (sum(n, i) == s) {
      cout << i << endl;
      return 0;
    }
  }
  if (s > n) {
    cout << -1 << endl;
    return 0;
  }
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  long long selisih = n - s;
  set<long long> faktor;
  for (long long i = 1; i * i <= selisih; i++) {
    if (selisih % i == 0) {
      faktor.insert(i);
      faktor.insert(selisih / i);
    }
  }
  long long jaw = 1e18;
  for (auto i : faktor) {
    if (i <= 1e6)
      continue;
    long long bit = i + 1;
    // debug(bit);
    if ((bit - 1) * (n / bit) == selisih) {
      assert(sum(n, bit) == s);
      jaw = min(jaw, bit);
    }
  }
  if (jaw == 1e18)
    cout << -1 << endl;
  else
    cout << jaw << endl;
}

// 2.000.002, selisih = bit * (n / bit) - (n / bit);
//(bit - 1) * (n / bit)

// 31415926535
// 1
Problem p04016 (C++)

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

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

int main() {
  ll n;
  ll s;
  cin >> n >> s;
  if (s > n) {
    cout << -1 << endl;
    return 0;
  }
  for (int b = 2; b < 1e6; b++) {
    if (f(b, n) == s) {
      cout << b << endl;
      return 0;
    }
  }
  // ab+c=n a+c=s
  // b=(n+a-s)/a=(n-s)/a+1
  for (ll a = 1e6; a >= 1; a--) {
    if ((n + a - s) % a == 0) {
      ll b = (n + a - s) / a;
      if (f(b, n) == s) {
        cout << b << endl;
        return 0;
      }
    }
  }
  if (n == s) {
    cout << n + 1 << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}


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

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

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

const int INF = 1e18 + 239;

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

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

  // 5 3
  // 12
  // 11

  int n, s;
  cin >> n >> s;
  for (int i = 2; i * i <= n; i++) {
    if (f(i, n) == s) {
      cout << i << endl;
      return 0;
    }
  }
  int cb = INF;
  for (int ff = 1; ff * ff < n; ff++) {
    // ff ss
    int ss = s - ff;
    // b * ff + ss == n
    if ((n - ss) > ff) {
      int cand = (n - ss) / ff;
      if (f(cand, n) == s)
        cb = min(cb, cand);
    }
  }
  if (f(n + 1, n) == s) {
    cb = min(cb, n + 1);
  }
  if (cb == INF) {
    cout << -1 << endl;
  } else {
    cout << cb << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

const int INF = 1e18 + 239;

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

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

  // 5 3
  // 12
  // 11

  int n, s;
  cin >> n >> s;
  for (int i = 2; i * i <= n; i++) {
    if (f(i, n) == s) {
      cout << i << endl;
      return 0;
    }
  }
  int cb = INF;
  for (int ff = 1; ff * ff < n; ff++) {
    // ff ss
    int ss = s - ff;
    // b * ff + ss == n
    if ((n - ss) >= 2 * ff) {
      int cand = (n - ss) / ff;
      if (f(cand, n) == s)
        cb = min(cb, cand);
    }
  }
  if (f(n + 1, n) == s) {
    cb = min(cb, n + 1);
  }
  if (cb == INF) {
    cout << -1 << endl;
  } else {
    cout << cb << endl;
  }
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

const int INF = 1000000009;
const long LINF = 1123456789012345678;

long n, s;

long f(long b, long m) {
  // printf("pass\n");
  if (m < b) {
    return m;
  } else {
    return f(b, m / b) + (m % b);
  }
}

int main() {
  scanf("%ld %ld", &n, &s);
  if (n < s) {
    printf("-1\n");
    return 0;
  }
  if (n == s) {
    printf("%ld\n", n + 1);
    return 0;
  }
  for (long b = 2; b * b <= n; b++) {
    // printf("%ld\n", b);
    if (f(b, n) == s) {
      printf("%ld\n", b);
      return 0;
    }
  }
  long ans = LINF;
  for (long p = 1; p * p < n; p++) {
    // printf("%ld\n", p);
    long b = (n - s) / p + 1;
    if (f(b, n) == s) {
      ans = std::min(ans, b);
    }
  }
  if (ans != LINF) {
    printf("%ld\n", ans);
  } else {
    printf("-1\n");
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

const int INF = 1000000009;
const long LINF = 1123456789012345678;

long n, s;

long f(long b, long m) {
  // printf("pass\n");
  if (m < b) {
    return m;
  } else {
    return f(b, m / b) + (m % b);
  }
}

int main() {
  scanf("%ld %ld", &n, &s);
  if (n < s) {
    printf("-1\n");
    return 0;
  }
  if (n == s) {
    printf("%ld\n", n + 1);
    return 0;
  }
  for (long b = 2; b * b <= n; b++) {
    // printf("%ld\n", b);
    if (f(b, n) == s) {
      printf("%ld\n", b);
      return 0;
    }
  }
  long ans = LINF;
  for (long p = 1; p * p < n; p++) {
    // printf("%ld\n", p);
    long b = (n - s) / p + 1;
    if (b > 1 && f(b, n) == s) {
      ans = std::min(ans, b);
    }
  }
  if (ans != LINF) {
    printf("%ld\n", ans);
  } else {
    printf("-1\n");
  }
  return 0;
}
Problem p04016 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define len(v) ll(v.size())
#define fi first
#define se second

template <class T> void cout_vec(const vector<T> &vec) {
  for (auto itr : vec)
    cout << itr << ' ';
  cout << endl;
}

typedef pair<ll, ll> P;
const ll mod = 1e9 + 7;
const ll inf = 1e15;

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 (ll b = 2; b * b <= n; b++) {
    ll now = 0;
    ll t = n;
    while (t) {
      now += t % b;
      t /= b;
    }
    if (now == s) {
      cout << b << endl;
      return 0;
    }
  }
  ll ans = inf;
  for (ll p = 1; p * p < n; p++) { // この時, nはb進数で2桁になるb=p*b+q, s=p+q
    ll b = (n - s) / p + 1;
    ll t = n;
    ll now = 0;
    if (b == 1)
      continue;
    while (t) {
      now += t % b;
      t /= b;
    }
    if (now == s)
      ans = min(ans, b);
  }
  if (ans == inf)
    cout << -1 << endl;
  else
    cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define len(v) ll(v.size())
#define fi first
#define se second

template <class T> void cout_vec(const vector<T> &vec) {
  for (auto itr : vec)
    cout << itr << ' ';
  cout << endl;
}

typedef pair<ll, ll> P;
const ll mod = 1e9 + 7;
const ll inf = 1e15;

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 (ll b = 2; b * b <= n; b++) {
    ll now = 0;
    ll t = n;
    while (t) {
      now += t % b;
      t /= b;
    }
    if (now == s) {
      cout << b << endl;
      return 0;
    }
  }
  ll ans = inf;
  for (ll p = 1; p * p < n; p++) { // この時, nはb進数で2桁になるb=p*b+q, s=p+q
    ll b = (n - s) / p + 1;
    ll t = n;
    ll now = 0;
    if (b == 1 || b == 0)
      continue;
    while (t) {
      now += t % b;
      t /= b;
    }
    if (now == s)
      ans = min(ans, b);
  }
  if (ans == inf)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
Problem p04016 (C++)

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

typedef long long int lli;
typedef pair<lli, int> pli;
long long int n, s;
const int SIZE = 400005;
bool isPrime[SIZE];
vector<pli> v;
lli INF = 12345987654321;
lli ans = INF, tmp = 1;

void check(int p) {
  if (p == v.size()) {
    lli b = tmp + 1, t = n, sum = 0;
    while (t) {
      sum += t % b;
      t /= b;
    }
    if (sum == s && b < ans)
      ans = b;
    return;
  }
  lli save = tmp;
  for (int i = 0; i <= v[p].second; ++i) {
    check(p + 1);
    tmp *= v[p].first;
  }
  tmp = save;
}

int main() {
  scanf("%lld%lld", &n, &s);
  long long int t = n - s;
  for (int i = 2; i < SIZE; ++i)
    isPrime[i] = true;
  for (lli i = 2; i < SIZE; ++i) {
    if (isPrime[i]) {
      for (lli j = i * i; j < SIZE; j += i)
        isPrime[j] = false;
      int cnt = 0;
      while (t % i == 0) {
        ++cnt;
        t /= i;
      }
      if (cnt)
        v.push_back({i, cnt});
    }
  }
  if (t > 1)
    v.push_back({t, 1});
  check(0);
  if (ans == INF)
    ans = -1;
  printf("%lld\n", ans);
}

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

typedef long long int lli;
typedef pair<lli, int> pli;
long long int n, s;
const int SIZE = 400005;
bool isPrime[SIZE];
vector<pli> v;
lli INF = 12345987654321;
lli ans = INF, tmp = 1;

void check(int p) {
  if (p == v.size()) {
    lli b = tmp + 1, t = n, sum = 0;
    while (t) {
      sum += t % b;
      t /= b;
    }
    if (sum == s && b < ans)
      ans = b;
    return;
  }
  lli save = tmp;
  for (int i = 0; i <= v[p].second; ++i) {
    check(p + 1);
    tmp *= v[p].first;
  }
  tmp = save;
}

int main() {
  scanf("%lld%lld", &n, &s);
  long long int t = n - s;
  if (t == 0) {
    printf("%lld\n", n + 1);
    return 0;
  }
  for (int i = 2; i < SIZE; ++i)
    isPrime[i] = true;
  for (lli i = 2; i < SIZE; ++i) {
    if (isPrime[i]) {
      for (lli j = i * i; j < SIZE; j += i)
        isPrime[j] = false;
      int cnt = 0;
      while (t % i == 0) {
        ++cnt;
        t /= i;
      }
      if (cnt)
        v.push_back({i, cnt});
    }
  }
  if (t > 1)
    v.push_back({t, 1});
  check(0);
  if (ans == INF)
    ans = -1;
  printf("%lld\n", ans);
}
Problem p04016 (C++)

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

int main() {
  ll N, S;
  ll ans = -1;

  cin >> N >> S;

  ll val = (ll)sqrt((double)N);
  // 実際に割るO(sqrt(N) log sqrt(N))
  for (ll i = 2; i <= val; i++) {
    ll sum = 0;
    ll n = N;
    while (n)
      sum += n % i, n /= i;
    if (!~ans && sum == S)
      ans = i;
  }

  for (ll i = val; i > 0; i--) {
    if ((N - S) % i == 0) {
      ll sum = 0;
      ll n = N;
      ll b = ((N - S) / i + 1);
      while (n)
        sum += n % b, n /= b;
      if (!~ans && sum == S)
        ans = b;
    }
  }
  if (!~ans && N == S)
    ans = N + 1;
  cout << ans << endl;
  return (0);
}


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

int main() {
  ll N, S;
  ll ans = -1;

  cin >> N >> S;

  ll val = (ll)sqrt((double)N);
  // 実際に割るO(sqrt(N) log sqrt(N))
  for (ll i = 2; i <= val; i++) {
    ll sum = 0;
    ll n = N;
    while (n)
      sum += n % i, n /= i;
    if (!~ans && sum == S)
      ans = i;
  }

  for (ll i = val; i > 0; i--) {
    if ((N - S) % i == 0) {
      ll sum = 0;
      ll n = N;
      ll b = ((N - S) / i + 1);
      if (b >= 2) {
        while (n)
          sum += n % b, n /= b;
        if (!~ans && sum == S)
          ans = b;
      }
    }
  }
  if (!~ans && N == S)
    ans = N + 1;
  cout << ans << 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++)
typedef long long ll;
typedef long double Double;

#ifndef INPUTS_DIR
#define INPUTS_DIR "./"
#endif

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

void solve(ll n, ll s) {
  ll b = 1e18;
  if (n == s) {
    b = n + 1;
  }

  for (int i = 2; i <= 1000000; i++) {
    if (sum(n, i) == s) {
      b = min(b, 1ll * i);
    }
  }

  for (int i = 1; i <= 1000000; i++) {
    if ((i + n - s) % i == 0) {
      if (sum(n, (i + n - s) / i) == s) {
        b = min(b, (i + n - s) / i);
      }
    }
  }

  if (b == 1e18) {
    cout << -1 << endl;
  } else {
    cout << b << endl;
  }
}

void main_(istream &cin) {
  ll s;
  ll n;
  cin >> n;
  cin >> s;
  solve(n, s);
}

int main() {
#ifdef INTELLIJ
  ifstream ifs(string(INPUTS_DIR) + "in_1.txt");
  if (ifs.is_open()) {
    main_(ifs);
    ifs.close();
  } else {
    cerr << "Error: no input." << endl;
  }
#else
  ios::sync_with_stdio(false);
  cin.tie(0);
  main_(cin);
#endif
}


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

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef long double Double;

#ifndef INPUTS_DIR
#define INPUTS_DIR "./"
#endif

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

void solve(ll n, ll s) {
  ll b = 1e18;
  if (n == s) {
    b = n + 1;
  }

  for (int i = 2; i <= 1000000; i++) {
    if (sum(n, i) == s) {
      b = min(b, 1ll * i);
    }
  }

  for (int i = 1; i <= 1000000; i++) {
    if ((i + n - s) % i == 0) {
      if ((i + n - s) / i > 1) {
        if (sum(n, (i + n - s) / i) == s) {
          b = min(b, (i + n - s) / i);
        }
      }
    }
  }

  if (b == 1e18) {
    cout << -1 << endl;
  } else {
    cout << b << endl;
  }
}

void main_(istream &cin) {
  ll s;
  ll n;
  cin >> n;
  cin >> s;
  solve(n, s);
}

int main() {
#ifdef INTELLIJ
  ifstream ifs(string(INPUTS_DIR) + "in_1.txt");
  if (ifs.is_open()) {
    main_(ifs);
    ifs.close();
  } else {
    cerr << "Error: no input." << endl;
  }
#else
  ios::sync_with_stdio(false);
  cin.tie(0);
  main_(cin);
#endif
}