prithivMLmods/Coder-Stat huggingface dataset:
Problem p04013 (Python)

Buggy submission (status: Time Limit Exceeded):
N, A = map(int, input().split())
lst = [int(x) for x in input().split()]
ret = 0
for i in range(2**N):
    n = 0
    cnt = 0
    for j in range(N):
        if i >> j & 1:
            n += lst[j]
            cnt += 1
    if cnt > 0 and n / cnt == A:
        ret += 1
print(ret)


Fixed submission:
N, A = map(int, input().split())
lst = [int(x) - A for x in input().split()]
dp = [[0] * (100 * N + 1) for _ in range(N + 1)]
dp[0][50 * N] = 1
for i in range(N):
    for j in range(50, 100 * N + 1 - 50):
        dp[i + 1][j] = dp[i][j] + dp[i][j - lst[i]]
print(dp[N][50 * N] - 1)
Problem p04013 (C++)

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

int N, A;
vector<int> x, y;
ll dp[55][5055];

int main() {
  cin >> N >> A;
  x.resize(N);
  y.resize(N);
  for (int i = 0; i < N; i++)
    scanf("%d", &x[i]);
  for (int i = 0; i < N; i++)
    y[i] = x[i] - A;
  dp[0][2500] = 1;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= 5000; j++) {
      dp[i + 1][j] += dp[i][j];
      dp[i + 1][j + y[i]] += dp[i][j];
    }
  }
  cout << dp[N][2500] - 1 << endl;
  return 0;
}

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

int N, A;
vector<int> x, y;
ll dp[55][5055];

int main() {
  cin >> N >> A;
  x.resize(N);
  y.resize(N);
  for (int i = 0; i < N; i++)
    scanf("%d", &x[i]);
  for (int i = 0; i < N; i++)
    y[i] = x[i] - A;
  dp[0][2500] = 1;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= 5000; j++) {
      dp[i + 1][j] += dp[i][j];
      dp[i + 1][j + y[i]] += dp[i][j];
    }
  }
  cout << dp[N][2500] - 1 << endl;
  return 0;
}
Problem p04013 (C++)

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

long dp[5005];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, a;
  cin >> n >> a;
  int x[50];
  for (int i = 0; i < n; ++i)
    cin >> x[i], x[i] -= a;

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

  cout << dp[2500] - 1 << '\n';
  return 0;
}

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

long dp[5500];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, a;
  cin >> n >> a;
  int x[50];
  for (int i = 0; i < n; ++i)
    cin >> x[i], x[i] -= a;

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

  cout << dp[2500] - 1 << '\n';
  return 0;
}
Problem p04013 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int INF = 1e9;

ll dp[51][2501][51];

int main() {
  ll n, a, tmp, ans = 0, cnt;
  cin >> n >> a;
  vector<int> x(n);
  rep(i, n) cin >> x[i];

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

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= n * a; ++j) {
      for (int k = 0; k < n; ++k) {
        dp[i + 1][j][k] += dp[i][j][k];
        dp[i + 1][j + x[i]][k + 1] += dp[i][j][k];
      }
    }
  }

  for (int i = 1; i <= n; ++i)
    ans += dp[n][i * a][i];

  cout << ans << endl;
  return 0;
}

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

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int INF = 1e9;

ll dp[55][2505][55];

int main() {
  ll n, a, tmp, ans = 0, cnt;
  cin >> n >> a;
  vector<int> x(n);
  rep(i, n) cin >> x[i];

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

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= n * a; ++j) {
      for (int k = 0; k < n; ++k) {
        dp[i + 1][j][k] += dp[i][j][k];
        dp[i + 1][j + x[i]][k + 1] += dp[i][j][k];
      }
    }
  }

  for (int i = 1; i <= n; ++i)
    ans += dp[n][i * a][i];

  cout << ans << endl;
  return 0;
}
Problem p04013 (C++)

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

using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

typedef long long ll;

const int NMAX = 51;
// const int VMAX = 51*51+1;
const int VMAX = 100;
ll dp[NMAX][NMAX][VMAX];

int main() {
  dp[0][0][0] = 1;
  int N, A;
  cin >> N >> A;
  vector<int> data(N);
  REP(i, N) cin >> data[i];
  REP(i, N) {
    int x = data[i];
    int from = i, to = i + 1;

    for (int a = 0; a < NMAX; a++)
      for (int b = 0; b < VMAX; b++)
        dp[to][a][b] = 0LL;

    for (int m = 0; m < N; m++) {
      for (int v = 0; v < VMAX; v++) {
        dp[to][m][v] += dp[from][m][v];
        if (m + 1 < NMAX && v + x < VMAX) {
          dp[to][m + 1][v + x] += dp[from][m][v];
#ifdef DEBUG
          cout << i << to << "," << m + 1 << "," << v + x << ","
               << dp[to][m + 1][v + x] << endl;
#endif
        }
      }
    }
  }
  ll ans = 0;
  for (int i = 1; i <= N; i++) {
#ifdef DEBUG
    cout << i << " " << dp[N][i][A * i] << endl;
#endif
    ans += dp[N][i][A * i];
  }
  cout << ans << endl;
  return 0;
}


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

