alexjercan/bugnet huggingface dataset:
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int josephus(int n, int k) {
  if (n == 1) {
    return 0;
  }
  return (josephus(n - 1, k) + k) % n;
}

int main() {
  while (true) {
    int n, k, m;
    cin >> n >> k >> m;
    if (n + k + m == 0) {
      break;
    }

    m = (m - k + 1 + n) % n;
    cout << (josephus(n, k) + m - 1 + n) % n + 1 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  while (true) {
    int n, k, m;
    cin >> n >> k >> m;
    if (n + k + m == 0) {
      break;
    }
    bool a[10001] = {false};
    m--;
    int count = n - 1;
    a[m] = true;
    while (true) {
      for (int i = m, c = 0; c <= k; i++) {
        if (!a[i % n]) {
          c++;
        }
        if (c == k) {
          a[i % n] = true;
          m = i % n;
        }
      }
      count--;
      if (count == 1) {
        break;
      }
    }

    for (int i = 0; i < n; i++) {
      if (!a[i]) {
        cout << i + 1 << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

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

  int n, k, m;
  while (cin >> n >> k >> m, n | k | m) {
    std::vector<int> v;
    for (int i = 1; i <= n; i++)
      v.push_back(i);
    int pos = m - 1;
    while (v.size() > 1) {
      v.erase(v.begin() + pos);
      pos = (pos + k - 1) % v.size();
    }
    cout << v[0] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

  int n, k, m;
  while (cin >> n >> k >> m, n | k | m) {
    int stones[n];
    for (int i = 0; i < n; i++)
      stones[i] = 1;
    int pos = m - 1;
    stones[m - 1] = 0;
    for (int i = 0; i < n - 2; i++) {
      int idx = 0;
      while (idx < k) {
        if (stones[pos] == 1) {
          idx++;
        }
        if (idx == k)
          stones[pos] = 0;
        pos = (pos + 1) % n;
      }
      // stones[pos]=0;
    }
    for (int i = 0; i < n; i++)
      if (stones[i] == 1)
        cout << i + 1 << endl;
  }
}
Problem p00854 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

#define NMAX 10000
#define NOT -1

int main() {

  int n, k, m;
  int be;
  int list[NMAX + 1];

  while (1) {

    cin >> n >> k >> m;
    if (n == 0 && k == 0 && m == 0)
      break;

    for (int i = 1; i < n; i++) {
      list[i] = i + 1;
    }
    list[n] = 1;

    if (m != 1) {
      list[m - 1] = list[m];
      list[m] = NOT;
      be = m;
      m = list[m - 1];
    } else {
      list[n] = list[m];
      list[m] = NOT;
      be = m;
      m = list[n];
    }

    n--;
    while (n != 1) {
      for (int i = 0; i < k - 1; i++) {
        be = m;
        m = list[m];
      }
      list[be] = list[m];
      list[m] = NOT;
      m = list[be];
      n--;
    }
    cout << m << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define NMAX 10000
#define NOT -1

int main() {

  int n, k, m;
  int be;
  int tmp;
  int list[NMAX + 1];

  while (1) {

    cin >> n >> k >> m;
    if (n == 0 && k == 0 && m == 0)
      break;

    for (int i = 1; i < n; i++) {
      list[i] = i + 1;
    }
    list[n] = 1;

    tmp = n;

    list[m - 1] = list[m];
    list[m] = NOT;
    m = list[m - 1];
    n--;
    while (n != 1) {
      for (int i = 0; i < k - 1; i++) {
        be = m;
        m = list[m];
      }
      list[be] = list[m];
      list[m] = NOT;
      m = list[be];
      n--;
    }
    for (int i = 0; i <= tmp; i++) {
      if (list[i] != NOT)
        cout << i << endl;
    }
  }

  return 0;
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k, m;
  while (1) {
    cin >> n >> k >> m;
    if (n + m + k == 0)
      break;
    vector<int> a(n);
    rep(i, 0, n) a[i] = i;
    int now = m - 1;
    rep(i, 0, n - 1) {
      // cout << now << ' ' << a[now] << endl;
      a.erase(a.begin() + now);
      now = (now + k - 1) % (int)(a.size());
    }
    cout << a[0] + 1 << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first;
#define se second;
using namespace std;
typedef pair<int, int> P;

signed main() {
  int n, m, k;
  while (1) {
    cin >> n >> k >> m;
    if (n + m + k == 0)
      break;
    vector<bool> f(n, true);
    f[m - 1] = false;
    int now = m - 1;
    rep(i, 0, n - 2) {
      int rest = n - 1 - i;
      int tmp = k % rest;
      if (tmp == 0)
        tmp = k;
      for (int j = 1;; j++) {
        int id = (now + j) % n;
        if (f[id]) {
          // cout << id + 1 << endl;
          tmp--;
        }
        if (tmp == 0) {
          now = id;
          // cout << now + 1 << endl;
          f[id] = false;
          break;
        }
      }
    }
    int ans = 0;
    rep(i, 0, n) if (f[i]) ans = i + 1;
    cout << ans << endl;
  }
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, k, m;
  while (cin >> n >> k >> m, (n || k || m)) {
    vector<int> stone;
    for (int i = 1; i <= n; i++)
      stone.push_back(i);

    m--;
    for (; stone.size() > 1;)
      stone.erase(stone.begin() + m), m = (m + k - 1) % stone.size();
    cout << stone[0] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, k, m;
  while (cin >> n >> k >> m, (n || k || m)) {
    vector<int> stone;
    for (int i = 1; i <= n; i++)
      stone.push_back(i);
    vector<int>::iterator it = stone.begin();
    for (int i = 1; i < m; i++, it++)
      ;
    stone.erase(it);
    int i = 1;
    for (; stone.size() > 1;) {
      if (it == stone.end())
        it = stone.begin();
      if (i % k == 0)
        it = stone.erase(it);
      else
        it++;
      i++;
    }
    cout << stone[0] << endl;
  }
  return 0;
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, k, m, e;
  vector<int> V;
  while (1) {
    cin >> n >> k >> m;
    if (n == 0 && k == 0 && m == 0)
      break;
    for (int i = 1; i <= n; i++)
      V.push_back(i);
    V.erase(V.begin() + m - 1);
    e = (m + k - 2 + V.size()) % V.size();
    while (V.size() != 1) {
      V.erase(V.begin() + e);
      e = (e + k - 1 + V.size()) % V.size();
    }
    cout << V[0] << endl;
    V.clear();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define N 10001
using namespace std;
int main() {
  int n, k, m, e, cnt, cnt1, data[N];
  while (1) {
    int flag = 0;
    cin >> n >> k >> m;
    if (n == 0 && k == 0 && m == 0)
      break;
    for (int i = 1; i <= n; i++)
      data[i] = 1;
    cnt1 = n - 2;
    if (m + 1 != n + 1)
      e = m;
    else
      e = 1, flag = 1;
    data[e] = 0;
    while (cnt1) {
      cnt = 0;
      while (1) {
        if (data[e] == 1)
          cnt++;
        if (cnt == k) {
          data[e] = 0;
          break;
        }
        e++;
        if (e == n + 1)
          e = 1;
      }
      cnt1--;
    }
    for (int i = 1; i <= n; i++) {
      if (data[i] != 0) {
        if (flag == 1)
          cout << i - 1 << endl;
        else
          cout << i << endl;
      }
    }
  }
  return 0;
}
Problem p00854 (C++) — buggy submission, verdict: 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 -= 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;
}

Error: TLE

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--;
    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;
}
Problem p00854 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
int f(int n, int k) {
  if (n == 1)
    return 0;
  return (f(n - 1, k) + k) % n;
}
int main() {
  int n, k, m;
  while (scanf("%d %d %d", &n, &k, &m), n) {
    printf("%d\\n", (m + f(n - 1, k)) % n + 1);
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
bool rm[10000];
int main() {
  int n, k, m;
  while (scanf("%d %d %d", &n, &k, &m), n) {
    memset(rm, 0, sizeof(rm));
    k--;
    m--;
    rm[m] = 1;
    int rest = n - 1;
    while (rest > 1) {
      int l = k % rest;
      for (int i = 0; i <= l; i++) {
        do
          m = (m + 1) % n;
        while (rm[m]);
      }
      rm[m] = 1;
      rest--;
    }
    for (int i = 0; i < n; i++) {
      if (!rm[i]) {
        printf("%d\\n", i + 1);
        break;
      }
    }
  }
}
Problem p00855 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

using namespace std;

typedef long long int lli;
const lli mx = 1299711;

int main() {
  lli n, s, t;
  bool a[mx];
  fill(a, a + mx, true);
  a[0] = a[1] = false;

  for (lli i = 2; i < mx; i++) {
    if (a[i]) {
      for (lli j = i + i; j < mx; j += i) {
        a[j] = false;
      }
    }
  }

  while (cin >> n && n) {
    for (lli i = n; i >= 2; i--) {
      if (a[i]) {
        s = i;
        break;
      }
    }
    for (lli i = n; i < mx; i++) {
      if (a[i]) {
        t = i;
        break;
      }
    }

    cout << (t - s) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int n, s, t;
  bool a[1000001];

  fill(a, a + 1000001, true);
  a[0] = a[1] = false;

  for (int i = 2; i <= 1000000; i++) {
    if (a[i]) {
      for (int j = 2 * i; j <= 1000000; j += i) {
        a[j] = false;
      }
    }
  }

  while (1) {
    cin >> n;
    if (n == 0) {
      break;
    }
    if (a[n]) {
      cout << 0 << endl;
      continue;
    }
    s = t = 0;

    for (int i = n - 1; i >= 0; i--) {
      if (a[i]) {
        s = i;
        break;
      }
    }

    for (int i = n + 1; i <= 1000000; i++) {
      if (a[i]) {
        t = i;
        break;
      }
    }

    cout << (t - s) << endl;
  }

  return 0;
}
Problem p00855 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  vector<bool> prime(1299709, 1);
  prime[1] = 0;
  for (int i = 2; i < prime.size(); i++) {
    if (prime[i] == 1) {
      for (int j = i * 2; j < prime.size(); j += i)
        prime[j] = 0;
    }
  }
  // for(int i=0;i<prime.size();i++)if(prime[i]==1)cout<<i<<" ";
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int ans = 0;
    for (int i = 0; i < prime.size(); i++) {
      if (prime[n + i] == 1)
        break;
      ans++;
    }
    for (int i = 0; i < prime.size(); i++) {
      if (prime[n - i] == 1)
        break;
      ans++;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  vector<bool> prime(1000001, 1);
  prime[1] = 0;
  for (int i = 2; i < prime.size(); i++) {
    if (prime[i] == 1) {
      for (int j = i * 2; j < prime.size(); j += i)
        prime[j] = 0;
    }
  }
  // for(int i=0;i<prime.size();i++)if(prime[i]==1)cout<<i<<" ";
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int ans = 0;
    for (int i = 0; i < prime.size(); i++) {
      if (prime[n + i] == 1)
        break;
      ans++;
    }
    for (int i = 0; i < prime.size(); i++) {
      if (prime[n - i] == 1)
        break;
      ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00855 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define MAX_N 1299709 + 1
using namespace std;

bool t[MAX_N];
int k;

bool isPrime(int x) {
  if (x == 2)
    return true;
  if (x < 2 || x % 2 == 0)
    return false;
  int i = 3;
  while (i * i <= x) {
    if (x % i == 0)
      return false;
    i += 2;
  }
  return true;
}

void table() {
  for (int i = 1; i < MAX_N; ++i) {
    if (isPrime(i))
      t[i] = 1;
  }
}

void solve() {
  int p = k, n = k;
  while (t[p] != 1)
    p--;
  while (t[n] != 1)
    n++;
  cout << n - p << endl;
}

int main() {
  table();
  while (cin >> k && k) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#define MAX_N 1299709
using namespace std;

bool t[MAX_N + 1];

bool isPrime(int x) {
  if (x == 2)
    return true;
  if (x < 2 || x % 2 == 0)
    return false;
  int i = 3;
  while (i * i <= x) {
    if (x % i == 0)
      return false;
    i += 2;
  }
  return true;
}

void table() {
  for (int i = 0; i < MAX_N; ++i) {
    if (isPrime(i))
      t[i] = 1;
  }
}

void solve(int k) {
  int p = k, n = k;
  while (t[p] == 0)
    p--;
  while (t[n] == 0)
    n++;
  cout << n - p << endl;
}

int main() {
  table();
  int k;
  while (cin >> k && k) {
    solve(k);
  }
  return 0;
}
Problem p00855 (C++) — buggy submission, verdict: Runtime Error

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

int main() {

  int n = 1299709 + 1;
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));

  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * 2; i <= n; i += p)
        prime[i] = false;
    }
  }

  int N;
  while (cin >> N && N != 0) {
    int ans = 0;
    if (N < 2 && N > 1299709) {
      ans = 0;
      cout << ans << '\\n';
      continue;
    }

    int l = N, r = N;
    while (!prime[l] || !prime[r]) {
      if (!prime[l])
        l--;
      if (!prime[r])
        r++;
    }
    ans = r - l;
    cout << ans << '\\n';
  }

  return 0;
}

Error: 0

Fixed submission:

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

int main() {

  int n = 1234567;
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));

  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * 2; i <= n; i += p)
        prime[i] = false;
    }
  }

  int N;
  while (cin >> N && N != 0) {
    int l = N, r = N;
    while (!prime[l] || !prime[r]) {
      if (!prime[l])
        l--;
      if (!prime[r])
        r++;
    }

    cout << r - l << '\\n';
  }

  return 0;
}
Problem p00855 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define N 1300000
using namespace std;
int start(int);
int goal(int);
int sosu[N];
int main() {
  int i, j, n, s, g;
  for (i = 0; i < N; i++)
    sosu[i] = 1;
  sosu[0] = 0;
  sosu[1] = 0;
  for (i = 0; i < N / 2; i++) {
    if (sosu[i] == 1) {
      for (j = i * 2; j < N; j += i) {
        sosu[j] = 0;
      }
    }
  }
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    if (sosu[n] != 1) {
      s = start(n);
      g = goal(n);
      cout << g - s << endl;
    } else
      cout << 0 << endl;
  }
  return 0;
}
int start(int n) {
  int i, result;
  if (n % 2 == 0)
    n--;
  else
    n -= 2;
  for (i = n; i >= 2; i -= 2) {
    if (sosu[i] == 1) {
      result = i;
      break;
    }
  }
  return result;
}
int goal(int n) {
  int i, result;
  if (n % 2 == 0)
    n++;
  else
    n += 2;
  for (i = n; i < N; i += 2) {
    if (sosu[i] == 1) {
      result = i;
      break;
    }
  }
  return result;
}

Error: 0

Fixed submission:

#include <iostream>
#define N 1200000
using namespace std;
int start(int);
int goal(int);
int sosu[N];
int main() {
  int i, j, n, s, g;
  for (i = 0; i < N; i++)
    sosu[i] = 1;
  sosu[0] = 0;
  sosu[1] = 0;
  for (i = 0; i < N / 2; i++) {
    if (sosu[i] == 1) {
      for (j = i * 2; j < N; j += i) {
        sosu[j] = 0;
      }
    }
  }
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    if (sosu[n] != 1) {
      s = start(n);
      g = goal(n);
      cout << g - s << endl;
    } else
      cout << 0 << endl;
  }
  return 0;
}
int start(int n) {
  int i, result;
  if (n % 2 == 0)
    n--;
  else
    n -= 2;
  for (i = n; i >= 2; i -= 2) {
    if (sosu[i] == 1) {
      result = i;
      break;
    }
  }
  return result;
}
int goal(int n) {
  int i, result;
  if (n % 2 == 0)
    n++;
  else
    n += 2;
  for (i = n; i < N; i += 2) {
    if (sosu[i] == 1) {
      result = i;
      break;
    }
  }
  return result;
}
Problem p00855 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int isPrime[2000001];
int main() {

  fill(isPrime, isPrime + 2000001, 1);
  isPrime[0] = isPrime[1] = 0;
  for (int i = 2; i * i < 2000001; i++) {
    if (isPrime[i])
      for (int j = i * 2; j < 2000001; j += i)
        isPrime[j] = 0;
  }
  int n;
  while (cin >> n, n) {
    int cnt = 0;
    int p, q;
    for (int i = n;; i++) {
      if (isPrime[i] == 1) {
        p = i;
        break;
      }
    }
    int f = 1;
    for (int i = n;; i--) {
      if (i < 0) {
        f = 0;
        break;
      }
      if (isPrime[i] == 1) {
        q = i;
        break;
      }
    }
    if (f == 0)
      cout << 0 << endl;
    else
      cout << p - q << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int isPrime[1000001];
  fill(isPrime, isPrime + 1000001, 1);
  isPrime[0] = isPrime[1] = 0;
  for (int i = 2; i * i < 1000001; i++) {
    if (isPrime[i])
      for (int j = i * 2; j < 1000001; j += i)
        isPrime[j] = 0;
  }
  int n;
  while (cin >> n, n) {
    int cnt = 0;
    int p, q;
    for (int i = n;; i++) {
      if (isPrime[i] == 1) {
        p = i;
        break;
      }
    }
    int f = 1;
    for (int i = n;; i--) {
      if (i < 0) {
        f = 0;
        break;
      }
      if (isPrime[i] == 1) {
        q = i;
        break;
      }
    }
    if (f == 0)
      cout << 0 << endl;
    else
      cout << p - q << endl;
  }
  return 0;
}
Problem p00855 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repp(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;

const int MAX_N = 2000000;
vector<int> prime;
bool is_prime[MAX_N + 1];
void sieve(int n) {
  rep(i, 0, n + 1) is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  rep(i, 2, n + 1) {
    if (is_prime[i]) {
      prime.push_back(i);
      for (int j = 2 * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
}

signed main() {
  sieve(MAX_N);
  int n;
  while (cin >> n, n) {
    if (is_prime[n]) {
      cout << 0 << endl;
      continue;
    }
    int ind = upper_bound(all(prime), n) - prime.begin();
    cout << prime[ind] - prime[ind - 1] << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repp(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;

const int MAX_N = 1300000;
vector<int> prime;
bool is_prime[MAX_N];
void sieve(int n) {
  rep(i, 0, n + 1) is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  rep(i, 2, n + 1) {
    if (is_prime[i]) {
      prime.push_back(i);
      for (int j = 2 * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
}

signed main() {
  sieve(MAX_N);
  int n;
  while (cin >> n, n) {
    if (is_prime[n]) {
      cout << 0 << endl;
      continue;
    }
    int ind = upper_bound(all(prime), n) - prime.begin();
    cout << prime[ind] - prime[ind - 1] << endl;
  }
}
Problem p00855 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

#define MAX 10000001

bool p[MAX];

int main() {
  memset(p, 1, sizeof(p));
  p[0] = p[1] = 0;
  for (int i = 0; i < MAX; i++) {
    if (p[i]) {
      for (int j = 2 * i; j < MAX; j += i) {
        p[j] = 0;
      }
    }
  }

  int n;
  while (cin >> n, n) {
    int next, prev;
    if (p[n] == 1)
      cout << 0 << endl;

    else {
      for (int i = n;; i++) {
        if (p[i] == 1) {
          next = i;
          break;
        }
      }
      for (int i = n;; i--) {
        if (p[i] == 1) {
          prev = i;
          break;
        }
      }
      cout << next - prev << endl;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

#define MAX 1000001

bool p[MAX];

int main() {
  memset(p, 1, sizeof(p));
  p[0] = p[1] = 0;
  for (int i = 0; i < MAX; i++) {
    if (p[i]) {
      for (int j = 2 * i; j < MAX; j += i) {
        p[j] = 0;
      }
    }
  }

  int n;
  while (cin >> n, n) {
    int next, prev;
    if (p[n] == 1)
      cout << 0 << endl;

    else {
      for (int i = n;; i++) {
        if (p[i] == 1) {
          next = i;
          break;
        }
      }
      for (int i = n;; i--) {
        if (p[i] == 1) {
          prev = i;
          break;
        }
      }
      cout << next - prev << endl;
    }
  }
  return 0;
}
Problem p00855 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
#define N 2000000
bool prime[N + 1];
int main() {
  for (int i = 2; i <= N; ++i)
    prime[i] = true;
  for (int i = 2; i <= N; ++i)
    if (prime[i])
      for (int j = 2; i * j <= N; ++j)
        prime[i * j] = false;

  int k;
  while (cin >> k, k) {
    int m = k, M = k;
    while (!prime[m])
      --m;
    while (!prime[M])
      ++M;
    cout << M - m << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
#define N 1000000
bool prime[N + 1];
int main() {
  for (int i = 2; i <= N; ++i)
    prime[i] = true;
  for (int i = 2; i <= N; ++i)
    if (prime[i])
      for (int j = 2; i * j <= N; ++j)
        prime[i * j] = false;

  int k;
  while (cin >> k, k) {
    int m = k, M = k;
    while (!prime[m])
      --m;
    while (!prime[M])
      ++M;
    cout << M - m << endl;
  }
  return 0;
}