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

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

using namespace std;

int main() {
  for (int n; cin >> n, n;) {
    int b[90000];
    for (int i = 0; i < n * (n + 1) / 2; i++) {
      cin >> b[i];
    }
    int a = sqrt(double(b[0]) * b[1] / b[n]) + .5;
    cout << a << endl;
    int c[250];
    for (int i = 0; i < n; i++) {
      c[i] = b[i] / a;
    }
    sort(c, c + n);
    for (int i = 0; i < n; i++) {
      cout << c[i] << ((i == n - 1) ? '\n' : ' ');
    }
  }
  return 0;
}

Fixed submission:
// 25
#include <algorithm>
#include <cmath>
#include <iostream>

using namespace std;

int main() {
  for (int n; cin >> n, n;) {
    long long b[90000];
    for (int i = 0; i < n * (n + 1) / 2; i++) {
      cin >> b[i];
    }
    int a = sqrt(double(b[0]) * b[1] / b[n]) + .5;
    cout << a << endl;
    int c[250];
    for (int i = 0; i < n; i++) {
      c[i] = b[i] / a;
    }
    sort(c, c + n);
    for (int i = 0; i < n; i++) {
      cout << c[i] << ((i == n - 1) ? '\n' : ' ');
    }
  }
  return 0;
}
Problem p00654 (C++)

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

using namespace std;

long long gcd(unsigned long long int x, unsigned long long int y) {
  return y == 0 ? x : gcd(y, x % y);
}

void solve() {
  int n;
  while (cin >> n, n) {
    int size = (n + 1) * n / 2;
    vector<unsigned long long int> Vecodd;
    vector<unsigned long long int> Veceven;
    for (int i = 0; i < size; ++i) {
      int temp;
      cin >> temp;
      if (temp % 2 == 0) {
        Veceven.push_back(temp);
      } else {
        Vecodd.push_back(temp);
      }
    }
    sort(Veceven.begin(), Veceven.end());
    sort(Vecodd.begin(), Vecodd.end());
    unsigned long long int minodd = Vecodd[0];
    unsigned long long int mineven[2] = {Veceven[0], Veceven[1]};
    unsigned long long int g = gcd(mineven[0], minodd);
    unsigned long long int Even = (unsigned long long int)sqrt(
        (double)mineven[0] / g * (mineven[1] / (minodd / g)));
    cout << Even << endl;
    size = Veceven.size();
    for (int i = 0; i < size - 1; ++i) {
      cout << Veceven[i] / Even << " ";
    }
    cout << Veceven[size - 1] / Even << endl;
  }
}

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

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

using namespace std;

long long gcd(unsigned long long int x, unsigned long long int y) {
  return y == 0 ? x : gcd(y, x % y);
}

