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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;

int main() {
  int ans[100000000], n;
  while (cin >> n, n) {
    vector<int> vec;
    rep(i, (int)((n - 1) / 2) + 1) ans[i] = 0;
    REP(i, 1, n) vec.push_back((i * i) % n);
    sort(vec.begin(), vec.end());
    vec.erase(unique(vec.begin(), vec.end()), vec.end());
    rep(i, vec.size()) {
      rep(j, vec.size()) {
        int diff = vec[i] - vec[j];
        if (diff < 0)
          diff += n;
        if (diff > (n - 1) / 2)
          diff = n - diff;
        if (1 <= diff && diff <= (n - 1) / 2)
          ans[diff]++;
      }
    }
    REP(i, 1, (int)((n - 1) / 2) + 1) cout << ans[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;

int main() {
  int ans[1000000], n;
  while (cin >> n, n) {
    vector<int> vec;
    rep(i, (int)((n - 1) / 2) + 1) ans[i] = 0;
    REP(i, 1, n) vec.push_back((i * i) % n);
    sort(vec.begin(), vec.end());
    vec.erase(unique(vec.begin(), vec.end()), vec.end());
    rep(i, vec.size()) {
      rep(j, vec.size()) {
        int diff = vec[i] - vec[j];
        if (diff < 0)
          diff += n;
        if (diff > (n - 1) / 2)
          diff = n - diff;
        if (1 <= diff && diff <= (n - 1) / 2)
          ans[diff]++;
      }
    }
    REP(i, 1, (int)((n - 1) / 2) + 1) cout << ans[i] << endl;
  }
  return 0;
}
Problem p00142 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main() {

  int n;
  while (cin >> n && n != 0) {
    bool isAppeared[10001];
    int data[10001];
    fill(isAppeared, isAppeared + 10001, false);
    fill(data, data + 10001, 0);
    // memset(isAppeared,0,sizeof(isAppeared));
    // memset(data,0,sizeof(data));
    for (int i = 1; i < n; i++) {
      int r = (i * i) % n;
      isAppeared[r] = true;
    }
    for (int i = 0; i < 10001; i++) {
      for (int j = 0; j < 10001; j++) {
        if (isAppeared[i] && isAppeared[j]) {
          int ret = i - j;
          if (ret < 0)
            ret += n;
          if (((n - 1) / 2) < ret) {
            ret = n - ret;
          }
          data[ret]++;
        }
      }
    }
    for (int i = 1; i <= ((n - 1) / 2); i++) {
      if (data[i] != 0)
        cout << data[i] << endl;
    }
  }

  return 0;
}

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

using namespace std;

int main() {

  int n;
  while (cin >> n && n != 0) {
    bool isAppeared[10001];
    int data[10001];
    fill(isAppeared, isAppeared + 10001, false);
    fill(data, data + 10001, 0);
    // memset(isAppeared,0,sizeof(isAppeared));
    // memset(data,0,sizeof(data));
    for (int i = 1; i < n; i++) {
      int r = (i * i) % n;
      isAppeared[r] = true;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (isAppeared[i] && isAppeared[j]) {
          int ret = i - j;
          if (ret < 0)
            ret += n;
          if (((n - 1) / 2) < ret) {
            ret = n - ret;
          }
          data[ret]++;
        }
      }
    }
    for (int i = 1; i <= ((n - 1) / 2); i++) {
      if (data[i] != 0)
        cout << data[i] << endl;
    }
  }

  return 0;
}
Problem p00142 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"

int main() {
  while (1) {
    int N;
    cin >> N;
    set<int> aset;
    for (int i = 1; i < N; ++i) {
      int k = (i * i) % N;
      aset.emplace(k);
    }
    vector<int> nums(N);
    for (auto it = aset.begin(); it != aset.end(); ++it) {
      for (auto jt = next(it); jt != aset.end(); ++jt) {
        int sa = *it - *jt;
        if (sa < 0)
          sa += N;
        if (sa > (N - 1) / 2)
          sa = N - sa;
        nums[sa]++;
      }
    }
    for (int i = 1; i <= (N - 1) / 2; ++i) {
      cout << nums[i] * 2 << endl;
    }
  }
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"

int main() {
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    set<int> aset;
    for (int i = 1; i < N; ++i) {
      int k = (i * i) % N;
      aset.emplace(k);
    }
    vector<int> nums(N);
    for (auto it = aset.begin(); it != aset.end(); ++it) {
      for (auto jt = next(it); jt != aset.end(); ++jt) {
        int sa = *it - *jt;
        if (sa < 0)
          sa += N;
        if (sa > (N - 1) / 2)
          sa = N - sa;
        nums[sa]++;
      }
    }
    for (int i = 1; i <= (N - 1) / 2; ++i) {
      cout << nums[i] * 2 << endl;
    }
  }
  return 0;
}
Problem p00142 (C++)

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

int main() {
  int n, arr[5000];
  std::vector<int> v;
  while (std::cin >> n, n) {
    std::fill(arr, arr + 5000, 0);
    v.clear();

    for (int i = 1; i <= (n - 1) / 2; i++) {
      if (std::find(v.begin(), v.end(), i * i % n) == v.end()) { // まだない
        v.push_back(i * i % n);
      }
    }

    for (int i = 0; i < v.size() - 1; i++) {
      for (int j = i + 1; j < v.size(); j++) {
        int value = v[i] - v[j];
        if (value < 0) {
          value += n;
        }
        if (value > (n - 1) / 2) {
          value = n - value;
        }
        arr[value]++;
      }
    }

    for (int i = 1; i <= (n - 1) / 2; i++) {
      std::cout << arr[i] * 2 << std::endl;
    }
  }
}

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

int main() {
  int n, arr[5000];
  std::vector<int> v;
  while (std::cin >> n, n) {
    std::fill(arr, arr + 5000, 0);
    v.clear();

    for (int i = 1; i <= (n - 1) / 2; i++) {
      if (std::find(v.begin(), v.end(), i * i % n) == v.end()) { // まだない
        v.push_back(i * i % n);
      }
    }

    for (int i = 0; i < v.size(); i++) {
      for (int j = i + 1; j < v.size(); j++) {
        int value = v[i] - v[j];
        if (value < 0) {
          value += n;
        }
        if (value > (n - 1) / 2) {
          value = n - value;
        }
        arr[value]++;
      }
    }

    for (int i = 1; i <= (n - 1) / 2; i++) {
      std::cout << arr[i] * 2 << std::endl;
    }
  }
}
Problem p00143 (C++)

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

using namespace std;

double square_calc(double x1, double y1, double x2, double y2, double x3,
                   double y3) {
  double square = 0;
  double a = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
  double b = sqrt((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1));
  double c = sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));
  double s = (a + b + c) / 2;
  return (sqrt(s * (s - a) * (s - b) * (s - c)));
}