using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

typedef long long ll;

const int NMAX = 51;
const int VMAX = 51 * 51 + 1;
// const int VMAX = 100;
ll dp[NMAX][NMAX][VMAX];

int main() {
  dp[0][0][0] = 1;
  int N, A;
  cin >> N >> A;
  vector<int> data(N);
  REP(i, N) cin >> data[i];
  REP(i, N) {
    int x = data[i];
    int from = i, to = i + 1;

    for (int a = 0; a < NMAX; a++)
      for (int b = 0; b < VMAX; b++)
        dp[to][a][b] = 0LL;

    for (int m = 0; m < N; m++) {
      for (int v = 0; v < VMAX; v++) {
        dp[to][m][v] += dp[from][m][v];
        if (m + 1 < NMAX && v + x < VMAX) {
          dp[to][m + 1][v + x] += dp[from][m][v];
#ifdef DEBUG
          cout << i << to << "," << m + 1 << "," << v + x << ","
               << dp[to][m + 1][v + x] << endl;
#endif
        }
      }
    }
  }
  ll ans = 0;
  for (int i = 1; i <= N; i++) {
#ifdef DEBUG
    cout << i << " " << dp[N][i][A * i] << endl;
#endif
    ans += dp[N][i][A * i];
  }
  cout << ans << endl;
  return 0;
}
Problem p04013 (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;

const int MAX_N = 50;

int N, A;
int X[MAX_N + 1];
long long dp[MAX_N + 1][MAX_N + 1][MAX_N * MAX_N + 1];

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

  cin >> N >> A;

  int sum = 0;

  for (int i = 1; i <= N; i++) {
    cin >> X[i];
    sum += X[i];
  }

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

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

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

  cout << ans << "\n";

  return 0;
}

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

#include <algorithm>
#include <iostream>

using namespace std;

const int MAX_N = 50;

int N, A;
int X[MAX_N + 1];
long long dp[MAX_N + 1][MAX_N + 1][MAX_N * MAX_N + 1];

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

  cin >> N >> A;

  int sum = 0;

  for (int i = 1; i <= N; i++) {
    cin >> X[i];
    sum += X[i];
  }

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

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

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

  cout << ans << "\n";

  return 0;
}
Problem p04013 (C++)

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

long n, a, x[100];

long dp[100][100];

int main(int argc, char const *argv[]) {
  cin >> n >> a;

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

  dp[0][0] = 1;

  for (int i = 0; i < n; i++) {
    for (int j = a * n; j >= x[i]; j--) {
      for (int k = n; k > 0; k--) {
        dp[j][k] += dp[j - x[i]][k - 1];
      }
    }
  }
  long ans = 0;
  for (int j = 0; j <= a * n; j++) {
    for (int k = 1; k <= n; k++) {
      if (j % k == 0 && j / k == a)
        ans += dp[j][k];
    }
  }
  cout << ans << endl;
  return 0;
}


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

long n, a, x[100];

long dp[3000][100];

