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

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)

int sosu[1111111];
int dp[1211111];
int main() {

  sosu[0] = 1;
  sosu[1] = 1;

  for (int i = 2; i < 1111111; i++) {
    if (sosu[i] == 1)
      continue;
    for (int j = i + i; j < 1111111; j += i) {
      sosu[j] = 1;
    }
  }

  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      break;

    rep(i, 1111111) dp[i] = 0;
    dp[0] = 1;

    int item[33];
    rep(i, n) { cin >> item[i]; }

    int ans = -1;
    rep(i, m + 1) {
      if (dp[i] == 0)
        continue;
      if (sosu[i] == 0)
        ans = i;
      rep(j, n) { dp[i + item[j]] = 1; }
    }
    if (ans == -1) {
      puts("NA");
    } else {
      printf("%d\n", ans);
    }
  }
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)

int sosu[1111111];
int dp[1211111];
int main() {

  sosu[0] = 1;
  sosu[1] = 1;

  for (int i = 2; i < 1111111; i++) {
    if (sosu[i] == 1)
      continue;
    for (int j = i + i; j < 1111111; j += i) {
      sosu[j] = 1;
    }
  }

  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      break;

    rep(i, 1111111) dp[i] = 0;
    dp[0] = 1;

    int item[33];
    rep(i, n) { cin >> item[i]; }

    int ans = -1;
    rep(i, m + 1) {
      if (dp[i] == 0)
        continue;
      if (sosu[i] == 0)
        ans = i;
      rep(j, n) {
        if (i + item[j] > m)
          continue;
        dp[i + item[j]] = 1;
      }
    }
    if (ans == -1) {
      puts("NA");
    } else {
      printf("%d\n", ans);
    }
  }
}
Problem p00202 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
#define INF (1 << 30)
using namespace std;

const int kN = 1000005;

void primeNumber(bool prime[kN]) {
  rep(i, kN) prime[i] = 1;
  prime[0] = prime[1] = 0;
  rep(i, kN) {
    if (prime[i]) {
      for (int j = i + i; j < kN; j += i) {
        prime[j] = 0;
      }
    }
  }
}

void primeOfCuisine(int n, int a[40], bool prime[kN]) {
  prime[0] = 1;
  rep(i, n) {
    rep(j, kN) {
      if (prime[j] == 1) {
        prime[j + a[i]] = 1;
      }
    }
  }
}

void outputAns(bool a[kN], bool b[kN], int x) {
  for (int i = x; i >= 0; i--) {
    if (a[i] == 1 && b[i] == 1) {
      cout << i << endl;
      return;
    }
  }
  cout << "NA" << endl;
  return;
}