void solve() {
  int n;
  while (cin >> n, n) {
    int size = (n + 1) * n / 2;
    vector<unsigned long long int> Vecodd;
    vector<unsigned long long int> Veceven;
    for (int i = 0; i < size; ++i) {
      unsigned long long int temp;
      cin >> temp;
      if (temp % 2 == 0) {
        Veceven.push_back(temp);
      } else {
        Vecodd.push_back(temp);
      }
    }
    sort(Veceven.begin(), Veceven.end());
    sort(Vecodd.begin(), Vecodd.end());
    unsigned long long int minodd = Vecodd[0];
    unsigned long long int mineven[2] = {Veceven[0], Veceven[1]};
    unsigned long long int g = gcd(mineven[0], minodd);
    unsigned long long int Even = (unsigned long long int)sqrt(
        (double)mineven[0] / g * (mineven[1] / (minodd / g)));
    cout << Even << endl;
    size = Veceven.size();
    for (int i = 0; i < size - 1; ++i) {
      cout << Veceven[i] / Even << " ";
    }
    cout << Veceven[size - 1] / Even << endl;
  }
}

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
long long fn(long long p, long long q) {
  long long s;
  while (q) {
    p %= q;
    s = p;
    p = q;
    q = s;
  }
  return p;
}
int main() {
  int i, j, k;
  int n;
  while (cin >> n && n) {
    long long a[31400], b[31400];
    j = 0;
    k = 0;
    for (i = 0; i < n * (n + 1) / 2; i++) {
      long long p;
      cin >> p;
      if (p % 2) {
        a[j] = p;
        j++;
      } else {
        b[k] = p;
        k++;
      }
    }
    sort(a, a + j);
    sort(b, b + k);
    long long c = b[0];
    for (i = 1; i < k; i++)
      c = fn(c, b[i]);
    for (; a[0] != (b[0] / c) * (b[1] / c); c /= 2)
      ;
    cout << c << endl;
    for (i = 0; i < k - 1; i++)
      cout << b[i] / c << " ";
    cout << b[i] / c << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
long long fn(long long p, long long q) {
  long long s;
  while (q) {
    p %= q;
    s = p;
    p = q;
    q = s;
  }
  return p;
}
int main() {
  int i, j, k;
  int n;
  while (cin >> n && n) {
    long long a[31400], b[31400];
    j = 0;
    k = 0;
    for (i = 0; i < n * (n + 1) / 2; i++) {
      long long p;
      cin >> p;
      if (p % 2) {
        a[j] = p;
        j++;
      } else {
        b[k] = p;
        k++;
      }
    }
    sort(a, a + j);
    sort(b, b + k);
    long long c = fn(a[0], b[0]);
    c = (long long)sqrt(b[0] / c * (b[1] / (a[0] / c)));
    cout << c << endl;
    for (i = 0; i < k - 1; i++)
      cout << b[i] / c << " ";
    cout << b[i] / c << endl;
  }
  return 0;
}
Problem p00654 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
long long a[100000];
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }

int cmp(const long long &a, const long long &b) {
  if (a % 2 == 0 && b % 2 == 1)
    return 1;
  else if (a % 2 == 1 && b % 2 == 0)
    return 0;
  else
    return a < b;
}
int main() {
  int N, n;
  while (1) {
    long long x, y, z, tmp, r1, r2, d2, m, p, q;
    scanf("%d", &n);
    if (n == 0)
      break;
    N = (n + 1) * n / 2;
    for (int i = 1; i <= N; i++)
      scanf("%lld", a + i);

    sort(a + 1, a + N + 1, cmp);
    x = a[n + 1];
    y = a[1];
    z = a[2];

    tmp = gcd(y, z);
    r1 = y / tmp;
    r2 = z / tmp;
    d2 = x / r1 / r2;

    p = 1;
    q = (1 << 31);
    while (p <= q) {
      m = (p + q) >> 1;
      if (m * m == d2)
        break;
      else if (m * m < d2)
        p = m + 1;
      else
        q = m - 1;
    }
    tmp /= m;

    printf("%lld\n", tmp);
    for (int i = 1; i <= n; i++) {
      if (i < n)
        printf("%lld ", a[i] / tmp);
      else
        printf("%lld\n", a[i] / tmp);
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
long long a[100000];
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }

int cmp(const long long &a, const long long &b) {
  if (a % 2 == 0 && b % 2 == 1)
    return 1;
  else if (a % 2 == 1 && b % 2 == 0)
    return 0;
  else
    return a < b;
}
int main() {
  int N, n;
  while (1) {
    long long x, y, z, tmp, r1, r2, d2, m, p, q;
    scanf("%d", &n);
    if (n == 0)
      break;
    N = (n + 1) * n / 2;
    for (int i = 1; i <= N; i++)
      scanf("%lld", a + i);

    sort(a + 1, a + N + 1, cmp);
    x = a[n + 1];
    y = a[1];
    z = a[2];

    tmp = gcd(y, z);
    r1 = y / tmp;
    r2 = z / tmp;
    d2 = x / r1 / r2;

    p = 1;
    q = (1 << 31) - 1;
    while (p <= q) {
      m = (p + q) >> 1;
      if (m * m == d2)
        break;
      else if (m * m < d2)
        p = m + 1;
      else
        q = m - 1;
    }
    tmp /= m;

    printf("%lld\n", tmp);
    for (int i = 1; i <= n; i++) {
      if (i < n)
        printf("%lld ", a[i] / tmp);
      else
        printf("%lld\n", a[i] / tmp);
    }
  }
  return 0;
}
Problem p00657 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int r, c;
int main() {
  while (true) {
    cin >> r >> c;
    if (r % 2 == 1 && c % 2 == 1) {
      cout << "no" << endl;
    } else {
      cout << "yes" << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int r, c;
int main() {
  while (true) {
    cin >> r >> c;
    if (r == 0 && c == 0) {
      break;
    }
    if (r % 2 == 1 && c % 2 == 1) {
      cout << "no" << endl;
    } else {
      cout << "yes" << endl;
    }
  }
  return 0;
}
Problem p00662 (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 a, b, c, d, e, f;
    cin >> a >> b >> c >> d >> e >> f;
    int x = a + d, y = b + e, z = c + f;
    if (!x && !y && !z)
      break;
    int ans = 0;
    int amin = min(min(x, y), z);
    for (int i = 0; i <= amin; ++i) {
      int sum = i;
      sum += (x - i) / 3 + (y - i) / 3 + (z - i) / 3;
      ans = max(ans, sum);
    }
    cout << ans << 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 a, b, c, d, e, f;
    cin >> a >> b >> c >> d >> e >> f;
    int x = a + d, y = b + e, z = c + f;
    if (!x && !y && !z)
      break;
    int ans = 0;
    int amin = min(min(x, y), z);
    for (int i = 0; i <= min(2, amin); ++i) {
      int sum = i;
      sum += (x - i) / 3 + (y - i) / 3 + (z - i) / 3;
      ans = max(ans, sum);
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p00662 (C++)

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

int main() {
  int a, b, c, d, e, f;
  while (cin >> a >> b >> c >> d >> e >> f, a || b || c || d || e || f) {
    a += d;
    b += e;
    c += f;
    int Max = 0;
    for (int i = 0; i <= min(a, min(b, c)); i++)
      Max = max(Max, (a - i) / 3 + (b - i) / 3 + (c - i) / 3 + i);
    cout << Max << endl;
  }
}

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

int main() {
  int a, b, c, d, e, f;
  while (cin >> a >> b >> c >> d >> e >> f, a || b || c || d || e || f) {
    a += d;
    b += e;
    c += f;
    int Max = 0;
    for (int i = 0; i <= min(2, min(a, min(b, c))); i++)
      Max = max(Max, (a - i) / 3 + (b - i) / 3 + (c - i) / 3 + i);
    cout << Max << endl;
  }
}
Problem p00662 (C++)

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

#define REP(i, s, e) for (int i = (int)s; i < (int)e; i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define fi first
#define se second
#define pb push_back
#define mp nake_pair

typedef long long ll;

typedef vector<int> vi;

int calc(int m, const vi &d) {
  int ret = m;
  rep(i, 3) if (m > d[i]) return -1e9;
  rep(i, 3) ret += (d[i] - m) / 3;
  return ret;
}

int main() {
  vi v(6);
  while (1) {
    rep(i, 6) cin >> v[i];
    if (v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0 && v[4] == 0 &&
        v[5] == 0)
      break;
    vi d(3);
    rep(i, 6) d[i % 3] += v[i];
    double l = 0, r = 1e9;
    rep(i, 10000) {
      double m1 = l + (r - l) / 3.0, m2 = l + (r - l) * 2.0 / 3.0;
      int f1 = calc(m1, d), f2 = calc(m2, d);
      if (f1 < f2)
        l = m1;
      else
        r = m2;
    }
    cout << calc(r, d) << endl;
  }
}

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

#define REP(i, s, e) for (int i = (int)s; i < (int)e; i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define fi first
#define se second
#define pb push_back
#define mp nake_pair

typedef long long ll;

typedef vector<int> vi;

int calc(int m, const vi &d) {
  int ret = m;
  rep(i, 3) if (m > d[i]) return -1e9;
  rep(i, 3) ret += (d[i] - m) / 3;
  return ret;
}

int main() {
  vi v(6);
  while (1) {
    rep(i, 6) cin >> v[i];
    if (v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0 && v[4] == 0 &&
        v[5] == 0)
      break;
    vi d(3);
    rep(i, 6) d[i % 3] += v[i];
    double l = 0, r = 1e9;
    rep(i, 1000) {
      double m1 = l + (r - l) / 3.0, m2 = l + (r - l) * 2.0 / 3.0;
      int f1 = calc(m1, d), f2 = calc(m2, d);
      if (f1 < f2)
        l = m1;
      else
        r = m2;
    }
    cout << calc(r, d) << endl;
  }
}
Problem p00662 (C++)

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

typedef long long ll;
int A, B, C;

int main() {
  int a, b, c, d, e, f;
  while (cin >> a >> b >> c >> d >> e >> f && (a || b || c || d || e || f)) {
    A = a + d;
    B = b + e;
    C = c + f;
    //    cout<< A << " "<< B << " " << C << endl;
    int n = min(A, min(B, C));
    int res = 0;
    res = max(res, A / 3 + B / 3 + C / 3);
    for (int i = n; i > min(-1, n - 4); i--) {
      res = max(res, i + (A - i) / 3 + (B - i) / 3 + (C - i) / 3);
    }
    cout << res << endl;
  }
}

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

typedef long long ll;
int A, B, C;

int main() {
  int a, b, c, d, e, f;
  while (cin >> a >> b >> c >> d >> e >> f && (a || b || c || d || e || f)) {
    A = a + d;
    B = b + e;
    C = c + f;
    //    cout<< A << " "<< B << " " << C << endl;
    int n = min(A, min(B, C));
    int res = 0;
    res = max(res, A / 3 + B / 3 + C / 3);
    for (int i = n; i > max(-1, n - 4); i--) {
      res = max(res, i + (A - i) / 3 + (B - i) / 3 + (C - i) / 3);
    }
    cout << res << endl;
  }
}
Problem p00662 (C++)

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

#define MOD 1000000007
#define INTMAX 0x7fffffff
#define INTMIN 0x80000000
#define LLMAX 0x7fffffffffffffffLL
#define LLMIN 0x8000000000000000LL

#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;
using ll = long long;

int main() {
  while (1) {
    int n[6];
    scanf("%d%d%d%d%d%d", n, n + 1, n + 2, n + 3, n + 4, n + 5);
    if (n[0] + n[1] + n[2] + n[3] + n[4] + n[5] == 0) {
      break;
    }
    int a = n[0] + n[3];
    int b = n[1] + n[4];
    int c = n[2] + n[5];
    int d = min(a, min(b, c));

    int ans = 0;
    rep(count4, d + 1) {
      ans = max(ans, count4 + (a - count4) / 3 + (b - count4) / 3 +
                         (c - count4) / 3);
    }
    printf("%d\n", ans);
  }
}

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

#define MOD 1000000007
#define INTMAX 0x7fffffff
#define INTMIN 0x80000000
#define LLMAX 0x7fffffffffffffffLL
#define LLMIN 0x8000000000000000LL

#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;
using ll = long long;

int main() {
  while (1) {
    int n[6];
    scanf("%d%d%d%d%d%d", n, n + 1, n + 2, n + 3, n + 4, n + 5);
    if (n[0] + n[1] + n[2] + n[3] + n[4] + n[5] == 0) {
      break;
    }
    int a = n[0] + n[3];
    int b = n[1] + n[4];
    int c = n[2] + n[5];
    int d = min(a, min(b, c));

    int ans = 0;
    rep(count4, min(d + 1, 3)) {
      ans = max(ans, count4 + (a - count4) / 3 + (b - count4) / 3 +
                         (c - count4) / 3);
    }
    printf("%d\n", ans);
  }
}
Problem p00662 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const double EPS = 1e-8;

int main() {
  int math, greedy, geometry, DP, graph, other;
  while (~scanf("%d %d %d %d %d %d", &math, &greedy, &geometry, &DP, &graph,
                &other)) {
    if (math + greedy + geometry + DP + graph + other == 0)
      break;

    int A = math + DP;
    int B = greedy + graph;
    int C = geometry + other;

    int res = 0;

    int max_game = min(min(A, B), C);
    for (int balance_game = 0; balance_game <= max_game; balance_game++) {
      int tmpA = A;
      int tmpB = B;
      int tmpC = C;
      tmpA -= balance_game;
      tmpB -= balance_game;
      tmpC -= balance_game;
      int math_game = tmpA / 3;
      int algorithm_game = tmpB / 3;
      int implement_game = tmpC / 3;

      res =
          max(balance_game + math_game + algorithm_game + implement_game, res);
    }
    printf("%d\n", res);
  }
}

Fixed submission:
#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const double EPS = 1e-8;

int main() {
  int math, greedy, geometry, DP, graph, other;
  while (~scanf("%d %d %d %d %d %d", &math, &greedy, &geometry, &DP, &graph,
                &other)) {
    if (math + greedy + geometry + DP + graph + other == 0)
      break;

    int A = math + DP;
    int B = greedy + graph;
    int C = geometry + other;

    int res = 0;

    int max_game = min(min(A, B), C);
    for (int balance_game = 0; balance_game <= min(2, max_game);
         balance_game++) {
      int tmpA = A;
      int tmpB = B;
      int tmpC = C;
      tmpA -= balance_game;
      tmpB -= balance_game;
      tmpC -= balance_game;
      int math_game = tmpA / 3;
      int algorithm_game = tmpB / 3;
      int implement_game = tmpC / 3;

      res =
          max(balance_game + math_game + algorithm_game + implement_game, res);
    }
    printf("%d\n", res);
  }
}
Problem p00662 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;
int a, b, c, d, e, f, sum1, sum2, sum3, sum, p;
int main() {
  while (true) {
    sum = 0;
    cin >> a >> b >> c >> d >> e >> f;
    if (a + b + c + d + e + f == 0) {
      break;
    }
    sum1 = a + d;
    sum2 = b + e;
    sum3 = c + f;
    p = 0;
    p = min(sum1, sum2);
    p = min(p, sum3);
    for (int i = 0; i <= p; i++) {
      sum = max(sum, i + (sum1 - i) / 3 + (sum2 - i) / 3 + (sum3 - i) / 3);
    }
    cout << sum << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int a, b, c, d, e, f, sum1, sum2, sum3, sum, p;
int main() {
  while (true) {
    sum = 0;
    cin >> a >> b >> c >> d >> e >> f;
    if (a + b + c + d + e + f == 0) {
      break;
    }
    sum1 = a + d;
    sum2 = b + e;
    sum3 = c + f;
    p = 0;
    p = min(sum1, sum2);
    p = min(p, sum3);
    for (int i = max(0, p - 10); i <= p; i++) {
      sum = max(sum, i + (sum1 - i) / 3 + (sum2 - i) / 3 + (sum3 - i) / 3);
    }
    cout << sum << endl;
  }
}
Problem p00663 (C++)

Buggy submission (status: Runtime Error):
// Name: SAT-EN-3
// Level: 3
// Category: 構文解析
// Note:

/**
 * 加法標準形なので、X&~Xの形を含まない項があればその項は真にでき、全体も真になる。
 *
 * オーダーは O(|S|)。
 */
#include <cassert>
#include <iostream>
#include <map>
#include <string>

using namespace std;

pair<char, bool> literal(const string &str, int &pos) {
  pair<char, bool> res('\0', false);
  if (str[pos] == '~') {
    res.second = true;
    ++pos;
  }
  res.first = str[pos++];
  return res;
}

bool parse(const string &str, int pos) {
  if (pos >= str.size())
    return false;
  assert(str[pos] == '(');
  ++pos;
  map<char, bool> vars;
  bool ok = true;
  for (int i = 0; i < 3; ++i) {
    pair<char, bool> var = literal(str, pos);
    if (vars.count(var.first) && vars[var.first] != var.second) {
      ok = false;
    }
    vars[var.first] = var.second;
    if (i != 2) {
      assert(str[pos] == '&');
      ++pos;
    }
  }
  assert(str[pos] == ')');
  ++pos;
  if (ok)
    return true;
  else
    return parse(str, pos);
}

bool solve() {
  string str;
  if (!(cin >> str))
    return false;
  if (str == "#")
    return false;

  cout << (parse(str, 0) ? "yes" : "no") << endl;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout.setf(ios::fixed);
  cout.precision(10);

  while (solve())
    ;
  return 0;
}

Fixed submission:
// Name: SAT-EN-3
// Level: 3
// Category: 構文解析
// Note:

/**
 * 加法標準形なので、X&~Xの形を含まない項があればその項は真にでき、全体も真になる。
 *
 * オーダーは O(|S|)。
 */
#include <cassert>
#include <iostream>
#include <map>
#include <string>

using namespace std;

pair<char, bool> literal(const string &str, int &pos) {
  pair<char, bool> res('\0', false);
  if (str[pos] == '~') {
    res.second = true;
    ++pos;
  }
  res.first = str[pos++];
  return res;
}

bool parse(const string &str, int pos) {
  if (pos >= str.size())
    return false;
  if (str[pos] == '|')
    ++pos;
  assert(str[pos] == '(');
  ++pos;
  map<char, bool> vars;
  bool ok = true;
  for (int i = 0; i < 3; ++i) {
    pair<char, bool> var = literal(str, pos);
    if (vars.count(var.first) && vars[var.first] != var.second) {
      ok = false;
    }
    vars[var.first] = var.second;
    if (i != 2) {
      assert(str[pos] == '&');
      ++pos;
    }
  }
  assert(str[pos] == ')');
  ++pos;
  if (ok)
    return true;
  else
    return parse(str, pos);
}

bool solve() {
  string str;
  if (!(cin >> str))
    return false;
  if (str == "#")
    return false;

  cout << (parse(str, 0) ? "yes" : "no") << endl;
  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout.setf(ios::fixed);
  cout.precision(10);

  while (solve())
    ;
  return 0;
}
Problem p00664 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int main() {
  int n, m, q;
  while (cin >> n >> m >> q, n + m + q) {
    vi h(n), w(m);
    int coh = 0, cow = 0;
    vvi in(q, vi(3));
    rep(i, q) rep(j, 3) cin >> in[n - 1 - i][j];
    int out = 0;
    rep(i, q) {
      if (in[i][0] && w[in[i][1]] == 0) {
        if (in[i][2])
          out += n - coh;
        cow++;
        w[in[i][1]] = true;
      } else if (in[i][0] == 0 && h[in[i][1]] == 0) {
        if (in[i][2])
          out += m - cow;
        coh++;
        h[in[i][1]] = true;
      }
    }
    cout << out << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int main() {
  int n, m, q;
  while (cin >> n >> m >> q, n + m + q) {
    vi h(n), w(m);
    int coh = 0, cow = 0;
    vvi in(q, vi(3));
    rep(i, q) rep(j, 3) cin >> in[q - 1 - i][j];
    int out = 0;
    rep(i, q) {
      if (in[i][0] && w[in[i][1]] == 0) {
        if (in[i][2])
          out += n - coh;
        cow++;
        w[in[i][1]] = true;
      } else if (in[i][0] == 0 && h[in[i][1]] == 0) {
        if (in[i][2])
          out += m - cow;
        coh++;
        h[in[i][1]] = true;
      }
    }
    cout << out << endl;
  }
}
Problem p00664 (C++)

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

int main() {
  while (1) {
    ll r, c, q;
    cin >> r >> c >> q;
    if (!r && !c && !q)
      return 0;

    ll A[N], B[N], O[N];
    for (int i = 0; i < q; i++)
      cin >> A[i] >> B[i] >> O[i];

    ll ans = 0, cnt[2] = {}, rc[] = {r, c}, used[2][N] = {};
    for (int i = q - 1; i >= 0; i--) {
      ll f = A[i], RC = B[i];
      if (used[f][RC]++)
        continue;
      cnt[f]++;
      assert(rc[f] - cnt[!f] >= 0);
      ans += (rc[f] - cnt[!f]) * O[i];
    }
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 50001
using namespace std;
typedef long long ll;

int main() {
  while (1) {
    ll r, c, q;
    cin >> r >> c >> q;
    if (!r && !c && !q)
      return 0;

    ll A[N], B[N], O[N];
    for (int i = 0; i < q; i++)
      cin >> A[i] >> B[i] >> O[i];

    ll ans = 0, cnt[2] = {}, rc[] = {r, c}, used[2][N] = {};
    for (int i = q - 1; i >= 0; i--) {
      ll f = A[i], RC = B[i];
      if (used[f][RC]++)
        continue;
      cnt[f]++;
      ans += (rc[!f] - cnt[!f]) * O[i];
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p00670 (C++)

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

int main() {
  int n, s;
  int r[20000];

  while (cin >> n >> s, n || s) {
    for (int i = 0; i < n; i++) {
      cin >> r[i];
    }

    int cnt = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (r[i] + r[j] > s) {
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
}

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

int main() {
  int n, s;
  int r[20000];

  while (cin >> n >> s, n || s) {
    for (int i = 0; i < n; i++) {
      cin >> r[i];
    }

    int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (r[i] > s) {
        cnt += n - i - 1;
      } else {
        for (int j = i + 1; j < n; j++) {
          if (r[i] + r[j] > s) {
            cnt++;
          }
        }
      }
    }
    cout << cnt << endl;
  }
}
Problem p00670 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define vi vector<int>
#define VS vector<string>
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back

using namespace std;

int main() {
  int n, S;
  while (cin >> n >> S, n + S) {
    vi r;
    for (int i = 0; i < n; i++) {
      int m;
      cin >> m;
      r.push_back(m);
    }
    int ans = 0;
    for (int i = 0; i < r.size(); i++) {
      for (int j = i + 1; j < r.size(); j++) {
        if (S < r[i] + r[j])
          ans++;
      }
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define vi vector<int>
#define VS vector<string>
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back

using namespace std;

int main() {
  int n, S;
  while (cin >> n >> S, n + S) {
    vi r;
    for (int i = 0; i < n; i++) {
      int m;
      cin >> m;
      r.push_back(m);
    }
    int ans = 0;
    for (int i = 0; i < r.size(); i++) {
      for (int j = 0; j < i; j++) {
        if (S < r[i] + r[j])
          ans++;
      }
    }
    cout << ans << endl;
  }
}
Problem p00676 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

double calc(double a, double b, double c) {
  double s = (a + b + c) / 2;
  return sqrt(s * (s - a) * (s - b) * (s - c));
}

void func(double a, double l, double x) {

  double tmp = (l + x) / 2;

  printf("%.10lf\n", calc(a, l, l) + calc(tmp, tmp, l) * 2);
}

int main() {

  double a, l, x;

  while (scanf("%lf %lf %lf", a, l, x) != EOF) {
    func(a, l, x);
  }
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

double calc(double a, double b, double c) {
  double s = (a + b + c) / 2;
  return sqrt(s * (s - a) * (s - b) * (s - c));
}

void func(double a, double l, double x) {

  double tmp = (l + x) / 2;

  printf("%.10lf\n", calc(a, l, l) + calc(tmp, tmp, l) * 2);
}

int main() {

  double a, l, x;

  while (scanf("%lf %lf %lf", &a, &l, &x) != EOF) {
    func(a, l, x);
  }
}
Problem p00676 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};

static const double EPS = 1e-8;

int main() {
  double a, l, x;

  while (~scanf("%lf %lf %lf", &a, &l, &x)) {
    double s = (a + l + l) / 2.0;
    double ca = sqrt(s * (s - a) * (s - l) * (s - l));

    double res = 0.0;

    for (double scale = 0.000001; scale < 1.0; scale += 0.000001) {
      double e1 = scale * (l + x);
      double e2 = (1.0 - scale) * (l + x);

      double s2 = (e1 + e2 + l) / 2.0;
      if (s2 * (s2 - e1) * (s2 - e2) * (s2 - l) < 0)
        continue;

      double ca2 = sqrt(s2 * (s2 - e1) * (s2 - e2) * (s2 - l));

      res = max(ca + ca2 + ca2, res);
    }

    printf("%lf\n", res);
  }
}

Fixed submission:
#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};

static const double EPS = 1e-8;

int main() {
  double a, l, x;

  while (~scanf("%lf %lf %lf", &a, &l, &x)) {
    double s = (a + l + l) / 2.0;
    double ca = sqrt(s * (s - a) * (s - l) * (s - l));

    double res = 0.0;

    for (double scale = 0.00001; scale < 1.0; scale += 0.00001) {
      double e1 = scale * (l + x);
      double e2 = (1.0 - scale) * (l + x);

      double s2 = (e1 + e2 + l) / 2.0;
      if (s2 * (s2 - e1) * (s2 - e2) * (s2 - l) < 0)
        continue;

      double ca2 = sqrt(s2 * (s2 - e1) * (s2 - e2) * (s2 - l));

      res = max(ca + ca2 + ca2, res);
    }

    printf("%lf\n", res);
  }
}
Problem p00676 (C++)

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

int main() {
  double a, l, x;
  while (~scanf("%lf%lf%lf", &a, &l, &x), a) {
    double s1 = (a + l + l) / 2, s2 = (l + l + x) / 2;
    double d = (l + x) / 2;
    printf("%.10lf\n", sqrt(s1 * (s1 - a) * (s1 - l) * (s1 - l)) +
                           sqrt(s2 * (s2 - d) * (s2 - d) * (s2 - l)) * 2);
  }
}

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

int main() {
  double a, l, x;
  while (~scanf("%lf%lf%lf", &a, &l, &x)) {
    double s1 = (a + l + l) / 2, s2 = (l + l + x) / 2;
    double d = (l + x) / 2;
    printf("%.10lf\n", sqrt(s1 * (s1 - a) * (s1 - l) * (s1 - l)) +
                           sqrt(s2 * (s2 - d) * (s2 - d) * (s2 - l)) * 2);
  }
}
Problem p00676 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {

  int a, l, x, i = 0;
  double M[1000], c, d, e, f;

  while (scanf("%d%d%d", &a, &l, &x) != EOF) {

    c = (double)((2 * x * l) + (x * x)) / 4;
    e = l * l - (double)(a * a) / 4;
    d = sqrt(c);
    f = sqrt(e);
    M[i] = ((double)a / 2) * f + l * d;

    i++;
  }

  for (int j = 0; j < i; j++) {
    printf("%.10f\n", M[j]);
  }

  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {

  int a, l, x, i = 0;
  double M[100000], c, d, e, f;

  while (scanf("%d%d%d", &a, &l, &x) != EOF) {

    c = (double)((2 * x * l) + (x * x)) / 4;
    e = l * l - (double)(a * a) / 4;
    d = sqrt(c);
    f = sqrt(e);
    M[i] = ((double)a / 2) * f + l * d;

    i++;
  }

  for (int j = 0; j < i; j++) {
    printf("%.10f\n", M[j]);
  }

  return 0;
}
Problem p00677 (C++)

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

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

using namespace std;

void knapsack(int yosan, const vector<pair<int, int>> &a) {
  int n = a.size();
  int dp[101] = {};
  rep(i, n) {
    int value = a[i].first, price = a[i].second;
    for (int j = yosan; j >= price; j--)
      dp[j] = max(dp[j], dp[j - price] + value);
  }
  int ans = max_element(dp, dp + yosan + 1) - dp;
  printf("%d %d\n", dp[ans], ans);
}

int main() {
  for (int n, days, yosan; ~scanf("%d%d%d", &n, &days, &yosan);) {
    vector<pair<int, int>> kanmi[100];
    rep(i, n) {
      int m;
      scanf("%d", &m);
      kanmi[i].resize(m);
      rep(j, m) scanf("%d%d", &kanmi[i][j].first, &kanmi[i][j].second);
    }

    vector<pair<int, int>> item;
    rep(t, days) {
      int id;
      scanf("%d", &id);
      item.insert(item.end(), kanmi[id].begin(), kanmi[id].end());
    }

    knapsack(yosan, item);
  }

  return 0;
}

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

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

using namespace std;

void knapsack(int yosan, const vector<pair<int, int>> &a) {
  int n = a.size();
  int dp[301] = {};
  rep(i, n) {
    int value = a[i].first, price = a[i].second;
    for (int j = yosan; j >= price; j--)
      dp[j] = max(dp[j], dp[j - price] + value);
  }
  int ans = max_element(dp, dp + yosan + 1) - dp;
  printf("%d %d\n", dp[ans], ans);
}

int main() {
  for (int n, days, yosan; ~scanf("%d%d%d", &n, &days, &yosan);) {
    vector<pair<int, int>> kanmi[100];
    rep(i, n) {
      int m;
      scanf("%d", &m);
      kanmi[i].resize(m);
      rep(j, m) scanf("%d%d", &kanmi[i][j].first, &kanmi[i][j].second);
    }

    vector<pair<int, int>> item;
    rep(t, days) {
      int id;
      scanf("%d", &id);
      item.insert(item.end(), kanmi[id].begin(), kanmi[id].end());
    }

    knapsack(yosan, item);
  }

  return 0;
}
Problem p00678 (C++)

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

using namespace std;

double EPS = 1e-10;

double add(double a, double b) {
  if (abs(a + b) < EPS * (abs(a) + abs(b)))
    return 0;
  return a + b;
}

struct point {
  double x, y;
  point() {}
  point(double x, double y) : x(x), y(y) {}
  point operator+(point p) { return point(add(x, p.x), add(y, p.y)); }
  point operator-(point p) { return point(add(x, -p.x), add(y, -p.y)); }
  point operator*(double d) { return point(x * d, y * d); }
  point operator/(double d) { return point(x / d, y / d); }
};

double dist(point a, point b) {
  return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}

int main(void) {
  int n, x, y, v;
  vector<point> P;
  vector<double> V;

  while (cin >> n, n) {
    P.clear();
    V.clear();

    for (int i = 0; i < n; i++) {
      cin >> x >> y >> v;
      P.push_back(point(x, y));
      V.push_back(v);
    }

    double r = 0.99999;
    point now(0, 0);

    for (double d = 10; d > EPS; d *= r) {
      int mx = 0;
      double mxd = 0;

      for (int j = 0; j < P.size(); j++) {
        if (dist(now, P[j]) / V[j] > mxd)
          mx = j, mxd = dist(now, P[j]) / V[j];
      }

      now = now + ((P[mx] - now) / dist(P[mx], now) * d);
    }

    double ans = 0;
    for (int i = 0; i < P.size(); i++) {
      ans = max(ans, dist(now, P[i]) / V[i]);
    }
    printf("%.8f\n", ans);
  }

  return 0;
}

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

using namespace std;

double EPS = 1e-10;

double add(double a, double b) {
  if (abs(a + b) < EPS * (abs(a) + abs(b)))
    return 0;
  return a + b;
}

struct point {
  double x, y;
  point() {}
  point(double x, double y) : x(x), y(y) {}
  point operator+(point p) { return point(add(x, p.x), add(y, p.y)); }
  point operator-(point p) { return point(add(x, -p.x), add(y, -p.y)); }
  point operator*(double d) { return point(x * d, y * d); }
  point operator/(double d) { return point(x / d, y / d); }
};

double dist(point a, point b) {
  return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}

int main(void) {
  int n, x, y, v;
  vector<point> P;
  vector<double> V;

  while (cin >> n, n) {
    P.clear();
    V.clear();

    for (int i = 0; i < n; i++) {
      cin >> x >> y >> v;
      P.push_back(point(x, y));
      V.push_back(v);
    }

    double r = 0.999;
    point now(0, 0);

    for (double d = 10; d > EPS; d *= r) {
      int mx = 0;
      double mxd = 0;

      for (int j = 0; j < P.size(); j++) {
        if (dist(now, P[j]) / V[j] > mxd)
          mx = j, mxd = dist(now, P[j]) / V[j];
      }

      now = now + ((P[mx] - now) / dist(P[mx], now) * d);
    }

    double ans = 0;
    for (int i = 0; i < P.size(); i++) {
      ans = max(ans, dist(now, P[i]) / V[i]);
    }
    printf("%.8f\n", ans);
  }

  return 0;
}
Problem p00686 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
using namespace std;
int main() {
  int w, h;
  int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    string a;
    int s = 0;
    int x = 1, y = 1, z;
    while (true) {
      cin >> a;
      if (a == "STOP")
        break;
      if (a == "RIGHT")
        s = (s + 1) % 4;
      if (a == "LEFT")
        s = (s + 3) % 4;
      if (a == "FORWARD") {
        cin >> z;
        x += dx[s] * z, y += dy[s] * z;
        if (x < 1)
          x = 1;
        if (x > w)
          x = w;
        if (y < 1)
          y = 1;
        if (y > h)
          y = h;
      }
      if (a == "BACKWARD") {
        cin >> z;
        x -= dx[s] * z, y -= dy[s] * z;
        if (x < 1)
          x = 1;
        if (x > w)
          x = w;
        if (y < 1)
          y = 1;
        if (y > h)
          y = h;
      }
    }
    cout << x << " " << y << endl;
  }
  while (true) {
    cin >> w;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;
int main() {
  int w, h;
  int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    string a;
    int s = 0;
    int x = 1, y = 1, z;
    while (true) {
      cin >> a;
      if (a == "STOP")
        break;
      if (a == "RIGHT")
        s = (s + 1) % 4;
      if (a == "LEFT")
        s = (s + 3) % 4;
      if (a == "FORWARD") {
        cin >> z;
        x += dx[s] * z, y += dy[s] * z;
        if (x < 1)
          x = 1;
        if (x > w)
          x = w;
        if (y < 1)
          y = 1;
        if (y > h)
          y = h;
      }
      if (a == "BACKWARD") {
        cin >> z;
        x -= dx[s] * z, y -= dy[s] * z;
        if (x < 1)
          x = 1;
        if (x > w)
          x = w;
        if (y < 1)
          y = 1;
        if (y > h)
          y = h;
      }
    }
    cout << x << " " << y << endl;
  }
  return 0;
}
Problem p00687 (C++)

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

#define loop(n, i) for (int i = 0; i < n; i++)
#define loop_from_to(from, to, i) for (int i = from; i <= to; i++)
#define repeat(n) for (int i__ = 0; i__ < n; i__++)
#define multi_cin(n, array)                                                    \
  loop(n, i) { cin >> array[i]; }
#define assign_if_smaller(var, val)                                            \
  if (var > val)                                                               \
    var = val;
#define assign_if_larger(var, val)                                             \
  if (var < val)                                                               \
    var = val;
#define each(itr, c)                                                           \
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
#define r_each(itr, c)                                                         \
  for (__typeof((c).rbegin()) itr = (c).rbegin(); itr != (c).rend(); itr++)

using namespace std;

int main(int argc, char const *argv[]) {
  while (1) {
    int n, a, b;
    cin >> n >> a >> b;
    if (!n && !a && !b)
      break;

    bool c[n];
    fill(c, c + n, false);
    c[a - 1] = true;
    c[b - 1] = true;

    int count = 0;
    loop(n, i) {
      if (!c[i])
        count++;
      else {
        if (i + a < n)
          c[i + a] = true;
        if (i + b < n)
          c[i + b] = true;
      }
    }
    cout << count << endl;
  }

  return 0;
}

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

#define loop(n, i) for (int i = 0; i < n; i++)
#define loop_from_to(from, to, i) for (int i = from; i <= to; i++)
#define repeat(n) for (int i__ = 0; i__ < n; i__++)
#define multi_cin(n, array)                                                    \
  loop(n, i) { cin >> array[i]; }
#define assign_if_smaller(var, val)                                            \
  if (var > val)                                                               \
    var = val;
#define assign_if_larger(var, val)                                             \
  if (var < val)                                                               \
    var = val;
#define each(itr, c)                                                           \
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
#define r_each(itr, c)                                                         \
  for (__typeof((c).rbegin()) itr = (c).rbegin(); itr != (c).rend(); itr++)

using namespace std;

int main(int argc, char const *argv[]) {
  while (1) {
    int n, a, b;
    cin >> n >> a >> b;
    if (!n && !a && !b)
      break;

    bool c[n];
    fill(c, c + n, false);
    if (a - 1 < n)
      c[a - 1] = true;
    if (b - 1 < n)
      c[b - 1] = true;

    int count = 0;
    loop(n, i) {
      if (!c[i])
        count++;
      else {
        if (i + a < n)
          c[i + a] = true;
        if (i + b < n)
          c[i + b] = true;
      }
    }
    cout << count << endl;
  }

  return 0;
}
Problem p00687 (C++)

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

using namespace std;

char c[1000001] = {0};

int main() {
  for (;;) {
    int n, a, b;

    scanf("%d %d %d", &n, &a, &b);
    if (n == 0 && a == 0 && b == 0)
      break;

    memset(c, 0, sizeof(c));
    for (int i = 0; a * i <= n; ++i) {
      for (int j = 0; a * i + b * j <= n; ++j) {
        c[a * i + b * j] = 1;
        // printf("  %d, %d\n", i, j);
      }
    }

    int t = 0;
    for (int i = 1; i <= n; ++i)
      if (c[i] == 0)
        ++t;

    printf("%d\n", t);
  }

  return 0;
}

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

using namespace std;

char c[1000001] = {0};

int main() {
  for (;;) {
    int n, a, b;

    scanf("%d %d %d", &n, &a, &b);
    if (n == 0 && a == 0 && b == 0)
      break;

    memset(c, 0, sizeof(c));
    c[0] = 1;
    for (int i = 0; i <= n; ++i) {
      if (c[i] == 1) {
        if (i + a <= n)
          c[i + a] = 1;
        if (i + b <= n)
          c[i + b] = 1;
      }
    }

    int t = 0;
    for (int i = 1; i <= n; ++i)
      if (c[i] == 0)
        ++t;

    printf("%d\n", t);
  }

  return 0;
}
Problem p00687 (C++)

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

typedef vector<int> Seq;

const int NMAX = 1000100;

int solve(int a, int b, int n) {
  bool countable[NMAX];
  REP(i, n + 1) { countable[i] = false; }
  countable[0] = true;

  int unables = 0;
  REP(i, n + 1) {
    if (countable[i]) {
      countable[i + a] = true;
      countable[i + b] = true;
    } else {
      unables++;
    }
  }
  return unables;
}

int main() {
  int a, b, n;
  while (cin >> n >> a >> b and n + a + b) {
    cout << solve(a, b, n) << endl;
  }
}

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

typedef vector<int> Seq;

const int NMAX = 2000100;

int solve(int a, int b, int n) {
  bool countable[NMAX];
  REP(i, n + 1) { countable[i] = false; }
  countable[0] = true;

  int unables = 0;
  REP(i, n + 1) {
    if (countable[i]) {
      countable[i + a] = true;
      countable[i + b] = true;
    } else {
      unables++;
    }
  }
  return unables;
}

int main() {
  int a, b, n;
  while (cin >> n >> a >> b and n + a + b) {
    cout << solve(a, b, n) << endl;
  }
}
Problem p00687 (C++)

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

using namespace std;

int main() {
  int n, a, b;
  bool t[1000001];
  while (cin >> n >> a >> b && (n + a + b)) {
    fill(t, t + 1000001, false);
    t[0] = true;
    for (int i = 0; i <= n; i++) {
      if (t[i]) {
        t[i + a] = t[i + b] = true;
      }
    }
    a = 0;
    for (int i = 1; i <= n; i++) {
      if (!t[i]) {
        a++;
      }
    }

    cout << a << endl;
  }

  return 0;
}

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

using namespace std;

int main() {
  int n, a, b;
  bool t[1000001];
  while (cin >> n >> a >> b && (n + a + b)) {
    fill(t, t + 1000001, false);
    t[0] = true;
    for (int i = 0; i <= n; i++) {
      if (t[i]) {
        if (i + a <= n) {
          t[i + a] = true;
        }
        if (i + b <= n) {
          t[i + b] = true;
        }
      }
    }
    a = 0;
    for (int i = 1; i <= n; i++) {
      if (!t[i]) {
        a++;
      }
    }

    cout << a << endl;
  }

  return 0;
}