int main(int argc, char const *argv[]) {
  cin >> n >> a;

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

  dp[0][0] = 1;

  for (int i = 0; i < n; i++) {
    for (int j = a * n; j >= x[i]; j--) {
      for (int k = n; k > 0; k--) {
        dp[j][k] += dp[j - x[i]][k - 1];
      }
    }
  }
  long ans = 0;
  for (int j = 0; j <= a * n; j++) {
    for (int k = 1; k <= n; k++) {
      if (j % k == 0 && j / k == a)
        ans += dp[j][k];
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p04013 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.14159265358979323846;
long long dp[51][51][2501] = {0};

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

  int n, a;
  cin >> n >> a;
  vector<int> arr(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> arr[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 <= 2500 - arr[i + 1]; ++k) {
        dp[i + 1][j][k] += dp[i][j][k];
        dp[i + 1][j + 1][k + arr[i + 1]] += dp[i][j][k];
      }
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    ans += dp[n][i][a * i];
  }
  cout << ans << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.14159265358979323846;
long long dp[55][55][2505] = {0};

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

  int n, a;
  cin >> n >> a;
  vector<int> arr(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> arr[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 <= 2500 - arr[i + 1]; ++k) {
        dp[i + 1][j][k] += dp[i][j][k];
        dp[i + 1][j + 1][k + arr[i + 1]] += dp[i][j][k];
      }
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    ans += dp[n][i][a * i];
  }
  cout << ans << '\n';

  return 0;
}
Problem p04013 (C++)

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

using namespace std;
using ll = long long;

int n;
ll ans, a, x[51], dp[51][50][2500];

ll _max(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}

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

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

using namespace std;
using ll = long long;

int n;
ll ans, a, x[55], dp[55][55][2600];

ll _max(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}

int main() {
  cin >> n >> a;
  for (int i = 1; i <= n; ++i)
    cin >> x[i];
  int X = a;
  for (int i = 0; i <= n; ++i)
    X = _max(X, x[i]);
  for (int j = 0; j <= n; ++j) {
    for (int k = 0; k <= n; ++k) {
      for (int s = 0; s <= n * X; ++s) {
        if (j == 0 && k == 0 && s == 0)
          dp[j][k][s] = 1;
        else if (j >= 1 && s < x[j])
          dp[j][k][s] = dp[j - 1][k][s];
        else if (j >= 1 && k >= 1 && s >= x[j])
          dp[j][k][s] = dp[j - 1][k][s] + dp[j - 1][k - 1][s - x[j]];
        else
          dp[j][k][s] = 0;
      }
    }
  }
  for (int i = 1; i <= n; ++i)
    ans += dp[n][i][a * i];
  cout << ans << endl;
}
Problem p04013 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long dp[51][51][10010];
int main() {
  long long n, ave, ans = 0;
  cin >> n >> ave;
  long long a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  // dp[見た枚数][使った枚数][総和]
  dp[0][0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n * ave; k++) {
        dp[i + 1][j + 1][k + a[i]] += dp[i][j][k]; // 使う
        dp[i + 1][j][k] += dp[i][j][k];            // 使わない
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    ans += dp[n][i][i * ave]; // i枚使ったとき平均がaveになるなら総和がi*ave
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long dp[55][55][10010];
int main() {
  long long n, ave, ans = 0;
  cin >> n >> ave;
  long long a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  // dp[見た枚数][使った枚数][総和]
  dp[0][0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n * ave; k++) {
        dp[i + 1][j + 1][k + a[i]] += dp[i][j][k]; // 使う
        dp[i + 1][j][k] += dp[i][j][k];            // 使わない
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    ans += dp[n][i][i * ave]; // i枚使ったとき平均がaveになるなら総和がi*ave
  }
  cout << ans << endl;
  return 0;
}
Problem p04013 (C++)

Buggy submission (status: Runtime Error):
// RAM RAM

#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define ppb pop_back
#define vi vector<int>
#define mp make_pair
#define arrayin(a, n)                                                          \
  for (int i = 0; i < n; i++) {                                                \
    cin >> a[i];                                                               \
  }
#define N 55

using namespace std;

int num[N];
int dp[N][N][N * N];
int solve(int i, int d, int sum, int n) {

  if (sum == 0 && d == 0) {
    return 1;
  }

  if (i >= n || d == 0) {
    return 0;
  }

  if (dp[i][d][sum] != -1) {
    return dp[i][d][sum];
  }
  int pos1 = solve(i + 1, d - 1, sum - num[i], n);
  int pos2 = solve(i + 1, d, sum, n);

  dp[i][d][sum] = pos2 + pos1;
  return pos1 + pos2;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < N * N; k++) {
        dp[i][j][k] = -1;
      }
    }
  }
  int n, a;
  cin >> n >> a;
  arrayin(num, n)
      // cout<<solve(0,1,5,n);
      int res = 0;

  for (int i = 1; i <= n; i++) {
    int temp = solve(0, i, i * a, n);
    // cout<<temp<<endl;
    res += (temp);
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
// RAM RAM

#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define ppb pop_back
#define vi vector<int>
#define mp make_pair
#define arrayin(a, n)                                                          \
  for (int i = 0; i < n; i++) {                                                \
    cin >> a[i];                                                               \
  }
#define N 55

using namespace std;

int num[N];
int dp[N][N][N * N];
int solve(int i, int d, int sum, int n) {

  if (sum == 0 && d == 0) {
    return 1;
  }

  if (i >= n || d == 0) {
    return 0;
  }

  if (dp[i][d][sum] != -1) {
    return dp[i][d][sum];
  }
  int pos1 = solve(i + 1, d - 1, sum - num[i], n);
  int pos2 = solve(i + 1, d, sum, n);

  dp[i][d][sum] = pos2 + pos1;
  return pos1 + pos2;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < N * N; k++) {
        dp[i][j][k] = -1;
      }
    }
  }
  int n, a;
  cin >> n >> a;
  arrayin(num, n)
      // cout<<solve(0,1,5,n);
      int res = 0;

  for (int i = 1; i <= n; i++) {
    int temp = solve(0, i, i * a, n);
    // cout<<temp<<endl;
    res += (temp);
  }
  cout << res << endl;
  return 0;
}
Problem p04013 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef deque<ll> dqll;

#define MOD 1000000007