int main() {
  int n, x, a[40];
  while (cin >> n >> x, n || x) {
    bool is_prime[kN] = {0};
    bool is_prime_of_cuisine[kN] = {0};
    rep(i, n) cin >> a[i];
    primeNumber(is_prime);
    primeOfCuisine(n, a, is_prime_of_cuisine);
    outputAns(is_prime, is_prime_of_cuisine, x);
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
#define INF (1 << 30)
using namespace std;

const int kN = 1000005;

void primeNumber(bool prime[kN]) {
  rep(i, kN) prime[i] = 1;
  prime[0] = prime[1] = 0;
  rep(i, kN) {
    if (prime[i]) {
      for (int j = i + i; j < kN; j += i) {
        prime[j] = 0;
      }
    }
  }
}

void primeOfCuisine(int n, int a[40], bool prime[kN]) {
  prime[0] = 1;
  rep(i, n) {
    rep(j, kN) {
      if (prime[j] == 1) {
        if (j + a[i] >= kN)
          continue;
        prime[j + a[i]] = 1;
      }
    }
  }
}

void outputAns(bool a[kN], bool b[kN], int x) {
  for (int i = x; i >= 0; i--) {
    if (a[i] == 1 && b[i] == 1) {
      cout << i << endl;
      return;
    }
  }
  cout << "NA" << endl;
  return;
}

int main() {
  int n, x, a[40];
  while (cin >> n >> x, n || x) {
    bool is_prime[kN] = {0};
    bool is_prime_of_cuisine[kN] = {0};
    rep(i, n) cin >> a[i];
    primeNumber(is_prime);
    primeOfCuisine(n, a, is_prime_of_cuisine);
    outputAns(is_prime, is_prime_of_cuisine, x);
  }
}
Problem p00202 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int main() {
  int n, x, ord[35];
  bool prime[1000000] = {1, 1, 0};

  rep(i, 1000000) {
    if (!prime[i]) {
      for (int j = i + i; j < 1000000; j += i) {
        prime[j] = 1;
      }
    }
  }

  while (cin >> n >> x, n || x) {
    bool total[1000000] = {1};
    rep(i, n) { cin >> ord[i]; }
    rep(i, x) {
      if (total[i]) {
        rep(j, n) { total[i + ord[j]] = 1; }
      }
    }
    bool c = true;
    for (int i = x; i >= 0; i--) {
      if (prime[i] == 0 && total[i] == 1) {
        cout << i << endl;
        c = false;
        break;
      }
    }
    if (c)
      cout << "NA" << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int main() {
  int n, x, ord[35];
  bool prime[1000000] = {1, 1, 0};

  rep(i, 1000000) {
    if (!prime[i]) {
      for (int j = i + i; j < 1000000; j += i) {
        prime[j] = 1;
      }
    }
  }

  while (cin >> n >> x, n || x) {
    bool total[1000000] = {1};
    rep(i, n) { cin >> ord[i]; }
    rep(i, x) {
      if (total[i]) {
        rep(j, n) {
          if (i + ord[j] > x)
            continue;
          total[i + ord[j]] = 1;
        }
      }
    }
    bool c = true;
    for (int i = x; i >= 0; i--) {
      if (prime[i] == 0 && total[i] == 1) {
        cout << i << endl;
        c = false;
        break;
      }
    }
    if (c)
      cout << "NA" << endl;
  }
}
Problem p00202 (C++)

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

const int Max = 1000001;

int dishes[31];
bool dish[Max];
bool isPrime[Max];

int main() {

  for (int i = 2; i < Max; i++)
    isPrime[i] = true;
  for (int i = 2; i < std::sqrt(Max); i++) {
    if (isPrime[i]) {
      for (int j = i * 2; j < Max; j += i)
        isPrime[j] = false;
    }
  }

  int n, x;
  while (std::cin >> n, n) {

    std::cin >> x;
    memset(dish, 0, sizeof(dish));

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

    dish[0] = true;
    for (int i = 0; i < x; i++) {
      if (dish[i]) {
        for (int j = 0; j < n; j++) {
          dish[i + dishes[j]] = true;
        }
      }
    }

    for (int i = x; i >= 0; i--) {
      if (dish[i] && isPrime[i]) {
        std::cout << i << std::endl;
        break;
      }
      if (i == 0)
        std::cout << "NA" << std::endl;
    }
  }

  return 0;
}

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

const int Max = 1000001;

int dishes[31];
bool dish[Max];
bool isPrime[Max];

int main() {

  for (int i = 2; i < Max; i++)
    isPrime[i] = true;
  for (int i = 2; i < std::sqrt(Max); i++) {
    if (isPrime[i]) {
      for (int j = i * 2; j < Max; j += i)
        isPrime[j] = false;
    }
  }

  int n, x;
  while (std::cin >> n, n) {

    std::cin >> x;
    memset(dish, 0, sizeof(dish));

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

    dish[0] = true;
    for (int i = 0; i < x; i++) {
      if (dish[i]) {
        for (int j = 0; j < n; j++) {
          if (i + dishes[j] <= Max)
            dish[i + dishes[j]] = true;
        }
      }
    }

    for (int i = x; i >= 0; i--) {
      if (dish[i] && isPrime[i]) {
        std::cout << i << std::endl;
        break;
      }
      if (i == 0)
        std::cout << "NA" << std::endl;
    }
  }

  return 0;
}
Problem p00202 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <math.h>

#define VMAX 1000000
#define KMAX 30

using namespace std;

int K[KMAX];
int T[KMAX][VMAX];

void eratos(int n, bool prime[]) {
  for (int i = 0; i <= n; i++)
    prime[i] = false;
  for (int i = 3; i <= n; i += 2)
    prime[i] = true;
  prime[2] = true;
  int lim = (int)sqrt((double)n) + 1;
  for (int i = 3; i <= lim; i += 2) {
    if (!prime[i])
      continue;
    for (int j = i + i; j <= n; j += i)
      prime[j] = false;
  }
}

int main() {
  bool prime[VMAX + 1];
  eratos(VMAX, prime);

  int k, v;

  while (cin >> k >> v && !(k == 0 && v == 0)) {
    for (int i = 0; i < k; i++)
      cin >> K[i];

    for (int j = 0; j <= v; j++)
      T[0][j] = 0;
    for (int j = K[0]; j <= v; j += K[0])
      T[0][j] = 1;
    for (int i = 0; i < k; i++)
      T[i][0] = 1;

    for (int i = 1; i < k; i++) {
      for (int j = 1; j < K[i]; j++)
        T[i][j] = T[i - 1][j];
      for (int j = K[i]; j <= v; j++) {
        T[i][j] = (T[i - 1][j] | T[i][j - K[i]]);
      }
    }
    int ans = -1;
    for (int i = v; i >= 2; i--) {
      if (prime[i] && T[k - 1][i]) {
        ans = i;
        break;
      }
    }
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}

Fixed submission:
#include <iostream>
#include <math.h>

#define VMAX 1000000
#define KMAX 30

using namespace std;

int K[KMAX];
char T[KMAX][VMAX];

void eratos(int n, bool prime[]) {
  for (int i = 0; i <= n; i++)
    prime[i] = false;
  for (int i = 3; i <= n; i += 2)
    prime[i] = true;
  prime[2] = true;
  int lim = (int)sqrt((double)n) + 1;
  for (int i = 3; i <= lim; i += 2) {
    if (!prime[i])
      continue;
    for (int j = i + i; j <= n; j += i)
      prime[j] = false;
  }
}

int main() {
  bool prime[VMAX + 1];
  eratos(VMAX, prime);

  int k, v;

  while (cin >> k >> v && !(k == 0 && v == 0)) {
    for (int i = 0; i < k; i++)
      cin >> K[i];

    for (int j = 0; j <= v; j++)
      T[0][j] = 0;
    for (int j = K[0]; j <= v; j += K[0])
      T[0][j] = 1;
    for (int i = 0; i < k; i++)
      T[i][0] = 1;

    for (int i = 1; i < k; i++) {
      for (int j = 1; j < K[i]; j++)
        T[i][j] = T[i - 1][j];
      for (int j = K[i]; j <= v; j++) {
        T[i][j] = (T[i - 1][j] | T[i][j - K[i]]);
      }
    }
    int ans = -1;
    for (int i = v; i >= 2; i--) {
      if (prime[i] && T[k - 1][i]) {
        ans = i;
        break;
      }
    }
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}
Problem p00202 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, x;
int v[30], dp[1000001];
bool sosu[1000001];
int main() {
  for (int i = 2; i < 1000000; i++) {
    if (!sosu[i]) {
      for (int j = 2; j * i <= 1000000; j++) {
        sosu[i * j] = 1;
      }
    }
  }
  while (1) {
    cin >> n >> x;
    if (n == 0 && x == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> v[i];
    }
    memset(dp, 0, sizeof(dp));
    dp[0] = 1;

    for (int i = 0; i < n; i++)
      for (int j = 0; j <= x; j++)
        if (dp[j])
          dp[j + v[i]] = dp[j];
    for (int i = x; i >= 0; i--) {
      if (!sosu[i] && dp[i]) {
        if (i)
          cout << i << endl;
        else
          cout << "NA" << endl;
        break;
      }
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, x;
int v[30], dp[1000001];
bool sosu[1000001];
int main() {
  for (int i = 2; i < 1000000; i++) {
    if (!sosu[i]) {
      for (int j = 2; j * i <= 1000000; j++) {
        sosu[i * j] = 1;
      }
    }
  }
  while (1) {
    cin >> n >> x;
    if (n == 0 && x == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> v[i];
    }
    memset(dp, 0, sizeof(dp));
    dp[0] = 1;

    for (int i = 0; i < n; i++)
      for (int j = 0; j <= x - v[i]; j++)
        if (dp[j])
          dp[j + v[i]] = dp[j];
    for (int i = x; i >= 0; i--) {
      if (!sosu[i] && dp[i]) {
        if (i)
          cout << i << endl;
        else
          cout << "NA" << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p00202 (C++)

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

int n, x, d[31];
bool p[MAX + 1], dp[MAX + 1];

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

int main() {
  isPrime();
  while (cin >> n >> x, n || x) {
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < n; i++)
      cin >> d[i];

    dp[0] = true;
    for (int i = 0; i <= x; i++) {
      if (dp[i]) {
        for (int j = 0; j < n; j++) {
          if (i + d[i] >= x)
            continue;
          dp[i + d[j]] = true;
        }
      }
    }

    bool ju = true;
    for (int i = x; i > 0; i--) {
      if (p[i] && dp[i]) {
        cout << i << endl;
        ju = false;
        break;
      }
    }
    if (ju)
      cout << "NA" << endl;
  }
}

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

int n, x, d[31];
bool p[MAX + 1], dp[MAX + 1];

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

int main() {
  isPrime();
  while (cin >> n >> x, n || x) {
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < n; i++)
      cin >> d[i];

    dp[0] = true;
    for (int i = 0; i <= x; i++) {
      if (dp[i]) {
        for (int j = 0; j < n; j++) {
          if (i + d[j] > x)
            continue;
          dp[i + d[j]] = true;
        }
      }
    }

    bool ju = true;
    for (int i = x; i > 0; i--) {
      if (p[i] && dp[i]) {
        cout << i << endl;
        ju = false;
        break;
      }
    }
    if (ju)
      cout << "NA" << endl;
  }
}
Problem p00202 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <string.h>
#include <utility>
#include <vector>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i, n) for (i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

string str;

int main() {
  bool prime[1000005] = {}; // true
  int n, x, i, j, k;

  rep(i, 1000005) prime[i] = true;
  for (i = 2; i < 1000005; i++) {
    if (prime[i])
      for (j = 2; i * j < 1000005; j++)
        prime[i * j] = false;
  }

  while (1) {
    int price[30] = {};
    int cost[1000005] = {};
    cin >> n >> x;
    if (n == 0 && x == 0)
      break;
    rep(i, n) {
      cin >> price[i];
      cost[price[i]]++;
    }

    rep(i, x) if (cost[i]) rep(j, n) cost[i + price[j]]++;

    for (i = x; i > 0; i--)
      if (cost[i] > 0 && prime[i])
        break;

    if (i)
      cout << i << endl;
    else
      cout << "NA" << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <string.h>
#include <utility>
#include <vector>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i, n) for (i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

string str;

int main() {
  bool prime[1000005] = {}; // true
  int n, x, i, j, k;

  rep(i, 1000005) prime[i] = true;
  for (i = 2; i < 1000005; i++) {
    if (prime[i])
      for (j = 2; i * j < 1000005; j++)
        prime[i * j] = false;
  }

  while (1) {
    int price[30] = {};
    int cost[2000005] = {};
    cin >> n >> x;
    if (n == 0 && x == 0)
      break;
    rep(i, n) {
      cin >> price[i];
      cost[price[i]]++;
    }

    rep(i, x) if (cost[i]) rep(j, n) cost[i + price[j]]++;

    for (i = x; i > 0; i--)
      if (cost[i] > 0 && prime[i])
        break;

    if (i)
      cout << i << endl;
    else
      cout << "NA" << endl;
  }
}
Problem p00202 (C++)

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

#define pb push_back
#define MAX 1000000

int dp[MAX];

using namespace std;

int main(void) {
  int n, x;
  int i1, i2;

  while (1) {
    cin >> n >> x;
    if (n + x == 0)
      break;

    vector<int> v(n);
    memset(dp, 0, sizeof(dp));

    for (i1 = 0; i1 < n; i1++) {
      cin >> v[i1];
      dp[v[i1]] = 1;
    }

    for (i1 = 0; i1 <= x; i1++) {
      if (dp[i1] == 1) {
        for (i2 = 0; i2 < n; i2++) {
          dp[i1 + v[i2]] = 1;
        }
      }
    }

    for (i1 = x; i1 > 0; i1--) {
      if (dp[i1] == 1) {

        for (i2 = 2; i2 * i2 <= i1; i2++) {
          if (i1 % i2 == 0)
            break;
        }
        if (i2 * i2 > i1)
          break;
      }
    }

    if (i1 == 0)
      cout << "NA" << endl;
    else
      cout << i1 << endl;
  }

  return 0;
}

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

#define pb push_back
#define MAX 1000000

int dp[MAX];

using namespace std;

int main(void) {
  int n, x;
  int i1, i2;

  while (1) {
    cin >> n >> x;
    if (n + x == 0)
      break;

    vector<int> v(n);
    memset(dp, 0, sizeof(dp));

    for (i1 = 0; i1 < n; i1++) {
      cin >> v[i1];
      dp[v[i1]] = 1;
    }

    for (i1 = 0; i1 <= x; i1++) {
      if (dp[i1] == 1) {
        for (i2 = 0; i2 < n; i2++) {
          if ((i1 + v[i2]) < MAX)
            dp[i1 + v[i2]] = 1;
        }
      }
    }

    for (i1 = x; i1 > 0; i1--) {
      if (dp[i1] == 1) {

        for (i2 = 2; i2 * i2 <= i1; i2++) {
          if (i1 % i2 == 0)
            break;
        }
        if (i2 * i2 > i1)
          break;
      }
    }

    if (i1 == 0)
      cout << "NA" << endl;
    else
      cout << i1 << endl;
  }

  return 0;
}
Problem p00202 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
bool dp[1000120];
bool notPrime[1000120];
int main() {
  notPrime[0] = notPrime[1] = true;
  for (int i = 2; i * i < 1000010; i++) {
    for (int j = 2 * i; j < 1000010; j += i) {
      notPrime[j] = true;
    }
  }
  int n, x;
  while (cin >> n >> x, n) {
    dp[0] = true;
    while (n--) {
      int a;
      cin >> a;
      for (int i = 0; i <= x; i++) {
        if (dp[i])
          dp[i + a] = true;
      }
    }
    bool flg = false;
    for (int i = x; i >= 2; i--) {
      if (!notPrime[i] & dp[i]) {
        cout << i << endl;
        flg = true;
        break;
      }
    }
    if (!flg)
      cout << "NA" << endl;
    for (int i = 0; i < 1000020; i++)
      dp[i] = false;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
bool dp[1000120];
bool notPrime[1000120];
int main() {
  notPrime[0] = notPrime[1] = true;
  for (int i = 2; i * i < 1000010; i++) {
    for (int j = 2 * i; j < 1000010; j += i) {
      notPrime[j] = true;
    }
  }
  int n, x;
  while (cin >> n >> x, n) {
    dp[0] = true;
    while (n--) {
      int a;
      cin >> a;
      for (int i = 0; i <= x; i++) {
        if (dp[i] && i + a <= 1000010)
          dp[i + a] = true;
      }
    }
    bool flg = false;
    for (int i = x; i >= 2; i--) {
      if (!notPrime[i] & dp[i]) {
        cout << i << endl;
        flg = true;
        break;
      }
    }
    if (!flg)
      cout << "NA" << endl;
    for (int i = 0; i < 1000020; i++)
      dp[i] = false;
  }
}
Problem p00202 (C++)

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

constexpr int MAX_N = 31, MAX_C = 1000001;
int n, max;
int cost[MAX_N];
bool dp[MAX_C];
bool prime_num[MAX_C];

int main() {
  while (1) {
    std::cin >> n >> max;
    if (n + max == 0)
      break;
    std::memset(prime_num, false, sizeof(prime_num));
    std::memset(dp, false, sizeof(dp));
    for (int i = 0; i < n; ++i)
      std::cin >> cost[i];
    for (int i = 2; i * i <= max; ++i) {
      if (!prime_num[i]) {
        for (int j = 2; i * j <= max; ++j)
          prime_num[i * j] = true;
      }
    }
    int ans = 0;
    for (int i = 0; i < n; ++i)
      dp[cost[i]] = true;

    for (int i = 1; i <= MAX_C; ++i) {
      for (int j = 0; j < n; ++j) {
        if (dp[i])
          dp[i + cost[j]] = true;
      }
    }

    for (int i = max; i >= 0; --i) {
      if (dp[i] && !prime_num[i]) {
        ans = i;
        break;
      }
    }
    if (!ans)
      std::cout << "NA" << std::endl;
    else
      std::cout << ans << std::endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>

constexpr int MAX_N = 31, MAX_C = 1000001;
int n, max;
int cost[MAX_N];
bool dp[MAX_C];
bool prime_num[MAX_C];

int main() {
  while (1) {
    std::cin >> n >> max;
    if (n + max == 0)
      break;
    std::memset(prime_num, false, sizeof(prime_num));
    std::memset(dp, false, sizeof(dp));
    for (int i = 0; i < n; ++i)
      std::cin >> cost[i];
    for (int i = 2; i * i <= max; ++i) {
      if (!prime_num[i]) {
        for (int j = 2; i * j <= max; ++j)
          prime_num[i * j] = true;
      }
    }
    int ans = 0;
    for (int i = 0; i < n; ++i)
      dp[cost[i]] = true;

    for (int i = 1; i <= MAX_C; ++i) {
      for (int j = 0; j < n; ++j) {
        if (i + cost[j] <= max && dp[i])
          dp[i + cost[j]] = true;
      }
    }

    for (int i = max; i >= 0; --i) {
      if (dp[i] && !prime_num[i]) {
        ans = i;
        break;
      }
    }
    if (!ans)
      std::cout << "NA" << std::endl;
    else
      std::cout << ans << std::endl;
  }
  return 0;
}
Problem p00203 (C++)

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

int main() {
  int W, H, mas[16][16];
  while (cin >> W >> H, W) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> mas[i][j];
      }
    }
    vector<vector<int>> dp(H + 1, vector<int>(W, 0));
    for (int i = 0; i < W; i++) {
      if (mas[0][i] != 1)
        dp[0][i] = 1;
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (dp[i][j] == 0)
          continue;
        if (mas[i][j] == 0) {
          for (int k = -1; k <= 1; k++) {
            if (j + k < 0 || j + k >= W)
              continue;
            if ((i == H - 1 && k == 0) ||
                (i != H - 1 && k == 0 && mas[i + 1][j + k] == 2) ||
                (i != H - 1 && mas[i + 1][j + k] == 0)) {
              dp[i + 1][j + k] += dp[i][j];
            }
          }
        } else if (mas[i][j] == 2) {
          if (i >= H - 2 || mas[i + 2][j] != 1) {
            dp[max(H, i + 2)][j] += dp[i][j];
          }
        }
      }
    }

    int ret = 0;
    for (int i = 0; i < W; i++) {
      ret += dp[H][i];
    }
    cout << ret << endl;
  }
}

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

int main() {
  int W, H, mas[16][16];
  while (cin >> W >> H, W) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> mas[i][j];
      }
    }
    vector<vector<int>> dp(H + 1, vector<int>(W, 0));
    for (int i = 0; i < W; i++) {
      if (mas[0][i] != 1)
        dp[0][i] = 1;
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (dp[i][j] == 0)
          continue;
        if (mas[i][j] == 0) {
          for (int k = -1; k <= 1; k++) {
            if (j + k < 0 || j + k >= W)
              continue;
            if ((i == H - 1 && k == 0) ||
                (i != H - 1 && k == 0 && mas[i + 1][j + k] == 2) ||
                (i != H - 1 && mas[i + 1][j + k] == 0)) {
              dp[i + 1][j + k] += dp[i][j];
            }
          }
        } else if (mas[i][j] == 2) {
          if (i >= H - 2 || mas[i + 2][j] != 1) {
            dp[min(H, i + 2)][j] += dp[i][j];
          }
        }
      }
    }

    int ret = 0;
    for (int i = 0; i < W; i++) {
      ret += dp[H][i];
    }
    cout << ret << endl;
  }
}
Problem p00203 (C++)

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

int main() {
  while (true) {
    int X, Y;
    scanf("%d%d", &X, &Y);
    int field[16][17] = {{0}};
    for (int y = 1; y <= Y; y++) {
      for (int x = 1; x <= X; x++) {
        scanf("%d", &(field[y][x]));
      }
    }
    int dp[16][17] = {{0}};
    for (int x = 1; x <= X; x++) {
      if (field[1][x] != 1) {
        dp[1][x] = 1;
      }
    }
    for (int y = 2; y <= Y; y++) {
      for (int x = 1; x <= X; x++) {
        if (field[y][x] == 1) {
          continue;
        }
        int sum = 0;
        if (field[y][x] == 0) {
          if (field[y - 1][x - 1] == 0) {
            sum += dp[y - 1][x - 1];
          }
          if (field[y - 1][x + 1] == 0) {
            sum += dp[y - 1][x + 1];
          }
        }
        if (field[y - 1][x] == 0) {
          sum += dp[y - 1][x];
        }
        if (field[y - 2][x] == 2) {
          sum += dp[y - 2][x];
        }
        dp[y][x] = sum;
      }
    }
    int answer = 0;
    for (int x = 1; x <= X; x++) {
      if (field[Y - 1][x] == 2) {
        answer += dp[Y - 1][x];
      }
      answer += dp[Y][x];
    }
    printf("%d\n", answer);
  }
  return 0;
}

Fixed submission:
#include <cstdio>

int main() {
  while (true) {
    int X, Y;
    scanf("%d%d", &X, &Y);
    if (X == 0 || Y == 0) {
      break;
    }
    int field[16][17] = {{0}};
    for (int y = 1; y <= Y; y++) {
      for (int x = 1; x <= X; x++) {
        scanf("%d", &(field[y][x]));
      }
    }
    int dp[16][17] = {{0}};
    for (int x = 1; x <= X; x++) {
      if (field[1][x] != 1) {
        dp[1][x] = 1;
      }
    }
    for (int y = 2; y <= Y; y++) {
      for (int x = 1; x <= X; x++) {
        if (field[y][x] == 1) {
          continue;
        }
        int sum = 0;
        if (field[y][x] == 0) {
          if (field[y - 1][x - 1] == 0) {
            sum += dp[y - 1][x - 1];
          }
          if (field[y - 1][x + 1] == 0) {
            sum += dp[y - 1][x + 1];
          }
        }
        if (field[y - 1][x] == 0) {
          sum += dp[y - 1][x];
        }
        if (field[y - 2][x] == 2) {
          sum += dp[y - 2][x];
        }
        dp[y][x] = sum;
      }
    }
    int answer = 0;
    for (int x = 1; x <= X; x++) {
      if (field[Y - 1][x] == 2) {
        answer += dp[Y - 1][x];
      }
      answer += dp[Y][x];
    }
    printf("%d\n", answer);
  }
  return 0;
}
Problem p00205 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
int main(void) {
  int a[5] = {0};
  int guu = 0, tyoki = 0, paa = 0;
  int n = 1;
  int i = 0;
  while (n != 0) {
    for (i = 0; i < 5; i++) {
      scanf("%d", &a[i]);

      if (a[i] == 0) {
        break;
      } else if (a[i] == 1) {
        guu = 1;
      } else if (a[i] == 2) {
        tyoki = 1;
      } else if (a[i] == 3) {
        paa = 1;
      }
    }
    for (i = 0; i < 5; i++) {
      if (guu == 1 && tyoki == 1 && paa == 1) {
        printf("3\n");
      } else if (guu == 1 && tyoki == 1 && paa == 0) {
        if (a[i] == 1) {
          printf("1\n");
        } else {
          printf("2\n");
        }
      } else if (guu == 1 && tyoki == 0 && paa == 0) {
        printf("3\n");
      } else if (guu == 1 && tyoki == 0 && paa == 1) {
        if (a[i] == 1) {
          printf("2\n");
        } else {
          printf("1\n");
        }
      } else if (tyoki == 1 && paa == 1) {
        if (a[i] == 2) {
          printf("1\n");
        } else {
          printf("2\n");
        }
      } else if (tyoki == 1 && paa == 0) {
        printf("3\n");
      } else if (paa == 1) {
        printf("3\n");
      }
      a[i] = 0;
    }
    guu = 0, tyoki = 0, paa = 0;
  }
}

Fixed submission:
#include <stdio.h>
int main(void) {
  int a[5] = {0};
  int guu = 0, tyoki = 0, paa = 0;
  int n = 1;
  int i = 0;
  while (n != 0) {
    for (i = 0; i < 5; i++) {
      scanf("%d", &a[i]);

      if (a[i] == 0) {
        break;
      } else if (a[i] == 1) {
        guu = 1;
      } else if (a[i] == 2) {
        tyoki = 1;
      } else if (a[i] == 3) {
        paa = 1;
      }
    }
    if (a[i] == 0) {
      break;
    }
    for (i = 0; i < 5; i++) {
      if (guu == 1 && tyoki == 1 && paa == 1) {
        printf("3\n");
      } else if (guu == 1 && tyoki == 1 && paa == 0) {
        if (a[i] == 1) {
          printf("1\n");
        } else {
          printf("2\n");
        }
      } else if (guu == 1 && tyoki == 0 && paa == 0) {
        printf("3\n");
      } else if (guu == 1 && tyoki == 0 && paa == 1) {
        if (a[i] == 1) {
          printf("2\n");
        } else {
          printf("1\n");
        }
      } else if (tyoki == 1 && paa == 1) {
        if (a[i] == 2) {
          printf("1\n");
        } else {
          printf("2\n");
        }
      } else if (tyoki == 1 && paa == 0) {
        printf("3\n");
      } else if (paa == 1) {
        printf("3\n");
      }
      a[i] = 0;
    }
    guu = 0, tyoki = 0, paa = 0;
  }
}
Problem p00205 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  while (true) {
    int cur = 0, d[5];
    rep(i, 5) cin >> d[i], d[i]--, cur |= (1 << d[i]);
    if (__builtin_popcount(cur) == 2) {
      rep(i, 5) cout << 2 - !!count(d, d + 5, (d[i] + 1) % 3) << endl;
    } else {
      rep(i, 5) cout << 3 << endl;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  while (true) {
    int cur = 0, d[5];
    rep(i, 5) {
      if (cin >> d[i])
        d[i]--, cur |= (1 << d[i]);
      else
        return 0;
    }
    if (__builtin_popcount(cur) == 2) {
      rep(i, 5) cout << 2 - !!count(d, d + 5, (d[i] + 1) % 3) << endl;
    } else {
      rep(i, 5) cout << 3 << endl;
    }
  }
}
Problem p00205 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
int main() {
  while (1) {
    int s[6], g[4] = {0}, n = -2, v = 0, i;
    for (i = 5; i; i--) {
      std::cin >> s[i];
      if (!*s)
        goto l;
      if (!g[s[i]]) {
        n++;
        g[s[i]] = 1;
        if (v - 1 == s[i] % 3 || !v)
          v = s[i];
      }
    }
    for (i = 5; i; i--)
      std::cout << (!n ? (v == s[i] ? 1 : 2) : 3) << "\n";
  }
l:;
  return 0;
}

Fixed submission:
#include <iostream>
int main() {
  while (1) {
    int s[6], g[4] = {0}, n = -2, v = 0, i;
    for (i = 5; i; i--) {
      std::cin >> s[i];
      if (!s[5])
        goto l;
      if (!g[s[i]]) {
        n++;
        g[s[i]] = 1;
        if (v - 1 == s[i] % 3 || !v)
          v = s[i];
      }
    }
    for (i = 5; i; i--)
      std::cout << (!n ? (v == s[i] ? 1 : 2) : 3) << "\n";
  }
l:;
  return 0;
}
Problem p00207 (C++)

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

int Board[102][102]; // Board[j, x, w][i, y, h]
bool isOK;           // 迷路がOKかNGか
int xs, ys, xg, yg;  // StartとGoalの座標

void DFS(int c, int x, int y) {
  if (Board[x][y] != c) {
    return;
  } else if (x == xg && y == yg) {
    isOK = true; // Goalなら迷路はOK
    return;
  }

  Board[x][y] = 0;

  DFS(c, x + 1, y);
  DFS(c, x, y + 1);
  DFS(c, x - 1, y);
  DFS(c, x, y - 1);
}

int main() {
  int w, h;
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    isOK = false;                   // 初期値をNGに
    for (int i = 0; i < 102; i++) { // Boardの初期化
      for (int j = 0; j < 102; j++)
        Board[j][i] = 0;
    }
    cin >> xs >> ys >> xg >> yg;

    int n, c, d, x, y;
    cin >> n;
    for (int t = 0; t < n; t++) {
      cin >> c >> d >> x >> y;
      int wb = 2 + (!d * 2); // d=0なら横長
      int hb = 2 + (d * 2);  // d=1なら縦長
      for (int i = 0; i < hb; i++) {
        for (int j = 0; j < wb; j++)
          Board[x + j][y + i] = c;
      }
    }

    DFS(Board[xs][ys], xs, ys);

    if (isOK)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }

  return 0;
}


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

int Board[102][102]; // Board[j, x, w][i, y, h]
bool isOK;           // 迷路がOKかNGか
int xs, ys, xg, yg;  // StartとGoalの座標

void DFS(int c, int x, int y) {
  if (Board[x][y] != c) {
    return;
  } else if (x == xg && y == yg) {
    isOK = true; // Goalなら迷路はOK
    return;
  }

  Board[x][y] = 0;

  DFS(c, x + 1, y);
  DFS(c, x, y + 1);
  DFS(c, x - 1, y);
  DFS(c, x, y - 1);
}

int main() {
  int w, h;
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    isOK = false;                   // 初期値をNGに
    for (int i = 0; i < 102; i++) { // Boardの初期化
      for (int j = 0; j < 102; j++)
        Board[j][i] = -1;
    }
    cin >> xs >> ys >> xg >> yg;

    int n, c, d, x, y;
    cin >> n;
    for (int t = 0; t < n; t++) {
      cin >> c >> d >> x >> y;
      int wb = 2 + (!d * 2); // d=0なら横長
      int hb = 2 + (d * 2);  // d=1なら縦長
      for (int i = 0; i < hb; i++) {
        for (int j = 0; j < wb; j++)
          Board[x + j][y + i] = c;
      }
    }

    DFS(Board[xs][ys], xs, ys);

    if (isOK)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }

  return 0;
}
Problem p00207 (C++)

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

int F[102][102];

void DFS(int Y, int X, int A) {
  if (F[Y][X] != A)
    return;

  F[Y][X] = 7;

  DFS(Y - 1, X, A);
  DFS(Y, X + 1, A);
  DFS(Y + 1, X, A);
  DFS(Y, X - 1, A);
}

int main() {
  int w, h;
  int xs, ys;
  int xg, yg;
  int n;
  int c, d, x, y;

  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    cin >> xs >> ys;
    cin >> xg >> yg;
    cin >> n;

    for (int i = 0; i < n; i++) {
      cin >> c >> d >> x >> y;
      if (d == 0) {
        for (int a = 0; a < 4; a++) {
          for (int b = 0; b < 2; b++) {
            F[y + b][x + a] = c;
          }
        }

      } else {
        for (int a = 0; a < 2; a++) {
          for (int b = 0; b < 4; b++) {
            F[y + b][x + a] = c;
          }
        }
      }
    }

    if (F[ys][xs] == 0 || F[ys][xs] == 1 || F[ys][xs] == 2 || F[ys][xs] == 3 ||
        F[ys][xs] == 4 || F[ys][xs] == 5) {
      DFS(ys, xs, F[ys][xs]);
    }

    if (F[yg][xg] == F[ys][xs]) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }

    for (int y = 1; y <= h; y++) {
      for (int x = 1; x <= w; x++) {
        F[y][x] = 0;
      }
    }
  }

  return 0;
}

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

int F[102][102];

void DFS(int Y, int X, int A) {
  if (F[Y][X] != A)
    return;

  F[Y][X] = 7;

  DFS(Y - 1, X, A);
  DFS(Y, X + 1, A);
  DFS(Y + 1, X, A);
  DFS(Y, X - 1, A);
}

int main() {
  int w, h;
  int xs, ys;
  int xg, yg;
  int n;
  int c, d, x, y;

  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    cin >> xs >> ys;
    cin >> xg >> yg;
    cin >> n;

    for (int i = 0; i < n; i++) {
      cin >> c >> d >> x >> y;
      if (d == 0) {
        for (int a = 0; a < 4; a++) {
          for (int b = 0; b < 2; b++) {
            F[y + b][x + a] = c;
          }
        }

      } else {
        for (int a = 0; a < 2; a++) {
          for (int b = 0; b < 4; b++) {
            F[y + b][x + a] = c;
          }
        }
      }
    }

    if (F[ys][xs] == 0 || F[ys][xs] == 1 || F[ys][xs] == 2 || F[ys][xs] == 3 ||
        F[ys][xs] == 4 || F[ys][xs] == 5) {
      DFS(ys, xs, F[ys][xs]);
    }

    if (F[yg][xg] == F[ys][xs]) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }

    /*for (int y = 0; y < h; y++)
        {
          for (int x = 0; x < w; x++)
            {
              cout << F[y][x];
            }
         cout << endl;
        }*/

    /*for (int y = 1; y <= h; y++)
        {
          for (int x = 1; x <= w; x++)
            {
              F[y][x] = 0;
            }
        }*/
  }

  return 0;
}
Problem p00207 (C++)

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

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

int w, h, xs, ys, xg, yg, n, cc;
int field[100][100];
bool flag;

void dfs(int x, int y, int c) {
  if (flag)
    return;
  if (x == xg && y == yg) {
    flag = true;
    return;
  }
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx >= 0 && nx < w && ny >= 0 && ny < h && field[nx][ny] == c) {
      field[nx][ny] = 0;
      dfs(nx, ny, c);
      field[nx][ny] = c;
    }
  }
}

int main() {
  while (1) {
    flag = false;
    cin >> w >> h;
    if (!w)
      break;
    for (int i = 0; i < w; i++) {
      for (int j = 0; j < h; j++)
        field[i][j] = 0;
    }
    cin >> xs >> ys >> xg >> yg >> n;
    xs--;
    ys--;
    xg--;
    yg--;
    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      if (d == 1) {
        for (int k = 0; k < 2; k++) {
          for (int j = 0; j < 4; j++)
            field[x + k - 1][y + j - 1] = c;
        }
      } else {
        for (int k = 0; k < 4; k++) {
          for (int j = 0; j < 2; j++)
            field[x + k - 1][y + j - 1] = c;
        }
      }
    }
    if (field[xs][ys] == 0) {
      cout << "NG" << endl;
      continue;
    }
    cc = field[xs][ys];
    field[xs][ys] = 0;
    dfs(xs, ys, cc);
    if (flag)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}

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

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

int w, h, xs, ys, xg, yg, n, cc;
int field[100][100];
bool flag;

void dfs(int x, int y, int c) {
  if (flag)
    return;
  if (x == xg && y == yg) {
    flag = true;
    return;
  }
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx >= 0 && nx < w && ny >= 0 && ny < h && field[nx][ny] == c) {
      field[nx][ny] = 0;
      dfs(nx, ny, c);
    }
  }
}