void solve() {
  int n;
  while (cin >> n, n) {
    for (int i = 0; i < n; ++i) {
      double xp1, yp1, xp2, yp2, xp3, yp3, xk, yk, xs, ys;
      cin >> xp1 >> yp1 >> xp2 >> yp2 >> xp3 >> yp3 >> xk >> yk >> xs >> ys;
      double square = square_calc(xp1, yp1, xp2, yp2, xp3, yp3);
      double square1 = square_calc(xk, yk, xp1, yp1, xp2, yp2) +
                       square_calc(xk, yk, xp1, yp1, xp3, yp3) +
                       square_calc(xk, yk, xp2, yp2, xp3, yp3);
      double square2 = square_calc(xs, ys, xp1, yp1, xp2, yp2) +
                       square_calc(xs, ys, xp1, yp1, xp3, yp3) +
                       square_calc(xs, ys, xp2, yp2, xp3, yp3);
      if (fabs(square - square1) <= 1e-6 && fabs(square - square2) >= 1e-6 ||
          fabs(square - square1) >= 1e-6 && fabs(square - square2) <= 1e-6) {
        cout << "OK" << endl;
      } else {
        cout << "NG" << endl;
      }
    }
  }
}

int main() {
  solve();
  return (0);
}

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

using namespace std;

double square_calc(double x1, double y1, double x2, double y2, double x3,
                   double y3) {
  double square = 0;
  double a = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
  double b = sqrt((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1));
  double c = sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));
  double s = (a + b + c) / 2;
  return (sqrt(s * (s - a) * (s - b) * (s - c)));
}

void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    double xp1, yp1, xp2, yp2, xp3, yp3, xk, yk, xs, ys;
    cin >> xp1 >> yp1 >> xp2 >> yp2 >> xp3 >> yp3 >> xk >> yk >> xs >> ys;
    double square = square_calc(xp1, yp1, xp2, yp2, xp3, yp3);
    double square1 = square_calc(xk, yk, xp1, yp1, xp2, yp2) +
                     square_calc(xk, yk, xp1, yp1, xp3, yp3) +
                     square_calc(xk, yk, xp2, yp2, xp3, yp3);
    double square2 = square_calc(xs, ys, xp1, yp1, xp2, yp2) +
                     square_calc(xs, ys, xp1, yp1, xp3, yp3) +
                     square_calc(xs, ys, xp2, yp2, xp3, yp3);
    if (fabs(square - square1) <= 1e-6 && fabs(square - square2) >= 1e-6 ||
        fabs(square - square1) >= 1e-6 && fabs(square - square2) <= 1e-6) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
}

int main() {
  solve();
  return (0);
}
Problem p00146 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <vector>
using namespace std;
#define PB push_back
const int N = 1e2 + 10;
const int MAXN = 1 << 16;
const double INF = 1 << 30;
int abs(int a) { return a > 0 ? a : -a; }
int main() {
  int n, s[N], d[N], v[N], from[MAXN][N], val[MAXN], all, now = 0, top;
  vector<int> ans;
  double dp[MAXN][N], temp;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d%d%d", &s[i], &d[i], &v[i]);
  all = 1 << n;
  for (int i = 0; i < all; i++) {
    val[i] = 0;
    for (int j = 0; j < n; j++) {
      dp[i][j] = INF;
      if (i & (1 << j))
        val[i] += v[j];
    }
    val[i] *= 20;
  }
  for (int i = 0; i < n; i++) {
    from[1 << i][i] = -1;
    dp[1 << i][i] = ((double)70 / 2000) * d[i];
  }
  for (int i = 0; i < all; i++)
    for (int j = 0; j < n; j++)
      if (i & (1 << j)) {
        for (int k = 0; k < n; k++)
          if (k != j && i & (1 << k)) {
            temp = dp[i ^ (1 << j)][k] +
                   ((double)(70 + val[i ^ (1 << j)]) / 2000) * abs(d[j] - d[k]);
            if (dp[i][j] > temp) {
              from[i][j] = k;
              dp[i][j] = temp;
            }
          }
      }
  all--;
  top = dp[all][0];
  for (int i = 1; i < n; i++)
    if (dp[all][i] < top) {
      top = dp[all][i];
      now = i;
    }
  while (all > 0) {
    ans.PB(s[now]);
    top = all;
    all -= 1 << now;
    now = from[top][now];
  }
  printf("%d", ans.back());
  for (int i = static_cast<int>(ans.size() - 2); i >= 0; i--)
    printf(" %d", ans[i]);
  printf("\n");
}