int main() {
  ll N, A;
  cin >> N >> A;

  vll lst(N);
  for (int i = 0; i < N; i++) {
    cin >> lst[i];
  }
  const int Nmax = 10;
  vector<vector<vll>> dp(N + 1,
                         vector<vll>(N + 1, vll(Nmax * (Nmax + 1) + 1, 0)));

  dp[0][0][0] = 1;
  for (int i = 0; i < N; i++) {
    for (int j = 1; j <= N; j++) {
      for (int k = 0; k <= Nmax * Nmax; k++) {
        dp[i + 1][j][k + lst[j - 1]] += dp[i][j - 1][k];
        dp[i][j][k] += dp[i][j - 1][k];
      } // k
    }   // j
  }     // i

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

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef deque<ll> dqll;

#define MOD 1000000007

int main() {
  ll N, A;
  cin >> N >> A;

  vll lst(N);
  for (int i = 0; i < N; i++) {
    cin >> lst[i];
  }
  const int Nmax = 50;
  vector<vector<vll>> dp(N + 1,
                         vector<vll>(N + 1, vll(Nmax * (Nmax + 1) + 1, 0)));

  dp[0][0][0] = 1;
  for (int i = 0; i < N; i++) {
    for (int j = 1; j <= N; j++) {
      for (int k = 0; k <= Nmax * Nmax; k++) {
        dp[i + 1][j][k + lst[j - 1]] += dp[i][j - 1][k];
        dp[i][j][k] += dp[i][j - 1][k];
      } // k
    }   // j
  }     // i

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

  cout << ans << endl;

  return 0;
}
Problem p04013 (C++)

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

#define LL long long
#define Dl double
#define Pb push_back
#define Po pop_back
#define Mp make_pair
#define Ub upper_bound
#define Lb lower_bound
#define In insert
#define All(x) x.begin(), x.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)

using namespace std;

typedef vector<LL> Vl;
typedef vector<Vl> VVl;
typedef pair<LL, LL> Pll;
typedef vector<Pll> Vpl;
typedef queue<LL> Ql;
typedef stack<LL> Sl;
#define X first
#define Y second

LL n, A, x[60];
LL dp[60][10000][60];

LL stt(LL id, LL now, LL N) {
  if (id >= n) {
    if (N && now % N == 0 && now == A * N)
      return 1;
    return 0;
  }

  if (dp[id][now][N] != -1)
    return dp[id][now][N];

  LL r1 = 0, r2 = 0;
  r1 = stt(id + 1, now + x[id], N + 1);
  r2 = stt(id + 1, now, N);

  return dp[id][now][N] = r1 + r2;
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  cin >> n >> A;

  mem(dp, -1);

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

  cout << stt(0, 0, 0) << '\n';

  return 0;
}


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

#define LL long long
#define Dl double
#define Pb push_back
#define Po pop_back
#define Mp make_pair
#define Ub upper_bound
#define Lb lower_bound
#define In insert
#define All(x) x.begin(), x.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)

using namespace std;

typedef vector<LL> Vl;
typedef vector<Vl> VVl;
typedef pair<LL, LL> Pll;
typedef vector<Pll> Vpl;
typedef queue<LL> Ql;
typedef stack<LL> Sl;
#define X first
#define Y second

LL n, A, x[60];
LL dp[60][2600][60];

LL stt(LL id, LL now, LL N) {
  if (id >= n) {
    if (N && now % N == 0 && now == A * N)
      return 1;
    return 0;
  }

  if (dp[id][now][N] != -1)
    return dp[id][now][N];

  LL r1 = 0, r2 = 0;
  r1 = stt(id + 1, now + x[id], N + 1);
  r2 = stt(id + 1, now, N);

  return dp[id][now][N] = r1 + r2;
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  cin >> n >> A;

  mem(dp, -1);

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

  cout << stt(0, 0, 0) << '\n';

  return 0;
}
Problem p04013 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cstdint>
#include <vector>
#define FOR(i, l, r) for (int i = (l); i < (r); ++i)
#define RFOR(i, l, r) for (int i = (l); i >= (int)(r); i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n - 1, 0)

using namespace std;
const int MX = 1e6;
const int inf = 1e9;
// const int mod = 1e9+7;
using ll = long long;
const int mod = 1000000007;

ll ans = 0;
int n;
int a;
int x[MX];
ll dp[52][52][3000];
signed main() {
  cin >> n >> a;
  rep(i, n) {
    cin >> x[i + 1];
    // cout << x[i+1] << endl;
  }

  dp[0][0][0] = 1;
  FOR(j, 1, n + 1) {
    rep(k, j + 1) {
      rep(s, 3000) {
        if (s - x[j] >= 0)
          dp[j][k][s] = dp[j - 1][k - 1][s - x[j]] + dp[j - 1][k][s];
        else
          dp[j][k][s] = dp[j - 1][k][s];
      }
    }
    // cout << j <<" " <<dp[j][1][7] << endl;
  }
  FOR(i, 1, n + 1) ans += dp[n][i][i * a];
  // cout << dp[1][1][7] << endl;
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <cstdint>
#include <vector>
#define FOR(i, l, r) for (int i = (l); i < (r); ++i)
#define RFOR(i, l, r) for (int i = (l); i >= (int)(r); i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n - 1, 0)

using namespace std;
const int MX = 1e6;
const int inf = 1e9;
// const int mod = 1e9+7;
using ll = long long;
const int mod = 1000000007;

ll ans = 0;
int n;
int a;
int x[MX];
ll dp[52][52][3000];
signed main() {
  cin >> n >> a;
  rep(i, n) {
    cin >> x[i + 1];
    // cout << x[i+1] << endl;
  }

  dp[0][0][0] = 1;
  FOR(j, 1, n + 1) {
    rep(k, j + 1) {
      rep(s, 3000) {
        if (k - 1 >= 0 && s - x[j] >= 0)
          dp[j][k][s] = dp[j - 1][k - 1][s - x[j]] + dp[j - 1][k][s];
        else
          dp[j][k][s] = dp[j - 1][k][s];
      }
    }
    // cout << j <<" " <<dp[j][1][7] << endl;
  }
  FOR(i, 1, n + 1) ans += dp[n][i][i * a];
  // cout << dp[1][1][7] << endl;
  cout << ans << endl;

  return 0;
}
Problem p04013 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long n, x;
long long a[59];
long long dp[59][59][59];
long long bt(int id, long long sum, int k) {
  if (id == n) {
    if (k == 0)
      return 0;
    if (sum / k == x && sum % k == 0) {
      return 1;
    }
    return 0;
  }
  long long &ret = dp[id][sum][k];
  if (ret != -1)
    return ret;
  ret = bt(id + 1, sum + a[id], k + 1) + bt(id + 1, sum, k);
  return ret;
}
int main() {
  memset(dp, -1, sizeof(dp));
  cin >> n >> x;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  cout << bt(0, 0, 0) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n, x;
long long a[59];
long long dp[59][2509][59];
long long bt(int id, long long sum, int k) {
  if (id == n) {
    if (k == 0)
      return 0;
    if (sum / k == x && sum % k == 0) {
      return 1;
    }
    return 0;
  }
  long long &ret = dp[id][sum][k];
  if (ret != -1)
    return ret;
  ret = bt(id + 1, sum + a[id], k + 1) + bt(id + 1, sum, k);
  return ret;
}
int main() {
  memset(dp, -1, sizeof(dp));
  cin >> n >> x;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  cout << bt(0, 0, 0) << endl;
}
Problem p04013 (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 max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define pye cout << "Yes" << endl;
#define pno cout << "No" << endl;
#define prin(num) cout << num << endl;
#define sprin(str) cout << "str" << endl;
#define INF 1000000000
#define MOD 1000000007LL
#define MAX 100001
#define NIL -1
#define WHITE 0
#define GRAY 1
#define BLACK 2
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PA;
typedef priority_queue<ll> PQ;
typedef pair<double, double> doup;
// typedef vector<ll> vec;
// typedef vector<vec> mat;

int main() {
  ll N, A;
  ll x[50];
  cin >> N >> A;
  ll i, j, k;
  ll dp[N + 1][N + 1][N * A + 1];
  for (i = 0; i < N; i++)
    cin >> x[i];

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

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

  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 max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define pye cout << "Yes" << endl;
#define pno cout << "No" << endl;
#define prin(num) cout << num << endl;
#define sprin(str) cout << "str" << endl;
#define INF 1000000000
#define MOD 1000000007LL
#define MAX 100001
#define NIL -1
#define WHITE 0
#define GRAY 1
#define BLACK 2
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PA;
typedef priority_queue<ll> PQ;
typedef pair<double, double> doup;
// typedef vector<ll> vec;
// typedef vector<vec> mat;

int main() {
  ll N, A;
  ll x[50];
  cin >> N >> A;
  ll i, j, k;
  ll dp[N + 1][N + 1][N * A + 1];
  for (i = 0; i < N; i++)
    cin >> x[i];

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

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

  prin(ans);
}
Problem p04013 (C++)

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

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i < (ll)(n); i++)
#define INF 1000000000000
#define MOD 1000000007
using ll = long long;

int main() {
  int n, a;
  cin >> n >> a;
  vector<int> x(n);
  int M = 0;
  rep(i, n) {
    cin >> x.at(i);
    M = max(M, x.at(i));
  }
  vector<vector<vector<ll>>> dp(
      55, vector<vector<ll>>(55, vector<ll>(M * n + 10, 0)));

  for (int j = 0; j <= n; j++) {
    for (int k = 0; k <= j; k++) {
      for (int s = 0; s <= j * M; s++) {
        if (j == 0 && k == 0 && s == 0)
          dp.at(j).at(k).at(s) = 1;
        else if (j >= 1 && x.at(j - 1) > s)
          dp.at(j).at(k).at(s) = dp.at(j - 1).at(k).at(s);
        else if (j >= 1 && k >= 1 && s >= x.at(j - 1))
          dp.at(j).at(k).at(s) = dp.at(j - 1).at(k).at(s) +
                                 dp.at(j - 1).at(k - 1).at(s - x.at(j - 1));
        else
          dp.at(j).at(k).at(s) = 0;
        // cout<<dp.at(j).at(k).at(s)<<endl;
      }
    }
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++)
    ans += dp.at(n).at(i).at(i * a);
  cout << ans << endl;
  return 0;
}


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

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i < (ll)(n); i++)
#define INF 1000000000000
#define MOD 1000000007
using ll = long long;

int main() {
  int n, a;
  cin >> n >> a;
  vector<int> x(n);
  int M = 0;
  rep(i, n) {
    cin >> x.at(i);
    M = max(M, x.at(i));
  }
  vector<vector<vector<ll>>> dp(55,
                                vector<vector<ll>>(55, vector<ll>(2600, 0)));

  for (int j = 0; j <= n; j++) {
    for (int k = 0; k <= j; k++) {
      for (int s = 0; s <= j * M; s++) {
        if (j == 0 && k == 0 && s == 0)
          dp.at(j).at(k).at(s) = 1;
        else if (j >= 1 && x.at(j - 1) > s)
          dp.at(j).at(k).at(s) = dp.at(j - 1).at(k).at(s);
        else if (j >= 1 && k >= 1 && s >= x.at(j - 1))
          dp.at(j).at(k).at(s) = dp.at(j - 1).at(k).at(s) +
                                 dp.at(j - 1).at(k - 1).at(s - x.at(j - 1));
        else
          dp.at(j).at(k).at(s) = 0;
        // cout<<dp.at(j).at(k).at(s)<<endl;
      }
    }
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++)
    ans += dp.at(n).at(i).at(i * a);
  cout << ans << endl;
  return 0;
}
Problem p04013 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define rep(i, b) for (ll i = 0; i < b; i++)
#define ll long long
using namespace std;
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvvl vector<vector<vector<ll>>>
#define debug(x) cerr << #x << " " << x << '\n';
bool cmp(pair<ll, ll> a, pair<ll, ll> b) { return a.second > b.second; }

int main() {
  ll n, a;
  cin >> n >> a;
  vl x(n);
  rep(i, n) {
    cin >> x[i];
    x[i] -= a;
  }
  vvl dp(n + 1, vl(2 * n * a + 1));
  dp[0][n * a] = 1;
  rep(i, n) {
    rep(j, 2 * n * a) {
      dp[i + 1][j] += dp[i][j];
      dp[i + 1][j + x[i]] += dp[i][j];
    }
  }
  cout << dp[n][n * a] - 1 << endl;
}

Fixed submission:
#include "bits/stdc++.h"
#define rep(i, b) for (ll i = 0; i < b; i++)
#define ll long long
using namespace std;
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvvl vector<vector<vector<ll>>>
#define debug(x) cerr << #x << " " << x << '\n';
bool cmp(pair<ll, ll> a, pair<ll, ll> b) { return a.second > b.second; }

int main() {
  ll n, a;
  cin >> n >> a;
  vl x(n);
  rep(i, n) {
    cin >> x[i];
    x[i] -= a;
  }
  vvl dp(n + 1, vl(5000));
  dp[0][n * a] = 1;
  rep(i, n) {
    rep(j, 2 * n * a) {
      dp[i + 1][j] += dp[i][j];
      dp[i + 1][j + x[i]] += dp[i][j];
    }
  }
  cout << dp[n][n * a] - 1 << endl;
}
Problem p04013 (C++)

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

using namespace std;

int n, a;
int x[50];
int64_t dp[2][51][50 * 50 + 1];

int main() {
  cin >> n >> a;
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 51; j++)
      for (int k = 0; k < 50 * 50 + 1; k++)
        dp[i][j][k] = 0;

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

  dp[0][0][0]++;
  for (int i = 1; i <= n; i++) {
    for (int k = 0; k <= i + 1; k++) {
      for (int v = 0; v <= 50 * k; v++) {
        dp[i & 1][k][v] =
            dp[(i + 1) & 1][k][v] +
            (k > 0 && v >= x[i - 1] ? dp[(i + 1) & 1][k - 1][v - x[i - 1]] : 0);
      }
    }
  }

  int64_t c = 0;
  for (int k = 1; k <= n; k++)
    c += dp[n & 1][k][a * k];

  cout << c << endl;

  return 0;
}


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

using namespace std;

int n, a;
int x[50];
int64_t dp[2][51][50 * 50 + 1];

int main() {
  cin >> n >> a;
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 51; j++)
      for (int k = 0; k < 50 * 50 + 1; k++)
        dp[i][j][k] = 0;

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

  dp[0][0][0]++;
  for (int i = 1; i <= n; i++) {
    for (int k = 0; k <= i; k++) {
      for (int v = 0; v <= 50 * k; v++) {
        dp[i & 1][k][v] =
            dp[(i + 1) & 1][k][v] +
            (k > 0 && v >= x[i - 1] ? dp[(i + 1) & 1][k - 1][v - x[i - 1]] : 0);
      }
    }
  }

  int64_t c = 0;
  for (int k = 1; k <= n; k++)
    c += dp[n & 1][k][a * k];

  cout << c << endl;

  return 0;
}
Problem p04013 (C++)

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

int n, a, ar[200];
long long dp[80][80][8000], ans;
bool visited[80][80][8000];

long long recursion(int i, int layer, int sum) {

  //    cout<<i<<" "<<layer<<" "<<sum;
  if (visited[i][layer][sum])
    return dp[i][layer][sum];

  if (sum == 1LL * layer * a) {

    //        cout<<" dhukse";
    dp[i][layer][sum]++;
  }

  //    cout<<endl;
  visited[i][layer][sum] = 1;

  long long &x = dp[i][layer][sum];

  for (int j = i + 1; j <= n; j++)
    x += recursion(j, layer + 1, sum + ar[j]);

  return x;
}

int main() {

  while (cin >> n >> a) {

    for (int i = 1; i <= n; i++)
      scanf("%d", ar + i);

    memset(dp, 0, sizeof(dp));
    memset(visited, 0, sizeof(visited));

    ans = 0;

    for (int i = 1; i <= n; i++)
      ans += recursion(i, 1, ar[i]);

    cout << ans << endl;
  }

  return 0;
}


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

int n, a, ar[52];
long long dp[52][52][2600], ans;
bool visited[52][52][2600];

long long recursion(int i, int layer, int sum) {

  //    cout<<i<<" "<<layer<<" "<<sum;
  if (visited[i][layer][sum])
    return dp[i][layer][sum];

  if (sum == 1LL * layer * a) {

    //        cout<<" dhukse";
    dp[i][layer][sum]++;
  }

  //    cout<<endl;
  visited[i][layer][sum] = 1;

  long long &x = dp[i][layer][sum];

  for (int j = i + 1; j <= n; j++)
    x += recursion(j, layer + 1, sum + ar[j]);

  return x;
}

int main() {

  while (cin >> n >> a) {

    for (int i = 1; i <= n; i++)
      scanf("%d", ar + i);

    memset(dp, 0, sizeof(dp));
    memset(visited, 0, sizeof(visited));

    ans = 0;

    for (int i = 1; i <= n; i++)
      ans += recursion(i, 1, ar[i]);

    cout << ans << endl;
  }

  return 0;
}
Problem p04013 (C++)

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

vector<int> vec;
ll dp[52][300][52];
ll dfs(int pos, int sum, int cnt) {

  if (pos == n) {
    if (cnt != 0 && sum % cnt == 0) {
      int temp = sum / cnt;
      if (temp == a) {
        return 1;
      }
    }
    return 0;
  }
  if (dp[pos][sum][cnt] != -1) {
    return dp[pos][sum][cnt];
  }
  ll ans = 0;
  ans += dfs(pos + 1, sum, cnt);
  ans += dfs(pos + 1, sum + vec[pos], cnt + 1);
  return dp[pos][sum][cnt] = ans;
}
int main() {

  cin >> n >> a;
  vec.resize(n);
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < n; i++) {
    cin >> vec[i];
  }
  ll res = dfs(0, 0, 0);
  cout << res << endl;

  return 0;
}


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