int main() {
  while (1) {
    flag = false;
    cin >> w >> h;
    if (!w)
      break;
    for (int i = 0; i < w; i++) {
      for (int j = 0; j < h; j++)
        field[i][j] = 0;
    }
    cin >> xs >> ys >> xg >> yg >> n;
    xs--;
    ys--;
    xg--;
    yg--;
    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      if (d == 1) {
        for (int k = 0; k < 2; k++) {
          for (int j = 0; j < 4; j++)
            field[x + k - 1][y + j - 1] = c;
        }
      } else {
        for (int k = 0; k < 4; k++) {
          for (int j = 0; j < 2; j++)
            field[x + k - 1][y + j - 1] = c;
        }
      }
    }
    if (field[xs][ys] == 0) {
      cout << "NG" << endl;
      continue;
    }
    cc = field[xs][ys];
    field[xs][ys] = 0;
    dfs(xs, ys, cc);
    if (flag)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}
Problem p00207 (C++)

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

int F[102][102];

void DFS(int Y, int X, int c) {
  if (F[Y][X] != c) {
    return;
  }

  F[Y][X] = -1;
  DFS(Y - 1, X, c);
  DFS(Y, X + 1, c);
  DFS(Y + 1, X, c);
  DFS(Y, X - 1, c);
}