Fixed submission:
#include <stdio.h>
#include <vector>
using namespace std;
#define PB push_back
const int N = 1e1 + 10;
const int MAXN = 1 << 16;
const double INF = 1 << 30;
int abs(int a) { return a > 0 ? a : -a; }
int main() {
  int n, s[N], d[N], v[N], from[MAXN][N], val[MAXN], all, now = 0, top;
  vector<int> ans;
  double dp[MAXN][N], temp;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d%d%d", &s[i], &d[i], &v[i]);
  all = 1 << n;
  for (int i = 0; i < all; i++) {
    val[i] = 0;
    for (int j = 0; j < n; j++) {
      dp[i][j] = INF;
      if (i & (1 << j))
        val[i] += v[j];
    }
    val[i] *= 20;
  }
  for (int i = 0; i < n; i++) {
    from[1 << i][i] = -1;
    dp[1 << i][i] = ((double)70 / 2000) * d[i];
  }
  for (int i = 0; i < all; i++)
    for (int j = 0; j < n; j++)
      if (i & (1 << j)) {
        for (int k = 0; k < n; k++)
          if (k != j && i & (1 << k)) {
            temp = dp[i ^ (1 << j)][k] +
                   ((double)(70 + val[i ^ (1 << j)]) / 2000) * abs(d[j] - d[k]);
            if (dp[i][j] > temp) {
              from[i][j] = k;
              dp[i][j] = temp;
            }
          }
      }
  all--;
  top = dp[all][0];
  for (int i = 1; i < n; i++)
    if (dp[all][i] < top) {
      top = dp[all][i];
      now = i;
    }
  while (all > 0) {
    ans.PB(s[now]);
    top = all;
    all -= 1 << now;
    now = from[top][now];
  }
  printf("%d", ans.back());
  for (int i = static_cast<int>(ans.size() - 2); i >= 0; i--)
    printf(" %d", ans[i]);
  printf("\n");
}
Problem p00147 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  int seat[17] = {0};
  int X = 0;
  int stop[100] = {0};
  int time = 0;
  queue<int> Q;
  while (X < 100) {
    int x;
    if (time % 5 == 0 && time < 500) {
      Q.push(time / 5);
    }
    while (1) {
      if (Q.size())
        x = Q.front();
      else
        x = -1;
      bool f = false;

      if (~x) {
        int people = (x % 5 == 1 ? 5 : 2);
        int use = 19 + 3 * (x % 3) + 17 * (x % 2);
        rep(i, 17 - people + 1) {
          bool flag = true;
          rep(j, people) { flag &= seat[i + j] == 0; }
          if (flag) {
            rep(j, people) { seat[i + j] = use; }
            f = true;
            break;
          }
        }
      }
      if (f) {
        stop[x] = time - x * 5;
        Q.pop();
        X++;
      }
      if (!f)
        break;
    }
    rep(i, 17) { seat[i] = max(0, seat[i] - 1); }
    time++;
  }
  int n, c = 0;

  while (cin >> n) {
    if (n < 50)
      c++;
    cout << stop[n] << endl;
  }
  int a = 0;
  while (c >= 2) {
    cout << a / a << endl;
  };
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  int seat[17] = {0};
  int X = 0;
  int stop[100] = {0};
  int time = 0;
  queue<int> Q;
  while (X < 100) {
    int x;
    if (time % 5 == 0 && time < 500) {
      Q.push(time / 5);
    }
    while (1) {
      if (Q.size())
        x = Q.front();
      else
        x = -1;
      bool f = false;

      if (~x) {
        int people = (x % 5 == 1 ? 5 : 2);
        int use = 19 + 3 * (x % 3) + 17 * (x % 2);
        rep(i, 17 - people + 1) {
          bool flag = true;
          rep(j, people) { flag &= seat[i + j] == 0; }
          if (flag) {
            rep(j, people) { seat[i + j] = use; }
            f = true;
            break;
          }
        }
      }
      if (f) {
        stop[x] = time - x * 5;
        Q.pop();
        X++;
      }
      if (!f)
        break;
    }
    rep(i, 17) { seat[i] = max(0, seat[i] - 1); }
    time++;
  }
  int n, c = 0;

  while (cin >> n) {
    if (n <= 5)
      c++;
    cout << stop[n] << endl;
  }
  int a = 0;
  while (c >= 2) {
    cout << a / a << endl;
  };
}
Problem p00147 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  int seat[17] = {0};
  int X = 0;
  int stop[100] = {0};
  int time = 0;
  queue<int> Q;
  while (X < 100) {
    int x;
    if (time % 5 == 0 && time < 500) {
      Q.push(time / 5);
    }
    while (1) {
      if (Q.size())
        x = Q.front();
      else
        x = -1;
      bool f = false;

      if (~x) {
        int people = (x % 5 == 1 ? 5 : 2);
        int use = 19 + 3 * (x % 3) + 17 * (x % 2);
        rep(i, 17 - people + 1) {
          bool flag = true;
          rep(j, people) { flag &= seat[i + j] == 0; }
          if (flag) {
            rep(j, people) { seat[i + j] = use; }
            f = true;
            break;
          }
        }
      }
      if (f) {
        stop[x] = time - x * 5;
        Q.pop();
        X++;
      }
      if (!f)
        break;
    }
    rep(i, 17) { seat[i] = max(0, seat[i] - 1); }
    time++;
  }
  int n, c = 0;
  while (cin >> n) {
    cout << stop[n] << endl;
    c++;
  }
  while (c >= 9) {
  };
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  int seat[17] = {0};
  int X = 0;
  int stop[100] = {0};
  int time = 0;
  queue<int> Q;
  while (X < 100) {
    int x;
    if (time % 5 == 0 && time < 500) {
      Q.push(time / 5);
    }
    while (1) {
      if (Q.size())
        x = Q.front();
      else
        x = -1;
      bool f = false;

      if (~x) {
        int people = (x % 5 == 1 ? 5 : 2);
        int use = 19 + 3 * (x % 3) + 17 * (x % 2);
        rep(i, 17 - people + 1) {
          bool flag = true;
          rep(j, people) { flag &= seat[i + j] == 0; }
          if (flag) {
            rep(j, people) { seat[i + j] = use; }
            f = true;
            break;
          }
        }
      }
      if (f) {
        stop[x] = time - x * 5;
        Q.pop();
        X++;
      }
      if (!f)
        break;
    }
    rep(i, 17) { seat[i] = max(0, seat[i] - 1); }
    time++;
  }
  int n, c = 0;
  while (cin >> n) {
    cout << stop[n] << endl;
    c++;
  }
  while (c < 9) {
  };
}
Problem p00148 (C++)

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

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

    int num = (n - 1) % 39 + 1;
    if (num < 10)
      cout << "3C0" << num << endl;
    else
      cout << "3C" << num << endl;
  }
  return 0;
}

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