vector<int> vec;
ll dp[52][3000][52];
ll dfs(int pos, int sum, int cnt) {

  if (pos == n) {
    if (cnt != 0 && sum % cnt == 0) {
      int temp = sum / cnt;
      if (temp == a) {
        return 1;
      }
    }
    return 0;
  }
  if (dp[pos][sum][cnt] != -1) {
    return dp[pos][sum][cnt];
  }
  ll ans = 0;
  ans += dfs(pos + 1, sum, cnt);
  ans += dfs(pos + 1, sum + vec[pos], cnt + 1);
  return dp[pos][sum][cnt] = ans;
}
int main() {

  cin >> n >> a;
  vec.resize(n);
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < n; i++) {
    cin >> vec[i];
  }
  ll res = dfs(0, 0, 0);
  cout << res << endl;

  return 0;
}
Problem p04013 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a;
  cin >> n >> a;
  int x[n + 1], maxV = 0;
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
    maxV = max(maxV, x[i]);
  }

  int xn = maxV * n;
  long long dp[n + 1][n + 1][xn + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= xn; k++) {
        if (i == 0 && j == 0 && k == 0) {
          dp[i][j][k] = 1;
        } else if (i >= 1 && k < x[i]) {
          dp[i][j][k] = dp[i - 1][j][k];
        } else if (i >= 1 && j >= 1 && k >= x[i]) {
          dp[i][j][k] = dp[i - 1][j][k] + dp[i - 1][j - 1][k - x[i]];
        } else {
          dp[i][j][k] = 0;
        }
      }
    }
  }

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

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a;
  cin >> n >> a;
  int x[n + 1], maxV = 0;
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
    maxV = max(maxV, x[i]);
  }

  int xn = 2500;
  long long dp[n + 1][n + 1][xn + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= xn; k++) {
        if (i == 0 && j == 0 && k == 0) {
          dp[i][j][k] = 1;
        } else if (i >= 1 && k < x[i]) {
          dp[i][j][k] = dp[i - 1][j][k];
        } else if (i >= 1 && j >= 1 && k >= x[i]) {
          dp[i][j][k] = dp[i - 1][j][k] + dp[i - 1][j - 1][k - x[i]];
        } else {
          dp[i][j][k] = 0;
        }
      }
    }
  }

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

  return 0;
}
Problem p04014 (C++)

