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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const ll INF = 1e18;

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

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

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const ll INF = 1e18;

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(A) A.begin(), A.end()
#define INF ((ll)(1e18))

using namespace std;

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

ll f(ll b, ll n) {
  if (b > n) {
    return n;
  } // end if

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

ll solve(ll n, ll s) {
  if (n == s) {
    return n + 1LL;
  } // end if

  ll ans = INF;
  for (ll b = 2LL; b <= sqrt(n); ++b) {
    if (f(b, n) == s) {
      ans = min(ans, b);
    } // end if
  }   // end for

  for (ll p = 1LL; p < sqrt(n); ++p) {
    ll b = (n - s) / p + 1LL;
    if (f(b, n) == s) {
      ans = min(ans, b);
    } // end if
  }   // end for

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n, s;
  cin >> n >> s;

  ll res = solve(n, s);

  cout << res << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(A) A.begin(), A.end()
#define INF ((ll)(1e18))

using namespace std;

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

ll f(ll b, ll n) {
  if (b > n) {
    return n;
  } // end if

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

ll solve(ll n, ll s) {
  if (n == s) {
    return n + 1LL;
  } // end if

  ll ans = INF;
  for (ll b = 2LL; b <= sqrt(n); ++b) {
    if (f(b, n) == s) {
      ans = min(ans, b);
    } // end if
  }   // end for

  for (ll p = 1LL; p < sqrt(n); ++p) {
    ll b = (n - s) / p + 1LL;
    if (b < 2LL)
      continue;
    if (f(b, n) == s) {
      ans = min(ans, b);
    } // end if
  }   // end for

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n, s;
  cin >> n >> s;

  ll res = solve(n, s);

  cout << res << endl;

  return 0;
}
Problem p04014 (C++)

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

#define int long long

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

int n, s;

int f(int b, int n) {
  if (n < b) {
    return n;
  }
  if (b == 0) {
    return -1;
  }
  int c = f(b, n / b);
  if (c == -1) {
    return -1;
  }
  return c + n % b;
}

signed main() {
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  for (int i = 2; i <= sqrt(n); i++) {
    if (f(i, n) == s) {
      cout << i << endl;
      return 0;
    }
  }
  for (int i = ceil(sqrt(n) - 1); i > 0; i--) {
    if ((s - n % i - i) % i == 0) {
      int x = (s - n % i - i) / i;
      if (f(n / i - x, n) == s) {
        cout << n / i - x << endl;
        return 0;
      }
    }
  }
  cout << -1 << endl;
  return 0;
}


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

#define int long long

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

int n, s;

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

signed main() {
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  for (int i = 2; i <= sqrt(n); i++) {
    if (f(i, n) == s) {
      cout << i << endl;
      return 0;
    }
  }
  for (int i = ceil(sqrt(n) - 1); i > 0; i--) {
    if ((s - n % i - i) % i == 0) {
      int x = (s - n % i - i) / i;
      if (f(n / i - x, n) == s) {
        cout << n / i - x << endl;
        return 0;
      }
    }
  }
  cout << -1 << endl;
  return 0;
}
Problem p04014 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <iostream>

using namespace std;

typedef long long ll;

const ll MAX_N = 1E+11;

ll N, S;

ll solve(ll n, ll s);
bool f(ll b, ll n, ll s);

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

  cin >> N >> S;

  cout << solve(N, S) << "\n";

  return 0;
}

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

bool f(ll b, ll n, ll s) {
  ll sc = 0;
  while (n >= 1) {
    ll p = n / b;
    sc += n - p * b;
    n = p;
  }
  return (s == sc);
}

Fixed submission:
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <iostream>

using namespace std;

typedef long long ll;

const ll MAX_N = 1E+11;

ll N, S;

ll solve(ll n, ll s);
bool f(ll b, ll n, ll s);

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

  cin >> N >> S;

  cout << solve(N, S) << "\n";

  return 0;
}

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

bool f(ll b, ll n, ll s) {
  ll sc = 0;
  while (n >= 1) {
    ll p = n / b;
    sc += n - p * b;
    n = p;
  }
  return (s == sc);
}
Problem p04014 (C++)

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

using namespace std;

typedef long long ll;

const ll inf = 1LL << 55;

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

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


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

using namespace std;

typedef long long ll;

const ll inf = 1LL << 55;

ll f(ll b, ll n) {
  if (b < 2)
    return inf;
  ll ret = 0;
  while (n >= b)
    ret += n % b, n /= b;
  return ret + n;
}

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

constexpr int MOD = 1000000007;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> PLL;

// does `b` exist such that:
// f(b, n) = s
// 1 <= n, s <= 10^11

int main() {
  LL N, S;
  cin >> N >> S;

  if (N == S) {
    cout << N + 1 << endl;
    return 0;
  }

  for (LL j = 2; j <= sqrt(N); ++j) {
    LL digsum = 0;
    for (LL t = N; t > 0; t /= j) {
      digsum += t % j;
    }
    if (digsum == S) {
      cout << j << endl;
      return 0;
    }
  }

  LL ans = -1;

  for (LL j = 1; j <= sqrt(N); ++j) {
    LL q = S - j;
    if ((N - q) % j == 0) {
      LL b = (N - q) / j;
      LL digsum = 0;
      for (LL t = N; t > 0; t /= b) {
        digsum += t % b;
      }
      if (digsum == S) {
        ans = b;
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

constexpr int MOD = 1000000007;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> PLL;

// does `b` exist such that:
// f(b, n) = s
// 1 <= n, s <= 10^11

int main() {
  LL N, S;
  cin >> N >> S;

  if (N == S) {
    cout << N + 1 << endl;
    return 0;
  }

  for (LL j = 2; j <= sqrt(N); ++j) {
    LL digsum = 0;
    for (LL t = N; t > 0; t /= j) {
      digsum += t % j;
    }
    if (digsum == S) {
      cout << j << endl;
      return 0;
    }
  }

  LL ans = -1;

  for (LL j = 1; j <= sqrt(N); ++j) {
    LL q = S - j;
    if ((N - q) % j == 0) {
      LL b = (N - q) / j;
      if (b == 0) {
        continue;
      }
      LL digsum = 0;
      for (LL t = N; t > 0; t /= b) {
        digsum += t % b;
      }
      if (digsum == S) {
        ans = b;
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p04014 (C++)

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

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

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

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

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

int main() {
  ll n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  for (ll b = 2; b <= (ll)sqrt((double)n); ++b) {
    if (f(b, n) == s) {
      cout << b << endl;
      return 0;
    }
  }
  for (ll p = (ll)sqrt((double)n); p > 0; --p) {
    ll b = (n - s) / p + 1;
    if (b < 2)
      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 <iostream>
using namespace std;

long long int n, s;

long long int ans;

long long int FUNC(long long int n, long long int b) {
  long long int c = 0;
  while (n > 0) {
    c += n % b;
    n = n / b;
  }
  return c;
}

int main(void) {

  cin >> n >> s;

  ans = 1000000000005;

  int R = (int)sqrt(n) + 1;
  for (int b = 2; b <= R; b++) {
    if (FUNC(n, b) == s) {
      ans = min(ans, (long long int)b);
    }
  }

  for (int i = R; i >= 1; i--) {
    long long int b = (n - s) / i + 1;
    if (FUNC(n, b) == s) {
      ans = min(ans, b);
    }
  }

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

  if (ans > 1000000000000) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }

  // system("pause");
  return 0;
}

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

long long int n, s;

long long int ans;

long long int FUNC(long long int n, long long int b) {
  long long int c = 0;
  while (n > 0) {
    c += n % b;
    n = n / b;
  }
  return c;
}

int main(void) {

  cin >> n >> s;

  ans = 1000000000005;

  int R = (int)sqrt(n) + 1;
  for (int b = 2; b <= R; b++) {
    if (FUNC(n, b) == s) {
      ans = min(ans, (long long int)b);
    }
  }

  for (int i = R; i >= 1; i--) {
    long long int b = (n - s) / i + 1;
    if (b < 2) {
      continue;
    }
    if (FUNC(n, b) == s) {
      ans = min(ans, b);
    }
  }

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

  if (ans > 1000000000000) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }

  // system("pause");
  return 0;
}
Problem p04014 (C++)

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

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

int main() {
  ll n, s;
  cin >> n >> s;
  ll b = -1;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  for (ll i = 2; i <= (ll)sqrt(n); i++) {
    if (s == digit_sum(i, n)) {
      b = i;
      break;
    }
  }
  if (b != -1) {
    cout << b << endl;
    return 0;
  }
  for (ll i = 1; i < sqrt(n); i++) {
    if (s == digit_sum((n + i - s) / i, n))
      b = b == -1 ? (n + i - s) / i : min(b, (n + i - s) / i);
  }
  cout << b << endl;
  return 0;
}

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

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

int main() {
  ll n, s;
  cin >> n >> s;
  ll b = -1;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  for (ll i = 2; i <= (ll)sqrt(n); i++) {
    if (s == digit_sum(i, n)) {
      b = i;
      break;
    }
  }
  if (b != -1) {
    cout << b << endl;
    return 0;
  }
  for (ll i = 1; i < sqrt(n); i++) {
    if ((n + i - s) / i < 2)
      continue;
    if (s == digit_sum((n + i - s) / i, n))
      b = b == -1 ? (n + i - s) / i : min(b, (n + i - s) / i);
  }
  cout << b << endl;
  return 0;
}
Problem p04014 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
typedef long long ll;
ll MOD = 1e9 + 7;

using namespace std;

int main() {
  ll n, s;
  ll ans = -1;
  cin >> n >> s;
  ll r = (ll)sqrt(n) + 1LL;
  for (ll i = 2; i < r; i++) {
    ll cp = n;
    ll temp = 0;
    while (cp != 0) {
      temp += cp % i;
      cp /= i;
    }
    if (temp == s) {
      ans = i;
      break;
    }
    // cerr<<i<<" "<<temp<<endl;
  }
  if (ans == -1) {
    ll mm = 1e15 - 1;
    // 2ケタの探索
    for (ll p = 1; p < r; p++) {
      ll q = s - p;
      ll i = (n - q) / p;
      ll cp = n;
      ll temp = 0;
      while (cp != 0) {
        temp += cp % i;
        cp /= i;
      }
      if (temp == s) {
        mm = min(i, mm);
      }
    }
    if (mm < 1e15 - 1)
      ans = mm;
  }

  if (s == n && ans == -1)
    ans = n + 1;
  if (s == 1 && ans == -1)
    ans = n;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
typedef long long ll;
ll MOD = 1e9 + 7;

using namespace std;

int main() {
  ll n, s;
  ll ans = -1;
  cin >> n >> s;
  ll r = (ll)sqrt(n) + 1LL;
  for (ll i = 2; i < r; i++) {
    ll cp = n;
    ll temp = 0;
    while (cp != 0) {
      temp += cp % i;
      cp /= i;
    }
    if (temp == s) {
      ans = i;
      break;
    }
    // cerr<<i<<" "<<temp<<endl;
  }
  if (ans == -1) {
    ll mm = 1e15 - 1;
    // 2ケタの探索
    for (ll p = 1; p < r; p++) {
      ll q = s - p;
      ll i = (n - q) / p;
      if (i < r)
        continue;
      ll cp = n;
      ll temp = 0;
      while (cp != 0) {
        temp += cp % i;
        cp /= i;
      }
      if (temp == s) {
        mm = min(i, mm);
      }
    }
    if (mm < 1e15 - 1)
      ans = mm;
  }

  if (s == n && ans == -1)
    ans = n + 1;
  if (s == 1 && ans == -1)
    ans = n;
  cout << ans << endl;
  return 0;
}
Problem p04014 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const long long LINF = 8931145141919364364, LMOD = 998244353;
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

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

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


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const long long LINF = 8931145141919364364, LMOD = 998244353;
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

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

int main() {
  long long n, s;
  cin >> n >> s;
  if (n == s)
    cout << n + 1 << endl;
  else {
    for (long long b = 2; b * b <= n; b++) {
      if (f(b, n) == s) {
        cout << b << endl;
        return 0;
      }
    }
    long long ans = -1;
    for (long long x = 1; x * x < n; x++) {
      long long b = (n - s) / x + 1;
      if (b >= 2 && f(b, n) == s) {
        ans = b;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p04014 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <math.h>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll INF = 2e16;
ll n, s, b, p, q, minb = INF, minb2 = INF;
double routen;

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

int main(int argc, char const *argv[]) {
  cin >> n >> s;
  if (s == n) {
    cout << n + 1 << endl;
    return 0;
  } else {
    routen = sqrt(n);
    for (int i = 2; i <= routen; i++) {
      if (func(i, n) == s) {
        if (i < minb)
          minb = i;
      }
    }
    if (minb != INF) {
      cout << minb << endl;
      return 0;
    } else {
      if ((n - s) <= 0) {
        cout << -1 << endl;
        return 0;
      }
      for (int p = 1; p < routen; p++) {
        ll canb = (n - s) / p + 1;
        if (func(canb, n) == s) {
          if (minb2 > canb)
            minb2 = canb;
        }
      }
      if (minb2 == INF) {
        cout << -1 << endl;
        return 0;
      } else {
        cout << minb2 << endl;
        return 0;
      }
    }
  }
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <math.h>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll INF = 2e16;
ll n, s, b, p, q, minb = INF, minb2 = INF;
double routen;

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

int main(int argc, char const *argv[]) {
  cin >> n >> s;
  if (s == n) {
    cout << n + 1 << endl;
    return 0;
  } else {
    routen = sqrt(n);
    for (int i = 2; i <= routen; i++) {
      if (func(i, n) == s) {
        if (i < minb)
          minb = i;
      }
    }
    if (minb != INF) {
      cout << minb << endl;
      return 0;
    } else {
      if ((n - s) <= 0) {
        cout << -1 << endl;
        return 0;
      }
      for (int p = 1; p < routen; p++) {
        ll canb = (n - s) / p + 1;
        if (canb < 2)
          break;
        if (func(canb, n) == s) {
          if (minb2 > canb)
            minb2 = canb;
        }
      }
      if (minb2 == INF) {
        cout << -1 << endl;
        return 0;
      } else {
        cout << minb2 << endl;
        return 0;
      }
    }
  }
}
Problem p04014 (C++)

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long int
int digitSum(int b, int n) {
  if (n < b)
    return n;
  else
    return digitSum(b, n / b) + n % b;
}

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

  int n, s;
  cin >> n >> s;
  if (n < s) {
    cout << "-1" << endl;
    return 0;
  } else if (s == n) {
    cout << n + 1 << endl;
    return 0;
  }
  if (n < 1000000) {
    REP(i, 2, n) {
      if (digitSum(i, n) == s) {
        cout << i << endl;
        return 0;
      }
    }
    cout << "-1" << endl;
    return 0;
  }
  int N = sqrt(n) + 1;
  REP(i, 2, N) {
    if (digitSum(i, n) == s) {
      cout << i << endl;
      return 0;
    }
  }
  for (int i = N; i >= 0; i--) {
    int b = min((n - s) / (i + 1) - 2, (int)2);
    // dump(b);
    rep(j, 5) {
      if (digitSum(b + j, n) == s) {
        cout << b + j << endl;
        return 0;
      }
    }
  }
  cout << "-1" << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long int
int digitSum(int b, int n) {
  if (n < b)
    return n;
  else
    return digitSum(b, n / b) + n % b;
}

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

  int n, s;
  cin >> n >> s;
  if (n < s) {
    cout << "-1" << endl;
    return 0;
  } else if (s == n) {
    cout << n + 1 << endl;
    return 0;
  }
  if (n < 1000000) {
    REP(i, 2, n) {
      if (digitSum(i, n) == s) {
        cout << i << endl;
        return 0;
      }
    }
    cout << "-1" << endl;
    return 0;
  }
  int N = sqrt(n) + 1;
  REP(i, 2, N) {
    if (digitSum(i, n) == s) {
      cout << i << endl;
      return 0;
    }
  }
  for (int i = N; i >= 0; i--) {
    int b = max((n - s) / (i + 1) - 2, (int)2);
    dump(b);
    rep(j, 5) {
      if (digitSum(b + j, n) == s) {
        cout << b + j << endl;
        return 0;
      }
    }
  }
  cout << "-1" << endl;

  return 0;
}
Problem p04014 (C++)

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

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

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

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

ll sum(ll a, ll b) {
  if (b == 0 || b == 1)
    return -1;
  ll sum = 0;
  while (a > 0) {
    sum += a % b;
    a /= b;
  }
  return sum;
}

int main() {
  ll n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  for (ll i = 2; i <= sqrt(n); i++) {
    if (sum(n, i) == s) {
      cout << i << endl;
      return 0;
    }
  }
  for (ll i = sqrt(n); i > 0; i--) {
    if (sum(n, (n - s) / i + 1) == s) {
      cout << (n - s) / i + 1 << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}
Problem p04014 (Python)

Buggy submission (status: Runtime Error):
from math import floor, sqrt, ceil


def f(b, n):
    if n < b:
        return n

    return f(b, floor(n / b)) + n % b


n = int(input())
s = int(input())

if n == s:
    print(n + 1)
    exit()

for b in range(2, floor(sqrt(n)) + 1):
    if f(b, n) == s:
        print(b)
        exit()

for p in range(1, ceil(sqrt(n)))[::-1]:
    b = (n - s) / p + 1
    if b.is_integer():
        b = int(b)
        if f(b, n) == s:
            print(b)
            exit()

print(-1)


Fixed submission:
from math import floor, sqrt, ceil


def f(b, n):
    if b < 2:
        return 0

    if n < b:
        return n

    return f(b, floor(n / b)) + n % b


n = int(input())
s = int(input())

if n == s:
    print(n + 1)
    exit()

for b in range(2, floor(sqrt(n)) + 1):
    if f(b, n) == s:
        print(b)
        exit()

for p in range(1, ceil(sqrt(n)))[::-1]:
    b = (n - s) / p + 1
    if b.is_integer():
        b = int(b)
        if f(b, n) == s:
            print(b)
            exit()

print(-1)
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 <sstream>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll f(ll b, ll n) {
  if (n < b)
    return n;
  return f(b, n / b) + n % b;
}
ll fast(ll b, ll n) {
  if (n < b)
    return n;
  return n / b + n % b;
}
int main() {
  ll n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  for (ll i = 2; i * i <= n; i++) {
    if (s == f(i, n)) {
      cout << i << endl;
      return 0;
    }
  }
  set<int> ans;
  for (ll i = 2; i * i <= n; i++) {
    ll p = n % i;
    ll b = (n - s) / p + 1;
    if (b > 1 && f(b, n) == s) {
      ans.insert(b);
    }
  }
  if (ans.size())
    cout << *ans.begin() << endl;
  else
    cout << "-1" << endl;
  return 0;
}


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

using namespace std;
using ll = long long;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll f(ll b, ll n) {
  if (n < b)
    return n;
  return f(b, n / b) + n % b;
}
ll fast(ll b, ll n) {
  if (n < b)
    return n;
  return n / b + n % b;
}
int main() {
  ll n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  for (ll i = 2; i * i <= n; i++) {
    if (s == f(i, n)) {
      cout << i << endl;
      return 0;
    }
  }
  set<ll> ans;
  for (ll p = 1; p * p <= n; p++) {
    ll b = (n - s) / p + 1;
    if (b > 1 && f(b, n) == s) {
      ans.insert(b);
    }
  }
  if (ans.size())
    cout << *ans.begin() << endl;
  else
    cout << "-1" << endl;
  return 0;
}
Problem p04014 (C++)

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

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

bool isOK(ll n, ll s, ll b) { return f(b, n) == s; }

int main() {
  ll n, s;
  cin >> n >> s;
  if (n < s) {
    cout << -1 << endl;
    return 0;
  }
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  for (ll i = 2; i * i <= n; i++) {
    if (isOK(n, s, i)) {
      cout << i << endl;
      return 0;
    }
  }
  ll ans = 1e18;
  for (ll i = 1; i * i < n; i++) {
    ll b = (n - s) / i + 1;
    if (isOK(n, s, b)) {
      ans = min(ans, b);
    }
  }
  if (ans == 1e18)
    cout << -1 << endl;
  else
    cout << ans << endl;
}


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

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

bool isOK(ll n, ll s, ll b) { return f(b, n) == s; }

int main() {
  ll n, s;
  cin >> n >> s;
  if (n < s) {
    cout << -1 << endl;
    return 0;
  }
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  for (ll i = 2; i * i <= n; i++) {
    if (isOK(n, s, i)) {
      cout << i << endl;
      return 0;
    }
  }
  ll ans = 1e18;
  for (ll i = 1; i * i < n; i++) {
    ll b = (n - s) / i + 1;
    if (b == 1)
      continue;
    if (isOK(n, s, b)) {
      ans = min(ans, b);
    }
  }
  if (ans == 1e18)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
Problem p04014 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define lint long long

using namespace std;

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

int main() {
  lint n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
  } else {
    lint minb = 99999999999999;
    for (lint i = 2; i <= sqrt(n); i++) {
      if (f(i, n) == s) {
        minb = min(minb, i);
      }
    }
    for (int i = 1; i < sqrt(n); i++) {
      lint b = (n - s) / i + 1;
      if (f(b, n) == s) {
        minb = min(minb, b);
      }
    }
    if (minb != 99999999999999)
      cout << minb << endl;
    else
      cout << -1 << endl;
  }
}


Fixed submission:
#include "bits/stdc++.h"
#define lint long long

using namespace std;

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

int main() {
  lint n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
  } else {
    lint minb = 99999999999999;
    for (lint i = 2; i <= sqrt(n); i++) {
      if (f(i, n) == s) {
        minb = min(minb, i);
      }
    }
    for (int i = 1; i < sqrt(n); i++) {
      lint b = (n - s) / i + 1;
      if (b >= 2 && f(b, n) == s) {
        minb = min(minb, b);
      }
    }
    if (minb != 99999999999999)
      cout << minb << endl;
    else
      cout << -1 << endl;
  }
}
Problem p04014 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define pye cout << "yes" << endl
#define pno cout << "no" << endl
#define pYe cout << "Yes" << endl
#define pNo cout << "No" << endl
#define pYE cout << "YES" << endl
#define pNO cout << "NO" << endl
#define prin(num) cout << num << endl
#define REP(i, n) for (i = 0; i < n; i++)
#define REPR(i, n) for (i = n; i >= 0; i--)
#define FOR(i, m, n) for (i = m; i < n; i++)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define INF 1000000000000000000
#define MOD 1000000007LL
#define MAX_V 100005
#define MAX_E 100005
#define NIL -1
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef priority_queue<P> PQ;

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

int main(void) {
  ll n, s;
  cin >> n;
  cin >> s;
  ll ans1 = INF, ans2 = INF;
  ll ans = INF;

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

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

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

  ll p;
  for (p = 1; p * p <= n; p++) {
    b = (n - s) / p + 1;
    if ((n - s) % p == 0 && f(b, n) == s) {
      ans2 = b;
    }
  }
  ans = min(ans1, ans2);
  if (ans == INF)
    ans = -1;
  prin(ans);
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define pye cout << "yes" << endl
#define pno cout << "no" << endl
#define pYe cout << "Yes" << endl
#define pNo cout << "No" << endl
#define pYE cout << "YES" << endl
#define pNO cout << "NO" << endl
#define prin(num) cout << num << endl
#define REP(i, n) for (i = 0; i < n; i++)
#define REPR(i, n) for (i = n; i >= 0; i--)
#define FOR(i, m, n) for (i = m; i < n; i++)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define INF 1000000000000000000
#define MOD 1000000007LL
#define MAX_V 100005
#define MAX_E 100005
#define NIL -1
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef priority_queue<P> PQ;

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

int main(void) {
  ll n, s;
  cin >> n;
  cin >> s;
  ll ans1 = INF, ans2 = INF;
  ll ans = INF;

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

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

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

  ll p;
  for (p = 1; p * p <= n; p++) {
    b = (n - s) / p + 1;
    if ((n - s) % p == 0 && f(b, n) == s) {
      ans2 = b;
    }
  }
  ans = min(ans1, ans2);
  if (ans == INF)
    ans = -1;
  prin(ans);
}
Problem p04015 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
// #define INF (1LL<<59)
#define int ll

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

  vector<int> x(n);
  rep(i, n) cin >> x[i];

  static int dp[51][51][2600];
  rep(i, 51) rep(j, 51) rep(k, 2600) dp[i][j][k] = 0;

  dp[0][0][0] = 1;

  for (int i = 1; i <= n; i++) {
    rep(j, 51) {
      rep(k, 3000) {
        dp[i][j][k] = dp[i - 1][j][k];
        if (k - x[i - 1] >= 0 && j - 1 >= 0)
          dp[i][j][k] += dp[i - 1][j - 1][k - x[i - 1]];
      }
    }
  }

  ll ans = 0;
  for (int j = 1; j <= n; j++) {
    ans += dp[n][j][j * a];
  }

  cout << ans << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
// #define INF (1LL<<59)
#define int ll

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

  vector<int> x(n);
  rep(i, n) cin >> x[i];

  static int dp[51][51][2600];
  rep(i, 51) rep(j, 51) rep(k, 2600) dp[i][j][k] = 0;

  dp[0][0][0] = 1;

  for (int i = 1; i <= n; i++) {
    rep(j, 51) {
      rep(k, 2600) {
        dp[i][j][k] = dp[i - 1][j][k];
        if (k - x[i - 1] >= 0 && j - 1 >= 0)
          dp[i][j][k] += dp[i - 1][j - 1][k - x[i - 1]];
      }
    }
  }

  ll ans = 0;
  for (int j = 1; j <= n; j++) {
    ans += dp[n][j][j * a];
  }

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

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

using namespace std;

long long dp[51][2501];

int main() {
  int N, A;
  cin >> N >> A;
  vector<int> x(N);
  for (int i = 0; i < N; i++) {
    cin >> x[i];
  }
  dp[0][0] = 1;

  for (int t = 0; t < N; t++) {
    for (int i = t + 1; i >= 0; i--) {
      for (int j = 0; j <= 50 * i; j++) {
        dp[i + 1][j + x[t]] += dp[i][j];
      }
    }
  }

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


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

long long dp[65][3501];

int main() {
  int N, A;
  cin >> N >> A;
  vector<int> x(N);
  for (int i = 0; i < N; i++) {
    cin >> x[i];
  }
  dp[0][0] = 1;

  for (int t = 0; t < N; t++) {
    for (int i = t + 1; i >= 0; i--) {
      for (int j = 0; j <= 50 * i; j++) {
        dp[i + 1][j + x[t]] += dp[i][j];
      }
    }
  }

  long long ans = 0;
  for (int i = 1; i <= N; i++) {
    ans += dp[i][i * A];
  }
  cout << ans << endl;
}
Problem p04015 (C++)

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

typedef long long ll;

int main() {
  int n, a;
  cin >> n >> a;
  int x[n];

  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    sum += x[i];
  }

  ll dp[n + 1][sum + 1][n + 1];
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= n; i++) {
    dp[i][0][0] = 1;
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= sum; j++) {
      for (int k = 1; k <= n; k++) {
        if (j < x[i])
          dp[i + 1][j][k] = dp[i][j][k];
        else {
          dp[i + 1][j][k] = dp[i][j][k] + dp[i][j - x[i]][k - 1];
        }
      }
    }
  }

  ll ans = 0;
  for (int i = 1; a * i <= sum; i++) {
    ans += dp[n][a * i][i];
  }

  cout << ans << endl;

  return 0;
}


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

typedef long long ll;

int main() {
  int n, a;
  cin >> n >> a;
  int x[n];

  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    sum += x[i];
  }

  ll dp[n + 1][sum + 1][n + 1];
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= n; i++) {
    dp[i][0][0] = 1;
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= sum; j++) {
      for (int k = 1; k <= n; k++) {
        if (j < x[i])
          dp[i + 1][j][k] = dp[i][j][k];
        else {
          dp[i + 1][j][k] = dp[i][j][k] + dp[i][j - x[i]][k - 1];
        }
      }
    }
  }

  ll ans = 0;
  for (int i = 1; a * i <= sum && i <= n; i++) {
    ans += dp[n][a * i][i];
  }

  cout << ans << endl;

  return 0;
}
Problem p04015 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2500;
ll dp[50][5005];
int n, m, x, y;
int main() {
  cin >> n >> m;
  dp[0][2500] = 1;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    y = x - m;
    for (int j = 0; j <= 5000; j++) {
      dp[i][j] += dp[i - 1][j];
      if (j + y < 0 || j + y > 5000)
        continue;
      dp[i][j + y] += dp[i - 1][j];
    }
  }
  cout << dp[n][2500] - 1 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2500;
ll dp[55][5005];
int n, m, x, y;
int main() {
  cin >> n >> m;
  dp[0][2500] = 1;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    y = x - m;
    for (int j = 0; j <= 5000; j++) {
      dp[i][j] += dp[i - 1][j];
      if (j + y < 0 || j + y > 5000)
        continue;
      dp[i][j + y] += dp[i - 1][j];
    }
  }
  cout << dp[n][2500] - 1 << endl;
  return 0;
}
Problem p04015 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define debug(x) cout << #x << " = " << x << endl
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n); i > -1; i--)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const double EPS = 1e-9;

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

  ll n, a;
  cin >> n >> a;
  ll x[n];
  rep(i, n) cin >> x[i];
  sort(x, x + n);
  ll mx = x[n - 1];
  ll dp[n + 1][n + 1][mx * n + 1];
  rep(i, n + 1) {
    rep(j, n + 1) {
      rep(k, mx * n + 1) {
        if (i == 0 && j == 0 && k == 0) {
          dp[i][j][k] = 1;
        } else if (i >= 1 && k < x[i - 1]) {
          dp[i][j][k] = dp[i - 1][j][k];
        } else if (i >= 1 && j >= 1 && k >= x[i - 1]) {
          dp[i][j][k] = dp[i - 1][j][k] + dp[i - 1][j - 1][k - x[i - 1]];
        } else {
          dp[i][j][k] = 0;
        }
      }
    }
  }
  ll ans = 0;
  for (ll l = 1; l <= n; l++) {
    ans += dp[n][l][a * l];
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define debug(x) cout << #x << " = " << x << endl
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n); i > -1; i--)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const double EPS = 1e-9;

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

  ll n, a;
  cin >> n >> a;
  ll x[n];
  rep(i, n) cin >> x[i];
  sort(x, x + n);
  ll mx = max(a, x[n - 1]);
  ll dp[n + 1][n + 1][mx * n + 1];
  rep(i, n + 1) {
    rep(j, n + 1) {
      rep(k, mx * n + 1) {
        if (i == 0 && j == 0 && k == 0) {
          dp[i][j][k] = 1;
        } else if (i >= 1 && k < x[i - 1]) {
          dp[i][j][k] = dp[i - 1][j][k];
        } else if (i >= 1 && j >= 1 && k >= x[i - 1]) {
          dp[i][j][k] = dp[i - 1][j][k] + dp[i - 1][j - 1][k - x[i - 1]];
        } else {
          dp[i][j][k] = 0;
        }
      }
    }
  }
  ll ans = 0;
  for (ll l = 1; l <= n; l++) {
    ans += dp[n][l][a * l];
  }
  cout << ans << endl;

  return 0;
}
Problem p04015 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
using namespace std;
LL dp[51][51][2501];
int main() {
  // freopen("In.txt" , "r" , stdin);
  int N, A;
  cin >> N >> A;
  vector<int> a(N);
  for (auto &x : a)
    cin >> x;
  dp[0][0][0] = 1;
  for (int i = 0; i < N; i++) {
    for (int picked = 0; picked <= 50; picked++) {
      for (int sum = 0; sum <= i * 50; sum++) {
        dp[i + 1][picked + 1][sum + a[i]] += dp[i][picked][sum];
        dp[i + 1][picked][sum] += dp[i][picked][sum];
      }
    }
  }
  LL ans = 0;
  for (int picked = 1; picked <= N; picked++)
    ans += dp[N][picked][picked * A];
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
LL dp[51][51][2501];
int main() {
  // freopen("In.txt" , "r" , stdin);
  int N, A;
  cin >> N >> A;
  vector<int> a(N);
  for (auto &x : a)
    cin >> x;
  dp[0][0][0] = 1;
  for (int i = 0; i < N; i++) {
    for (int picked = 0; picked <= i; picked++) {
      for (int sum = 0; sum <= i * 50; sum++) {
        dp[i + 1][picked + 1][sum + a[i]] += dp[i][picked][sum];
        dp[i + 1][picked][sum] += dp[i][picked][sum];
      }
    }
  }
  LL ans = 0;
  for (int picked = 1; picked <= N; picked++)
    ans += dp[N][picked][picked * A];
  cout << ans;
  return 0;
}
Problem p04015 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
using ll = long long;

ll dp[51][51][2501] = {};

int main() {
  ll n, a, ans = 0;
  cin >> n >> a;
  vector<int> x(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> x[i];
  dp[0][0][0] = 1;
  for (int j = 1; j <= n; j++) {
    for (int k = 0; k <= n; k++) {
      for (int s = 0; s <= 2500; s++) {
        if (s < x[j] && k > 0)
          dp[j][k][s] = dp[j - 1][k][s];
        else
          dp[j][k][s] = dp[j - 1][k][s] + dp[j - 1][k - 1][s - x[j]];
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    ans += dp[n][i][i * a];
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
using ll = long long;

ll dp[51][51][2501] = {};

int main() {
  ll n, a, ans = 0;
  cin >> n >> a;
  vector<int> x(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> x[i];
  dp[0][0][0] = 1;
  for (int j = 1; j <= n; j++) {
    for (int k = 0; k <= n; k++) {
      for (int s = 0; s <= 2500; s++) {
        dp[j][k][s] = dp[j - 1][k][s];
        if (k > 0 && s >= x[j])
          dp[j][k][s] = dp[j - 1][k][s] + dp[j - 1][k - 1][s - x[j]];
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    ans += dp[n][i][i * a];
  }
  cout << ans << endl;
  return 0;
}