int main() {
  int w, h, xs, ys, xg, yg, n;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    for (int i = 0; i < 20; i++) {
      for (int j = 0; j < 20; j++) {
        F[i][j] = -1;
      }
    }

    cin >> xs >> ys >> xg >> yg >> n;

    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      if (d == 0) {
        for (int j = 0; j < 4; j++) {
          F[x + j][y] = c;
          F[x + j][y + 1] = c;
        }
      } else {
        for (int j = 0; j < 4; j++) {
          F[x][y + j] = c;
          F[x + 1][y + j] = c;
        }
      }
    }

    if (F[xs][ys] == 0) {
      cout << "NG" << endl;
    } else {
      DFS(xs, ys, F[xs][ys]);

      if (F[xg][yg] == -1) {
        cout << "OK" << endl;
      } else {
        cout << "NG" << endl;
      }
    }
  }
}


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

int F[102][102];

void DFS(int Y, int X, int c) {
  if (F[Y][X] != c) {
    return;
  }

  F[Y][X] = -1;
  DFS(Y - 1, X, c);
  DFS(Y, X + 1, c);
  DFS(Y + 1, X, c);
  DFS(Y, X - 1, c);
}

int main() {
  int w, h, xs, ys, xg, yg, n;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    for (int i = 0; i < 102; i++) {
      for (int j = 0; j < 102; j++) {
        F[i][j] = 0;
      }
    }

    cin >> xs >> ys >> xg >> yg >> n;

    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      if (d == 0) {
        for (int j = 0; j < 4; j++) {
          F[x + j][y] = c;
          F[x + j][y + 1] = c;
        }
      } else {
        for (int j = 0; j < 4; j++) {
          F[x][y + j] = c;
          F[x + 1][y + j] = c;
        }
      }
    }

    if (F[xs][ys] == 0) {
      cout << "NG" << endl;
    } else {
      DFS(xs, ys, F[xs][ys]);

      if (F[xg][yg] == -1) {
        cout << "OK" << endl;
      } else {
        cout << "NG" << endl;
      }
    }
  }
}
Problem p00207 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, k) for (int i = j; i < k; i++)
int w, h, xs, ys, xg, yg, n;
int m[102][102];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool DFS(int x, int y, int c) {
  if (m[x][y] != c)
    return false;
  if (x == xg && y == yg)
    return true;
  m[x][y] = -1;
  bool ans = false;
  rep(i, 0, 4) { ans = max(ans, DFS(x + dx[i], y + dy[i], c)); }
  return ans;
}
int main() {
  cin >> w >> h;
  while (w != 0) {
    cin >> xs >> ys >> xg >> yg >> n;
    xs++;
    ys++;
    xg++;
    yg++;
    rep(i, 0, 102) rep(j, 0, 102) m[i][j] = -1;
    rep(i, 0, n) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      x++;
      y++;
      if (d == 0) {
        rep(j, x, x + 4) rep(k, y, y + 2) m[j][k] = c;
      } else {
        rep(j, x, x + 2) rep(k, y, y + 4) m[j][k] = c;
      }
    }
    string ans[2] = {"NG", "OK"};
    cout << ans[DFS(xs, ys, m[xs][ys])] << endl;
    cin >> w >> h;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, k) for (int i = j; i < k; i++)