Buggy submission (status: Runtime Error):

// D - 桁和

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

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 << s + 1 << endl;
    return 0;
  }

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

  ll max_b = -1;

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

  cout << max_b << endl;

  return 0;
}

Fixed submission:

// D - 桁和

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

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 << s + 1 << endl;
    return 0;
  }

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

  ll max_b = -1;

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

  cout << max_b << endl;

  return 0;
}
Problem p04014 (C++)

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

using namespace std;

long long getDigitSum(long long b, long long n) {
  if (b > n) {
    return n;
  }

  return getDigitSum(b, (long long)(n / b)) + n % b;
}

int main() {
  long long n, s;

  cin >> n >> s;

  long long rn = (long long)sqrt(n);

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

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

  for (long long b = 2; b <= rn + 1; b++) {
    long long sum = getDigitSum(b, n);
    if (sum == s) {
      cout << b << endl;
      return 0;
    }
  }

  long long ans = -1;
  for (long long p = 1; p <= rn; p++) {
    long long b = (n - s) / p + 1;
    long long sum = getDigitSum(b, n);
    if (sum == s) {
      ans = b;
    }
  }
  cout << ans << endl;

  return 0;
}

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

using namespace std;

long long getDigitSum(long long b, long long n) {
  if (b > n) {
    return n;
  }

  return getDigitSum(b, (long long)(n / b)) + n % b;
}