int main() {
  int n;
  while (cin >> n) {
    int num = (n - 1) % 39 + 1;
    if (num < 10)
      cout << "3C0" << num << endl;
    else
      cout << "3C" << num << endl;
  }
  return 0;
}
Problem p00148 (C++)

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

using namespace std;

int main() {
  int n;

  for (;;) {
    cin >> n;
    if (n == 0)
      break;
    int ans = n % 39;
    if (n % 39 == 0)
      ans = 39;
    printf("3C%02d\n", ans);
  }
}

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

using namespace std;

int main() {
  int n;

  for (; cin >> n;) {
    int ans = n % 39;
    if (n % 39 == 0)
      ans = 39;
    printf("3C%02d\n", ans);
  }
}
Problem p00148 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
int main(void) {
  int a, b;
  while (scanf("%d", &a) != EOF) {
    while (1) {
      b = a - 39;
      if (a <= 39)
        break;
    }
    printf("3C%02d\n", a);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
int main(void) {
  int a, b;
  while (scanf("%d", &a) != EOF) {
    b = a % 39;
    if (b == 0)
      b = 39;
    printf("3C%02d\n", b);
  }
  return 0;
}
Problem p00148 (C++)

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

int main(void) {
  int n;
  while (cin >> n, n) {
    printf("3C%02d\n", n % 39 == 0 ? 39 : n % 39);
  }
  return 0;
}

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

int main(void) {
  int n;
  while (scanf("%d", &n) != EOF) {
    printf("3C%02d\n", n % 39 == 0 ? 39 : n % 39);
  }
  return 0;
}
Problem p00148 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
using namespace std;
#define rep2(x, from, to) for (int x = (from); x < (to); ++(x))
#define rep(x, to) rep2(x, 0, to)
int main() {
  int n;
  while (scanf("%d", &n)) {
    printf("3C%02d\n", n % 39 == 0 ? 39 : n % 39);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
using namespace std;
#define rep2(x, from, to) for (int x = (from); x < (to); ++(x))
#define rep(x, to) rep2(x, 0, to)
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    printf("3C%02d\n", n % 39 == 0 ? 39 : n % 39);
  }
  return 0;
}
Problem p00149 (C++)

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

using namespace std;

int main(int argc, char *argv[]) {
  double lhs, rhs;
  int al = 0;
  int ar = 0;
  int bl = 0;
  int br = 0;
  int cl = 0;
  int cr = 0;
  int dl = 0;
  int dr = 0;
  while (cin >> lhs >> rhs, lhs || rhs) {
    if (lhs >= 1.1)
      al++;
    if (rhs >= 1.1)
      ar++;
    if (1.1 > lhs && lhs >= 0.6)
      bl++;
    if (1.1 > rhs && rhs >= 0.6)
      br++;
    if (0.6 > lhs && lhs >= 0.2)
      cl++;
    if (0.6 > rhs && rhs >= 0.2)
      cr++;
    if (0.2 > lhs && lhs >= 0.1)
      dl++;
    if (0.2 > rhs && rhs >= 0.1)
      dr++;
  }
  cout << al << " " << ar << endl;
  cout << bl << " " << br << endl;
  cout << cl << " " << cr << endl;
  cout << dl << " " << dr << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  double lhs, rhs;
  int al = 0;
  int ar = 0;
  int bl = 0;
  int br = 0;
  int cl = 0;
  int cr = 0;
  int dl = 0;
  int dr = 0;
  while (cin >> lhs >> rhs) {
    if (lhs >= 1.1)
      al++;
    if (rhs >= 1.1)
      ar++;
    if (1.1 > lhs && lhs >= 0.6)
      bl++;
    if (1.1 > rhs && rhs >= 0.6)
      br++;
    if (0.6 > lhs && lhs >= 0.2)
      cl++;
    if (0.6 > rhs && rhs >= 0.2)
      cr++;
    if (0.2 > lhs && lhs >= 0.1)
      dl++;
    if (0.2 > rhs && rhs >= 0.1)
      dr++;
  }
  cout << al << " " << ar << endl;
  cout << bl << " " << br << endl;
  cout << cl << " " << cr << endl;
  cout << dl << " " << dr << endl;
  return 0;
}
Problem p00149 (C++)

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

int main() {
  int LA, LB, LC, LD, RA, RB, RC, RD;
  LA = LB = LC = LD = RA = RB = RC = RD = 0;
  while (1) {
    double a, b;
    cin >> a >> b;
    if (cin.fail())
      ;
    if (a < 0.2)
      LD++;
    else if (a < 0.6)
      LC++;
    else if (a < 1.1)
      LB++;
    else
      LA++;
    if (b < 0.2)
      RD++;
    else if (b < 0.6)
      RC++;
    else if (b < 1.1)
      RB++;
    else
      RA++;
  }
  cout << LA << " " << RA << endl
       << LB << " " << RB << endl
       << LC << " " << RC << endl
       << LD << " " << RD << endl;
}

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

int main() {
  int LA, LB, LC, LD, RA, RB, RC, RD;
  LA = LB = LC = LD = RA = RB = RC = RD = 0;
  while (1) {
    double a, b;
    cin >> a >> b;
    if (cin.fail())
      break;
    if (a < 0.2)
      LD++;
    else if (a < 0.6)
      LC++;
    else if (a < 1.1)
      LB++;
    else
      LA++;
    if (b < 0.2)
      RD++;
    else if (b < 0.6)
      RC++;
    else if (b < 1.1)
      RB++;
    else
      RA++;
  }
  cout << LA << " " << RA << endl
       << LB << " " << RB << endl
       << LC << " " << RC << endl
       << LD << " " << RD << endl;
}
Problem p00150 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
const int MAX_V = 10000; // 100???????????????????f???????¢\????°???¬?????
int prime[MAX_V + 1];
int main() {
  int k, i, max, min, n; // 1????????f???????C0????????f????????????????????¢
  for (i = 2; i <= MAX_V; i++) {
    prime[i] = 1; // 2???????£????f????????????°??????¨
  }
  for (i = 2; i * i <= MAX_V; i++) {
    if (prime[i]) {
      for (k = 2 * i; k <= MAX_V; k += i) {
        prime[k] =
            0; // ???f??????2???{???????£????{?????????f????????????????????¢
      }
    }
  }
  while (scanf("%d", &n)) {
    if (n == 0)
      break;
    max = 0;
    min = 0;
    for (i = n; i > 0; i++) {
      if (prime[i] && prime[i - 2]) {
        max = i;
        min = max - 2;
        // printf("%d %d\n",i-2,i);
        break;
      }
    }
    printf("%d %d\n", min, max);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
const int MAX_V = 10000; // 100???????????????????f???????¢\????°???¬?????
int prime[MAX_V + 1];
int main() {
  int k, i, max, min, n; // 1????????f???????C0????????f????????????????????¢
  for (i = 2; i <= MAX_V; i++) {
    prime[i] = 1; // 2???????£????f????????????°??????¨
  }
  for (i = 2; i * i <= MAX_V; i++) {
    if (prime[i]) {
      for (k = 2 * i; k <= MAX_V; k += i) {
        prime[k] =
            0; // ???f??????2???{???????£????{?????????f????????????????????¢
      }
    }
  }
  while (scanf("%d", &n)) {
    if (n == 0)
      break;
    max = 0;
    min = 0;
    for (i = n; i > 0; i--) {
      if (prime[i] && prime[i - 2]) {
        max = i;
        min = max - 2;
        // printf("%d %d\n",i-2,i);
        break;
      }
    }
    printf("%d %d\n", min, max);
  }
  return 0;
}
Problem p00150 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "stdio.h"
const int MAX_V = 10000; // 1????????§????´???°??¨?????????
int prime[MAX_V + 1];    // 1????´???°,0????´???°??§?????????
int main() {
  int i, k, n;
  for (i = 2; i <= MAX_V; i++) {
    prime[i] = 1; // 2??\???????´???°??¨??????
  }
  for (i = 2; i * i <= MAX_V; i++) {
    if (prime[i]) {
      for (k = 2 * i; k <= MAX_V; k += i) {
        prime[k] = 0; // ?´???°???2?????\???????????°????´???°??§?????????
      }
    }
  }
  while (scanf("%d", &n) != 0) {
    int p1 = 0, q1 = 0;
    for (i = n; 3 <= i; i--) {
      if (prime[i] == 1 && prime[i - 2] == 1) { //??????????´???°
        q1 = i;
        p1 = i - 2;
        break;
      }
    }
    printf("%d %d\n", p1, q1); // for?????????????????????
  }
  return 0;
}
//????¨??????¨????????????????????????

Fixed submission:
#include "stdio.h"
const int MAX_V = 10000; // 1????????§????´???°??¨?????????
int prime[MAX_V + 1];    // 1????´???°,0????´???°??§?????????
int main() {
  int i, k, n;
  for (i = 2; i <= MAX_V; i++) {
    prime[i] = 1; // 2??\???????´???°??¨??????
  }
  for (i = 2; i * i <= MAX_V; i++) {
    if (prime[i]) {
      for (k = 2 * i; k <= MAX_V; k += i) {
        prime[k] = 0; // ?´???°???2?????\???????????°????´???°??§?????????
      }
    }
  }
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    //        while(scanf("%d", &n) != 0) {
    int p1 = 0, q1 = 0;
    for (i = n; 3 <= i; i--) {
      if (prime[i] == 1 && prime[i - 2] == 1) { //??????????´???°
        q1 = i;
        p1 = i - 2;
        break;
      }
    }
    printf("%d %d\n", p1, q1); // for?????????????????????
  }
  return 0;
}
//????¨??????¨????????????????????????
Problem p00150 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "stdio.h"
// #include "time.h"
//?????§??????????´???°?????????????????????
//  5<= n <= 10000
// p1=?°????????´???°p2=??§???????´???°

int isprime(int n) {
  int i;
  for (i = 2; i < n;
       i++) { //??????????§????????????¨??§??¶??°??§??????????´???°??¨??????
    if (n % i == 0)
      return 0; //????????????????????§?´???°??§?????????
  }
  return 1;
}
int main() {
  int i, n, p1, q1;
  //     clock_t start, now;//??£?¨?
  while (1) {
    scanf("%d", &n);
    //   start = clock(); // ????§???????
    if (n == 0)
      break;
    for (i = n; 3 <= i; i--) {
      if (isprime(i) == 1 && isprime(i - 2) == 1)
        break;
    }
    p1 = i - 2;
    q1 = i;
    // now = clock(); // ????????????
    // printf("%f\n", (double)(now - start) / CLOCKS_PER_SEC);//??¬???
    printf("%d %d\n", p1, q1);
  }
  return 0;
}

Fixed submission:
#include "stdio.h"
// #include "time.h"
//?????§??????????´???°?????????????????????
//  5<= n <= 10000
// p1=?°????????´???°p2=??§???????´???°

int isprime(int n) {
  int i;
  for (i = 2; i * i <= n;
       i++) { //??????????§????????????¨??§??¶??°??§??????????´???°??¨??????
    if (n % i == 0)
      return 0; //????????????????????§?´???°??§?????????
  }
  return 1;
}
int main() {
  int i, n, p1, q1;
  //     clock_t start, now;//??£?¨?
  while (1) {
    scanf("%d", &n);
    //   start = clock(); // ????§???????
    if (n == 0)
      break;
    for (i = n; 3 <= i; i--) {
      if (isprime(i) == 1 && isprime(i - 2) == 1)
        break;
    }
    p1 = i - 2;
    q1 = i;
    // now = clock(); // ????????????
    // printf("%f\n", (double)(now - start) / CLOCKS_PER_SEC);//??¬???
    printf("%d %d\n", p1, q1);
  }
  return 0;
}
Problem p00150 (C++)

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

bool isPrime(int n) {
  for (int i = 2; i < n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

int main(void) {
  int n;
  while (scanf("%d", &n), n) {

    for (int i = n; 5 <= i; i--) {
      // cout << i << endl;
      if (isPrime(i) && isPrime(i - 2)) {
        cout << i - 2 << " " << i << endl;
        break;
      }
    }
  }

  return 0;
}

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

bool isPrime(int n) {
  if (n % 2 == 0)
    return false;
  for (int i = 3; i < n; i += 2) {
    if (n % i == 0)
      return false;
  }
  return true;
}

int main(void) {
  int n;
  while (scanf("%d", &n), n) {

    for (int i = n; 5 <= i; i--) {
      // cout << i << endl;
      if (isPrime(i) && isPrime(i - 2)) {
        cout << i - 2 << " " << i << endl;
        break;
      }
    }
  }

  return 0;
}
Problem p00150 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int lis[10001];
int main() {
  for (int i = 2; i <= 10000; i++) {
    int flg = 1;
    for (int j = 2; j <= 10000; j++) {
      if (i % j == 0 && i != j)
        flg = 0;
    }
    lis[i] = flg;
  }
  while (1) {
    int a;
    cin >> a;
    for (int i = a;; a--) {
      if (lis[a] && lis[a - 2]) {
        cout << a - 2 << ' ' << a << endl;
        break;
      }
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int lis[10001];
int main() {
  for (int i = 2; i <= 10000; i++) {
    int flg = 1;
    for (int j = 2; j <= 10000; j++) {
      if (i % j == 0 && i != j)
        flg = 0;
    }
    lis[i] = flg;
  }
  while (1) {
    int a;
    cin >> a;
    if (a == 0)
      break;
    for (int i = a;; a--) {
      if (lis[a] && lis[a - 2]) {
        cout << a - 2 << ' ' << a << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p00150 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
#include <time.h>
int isprime(int n) {
  for (int i = 2; i < n; i++) {
    if (n % i == 0) {
      return 0;
    }
  }
  return 1;
}
int main() {
  int i, p, q, n;
  while (1) {
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    for (i = n; i >= 2; i--) {
      if (isprime(i) && isprime(i - 2)) {
        break;
      }
    }
    printf("%d %d\n", i - 2, i);
  }
  return 0;
}

// clock_t start, now;
// now = clock();
// printf("%f\n",(double)(now - start) / CLOCKS_PER_SEC);

Fixed submission:
#include <stdio.h>
#include <time.h>
int isprime(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      return 0;
    }
  }
  return 1;
}
int main() {
  int i, p, q, n;
  while (1) {
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    for (i = n; i >= 2; i--) {
      if (isprime(i) && isprime(i - 2)) {
        break;
      }
    }
    printf("%d %d\n", i - 2, i);
  }
  return 0;
}

// clock_t start, now;
// now = clock();
// printf("%f\n",(double)(now - start) / CLOCKS_PER_SEC);
Problem p00151 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int ans;
int n;
char ma[100][100];

void cheak(int px, int dx, int py, int dy) {
  int cnt = 1;

  while (1) {
    px += dx;
    py += dy;
    if (!(0 <= px && px < n && 0 <= py && py < n) || ma[py][px] != '1')
      break;
    cnt++;
  }
  ans = max(ans, cnt);
}

int main() {
  while (cin >> n, n) {

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

    ans = 0;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {

        if (ma[i][j] == '1') {
          for (int l = 0; l < 8; l++) {
            cheak(j, dx[l], i, dy[l]);
          }
        }
      }
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int ans;
int n;
char ma[300][300];

void cheak(int px, int dx, int py, int dy) {
  int cnt = 1;

  while (1) {
    px += dx;
    py += dy;
    if (!(0 <= px && px < n && 0 <= py && py < n) || ma[py][px] != '1')
      break;
    cnt++;
  }
  ans = max(ans, cnt);
}

int main() {
  while (cin >> n, n) {

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

    ans = 0;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {

        if (ma[i][j] == '1') {
          for (int l = 0; l < 8; l++) {
            cheak(j, dx[l], i, dy[l]);
          }
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00151 (C++)

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

string field[256];

void rotate(int n) {
  string tmp[256];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      tmp[i][j] = field[n - j - 1][i];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      field[i][j] = tmp[i][j];
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    int ma = 0;
    for (int i = 0; i < n; i++)
      cin >> field[i];
    for (int i = 0; i < n; i++) {
      int cnt = 0;
      for (int j = 0; j < n; j++) {
        if (field[i][j] == '1') {
          cnt++;
          ma = max(ma, cnt);
        } else
          cnt = 0;
      }
      cnt = 0;
      for (int j = 0; j < n; j++) {
        if (field[j][i] == '1') {
          cnt++;
          ma = max(ma, cnt);
        } else
          cnt = 0;
      }
    }
    for (int k = 0; k < 2; k++) {
      if (k)
        rotate(n);
      for (int i = 0; i < 2 * n - 1; i++) {
        int cnt = 0;
        for (int j = 0; j < n; j++) {
          if (i - j >= n || i - j < 0)
            continue;
          if (field[j][i - j] == '1') {
            cnt++;
            ma = max(ma, cnt);
          } else
            cnt = 0;
        }
      }
    }
    cout << ma << endl;
  }
  return 0;
}

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

string field[256];

void rotate(int n) {
  string tmp[256];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      tmp[i] += field[n - j - 1][i];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      field[i][j] = tmp[i][j];
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    int ma = 0;
    for (int i = 0; i < n; i++)
      cin >> field[i];
    for (int i = 0; i < n; i++) {
      int cnt = 0;
      for (int j = 0; j < n; j++) {
        if (field[i][j] == '1') {
          cnt++;
          ma = max(ma, cnt);
        } else
          cnt = 0;
      }
      cnt = 0;
      for (int j = 0; j < n; j++) {
        if (field[j][i] == '1') {
          cnt++;
          ma = max(ma, cnt);
        } else
          cnt = 0;
      }
    }
    for (int k = 0; k < 2; k++) {
      if (k)
        rotate(n);
      for (int i = 0; i < 2 * n - 1; i++) {
        int cnt = 0;
        for (int j = 0; j < n; j++) {
          if (i - j >= n || i - j < 0)
            continue;
          if (field[j][i - j] == '1') {
            cnt++;
            ma = max(ma, cnt);
          } else
            cnt = 0;
        }
      }
    }
    cout << ma << endl;
  }
  return 0;
}
Problem p00151 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
int main() {
  int n;
  while (scanf("%d", &n), n) {
    int m = 0, v[4][2] = {{0, -1}, {-1, 0}, {1, -1}, {-1, -1}},
        map[257][257] = {{0}};
    for (int y = 1; y <= n; y++)
      for (int x = 1; x <= n; x++)
        scanf("%1d", &map[x][y]);

    for (int i = 0; i < 4; i--) {
      int dp[257][257] = {{0}};
      for (int k = 1; k <= n; k++)
        for (int j = 1; j <= n; j++)
          m = std::max(
              m, dp[j][k] =
                     map[j][k] ? map[j][k] + dp[j + v[i][0]][k + v[i][1]] : 0);
    }
    printf("%d\n", m);
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
int main() {
  int n;
  while (scanf("%d", &n), n) {
    int m = 0, v[4][2] = {{0, -1}, {-1, 0}, {1, -1}, {-1, -1}},
        map[257][257] = {{0}};
    for (int y = 1; y <= n; y++)
      for (int x = 1; x <= n; x++)
        scanf("%1d", &map[x][y]);

    for (int i = 0; i < 4; i++) {
      int dp[257][257] = {{0}};
      for (int k = 1; k <= n; k++)
        for (int j = 1; j <= n; j++)
          m = std::max(
              m, dp[j][k] =
                     map[j][k] ? map[j][k] + dp[j + v[i][0]][k + v[i][1]] : 0);
    }
    printf("%d\n", m);
  }
}
Problem p00151 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;
int main(void) {
  int i, j, k, n, max, cnt, x, y, dx[] = {1, 1, 0, -1}, dy[] = {0, 1, 1, 1};
  char xy[225][226];
  while (1) {
    max = 0;
    cin >> n;
    if (n == 0)
      break;
    for (i = 0; i < n; i++)
      cin >> xy[i];
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        if (xy[i][j] == '1') {
          for (k = 0; k < 4; k++) {
            cnt = 1;
            y = i;
            x = j;
            while (1) {
              y = y + dy[k];
              x = x + dx[k];
              if (y >= n || x >= n || y < 0 || x < 0)
                break;
              if (xy[y][x] != '1')
                break;
              cnt++;
            }
            if (max < cnt)
              max = cnt;
          }
        }
      }
    }
    cout << max << endl;
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;
int main(void) {
  int i, j, k, n, max, cnt, x, y, dx[] = {1, 1, 0, -1}, dy[] = {0, 1, 1, 1};
  char xy[255][256];
  while (1) {
    max = 0;
    cin >> n;
    if (n == 0)
      break;
    for (i = 0; i < n; i++)
      cin >> xy[i];
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        if (xy[i][j] == '1') {
          for (k = 0; k < 4; k++) {
            cnt = 1;
            y = i;
            x = j;
            while (1) {
              y = y + dy[k];
              x = x + dx[k];
              if (y >= n || x >= n || y < 0 || x < 0)
                break;
              if (xy[y][x] != '1')
                break;
              cnt++;
            }
            if (max < cnt)
              max = cnt;
          }
        }
      }
    }
    cout << max << endl;
  }
  return 0;
}
Problem p00154 (C++)

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

#define MAX_N 150000
#define MAX_CARD 120

long long dp[MAX_CARD][MAX_N];
long long card[MAX_CARD][2];
int n, m, a, b, cnt, sum;

int main() {
  while (true) {
    memset(dp, 0, sizeof(dp));
    memset(card, 0, sizeof(card));
    // cin.
    cin >> n;
    if (n == 0) {
      break;
    }
    cnt = 0;
    sum = 0;
    for (int i = 0; i < n; i++) {
      cin >> a >> b;
      card[i][0] = a;
      card[i][1] = b;
      for (int j = 0; j < b; j++) {
        sum += a;
      }
    }
    // dp.
    dp[0][0] = 1;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= sum; j++) {
        if (dp[i - 1][j] != 0) {
          for (int k = 0; k <= card[i - 1][1]; k++) {
            dp[i][j + k * card[i - 1][0]] += dp[i - 1][j];
          }
        }
      }
    }
    // cin2.
    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> a;
      cout << dp[n][a] << endl;
    }
  }
  return 0;
}

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

#define MAX_N 82000
#define MAX_CARD 100

long long dp[MAX_CARD][MAX_N];
long long card[MAX_CARD][2];
int n, m, a, b, cnt, sum;

int main() {
  while (true) {
    memset(dp, 0, sizeof(dp));
    memset(card, 0, sizeof(card));
    // cin.
    cin >> n;
    if (n == 0) {
      break;
    }
    cnt = 0;
    sum = 0;
    for (int i = 0; i < n; i++) {
      cin >> a >> b;
      card[i][0] = a;
      card[i][1] = b;
      for (int j = 0; j < b; j++) {
        sum += a;
      }
    }
    // dp.
    dp[0][0] = 1;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= sum; j++) {
        if (dp[i - 1][j] != 0) {
          for (int k = 0; k <= card[i - 1][1]; k++) {
            dp[i][j + k * card[i - 1][0]] += dp[i - 1][j];
          }
        }
      }
    }
    // cin2.
    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> a;
      cout << dp[n][a] << endl;
    }
  }
  return 0;
}
Problem p00154 (C++)

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

int a[8][1000];
int main() {
  int b;
  while (cin >> b, b) {
    memset(a, 0, sizeof(a));
    for (int c = 0; c < b; c++) {
      a[c][0] = 1;
      int d, e;
      cin >> d >> e;
      for (int f = 0; f <= e; f++) {
        for (int g = max(f * d, 1); g <= 1000; g++) {
          a[c + 1][g] += a[c][g - f * d];
        }
      }
    }
    int s;
    cin >> s;
    for (int n = 0; n < s; n++) {
      int w;
      cin >> w;
      cout << a[b][w] << endl;
    }
  }
}

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

int a[8][1001];
int main() {
  int b;
  while (cin >> b, b) {
    memset(a, 0, sizeof(a));
    for (int c = 0; c < b; c++) {
      a[c][0] = 1;
      int d, e;
      cin >> d >> e;
      for (int f = 0; f <= e; f++) {
        for (int g = max(f * d, 1); g <= 1000; g++) {
          a[c + 1][g] += a[c][g - f * d];
        }
      }
    }
    int s;
    cin >> s;
    for (int n = 0; n < s; n++) {
      int w;
      cin >> w;
      cout << a[b][w] << endl;
    }
  }
}
Problem p00156 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
#define MAX_N 1000
int X[MAX_N][MAX_N], dist[MAX_N][MAX_N], W, H, gx, gy;
char c;
int main() {
  while (true) {
    for (int i = 0; i < MAX_N; i++) {
      for (int j = 0; j < MAX_N; j++) {
        X[i][j] = 1000000000;
        dist[i][j] = 1000000000;
      }
    }
    cin >> W >> H;
    queue<pair<int, int>> Q;
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> c;
        if (c == '&') {
          gx = i;
          gy = j;
        }
        if (c == '#') {
          X[i][j] = 1;
        } else {
          X[i][j] = 0;
        }
        if (i == 1 || i == H || j == 1 || j == W) {
          Q.push(make_pair(i, j));
          dist[i][j] = X[i][j];
        }
      }
    }
    while (!Q.empty()) {
      pair<int, int> pa = Q.front();
      Q.pop();
      int cx = pa.first, cy = pa.second;
      int dx[4] = {1, 0, -1, 0};
      int dy[4] = {0, 1, 0, -1};
      for (int i = 0; i < 4; i++) {
        int ex = cx + dx[i], ey = cy + dy[i];
        if (X[ex][ey] >= 2) {
          continue;
        }
        int dis = dist[cx][cy];
        if (X[cx][cy] == 0 && X[ex][ey] == 1) {
          dis += 1;
        }
        if (dist[ex][ey] > dis) {
          dist[ex][ey] = dis;
          Q.push(make_pair(ex, ey));
        }
      }
    }
    cout << dist[gx][gy] << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
#define MAX_N 1000
int X[MAX_N][MAX_N], dist[MAX_N][MAX_N], W, H, gx, gy;
char c;
int main() {
  while (true) {
    for (int i = 0; i < MAX_N; i++) {
      for (int j = 0; j < MAX_N; j++) {
        X[i][j] = 1000000000;
        dist[i][j] = 1000000000;
      }
    }
    cin >> W >> H;
    queue<pair<int, int>> Q;
    if (W == 0 && H == 0) {
      break;
    }
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> c;
        if (c == '&') {
          gx = i;
          gy = j;
        }
        if (c == '#') {
          X[i][j] = 1;
        } else {
          X[i][j] = 0;
        }
        if (i == 1 || i == H || j == 1 || j == W) {
          Q.push(make_pair(i, j));
          dist[i][j] = X[i][j];
        }
      }
    }
    while (!Q.empty()) {
      pair<int, int> pa = Q.front();
      Q.pop();
      int cx = pa.first, cy = pa.second;
      int dx[4] = {1, 0, -1, 0};
      int dy[4] = {0, 1, 0, -1};
      for (int i = 0; i < 4; i++) {
        int ex = cx + dx[i], ey = cy + dy[i];
        if (X[ex][ey] >= 2) {
          continue;
        }
        int dis = dist[cx][cy];
        if (X[cx][cy] == 0 && X[ex][ey] == 1) {
          dis += 1;
        }
        if (dist[ex][ey] > dis) {
          dist[ex][ey] = dis;
          Q.push(make_pair(ex, ey));
        }
      }
    }
    cout << dist[gx][gy] << endl;
  }
  return 0;
}
Problem p00157 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> MAT;
#define h first
#define r second
#define rep(i, n) for (int i = 0; i < n; i++)
int dp[1000][1000];
MAT info[200];
int n, m;

int dfs(int a, int b) {
  if (~dp[a][b])
    return dp[a][b];

  int hoge = 0;
  rep(i, n) {
    if (info[i].first < a && info[i].second < b) {
      hoge = max(hoge, dfs(info[i].first, info[i].second) + 1);
    }
  }
  return hoge;
}
int main() {
  while (cin >> n, n) {
    rep(i, 1000) rep(j, 1000) dp[i][j] = -1;

    int ret = 0;
    rep(i, n) cin >> info[i].h >> info[i].r;
    cin >> m;
    rep(i, m) cin >> info[n + i].h >> info[n + i].r;

    n = n + m;

    rep(i, n) { ret = max(ret, dfs(info[i].first, info[i].second) + 1); }
    cout << ret << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> MAT;
#define h first
#define r second
#define rep(i, n) for (int i = 0; i < n; i++)
int dp[1000][1000];
MAT info[200];
int n, m;

int dfs(int a, int b) {
  if (~dp[a][b])
    return dp[a][b];

  int hoge = 0;
  rep(i, n) {
    if (info[i].first < a && info[i].second < b) {
      hoge = max(hoge, dfs(info[i].first, info[i].second) + 1);
    }
  }
  return dp[a][b] = hoge;
}
int main() {
  while (cin >> n, n) {
    rep(i, 1000) rep(j, 1000) dp[i][j] = -1;

    int ret = 0;
    rep(i, n) cin >> info[i].h >> info[i].r;
    cin >> m;
    rep(i, m) cin >> info[n + i].h >> info[n + i].r;

    n = n + m;

    rep(i, n) { ret = max(ret, dfs(info[i].first, info[i].second) + 1); }
    cout << ret << endl;
  }
}