int w, h, xs, ys, xg, yg, n;
int m[102][102];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool DFS(int x, int y, int c) {
  if (m[x][y] != c)
    return false;
  if (x == xg && y == yg)
    return true;
  m[x][y] = -1;
  bool ans = false;
  rep(i, 0, 4) { ans = max(ans, DFS(x + dx[i], y + dy[i], c)); }
  return ans;
}
int main() {
  cin >> w >> h;
  while (w != 0) {
    cin >> xs >> ys >> xg >> yg >> n;
    xs++;
    ys++;
    xg++;
    yg++;
    rep(i, 0, 102) rep(j, 0, 102) m[i][j] = -1;
    rep(i, 0, n) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      x++;
      y++;
      if (d == 0) {
        rep(j, x, x + 4) rep(k, y, y + 2) m[j][k] = c;
      } else {
        rep(j, x, x + 2) rep(k, y, y + 4) m[j][k] = c;
      }
    }
    string ans[2] = {"NG", "OK"};
    if (m[xs][ys] == -1)
      cout << "NG" << endl;
    else
      cout << ans[DFS(xs, ys, m[xs][ys])] << endl;
    cin >> w >> h;
  }
  return 0;
}
Problem p00207 (C++)

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

int F[102][102];
int xg, yg, w, h;