int main() {
  long long n, s;

  cin >> n >> s;

  long long rn = (long long)sqrt(n);

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

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

  for (long long b = 2; b <= rn + 1; b++) {
    long long sum = getDigitSum(b, n);
    if (sum == s) {
      cout << b << endl;
      return 0;
    }
  }

  long long ans = -1;
  for (long long p = 1; p <= rn; p++) {
    long long b = (n - s) / p + 1;
    if (b <= 1) {
      continue;
    }
    long long sum = getDigitSum(b, n);
    if (sum == s) {
      ans = b;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p04014 (C++)

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

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

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

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

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

  long long start = 1;
  while (start * start <= n) {
    start++;
  }

  for (long long a = start - 1; a >= 1; a--) {
    // n = a * b + c
    // a + c = s
    long long b = (n - s + a) / a;
    if (b == 1) {
      continue;
    }
    if (f(b, n) == s) {
      cout << b << endl;
      return 0;
    }
  }

  cout << -1 << endl;
}

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

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

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

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

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

  long long start = 1;
  while (start * start <= n) {
    start++;
  }

  for (long long a = start - 1; a >= 1; a--) {
    // n = a * b + c
    // a + c = s
    long long b = (n - s + a) / a;
    if (b <= 1) {
      continue;
    }
    if (f(b, n) == s) {
      cout << b << endl;
      return 0;
    }
  }

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

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (i = 0; i < (n); i++)
#define loop(i, a, n) for (i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define int ll
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

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

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

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

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

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


Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (i = 0; i < (n); i++)
#define loop(i, a, n) for (i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define int ll
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

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

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

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

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

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

  for (int p = sqrt(n); p > 0; p--) {
    if ((n - s) % p)
      continue;
    int b = (n - s + p) / p;
    if (f(b, n) == s) {
      cout << b << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}
Problem p04014 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

double EPS = 1e-9;
int INFi = 1000000005;
long long INFll = 1000000000000000005ll;
double PI = acos(-1);
int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1};
int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1};
ll MOD = 1000000007;

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

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

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

  ll rn = sqrt(n);

  REP(i, rn - 1) {
    ll x = n;
    ll ans = 0;
    while (x > 0) {
      ans += x % (i + 2);
      x /= (i + 2);
    }
    if (ans == s) {
      cout << i + 2 << endl;
      return 0;
    }
  }

  rn = floor(sqrt(n));

  for (int p = rn; p >= 0; p--) {
    if ((n - s) % (p + 1) == 0) {
      ll b = 1 + (n - s) / (p + 1);
      ll x = n;
      ll ans = 0;
      while (x > 0) {
        ans += x % (b);
        x /= (b);
      }
      if (ans == s) {
        cout << b << endl;
        return 0;
      }
    }
  }

  cout << -1 << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

double EPS = 1e-9;
int INFi = 1000000005;
long long INFll = 1000000000000000005ll;
double PI = acos(-1);
int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1};
int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1};
ll MOD = 1000000007;

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;
  }

  ll rn = sqrt(n);

  REP(i, rn - 1) {
    ll x = n;
    ll ans = 0;
    while (x > 0) {
      ans += x % (i + 2);
      x /= (i + 2);
    }
    if (ans == s) {
      cout << i + 2 << endl;
      return 0;
    }
  }

  rn = floor(sqrt(n));

  for (int p = rn; p >= 0; p--) {
    if ((n - s) % (p + 1) == 0) {
      ll b = 1 + (n - s) / (p + 1);
      ll x = n;
      ll ans = 0;
      while (x > 0) {
        ans += x % (b);
        x /= (b);
      }
      if (ans == s) {
        cout << b << endl;
        return 0;
      }
    }
  }

  cout << -1 << endl;

  return 0;
}