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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int x;
  while (cin >> x, x) {
    int ans = 0;
    FOR(i, 0, 200) {
      FOR(j, i, 200) {
        FOR(k, j, 200) {
          FOR(l, k, 200) {
            if (i * i + j * j + k * k + l * l == x)
              ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int x;
  while (cin >> x, x) {
    int ans = 0;
    FOR(i, 0, 185) {
      FOR(j, i, 185) {
        if (i * i + j * j > x)
          break;
        FOR(k, j, 185) {
          if (i * i + j * j + k * k > x)
            break;
          FOR(l, k, 185) {
            if (i * i + j * j + k * k + l * l > x)
              break;
            if (i * i + j * j + k * k + l * l == x)
              ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00820 (C++)

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

using std::cin;
using std::cout;
using std::endl;

int dfs(int n, int sum, int k, int sn) {
  int ret = 0;
  if (k < 0) {
    return 0;
  } else {
    if (sn == sum) {
      return 1;
    }
  }
  if (sn > sum || n == 0) {
    return 0;
  }

  ret += dfs(n, sum, k - 1, sn + pow(n, 2));
  ret += dfs(n - 1, sum, k, sn);

  return ret;
}

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

  int lim, ret;
  bool find = false;
  while (n != 0) {
    find = false;
    for (int i = 1; !(find); i++) {
      if (!(pow(i, 2) < n)) {
        lim = i;
        find = true;
      }
    }
    ret = dfs(lim, n, 4, 0);
    cout << ret << endl;
    cin >> n;
  }
  return 0;
}

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

using std::cin;
using std::cout;
using std::endl;

int dfs(int n, int sum, int k, int sn) {
  int ret = 0;
  if (k < 0) {
    return 0;
  } else {
    if (sn == sum) {
      return 1;
    }
  }
  if (sn > sum || n == 0) {
    return 0;
  }
  if (sn + (k * pow(n, 2)) < sum) {
    return 0;
  }

  ret += dfs(n, sum, k - 1, sn + pow(n, 2));
  ret += dfs(n - 1, sum, k, sn);

  return ret;
}

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

  int lim, ret;
  bool find = false;
  while (n != 0) {
    find = false;
    for (int i = 1; !(find); i++) {
      if (!(pow(i, 2) < n)) {
        lim = i;
        find = true;
      }
    }
    ret = dfs(lim, n, 4, 0);
    cout << ret << endl;
    cin >> n;
  }
  return 0;
}
Problem p00820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int n;
int ans;
void saiki(int kai, int sum, int i) {
  if (sum == n)
    ans++;
  else if (kai < 4 && sum < n)
    for (int j = i; j * j <= n; j++)
      saiki(kai + 1, sum + j * j, j);
}

int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    ans = 0;
    saiki(0, 0, 1);
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int n;
int ans;
void saiki(int kai, int sum, int i) {
  if (sum == n)
    ans++;
  else if (kai < 4 && sum < n)
    for (int j = i; sum + j * j <= n; j++)
      saiki(kai + 1, sum + j * j, j);
}

int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    ans = 0;
    saiki(0, 0, 1);
    cout << ans << endl;
  }
  return 0;
}
Problem p00820 (C++)

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

#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define DEB 0
#define all(x) x.begin(), x.end()

// max:2192

unsigned short int dp[5][32770];

int main() {
  unsigned short int i, j, k, n;
  for (i = 1; i * i < (1 << 15); i++) {
    dp[1][i * i] = 1;
    for (j = i * i + 1; j < (1 << 15); j++) {
      for (k = 1; k < 4; k++) {
        dp[k + 1][j] += dp[k][j - i * i];
      }
    }
  }
#if DEB
  int mx = 0;
  rep(i, 1 << 15) {
    rep(j, 5) { mx = max(mx, dp[j][i]); }
  }
  printf("max:%d\n", mx);
#endif
  while (scanf("%d", &n), n) {
    printf("%d\n", dp[1][n] + dp[2][n] + dp[3][n] + dp[4][n]);
  }
  return 0;
}

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

#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define DEB 0
#define all(x) x.begin(), x.end()

// max:2192

unsigned short int dp[5][32770];

int main() {
  unsigned short int i, j, k;
  int n;
  for (i = 1; i * i < (1 << 15); i++) {
    dp[1][i * i] = 1;
    for (j = i * i + 1; j < (1 << 15); j++) {
      for (k = 1; k < 4; k++) {
        dp[k + 1][j] += dp[k][j - i * i];
      }
    }
  }
#if DEB
  int mx = 0;
  rep(i, 1 << 15) {
    rep(j, 5) { mx = max(mx, dp[j][i]); }
  }
  printf("max:%d\n", mx);
#endif
  while (scanf("%d", &n), n) {
    printf("%d\n", dp[1][n] + dp[2][n] + dp[3][n] + dp[4][n]);
  }
  return 0;
}
Problem p00820 (C++)

Buggy submission (status: Runtime Error):
// Enjoy your stay.

#include <bits/stdc++.h>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X) for (auto it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())

using namespace std;

typedef istringstream iss;
typedef long long ll;
typedef pair<ll, ll> pi;
typedef stringstream sst;
typedef vector<ll> vi;

int n, ans[1 << 15];

void f(int cur, int n, int pre) {
  if (n >= 1 << 15)
    return;
  ans[n]++;
  if (cur == 4)
    return;
  rep2(i, pre, INF) { f(cur + 1, n + i * i, i); }
}

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

  f(0, 0, 1);
  while (cin >> n && n) {
    cout << ans[n] << endl;
  }
}

Fixed submission:
// Enjoy your stay.

#include <bits/stdc++.h>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X) for (auto it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())

using namespace std;

typedef istringstream iss;
typedef long long ll;
typedef pair<ll, ll> pi;
typedef stringstream sst;
typedef vector<ll> vi;

int n, ans[1 << 15];

void f(int cur, int n, int pre) {
  if (n >= 1 << 15)
    return;
  ans[n]++;
  if (cur == 4)
    return;
  rep2(i, pre, 182) { f(cur + 1, n + i * i, i); }
}

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

  f(0, 0, 1);
  while (cin >> n && n) {
    cout << ans[n] << endl;
  }
}
Problem p00820 (C++)

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

vector<int> sq;
bool isSqNum[MAX + 1];
void makeTable() {
  int tmp;
  for (int i = 0; i <= MAX; i++) {
    isSqNum[i] = false;
  }
  sq.clear();
  for (int i = 1; (tmp = (int)pow((double)i, 2.0)) <= MAX; i++) {
    sq.push_back(tmp);
    isSqNum[tmp] = true;
  }
}

int n;
int ans;

void rec(int cnt, int sum, int level) {
  if (cnt >= 4)
    return;
  if (sum > n)
    return;
  if (n - sum >= sq[level] && isSqNum[n - sum]) {
    ans++;
  }
  for (int i = level; i < sq.size(); i++) {
    rec(cnt + 1, sum + sq[i], i);
  }
}

int main() {
  makeTable();
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    ans = 0;
    rec(0, 0, 0);
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>
#define MAX 32768
using namespace std;

vector<int> sq;
bool isSqNum[MAX + 1];
void makeTable() {
  int tmp;
  for (int i = 0; i <= MAX; i++) {
    isSqNum[i] = false;
  }
  sq.clear();
  for (int i = 1; (tmp = (int)pow((double)i, 2.0)) <= MAX; i++) {
    sq.push_back(tmp);
    isSqNum[tmp] = true;
  }
}

int n;
int ans;

void rec(int cnt, int sum, int level) {
  if (cnt >= 4)
    return;
  if (sum > n)
    return;
  if (n - sum >= sq[level] && isSqNum[n - sum]) {
    ans++;
  }
  for (int i = level; i < sq.size() && sum + sq[i] <= n; i++) {
    rec(cnt + 1, sum + sq[i], i);
  }
}

int main() {
  makeTable();
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    ans = 0;
    rec(0, 0, 0);
    cout << ans << endl;
  }
  return 0;
}
Problem p00820 (C++)

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

const int MAX = 33000;
const int SQRT = 200;

int dp[MAX][5][SQRT];

int main() {

  dp[0][0][1] = 1;
  for (int i = 0; i < MAX; i++) {
    for (int j = 0; j < 5; j++) {
      for (int k = 1; k < SQRT; k++)
        if (dp[i][j][k] > 0) {
          for (int l = k; l < SQRT; l++) { // l*lを加算
            if (i + l * l >= MAX || j + 1 >= 5)
              break;
            dp[i + l * l][j + 1][l] += dp[i][j][k];
          }
        }
    }
  }

  int n;
  while (cin >> n, n) {
    int ans = 0;
    for (int i = 1; i < 5; i++) {
      for (int j = 1; j < SQRT; j++)
        ans += dp[n][i][j];
    }
    cout << ans << endl;
  }
}


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

const int MAX = 32770; // 1<<15
const int SQRT = 182;

int dp[MAX][5][SQRT];

int main() {

  dp[0][0][1] = 1;
  for (int i = 0; i < MAX; i++) {
    for (int j = 0; j < 5; j++) {
      for (int k = 1; k < SQRT; k++)
        if (dp[i][j][k] > 0) {
          for (int l = k; l < SQRT; l++) { // l*lを加算
            if (i + l * l >= MAX || j + 1 >= 5)
              break;
            dp[i + l * l][j + 1][l] += dp[i][j][k];
          }
        }
    }
  }

  int n;
  while (cin >> n, n) {
    int ans = 0;
    for (int i = 1; i < 5; i++) {
      for (int j = 1; j < SQRT; j++)
        ans += dp[n][i][j];
    }
    cout << ans << endl;
  }
}
Problem p00820 (C++)

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int n;

int dfs(int sum, int d, int c) {
  //  printf("%d %d\n", sum, c);
  if (sum == n)
    return 1;
  if (c >= 4)
    return 0;
  int res = 0;
  REP(i, d, sqrt(n) + 1) {
    if (sum + i * i > n)
      break;
    ;
    res += dfs(sum + i * i, i, c + 1);
  }
  return res;
}

int main() {
  while (scanf("%d", &n) && n) {
    printf("%d\n", dfs(0, 1, 0));
  }
  return 0;
}

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int n;

int dfs(int sum, int d, int c) {
  //  printf("%d %d\n", sum, c);
  if (sum == n)
    return 1;
  if (c >= 4)
    return 0;
  int res = 0;
  for (int i = d; i * i <= n; i++) {
    if (sum + i * i > n)
      break;
    ;
    res += dfs(sum + i * i, i, c + 1);
  }
  return res;
}

int main() {
  while (scanf("%d", &n) && n) {
    printf("%d\n", dfs(0, 1, 0));
  }
  return 0;
}
Problem p00836 (C++)

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

int main() {
  int N;

  bool isPrime[10001];
  for (int i = 0; i < 10001; i++)
    isPrime[i] = 1;
  isPrime[0] = isPrime[1] = 0;
  for (int i = 0; i < 10001; i++) {
    if (!isPrime[i])
      continue;
    for (int j = 2; j * i < 10001; j++) {
      isPrime[i * j] = 0;
    }
  }

  vector<int> prime;
  prime.push_back(0);
  for (int i = 0; i < 10001; i++)
    if (isPrime[i])
      prime.push_back(i);
  for (int i = 1; i < prime.size(); i++)
    prime[i] += prime[i - 1];

  while (cin >> N, N) {
    int ans = 0;
    int i = 0, j = 0;
    while (i < prime.size() || j < prime.size()) {
      int sub = prime[j] - prime[i];
      if (sub < N)
        j++;
      else if (sub >= N) {
        i++;
        if (sub == N)
          ans++;
      }
    }
    cout << ans << endl;
  }

  return 0;
}

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

int main() {
  int N;

  bool isPrime[10001];
  for (int i = 0; i < 10001; i++)
    isPrime[i] = 1;
  isPrime[0] = isPrime[1] = 0;
  for (int i = 0; i < 10001; i++) {
    if (!isPrime[i])
      continue;
    for (int j = 2; j * i < 10001; j++) {
      isPrime[i * j] = 0;
    }
  }

  vector<int> prime;
  prime.push_back(0);
  for (int i = 0; i < 10001; i++)
    if (isPrime[i])
      prime.push_back(i);
  for (int i = 1; i < prime.size(); i++)
    prime[i] += prime[i - 1];

  while (cin >> N, N) {
    int ans = 0;
    int i = 0, j = 0;
    while (i < prime.size() && j < prime.size()) {
      int sub = prime[j] - prime[i];
      if (sub < N)
        j++;
      else if (sub >= N) {
        i++;
        if (sub == N)
          ans++;
      }
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p00836 (C++)

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

const int M = 10000;

int main() {
  vector<int> primes;

  for (int i = 2; i < M; i++) {
    bool flag = true;

    for (int j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        flag = false;
        break;
      }
    }

    if (flag) {
      primes.push_back(i);
    }
  }

  for (int n; cin >> n && n;) {
    int sum = 0, front = 0, back = 0, count = 0;

    while (true) {
      while (sum < n && front < primes.size()) {
        sum += primes[front++];
      }

      while (sum > n && back <= front) {
        sum -= primes[back++];
      }

      if (sum == n) {
        count++;
        sum -= primes[back++];
      }

      if (front == back) {
        break;
      }
    }

    cout << count << endl;
  }
}

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

const int M = 20000;

int main() {
  vector<int> primes;

  for (int i = 2; i < M; i++) {
    bool flag = true;

    for (int j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        flag = false;
        break;
      }
    }

    if (flag) {
      primes.push_back(i);
    }
  }

  for (int n; cin >> n && n;) {
    int sum = 0, front = 0, back = 0, count = 0;

    while (true) {
      while (sum < n && front < primes.size()) {
        sum += primes[front++];
      }

      while (sum > n && back <= front) {
        sum -= primes[back++];
      }

      if (sum == n) {
        count++;
        sum -= primes[back++];
      }

      if (front == back) {
        break;
      }
    }

    cout << count << endl;
  }
}
Problem p00841 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#define INF 2000000
#define t first
#define x second
using namespace std;
typedef pair<double, int> P;
double b, v, e, f;
int r;

double mk(int x) {
  if (x >= r)
    return 1.0 / (v - e * (x - r));
  return 1.0 / (v - f * (r - x));
}

int main() {
  while (1) {
    int n, a[110];
    cin >> n;
    if (n == 0)
      break;
    for (int i = 1; i <= n; i++)
      cin >> a[i];
    cin >> b;
    cin >> r >> v >> e >> f;
    P dp[10100] = {};
    for (int i = 1; i <= a[n]; i++)
      dp[i] = make_pair(INF, 0);

    for (int i = 0; i < n; i++) {
      int tx;
      double tt;
      // No Change
      tx = dp[a[i]].x, tt = dp[a[i]].t;
      for (int j = a[i] + 1; j <= a[n]; j++) {
        double k = mk(tx);
        tt += k, tx++;
        dp[j] = min(dp[j], make_pair(tt, tx));
      }

      // Change
      tx = 0, tt = dp[a[i]].t + b;
      for (int j = a[i] + 1; j <= a[n]; j++) {
        double k = mk(tx);
        tt += k;
        tx++;
        dp[j] = min(dp[j], make_pair(tt, tx));
      }
    }
    printf("%.4f\n", dp[a[n]].t);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#define INF 2000000
#define t first
#define x second
using namespace std;
typedef pair<double, int> P;
double b, v, e, f;
int r;

double mk(int x) {
  if (x >= r)
    return 1.0 / (v - e * (x - r));
  return 1.0 / (v - f * (r - x));
}

int main() {
  while (1) {
    int n, a[110] = {};
    cin >> n;
    if (n == 0)
      break;
    for (int i = 1; i <= n; i++)
      cin >> a[i];
    cin >> b;
    cin >> r >> v >> e >> f;
    P dp[10100] = {};
    for (int i = 1; i <= a[n]; i++)
      dp[i] = make_pair(INF, 0);

    for (int i = 0; i < n; i++) {
      int tx;
      double tt;
      // No Change
      tx = dp[a[i]].x, tt = dp[a[i]].t;
      for (int j = a[i] + 1; j <= a[n]; j++) {
        double k = mk(tx);
        tt += k, tx++;
        dp[j] = min(dp[j], make_pair(tt, tx));
      }

      // Change
      tx = 0, tt = dp[a[i]].t + b;
      for (int j = a[i] + 1; j <= a[n]; j++) {
        double k = mk(tx);
        tt += k;
        tx++;
        dp[j] = min(dp[j], make_pair(tt, tx));
      }
    }
    printf("%.4f\n", dp[a[n]].t);
  }
  return 0;
}
Problem p00848 (C++)

Buggy submission (status: Memory Limit Exceeded):
// 37
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int cmp[1121] = {};
  vector<int> prm;
  for (int i = 2; i <= 1120; i++) {
    if (!cmp[i]) {
      prm.push_back(i);
      for (int j = 2; i * j <= 1120; j++) {
        cmp[i * j] = true;
      }
    }
  }
  static int dp[1121][1000][15];
  dp[0][0][0] = 1;
  for (int i = 0; i < 1121; i++) {
    for (int j = 0; j <= prm.size(); j++) {
      for (int l = j + 1; l <= prm.size(); l++) {
        for (int k = 0; k < 14; k++) {
          if (i + prm[l - 1] <= 1120) {
            dp[i + prm[l - 1]][l][k + 1] += dp[i][j][k];
          }
        }
      }
    }
  }
  for (int n, k; cin >> n >> k, n | k;) {
    int a = 0;
    for (int i = 0; i <= prm.size(); i++) {
      a += dp[n][i][k];
    }
    cout << a << endl;
  }
  return 0;
}

Fixed submission:
// 37
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int cmp[1121] = {};
  vector<int> prm;
  for (int i = 2; i <= 1120; i++) {
    if (!cmp[i]) {
      prm.push_back(i);
      for (int j = 2; i * j <= 1120; j++) {
        cmp[i * j] = true;
      }
    }
  }
  static int dp[1121][200][15];
  dp[0][0][0] = 1;
  for (int i = 0; i < 1121; i++) {
    for (int j = 0; j <= prm.size(); j++) {
      for (int l = j + 1; l <= prm.size(); l++) {
        for (int k = 0; k < 14; k++) {
          if (i + prm[l - 1] <= 1120) {
            dp[i + prm[l - 1]][l][k + 1] += dp[i][j][k];
          }
        }
      }
    }
  }
  for (int n, k; cin >> n >> k, n | k;) {
    int a = 0;
    for (int i = 0; i <= prm.size(); i++) {
      a += dp[n][i][k];
    }
    cout << a << endl;
  }
  return 0;
}
Problem p00848 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;

vector<int> sieve_of_eratosthenes(int n) {
  vector<int> primes(n);
  for (int i = 2; i < n; ++i)
    primes[i] = i;
  for (int i = 2; i * i < n; ++i)
    if (primes[i])
      for (int j = i * i; j < n; j += i)
        primes[j] = 0;
  return primes;
}

int dp[15][1121][1121];

int main() {
  vector<int> a = sieve_of_eratosthenes(1121);
  vector<int> prime;
  for (int i = 0; i < a.size(); i++)
    if (a[i] != 0)
      prime.push_back(a[i]);

  int n, k;
  while (cin >> n >> k && n != 0) {
    memset(dp, 0, sizeof(dp));

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

    for (int i = 0; i < k; i++) {
      for (int j = 0; j < prime.size(); j++) {
        for (int num = 0; num <= n; num++) {
          if (dp[i][j][num] == 0)
            continue;
          for (int next = j; next < prime.size(); next++) {
            if (num + prime[next] > n)
              break;
            dp[i + 1][next + 1][num + prime[next]] += dp[i][j][num];
          }
        }
      }
    }
    int ans = 0;
    for (int i = 0; i <= prime.size(); i++) {
      ans += dp[k][i][n];
    }
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;

vector<int> sieve_of_eratosthenes(int n) {
  vector<int> primes(n);
  for (int i = 2; i < n; ++i)
    primes[i] = i;
  for (int i = 2; i * i < n; ++i)
    if (primes[i])
      for (int j = i * i; j < n; j += i)
        primes[j] = 0;
  return primes;
}

int dp[15][200][1121];

int main() {
  vector<int> a = sieve_of_eratosthenes(1121);
  vector<int> prime;
  for (int i = 0; i < a.size(); i++)
    if (a[i] != 0)
      prime.push_back(a[i]);

  int n, k;
  while (cin >> n >> k && n != 0) {
    memset(dp, 0, sizeof(dp));

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

    for (int i = 0; i < k; i++) {
      for (int j = 0; j < prime.size(); j++) {
        for (int num = 0; num <= n; num++) {
          if (dp[i][j][num] == 0)
            continue;
          for (int next = j; next < prime.size(); next++) {
            if (num + prime[next] > n)
              break;
            dp[i + 1][next + 1][num + prime[next]] += dp[i][j][num];
          }
        }
      }
    }
    int ans = 0;
    for (int i = 0; i <= prime.size(); i++) {
      ans += dp[k][i][n];
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p00848 (C++)

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

using namespace std;

typedef long long ll;

vector<int> pn;
vector<bool> is_p;

void enum_prime() {
  is_p.resize(1515, true);
  is_p[0] = is_p[1] = false;
  for (int i = 2; i <= 1515; i++) {
    if (!is_p[i])
      continue;
    pn.push_back(i);
    for (int j = i + i; j <= 1515; j += i)
      is_p[j] = false;
  }
}

int dp[1515][1515][15];

int solve(int n, int p, int k) {
  if (k == 0 && n == 0)
    return 1;
  if (k < 0 || n < 0 || p < 0)
    return 0;

  int &ret = dp[n][p][k];
  if (~ret)
    return ret;

  ret = solve(n, p - 1, k);
  if (n >= pn[p])
    ret += solve(n - pn[p], p - 1, k - 1);

  return ret;
}

int main() {
  enum_prime();
  int n, k;
  while (cin >> n >> k, n || k) {
    memset(dp, -1, sizeof(dp));
    int p = upper_bound(pn.begin(), pn.end(), n) - pn.begin() - 1;
    cout << solve(n, p, k) << endl;
  }
  return 0;
}

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

using namespace std;

typedef long long ll;

vector<int> pn;
vector<bool> is_p;

void enum_prime() {
  is_p.resize(1515, true);
  is_p[0] = is_p[1] = false;
  for (int i = 2; i <= 1515; i++) {
    if (!is_p[i])
      continue;
    pn.push_back(i);
    for (int j = i + i; j <= 1515; j += i)
      is_p[j] = false;
  }
}

int dp[1200][300][15];

int solve(int n, int p, int k) {
  if (k == 0 && n == 0)
    return 1;
  if (k < 0 || n < 0 || p < 0)
    return 0;

  int &ret = dp[n][p][k];
  if (~ret)
    return ret;

  ret = solve(n, p - 1, k);
  if (n >= pn[p])
    ret += solve(n - pn[p], p - 1, k - 1);

  return ret;
}

int main() {
  enum_prime();
  int n, k;
  while (cin >> n >> k, n || k) {
    memset(dp, -1, sizeof(dp));
    int p = upper_bound(pn.begin(), pn.end(), n) - pn.begin() - 1;
    cout << solve(n, p, k) << endl;
  }
  return 0;
}
Problem p00848 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 20
#define MAX_K 1355
long long dp[MAX_N + 1][MAX_K][300];
long long sum[MAX_N + 1][MAX_K];
int prime[MAX_K];
int main() {
  prime[0] = 1;
  prime[1] = 1;
  for (int i = 2; i * i < MAX_K; i++) {
    for (int j = i * i; j < MAX_K; j += i) {
      prime[j] = 1;
    }
  }
  for (int i = 0; i < MAX_K; i++)
    prime[i] = 1 - prime[i];
  vector<int> p;
  p.push_back(0);
  for (int i = 0; i < MAX_K; i++) {
    if (prime[i] == 1) {
      p.push_back(i);
    }
  }
  dp[0][0][0] = 1;
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_K; j++) {
      for (int k = 0; k < p.size(); k++) {
        for (int l = k + 1; l < p.size(); l++) {
          if (j + p[l] >= MAX_K) {
            continue;
          }
          dp[i + 1][j + p[l]][l] += dp[i][j][k];
        }
      }
    }
  }
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_K; j++) {
      for (int k = 0; k < p.size(); k++) {
        sum[i][j] += dp[i][j][k];
      }
    }
  }
  int n, k;
  while (true) {
    cin >> n >> k;
    if (n == 0 && k == 0) {
      break;
    }
    cout << sum[k][n] << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 20
#define MAX_K 1355
long long dp[MAX_N + 1][MAX_K][220];
long long sum[MAX_N + 1][MAX_K];
int prime[MAX_K];
int main() {
  prime[0] = 1;
  prime[1] = 1;
  for (int i = 2; i * i < MAX_K; i++) {
    for (int j = i * i; j < MAX_K; j += i) {
      prime[j] = 1;
    }
  }
  for (int i = 0; i < MAX_K; i++)
    prime[i] = 1 - prime[i];
  vector<int> p;
  p.push_back(0);
  for (int i = 0; i < MAX_K; i++) {
    if (prime[i] == 1) {
      p.push_back(i);
    }
  }
  dp[0][0][0] = 1;
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_K; j++) {
      for (int k = 0; k < p.size(); k++) {
        for (int l = k + 1; l < p.size(); l++) {
          if (j + p[l] >= MAX_K) {
            continue;
          }
          dp[i + 1][j + p[l]][l] += dp[i][j][k];
        }
      }
    }
  }
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_K; j++) {
      for (int k = 0; k < p.size(); k++) {
        sum[i][j] += dp[i][j][k];
      }
    }
  }
  int n, k;
  while (true) {
    cin >> n >> k;
    if (n == 0 && k == 0) {
      break;
    }
    cout << sum[k][n] << endl;
  }
  return 0;
}
Problem p00854 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int f(int n, int k) { return n == 1 ? 0 : 1 + (k - 1 + f(n - 1, k)) % (n - 1); }

int main() {
  for (;;) {
    int n, k, m;
    cin >> n >> k >> m;
    cout << 1 + (m - 1 + f(n, k)) % n << endl;
  }
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int f(int n, int k) { return n == 1 ? 0 : 1 + (k - 1 + f(n - 1, k)) % (n - 1); }

int main() {
  for (;;) {
    int n, k, m;
    cin >> n >> k >> m;
    if (n == 0)
      break;
    cout << 1 + (m - 1 + f(n, k)) % n << endl;
  }
}
Problem p00854 (C++)

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

bool isExist[10000];
int n, k, m;

int main() {
  while (cin >> n >> k >> m) {
    if (!n)
      break;
    int i;

    for (i = 0; i < n; i++) {
      isExist[i] = true;
    }

    int cur = m - 1;
    isExist[cur] = false;
    for (i = 1; i < n; i++) {
      int step = (k - 1) % (n - i) + 1;
      while (true) {
        if (isExist[cur])
          step--;
        if (step <= 0)
          break;
        cur = (cur + 1) % n;
      }
      isExist[cur] = false;
    }
    cout << cur + 1 << endl;
  }
  return 0;
}

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

bool isExist[10000];
int n, k, m;

int main() {
  while (cin >> n >> k >> m) {
    if (!n)
      break;
    int i;

    for (i = 0; i < n; i++) {
      isExist[i] = true;
    }

    int cur = m - 1;
    isExist[cur] = false;
    for (i = 1; i < n; i++) {
      int step = (k - 1) % (n - i) + 1;
      while (true) {
        if (isExist[cur])
          step--;
        if (step <= 0)
          break;
        cur++;
        if (cur == n)
          cur = 0;
      }
      isExist[cur] = false;
    }
    cout << cur + 1 << endl;
  }
  return 0;
}
Problem p00854 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long int ll;
typedef double D;     // ??§?¨?????????????double???long double?????????
typedef complex<D> P; // Point
typedef pair<P, P> L; // Line
typedef vector<P> VP;

const int INF = (1 << 30) - 1;
const D EPS = 1e-9;

#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

int N, M, K;

void solve() {
  vector<int> next_stones(N);
  for (int i = 0; i < N; i++)
    next_stones[i] = (i + 1) % N;

  // first-remove
  int cur = M - 1;
  int pcur = (cur - 1 + N) % N;
  int remained_stone = N;

  while (remained_stone-- > 1) {
    next_stones[pcur] = next_stones[cur];
    for (int i = 0; i < K; i++) {
      pcur = cur;
      cur = next_stones[cur];
    }
  }
  cout << cur + 1 << endl;
}

// g++ -std=c++0x -msse4.2 -O3
// #include <bits/stdc++.h>
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  while (cin >> N >> M >> K, N | M | K)
    solve();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long int ll;
typedef double D;     // ??§?¨?????????????double???long double?????????
typedef complex<D> P; // Point
typedef pair<P, P> L; // Line
typedef vector<P> VP;

const int INF = (1 << 30) - 1;
const D EPS = 1e-9;

#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

int N, M, K;

void solve() {
  vector<int> next_stones(N);
  for (int i = 0; i < N; i++)
    next_stones[i] = (i + 1) % N;

  // first-remove
  int cur = M - 1;
  int pcur = (cur - 1 + N) % N;
  int remained_stone = N;

  while (remained_stone-- > 1) {
    next_stones[pcur] = next_stones[cur];
    for (int i = 0; i < K; i++) {
      pcur = cur;
      cur = next_stones[cur];
    }
  }
  cout << cur + 1 << endl;
}

// g++ -std=c++0x -msse4.2 -O3
// #include <bits/stdc++.h>
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  while (cin >> N >> K >> M, N | K | M)
    solve();

  return 0;
}
Problem p00854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <list>

using std::cin;
using std::cout;
using std::endl;
using std::list;

int main(void) {
  int n, k, m;
  cin >> n >> k >> m;

  while (n != 0 || k != 0 || m != 0) {
    list<int> nums(n, 0);
    int num = 1;
    for (list<int>::iterator itr = nums.begin(); itr != nums.end(); ++itr) {
      *itr = num;
      num++;
    }

    int count_s = n;
    int rm = m;
    int l;
    bool first = true;

    for (list<int>::iterator itr = nums.begin(); count_s != 1;) {
      if (first) {
        for (int i = 0; i < rm - 1; i++) {
          ++itr;
          if (itr == nums.end()) {
            itr = nums.begin();
          }
        }
        first = false;
      } else {
        for (int i = 0; i < rm; i++) {
          ++itr;
          if (itr == nums.end()) {
            itr = nums.begin();
          }
        }
      }

      itr = nums.erase(itr);
      if (count_s == n) {
        rm = k;
      }
      count_s--;
      if (count_s != 1) {
        --itr;
      }
    }
    cout << *nums.begin() << endl;
    cin >> n >> k >> m;
  }

  return 0;
}

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

using std::cin;
using std::cout;
using std::endl;
using std::list;

int main(void) {
  int n, k, m;
  cin >> n >> k >> m;

  while (n != 0 || k != 0 || m != 0) {
    list<int> nums(n, 0);
    int num = 1;
    for (list<int>::iterator itr = nums.begin(); itr != nums.end(); ++itr) {
      *itr = num;
      num++;
    }

    int count_s = n;
    int rm = m;
    int l;
    bool first = true;

    for (list<int>::iterator itr = nums.begin(); count_s != 1;) {
      if (first) {
        for (int i = 0; i < rm - 1; i++) {
          ++itr;
          if (itr == nums.end()) {
            itr = nums.begin();
          }
        }
        first = false;
      } else {
        l = ((k - 1) % count_s) + 1;
        for (int i = 0; i < l; i++) {
          ++itr;
          if (itr == nums.end()) {
            itr = nums.begin();
          }
        }
      }

      itr = nums.erase(itr);
      if (count_s == n) {
        rm = k;
      }
      count_s--;
      if (count_s != 1) {
        --itr;
      }
    }
    cout << *nums.begin() << endl;
    cin >> n >> k >> m;
  }

  return 0;
}
Problem p00854 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int n, k, m;
  while (scanf("%d%d%d", &n, &k, &m)) {
    m--;
    vector<int> v(n);
    iota(v.begin(), v.end(), 1);
    while (v.size() > 1) {
      v.erase(v.begin() + m);
      m = (m + k - 1) % v.size();
    }
    cout << v[0] << endl;
  }
}

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

int main() {
  int n, k, m;
  while (scanf("%d%d%d", &n, &k, &m), n) {
    m--;
    vector<int> v(n);
    iota(v.begin(), v.end(), 1);
    while (v.size() > 1) {
      v.erase(v.begin() + m);
      m = (m + k - 1) % v.size();
    }
    cout << v[0] << endl;
  }
}
Problem p00854 (C++)

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

using namespace std;

int main() {
  int n, k, m;
  while (cin >> n >> k >> m, n || k || m) {
    int stone[10000];
    for (int i = 0; i < n; i++)
      stone[i] = (i + 1) % n;
    m--;
    stone[m - 1] = stone[stone[m - 1]];
    while (1) {
      if (stone[m] == m)
        break;
      for (int i = 0; i < k - 1; i++)
        m = stone[m];
      stone[m] = stone[stone[m]];
    }
    cout << m + 1 << endl;
  }
  return 0;
}

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

using namespace std;

int main() {
  int n, k, m;
  while (cin >> n >> k >> m, n || k || m) {
    int stone[10000];
    for (int i = 0; i < n; i++)
      stone[i] = (i + 1) % n;
    m -= 2;
    if (m < 0)
      m += n;
    stone[m] = stone[stone[m]];
    while (stone[m] != m) {
      for (int i = 0; i < k - 1; i++)
        m = stone[m];
      stone[m] = stone[stone[m]];
    }
    cout << m + 1 << endl;
  }
  return 0;
}
Problem p00854 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstring>
#include <iostream>
using namespace std;
bool x[10000];
int a, b, c, d, e, p;
int main() {
  while (true) {
    memset(x, true, 10000);
    cin >> a >> b >> c;
    if (a == 0) {
      break;
    }
    d = a - 1;
    x[c - 1] = false;
    p = c - 1;
    while (d >= 2) {
      e = b % d + d;
      for (int i = 0; i < e; i++) {
        while (true) {
          p++;
          if (x[p % a] == true) {
            break;
          }
        }
      }
      x[p % a] = false;
      d--;
    }
    for (int i = 0; i < a; i++) {
      if (x[i] == true) {
        p = i;
      }
    }
    cout << p + 1 << endl;
  }
}

Fixed submission:
#include <cstring>
#include <iostream>
using namespace std;
bool x[10000];
int a, b, c, d, e, p;
int main() {
  while (true) {
    memset(x, true, 10000);
    cin >> a >> b >> c;
    if (a == 0) {
      break;
    }
    d = a - 1;
    x[c - 1] = false;
    p = c - 1;
    while (d >= 2) {
      e = b % d;
      if (e == 0) {
        e = d;
      }
      for (int i = 0; i < e; i++) {
        while (true) {
          p++;
          if (x[p % a] == true) {
            break;
          }
        }
      }
      x[p % a] = false;
      d--;
    }
    for (int i = 0; i < a; i++) {
      if (x[i] == true) {
        p = i;
      }
    }
    cout << p + 1 << endl;
  }
}
Problem p00855 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;

// →↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};

const double EPS = 1e-9;

const int M = 1299800;
bool isPrime[M];

vector<int> primes;

void sieve() {
  for (int i = 2; i < M; i++)
    isPrime[i] = true;
  for (int i = 2; i * i < M; i++) {
    if (!isPrime[i])
      continue;
    for (int j = i * i; j < M; j += i) {
      isPrime[j] = false;
    }
  }
  for (int i = 2; i < M; i++) {
    if (isPrime[i])
      primes.push_back(i);
  }
}

int main() {
  sieve();
  while (true) {
    int n;
    cin >> n;
    if (isPrime[n]) {
      cout << 0 << endl;
    } else {
      cout << (*upper_bound(all(primes), n)) -
                  *(upper_bound(all(primes), n) - 1)
           << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;

// →↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};

const double EPS = 1e-9;

const int M = 1299800;
bool isPrime[M];

vector<int> primes;

void sieve() {
  for (int i = 2; i < M; i++)
    isPrime[i] = true;
  for (int i = 2; i * i < M; i++) {
    if (!isPrime[i])
      continue;
    for (int j = i * i; j < M; j += i) {
      isPrime[j] = false;
    }
  }
  for (int i = 2; i < M; i++) {
    if (isPrime[i])
      primes.push_back(i);
  }
}

int main() {
  sieve();
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    if (isPrime[n]) {
      cout << 0 << endl;
    } else {
      cout << (*upper_bound(all(primes), n)) -
                  *(upper_bound(all(primes), n) - 1)
           << endl;
    }
  }
  return 0;
}
Problem p00855 (C++)

Buggy submission (status: Runtime Error):

#include <iostream>
#include <vector>
using namespace std;

int pmem[130000];
vector<int> prime;
void init() {
  for (int i = 2; i < 1300000; i++) {
    if (pmem[i])
      continue;
    pmem[i] = 1;
    prime.push_back(i);
    for (int j = i + i; j < 1300000; j += i)
      pmem[j] = -1;
  }
}

int main() {
loop:
  init();
  int n;
  cin >> n;
  if (!n)
    return 0;
  for (int i = 0; i < prime.size(); i++) {
    if (prime[i] == n) {
      cout << 0 << endl;
      goto loop;
    }
    if (prime[i] > n) {
      cout << prime[i] - prime[i - 1] << endl;
      goto loop;
    }
  }
}

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int pmem[1300000];
vector<int> prime;
void init() {
  for (int i = 2; i < 1300000; i++) {
    if (pmem[i])
      continue;
    pmem[i] = 1;
    prime.push_back(i);
    for (int j = i + i; j < 1300000; j += i)
      pmem[j] = -1;
  }
}

int main() {
loop:
  init();
  int n;
  cin >> n;
  if (!n)
    return 0;
  for (int i = 0; i < prime.size(); i++) {
    if (prime[i] == n) {
      cout << 0 << endl;
      goto loop;
    }
    if (prime[i] > n) {
      cout << prime[i] - prime[i - 1] << endl;
      goto loop;
    }
  }
}
Problem p00855 (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 <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define COUNT(i, n) for (int i = 1; i <= n; i++)
#define ITER(c) __typeof((c).begin())
#define each(c, it) for (ITER(c) it = (c).begin(); it != (c).end(); it++)
#define ALL(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define MEMSET(v, h) memset((v), h, sizeof(v))

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;

int main(void) {
  const int N = (int)1e6;
  bool isP[N];
  rep(i, N) isP[i] = true;
  for (int i = 2; i * i < N; i++)
    if (isP[i])
      for (int j = i; i * j < N; j++)
        isP[i * j] = false;

  for (int n, r, res; cin >> n, n; cout << res << endl) {
    r = n;
    if (isP[n])
      res = 0;
    else {
      while (!isP[--r])
        ;
      while (!isP[++n])
        ;
    }
    res = n - r;
  }

  return 0;
}

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

#define rep(i, n) for (int i = 0; i < n; i++)
#define COUNT(i, n) for (int i = 1; i <= n; i++)
#define ITER(c) __typeof((c).begin())
#define each(c, it) for (ITER(c) it = (c).begin(); it != (c).end(); it++)
#define ALL(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define MEMSET(v, h) memset((v), h, sizeof(v))

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;

int main(void) {
  const int N = 1299710;
  bool isP[N];
  rep(i, N) isP[i] = true;
  for (int i = 2; i * i < N; i++)
    if (isP[i])
      for (int j = i; i * j < N; j++)
        isP[i * j] = false;

  for (int n, r, res; cin >> n, n; cout << res << endl) {
    r = n;
    if (isP[n])
      res = 0;
    else {
      while (!isP[--r])
        ;
      while (!isP[++n])
        ;
    }
    res = n - r;
  }

  return 0;
}
Problem p00855 (C++)

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

int main() {
  bool *prime = (bool *)malloc(sizeof(bool) * MAX_LIST);
  // bool prime[MAX_LIST];
  for (int i = 0; i < MAX_LIST; i++) {
    prime[i] = 1;
  }
  prime[0] = prime[1] = 0;
  for (int i = 0; i * i < MAX_LIST; i++) {
    if (prime[i] == 1) {
      for (int j = i * 2; j < MAX_LIST; j = j + i) {
        prime[j] = 0;
      }
    }
  }
  int in;
  while (cin >> in, in) {
    int ans = 0;
    int i = 0;
    while (prime[in + i] == 0) {
      ans++;
      i++;
    }
    i = 0;
    while (prime[in - i] == 0) {
      ans++;
      i++;
    }
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
#define MAX_LIST 10000000

int main() {
  bool *prime = (bool *)malloc(sizeof(bool) * MAX_LIST);
  // bool prime[MAX_LIST];
  for (int i = 0; i < MAX_LIST; i++) {
    prime[i] = 1;
  }
  prime[0] = prime[1] = 0;
  for (int i = 0; i * i < MAX_LIST; i++) {
    if (prime[i] == 1) {
      for (int j = i * 2; j < MAX_LIST; j = j + i) {
        prime[j] = 0;
      }
    }
  }
  int in;
  while (cin >> in, in) {
    int ans = 0;
    int i = 0;
    while (prime[in + i] == 0) {
      ans++;
      i++;
    }
    i = 0;
    while (prime[in - i] == 0) {
      ans++;
      i++;
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p00855 (C++)

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

using namespace std;

const int max_p = 1299709;
bool prime[max_p + 1];

void initial_prime() {
  for (int i = 0; i < max_p; i++)
    prime[i] = true;
  prime[0] = prime[1] = false;

  for (int i = 2; i * i < max_p; i++)
    if (prime[i])
      for (int j = 2 * i; j <= max_p; j += i)
        prime[j] = false;
}

int main() {
  int k;

  initial_prime();

  while (cin >> k, k) {
    int l, r;
    int ans = 0;

    l = r = k;
    while (!prime[l--])
      ans++;
    while (!prime[r++])
      ans++;
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

const int max_p = 1300000;
bool prime[max_p + 1];

void initial_prime() {
  for (int i = 0; i < max_p; i++)
    prime[i] = true;
  prime[0] = prime[1] = false;

  for (int i = 2; i * i < max_p; i++)
    if (prime[i])
      for (int j = 2 * i; j <= max_p; j += i)
        prime[j] = false;
}

int main() {
  int k;

  initial_prime();

  while (cin >> k, k) {
    int l, r;
    int ans = 0;

    l = r = k;
    while (!prime[l--])
      ans++;
    while (!prime[r++])
      ans++;
    cout << ans << endl;
  }

  return 0;
}