void DFS(int Y, int X, int c) {
  // if(Y < 1 || X < 1)
  // return;
  if (F[Y][X] != c) {
    return;
  }

  F[Y][X] = -1;
  DFS(Y + 1, X, c);
  DFS(Y - 1, X, c);
  DFS(Y, X + 1, c);
  DFS(Y, X - 1, c);
}

int main() {
  int xs, ys, n;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < 102; i++) {
      for (int j = 0; j < 102; j++) {
        F[i][j] = 0;
      }
    }

    cin >> xs >> ys >> xg >> yg >> n;

    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      if (c == 0)
        c = 1919810;
      if (d == 0) {
        for (int j = 0; j < 4; j++) {
          F[x + j][y] = c;
          F[x + j][y + 1] = c;
        }
      } else {
        for (int j = 0; j < 4; j++) {
          F[x][y + j] = c;
          F[x + 1][y + j] = c;
        }
      }
    }
    DFS(xs, ys, F[xs][ys]);

    if (F[xg][yg] == -1) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
}


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

int F[102][102];
int xg, yg, w, h;

void DFS(int Y, int X, int c) {
  // if(Y < 1 || X < 1)
  // return;
  if (F[Y][X] != c) {
    return;
  }

  F[Y][X] = -1;
  DFS(Y + 1, X, c);
  DFS(Y - 1, X, c);
  DFS(Y, X + 1, c);
  DFS(Y, X - 1, c);
}

int main() {
  int xs, ys, n;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < 102; i++) {
      for (int j = 0; j < 102; j++) {
        F[i][j] = 10;
      }
    }

    cin >> xs >> ys >> xg >> yg >> n;

    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      if (c == 0)
        c = 1919810;
      if (d == 0) {
        for (int j = 0; j < 4; j++) {
          F[x + j][y] = c;
          F[x + j][y + 1] = c;
        }
      } else {
        for (int j = 0; j < 4; j++) {
          F[x][y + j] = c;
          F[x + 1][y + j] = c;
        }
      }
    }
    DFS(xs, ys, F[xs][ys]);

    if (F[xg][yg] == -1) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
}
Problem p00207 (C++)

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

int F[105][105];
int xg, yg, w, h;

void DFS(int Y, int X, int c) {
  if (F[Y][X] != c || Y > w || X > h) {
    return;
  }

  F[Y][X] = -1;
  DFS(Y + 1, X, c);
  DFS(Y - 1, X, c);
  DFS(Y, X + 1, c);
  DFS(Y, X - 1, c);
}

int main() {
  int xs, ys, n;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < 105; i++) {
      for (int j = 0; j < 105; j++) {
        F[i][j] = 0;
      }
    }

    cin >> xs >> ys >> xg >> yg >> n;

    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;

      if (d == 0) {
        for (int j = 0; j < 4; j++) {
          F[x + j][y] = c;
          F[x + j][y + 1] = c;
        }
      } else {
        for (int j = 0; j < 4; j++) {
          F[x][y + j] = c;
          F[x + 1][y + j] = c;
        }
      }
    }
    DFS(xs, ys, F[xs][ys]);

    if (F[xg][yg] == -1) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
}


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

int F[105][105];
int xg, yg, w, h;

void DFS(int Y, int X, int c) {
  if (Y < 1 || X < 1)
    return;
  if (F[Y][X] != c || Y > w || X > h) {
    return;
  }

  F[Y][X] = -1;
  DFS(Y + 1, X, c);
  DFS(Y - 1, X, c);
  DFS(Y, X + 1, c);
  DFS(Y, X - 1, c);
}

int main() {
  int xs, ys, n;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < 105; i++) {
      for (int j = 0; j < 105; j++) {
        F[i][j] = 0;
      }
    }

    cin >> xs >> ys >> xg >> yg >> n;

    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;

      if (d == 0) {
        for (int j = 0; j < 4; j++) {
          F[x + j][y] = c;
          F[x + j][y + 1] = c;
        }
      } else {
        for (int j = 0; j < 4; j++) {
          F[x][y + j] = c;
          F[x + 1][y + j] = c;
        }
      }
    }
    DFS(xs, ys, F[xs][ys]);

    if (F[xg][yg] == -1) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
}