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

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

template <class t> void vin(vector<t> &v, int n) {
  v.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
}

const int MAX_N = 100001;

int prime[MAX_N];
bool is_prime[MAX_N + 1];

void eratos(int n) {
  int p = 0;
  for (int i = 0; i <= n; ++i)
    is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; ++i) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
}

int main() {
  eratos(MAX_N);
  double m, a, b;
  while (cin >> m >> a >> b and (m or a or b)) {
    double best = 0, P = 0, Q = 0;
    for (double p = 2; p <= m; ++p) {
      if (!is_prime[(int)p])
        continue;
      for (double q = 2; q <= m; ++q) {
        if (!is_prime[(int)q])
          continue;
        if (a / b > p / q or p / q > 1.0)
          continue;
        if (p * q > m)
          continue;
        if (best < p * q) {
          best = p * q;
          P = p;
          Q = q;
        }
      }
    }
    cout << P << ' ' << Q << endl;
  }
  return 0;
}


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

template <class t> void vin(vector<t> &v, int n) {
  v.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
}

const int MAX_N = 100001;

int prime[MAX_N];
bool is_prime[MAX_N + 1];

void eratos(int n) {
  int p = 0;
  for (int i = 0; i <= n; ++i)
    is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; ++i) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
}

int main() {
  eratos(MAX_N);
  double m, a, b;
  while (cin >> m >> a >> b and (m or a or b)) {
    double best = 0, P = 0, Q = 0;
    for (double p = 2; p <= m; ++p) {
      if (!is_prime[(int)p])
        continue;
      int qq = m / p;
      for (double q = qq; q >= 2; --q) {
        if (is_prime[(int)q]) {
          if (a / b > p / q or p / q > 1.0)
            continue;
          if (best < p * q) {
            best = p * q;
            P = p;
            Q = q;
          }
          break;
        }
      }
    }
    cout << P << ' ' << Q << endl;
  }
  return 0;
}
Problem p00811 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

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

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

  vector<ll> ps;
  for (int i = 2; i <= 100000; i++) {
    if (isPrime(i))
      ps.push_back(i);
  }

  ll m, a, b;
  while (cin >> m >> a >> b, m) {
    ll mx = -1, mp = -1, mq = -1;
    for (int i = 0; i < ps.size(); i++) {
      for (int j = i; j < ps.size(); j++) {
        ll p = ps[i], q = ps[j];
        if (p * q <= m && a * q <= b * p) {
          if (mx < p * q) {
            mx = p * q;
            mp = p;
            mq = q;
          }
        }
      }
    }
    cout << mp << " " << mq << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

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

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

  vector<ll> ps;
  for (int i = 2; i <= 100000; i++) {
    if (isPrime(i))
      ps.push_back(i);
  }

  ll m, a, b;
  while (cin >> m >> a >> b, m) {
    ll mx = -1, mp = -1, mq = -1;
    for (int i = 0; i < ps.size(); i++) {
      for (int j = i; j < ps.size(); j++) {
        ll p = ps[i], q = ps[j];
        if (p * q <= m && a * q <= b * p) {
          if (mx < p * q) {
            mx = p * q;
            mp = p;
            mq = q;
          }
        } else
          break;
      }
    }
    cout << mp << " " << mq << endl;
  }
}
Problem p00811 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
int m, a, b;
bool prime[100001];
vector<int> pp;
int main(void) {
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;
  for (int i = 2; i <= 100000; i++) {
    if (prime[i]) {
      pp.push_back(i);
      for (int j = i * 2; j <= 100000; j += i) {
        prime[j] = false;
      }
    }
  }
  while (1) {
    scanf("%d %d %d", &m, &a, &b);
    if (m + a + b == 0)
      break;
    int rp = 0, rq = 0;
    for (int i = 0; i < pp.size(); i++) {
      int k = lower_bound(pp.begin(), pp.end(), m / pp[i]) - pp.begin();
      for (int j = max(k - 2, i); j < pp.size(); j++) {
        if (pp[i] * pp[j] >= m)
          break;
        if (a * pp[j] > b * pp[i])
          continue;
        if (rp * rq < pp[i] * pp[j]) {
          rp = pp[i];
          rq = pp[j];
        }
      }
    }
    printf("%d %d\n", rp, rq);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
int m, a, b;
bool prime[100001];
vector<int> pp;
int main(void) {
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;
  for (int i = 2; i <= 100000; i++) {
    if (prime[i]) {
      pp.push_back(i);
      for (int j = i * 2; j <= 100000; j += i) {
        prime[j] = false;
      }
    }
  }
  while (1) {
    scanf("%d %d %d", &m, &a, &b);
    if (m + a + b == 0)
      break;
    int rp = 0, rq = 0;
    for (int i = 0; i < pp.size(); i++) {
      if (pp[i] * pp[i] > m)
        break;
      for (int j = i; j < pp.size(); j++) {
        if (pp[i] * pp[j] > m)
          break;
        if (a * pp[j] > b * pp[i])
          continue;
        if (rp * rq < pp[i] * pp[j]) {
          rp = pp[i];
          rq = pp[j];
        }
      }
    }
    printf("%d %d\n", rp, rq);
  }
  return 0;
}
Problem p00811 (C++)

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

using namespace std;

#define MMAX 100000
#define YES 0
#define NO 1

struct S {
  int p, q, pq;
};
int main() {

  int m, a, b;
  int hurui[MMAX + 1] = {YES};
  int prime[MMAX + 1];
  int cnt = 0;
  S ans = {-1, -1, -1};

  hurui[0] = NO, hurui[1] = NO;
  for (int i = 2; i < MMAX; i++) {
    if (hurui[i] == YES) {
      prime[cnt] = i;
      cnt++;
      for (int j = i * 2; j < MMAX; j += i) {
        hurui[j] = NO;
      }
    }
  }

  while (1) {

    cin >> m >> a >> b;
    if (a == 0 && b == 0 && m == 0)
      break;

    for (int i = 0; i < cnt; i++) {
      for (int j = 0; j < cnt; j++) {
        if (prime[i] * prime[j] <= m &&
            (double)prime[i] / prime[j] >= (double)a / b &&
            (double)prime[i] / prime[j] <= 1) {
          if (ans.pq < prime[i] * prime[j]) {
            ans.p = prime[i], ans.q = prime[j], ans.pq = prime[i] * prime[j];
          }
        }
      }
    }
    cout << ans.p << " " << ans.q << endl;
    ans.p = -1, ans.q = -1, ans.pq = -1;
  }
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

#define MMAX 100000
#define YES 0
#define NO 1

struct S {
  int p, q, pq;
};
int main() {

  int m, a, b;
  int hurui[MMAX + 1] = {YES};
  int prime[MMAX + 1];
  int cnt = 0;
  S ans = {-1, -1, -1};

  hurui[0] = NO, hurui[1] = NO;
  for (int i = 2; i < MMAX; i++) {
    if (hurui[i] == YES) {
      prime[cnt] = i;
      cnt++;
      for (int j = i * 2; j < MMAX; j += i) {
        hurui[j] = NO;
      }
    }
  }

  while (1) {

    cin >> m >> a >> b;
    if (a == 0 && b == 0 && m == 0)
      break;

    for (int i = 0; m >= prime[i] * prime[0]; i++) {
      for (int j = 0; m >= prime[i] * prime[j]; j++) {
        if ((double)prime[i] / prime[j] >= (double)a / b &&
            (double)prime[i] / prime[j] <= 1) {
          if (ans.pq < prime[i] * prime[j]) {
            ans.p = prime[i], ans.q = prime[j], ans.pq = prime[i] * prime[j];
          }
        }
      }
    }
    cout << ans.p << " " << ans.q << endl;
    ans.p = -1, ans.q = -1, ans.pq = -1;
  }
  return 0;
}
Problem p00811 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define fir first
#define sec second
#define PB push_back
#define MP make_pair
using namespace std;
int SIZE = 1e5;
int main() {
  int prime[SIZE + 1];
  vector<int> V;
  memset(prime, 1, sizeof(prime));
  prime[0] = prime[1] = 0;
  REP(i, SIZE + 1) {
    if (prime[i]) {
      for (int j = i * 2; j <= SIZE; j += i)
        prime[j] = 0;
      V.PB(i);
    }
  }
  REP(i, SIZE) {
    if (prime[i + 1])
      prime[i + 1] = i + 1;
    else
      prime[i + 1] = prime[i];
  }
  while (1) {
    int m, a, b;
    cin >> m >> a >> b;
    if (m + a + b == 0)
      break;
    pair<int, int> ans;
    ans = MP(-1, 1);
    REP(i, V.size()) {
      if (m / V[i] <= 1)
        break;
      int p = V[i];
      int q[3];
      q[0] = p;
      q[1] = prime[m / V[i]];
      q[2] = prime[p * b / a];
      REP(i, 3) {
        if (q[i] >= p && p * b >= q[i] * a && q[i] * p <= m) {
          if (ans.fir * ans.sec < p * q[i])
            ans = MP(p, q[i]);
        }
      }
    }
    cout << ans.fir << " " << ans.sec << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define fir first
#define sec second
#define PB push_back
#define MP make_pair
using namespace std;
int SIZE = 1e5;
int main() {
  int prime[SIZE + 1];
  vector<int> V;
  memset(prime, 1, sizeof(prime));
  prime[0] = prime[1] = 0;
  REP(i, SIZE + 1) {
    if (prime[i]) {
      for (int j = i * 2; j <= SIZE; j += i)
        prime[j] = 0;
      V.PB(i);
    }
  }
  REP(i, SIZE) {
    if (prime[i + 1])
      prime[i + 1] = i + 1;
    else
      prime[i + 1] = prime[i];
  }
  while (1) {
    int m, a, b;
    cin >> m >> a >> b;
    if (m + a + b == 0)
      break;
    pair<int, int> ans;
    ans = MP(-1, 1);
    REP(i, V.size()) {
      if (m / V[i] <= 1)
        break;
      int p = V[i];
      int q[3];
      q[0] = p;
      q[1] = prime[m / V[i]];
      q[2] = prime[min(SIZE, p * b / a)];
      REP(j, 3) {
        if (q[j] >= p && p * b >= q[j] * a && q[j] * p <= m) {
          if (ans.fir * ans.sec < p * q[j])
            ans = MP(p, q[j]);
        }
      }
    }
    cout << ans.fir << " " << ans.sec << endl;
  }
}
Problem p00811 (C++)

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

const int MAX = 100001;

vector<int> prime;

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

signed main() {
  for (int i = 2; i < MAX; i++) {
    if (is_prime(i))
      prime.push_back(i);
  }
  while (true) {
    int m, a, b;
    cin >> m >> a >> b;
    if (!m && !a && !b)
      return 0;
    int p, q;
    p = q = 1;
    for (int i = 0; i < prime.size(); i++) {
      int ptmp = prime[i];
      for (int j = i; j < prime.size(); j++) {
        int qtmp = prime[j];
        if (ptmp * qtmp > m)
          break;
        double res = (double)ptmp / qtmp;
        if (res < (double)a / b)
          break;
        if (ptmp * qtmp > p * q) {
          p = ptmp;
          q = qtmp;
        }
      }
    }
    cout << p << " " << q << endl;
  }
}


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

const int MAX = 50000;

vector<int> prime;

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

signed main() {
  for (int i = 2; i < MAX; i++) {
    if (is_prime(i))
      prime.push_back(i);
  }
  while (true) {
    int m, a, b;
    cin >> m >> a >> b;
    if (!m && !a && !b)
      return 0;
    int p, q;
    p = q = 1;
    for (int i = 0; i < prime.size(); i++) {
      int ptmp = prime[i];
      for (int j = i; j < prime.size(); j++) {
        int qtmp = prime[j];
        if (ptmp * qtmp > m)
          break;
        double res = (double)ptmp / qtmp;
        if (res < (double)a / b)
          break;
        if (ptmp * qtmp > p * q) {
          p = ptmp;
          q = qtmp;
        }
      }
    }
    cout << p << " " << q << endl;
  }
}
Problem p00811 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> v;
int main() {
  for (int i = 2; i < 50000; i++) {
    bool ok = true;
    for (int j = 0; j < v.size(); j++) {
      if (i % v[j] == 0)
        ok = false;
    }
    if (ok)
      v.push_back(i);
  }
  int m, a, b;
  while (cin >> m >> a >> b && m) {
    int p = -1, q = 0;
    for (int i = 0; i < v.size(); i++) {
      for (int j = i; j < v.size(); j++) {
        if (v[j] <= b * v[i] / a && v[i] * v[j] <= m) {
          if (p * q < v[i] * v[j])
            p = v[i], q = v[j];
        }
      }
    }
    cout << p << " " << q << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> v;
int main() {
  for (int i = 2; i < 50000; i++) {
    bool ok = true;
    for (int j = 0; j < v.size(); j++) {
      if (i % v[j] == 0)
        ok = false;
    }
    if (ok)
      v.push_back(i);
  }
  int m, a, b;
  while (cin >> m >> a >> b && m) {
    int p = -1, q = 0;
    for (int i = 0; i < v.size(); i++) {
      for (int j = i; j < v.size(); j++) {
        if (v[i] * v[j] > m)
          break;
        if (v[j] <= b * v[i] / a) {
          if (p * q < v[i] * v[j])
            p = v[i], q = v[j];
        }
      }
    }
    cout << p << " " << q << endl;
  }
  return 0;
}
Problem p00811 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int main() {
  int m, a, b, i, j;
  for (i = 2; i < 50000; i++) {
    bool ok = 1;
    for (j = 0; j < v.size(); j++)
      if (!i % v[j])
        ok = 0;
    if (ok)
      v.push_back(i);
  }
  while (cin >> m >> a >> b && m) {
    int p = 0, q = 0;
    for (i = 0; i < v.size(); i++) {
      for (j = i; j < v.size(); j++) {
        if (v[i] * v[j] > m)
          break;
        if (v[j] <= b * v[i] / a && p * q < v[i] * v[j])
          p = v[i], q = v[j];
      }
    }
    cout << p << " " << q << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int main() {
  int m, a, b, i, j;
  for (i = 2; i < 50000; i++) {
    bool ok = 1;
    for (j = 0; j < v.size(); j++)
      if (i % v[j] == 0)
        ok = 0;
    if (ok)
      v.push_back(i);
  }
  while (cin >> m >> a >> b && m) {
    int p = 0, q = 0;
    for (i = 0; i < v.size(); i++) {
      for (j = i; j < v.size(); j++) {
        if (v[i] * v[j] > m)
          break;
        if (v[j] <= b * v[i] / a && p * q < v[i] * v[j])
          p = v[i], q = v[j];
      }
    }
    cout << p << " " << q << endl;
  }
  return 0;
}
Problem p00811 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (i = 0; i < n; i++)
#define loop(i, a, n) for (i = a; i < n; i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define int ll
typedef vector<int> vi;
typedef pair<int, int> pii;

signed main(void) {
  int i, j;
  int m, a, b;
  vi p(100005, true);
  for (i = 2; i < 100005; i++)
    if (p[i])
      for (j = 2; i * j < 100005; j++)
        p[i * j] = false;
  vi prime;
  loop(i, 2, 100005) if (p[i]) prime.push_back(i);
  while (1) {
    cin >> m >> a >> b;
    if (m + a + b == 0)
      break;
    int g = __gcd(a, b);
    a /= g;
    b /= g;
    int x, y;
    int ans = 0;
    rep(i, prime.size())
        loop(j, i, prime.size()) if (prime[i] * prime[j] <= m &&
                                     prime[i] * prime[j] > ans &&
                                     b * prime[i] >= a * prime[j]) {
      ans = prime[i] * prime[j];
      x = prime[i];
      y = prime[j];
      //	cout<<x<<" "<<y<<" "<<ans<<endl;
    }
    cout << x << " " << y << endl;
  }
}

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (i = 0; i < n; i++)
#define loop(i, a, n) for (i = a; i < n; i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define int ll
typedef vector<int> vi;
typedef pair<int, int> pii;

signed main(void) {
  int i, j;
  int m, a, b;
  vi p(100005, true);
  for (i = 2; i < 100005; i++)
    if (p[i])
      for (j = 2; i * j < 100005; j++)
        p[i * j] = false;
  vi prime;
  loop(i, 2, 100005) if (p[i]) prime.push_back(i);
  while (1) {
    cin >> m >> a >> b;
    if (m + a + b == 0)
      break;
    int g = __gcd(a, b);
    a /= g;
    b /= g;
    int x, y;
    int ans = 0;
    rep(i, prime.size())
        loop(j, i, prime.size()) if (prime[i] * prime[j] > m ||
                                     b * prime[i] < a * prime[j]) break;
    else if (prime[i] * prime[j] > ans) {
      ans = prime[i] * prime[j];
      x = prime[i];
      y = prime[j];
      //	cout<<x<<" "<<y<<" "<<ans<<endl;
    }
    cout << x << " " << y << endl;
  }
}
Problem p00811 (C++)

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

bool isprime[100001];

void eratos(int n) {

  for (int i = 0; i <= n; i++) {
    isprime[i] = true;
  }
  isprime[0] = isprime[1] = false;
  for (int i = 2; i <= sqrt(n); i++) {
    if (isprime[i]) {
      int j = i + i;
      while (j <= n) {
        isprime[j] = false;
        j = j + i;
      }
    }
  }
}
int main() {

  eratos(100000);

  int m, a, b;

  while (1) {

    cin >> m >> a >> b;
    if (m == 0 && a == 0 && b == 0)
      break;

    for (int i = m; i > 0; i--) {
      bool flag = false;
      for (int j = 1; j < i; j++) {
        if (i % j != 0)
          continue;
        if (isprime[j] == 0 || isprime[i / j] == 0)
          continue;
        if ((double)a / b > (double)j / (i / j) || (double)j / (i / j) > 1)
          continue;
        cout << j << " " << i / j << endl;
        flag = true;
        break;
      }
      if (flag)
        break;
    }
  }

  return 0;
}

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

bool isprime[100001];

void eratos(int n) {

  for (int i = 0; i <= n; i++) {
    isprime[i] = true;
  }
  isprime[0] = isprime[1] = false;
  for (int i = 2; i <= sqrt(n); i++) {
    if (isprime[i]) {
      int j = i + i;
      while (j <= n) {
        isprime[j] = false;
        j = j + i;
      }
    }
  }
}
int main() {

  eratos(100000);

  int m, a, b;

  while (1) {

    cin >> m >> a >> b;
    if (m == 0 && a == 0 && b == 0)
      break;

    for (int i = m; i > 0; i--) {
      bool flag = false;
      for (int j = 1; j <= sqrt(i); j++) {
        if (i % j != 0)
          continue;
        if (isprime[j] == 0 || isprime[i / j] == 0)
          continue;
        if ((double)a / b > (double)j / (i / j) || (double)j / (i / j) > 1)
          continue;
        cout << j << " " << i / j << endl;
        flag = true;
        break;
      }
      if (flag)
        break;
    }
  }

  return 0;
}
Problem p00811 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
#define M 100000
using namespace std;
bool prime[M];
void primeInit() {
  for (int i = 0; i < M; i++) {
    prime[i] = false;
  }
}
void p() {
  prime[2] = true;
  for (int i = 3; i < M; i++) {
    bool flag = false;
    for (int j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        flag = true;
        break;
      }
    }
    if (!flag)
      prime[i] = true;
  }
}
int main() {
  int m, a, b;
  primeInit();
  p();
  while (cin >> m >> a >> b && m != 0 || a != 0 || b != 0) {
    double ma = 0;
    int mp = 0;
    int mq = 0;
    for (int p = 2; p <= m / 2; p++) {
      if (!prime[p])
        continue;
      for (int q = 2; q <= m / 2; q++) {
        if (!prime[q])
          continue;
        if (1.0 * p * q > ma && 1.0 * p * q <= m && (1.0 * p / q) <= 1 &&
            (1.0 * p / q) >= (a * 1.0 / b)) {
          ma = p * q;
          mp = p;
          mq = q;
        }
      }
    }

    cout << mp << " " << mq << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
#define M 100000
using namespace std;
bool prime[M];
void primeInit() {
  for (int i = 0; i < M; i++) {
    prime[i] = false;
  }
}
void p() {
  prime[2] = true;
  for (int i = 3; i < M; i++) {
    bool flag = false;
    for (int j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        flag = true;
        break;
      }
    }
    if (!flag)
      prime[i] = true;
  }
}
int main() {
  int m, a, b;
  primeInit();
  p();
  while (cin >> m >> a >> b && m != 0 || a != 0 || b != 0) {
    double ma = 0;
    int mp = 0;
    int mq = 0;
    for (int p = 2; p <= m / 2; p++) {
      if (!prime[p])
        continue;
      for (int q = 2; q <= m / p; q++) {
        if (!prime[q])
          continue;
        if (1.0 * p * q > ma && 1.0 * p * q <= m && (1.0 * p / q) <= 1 &&
            (1.0 * p / q) >= (a * 1.0 / b)) {
          ma = p * q;
          mp = p;
          mq = q;
        }
      }
    }

    cout << mp << " " << mq << endl;
  }
  return 0;
}
Problem p00811 (C++)

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

using namespace std;

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

  // sieve of eratosthenes
  vector<int> pnums(1000000, 0);
  for (int i = 2; i < 1000000; i++)
    pnums[i] = 1;
  for (int i = 2; i * i < 1000000; i++) {
    if (pnums[i]) {
      for (int j = i * i; j < 1000000; j += i) {
        pnums[j] = 0;
      }
    }
  }
  vector<int> primes;
  for (int i = 2; i < 100000; i++)
    if (pnums[i])
      primes.push_back(i);
  int m, a, b;
  while (cin >> m >> a >> b, m || a || b) {
    int p = 0, q = 0, ma = p * q;
    for (int i = 0; i < (int)primes.size(); i++) {
      for (int j = i; j < (int)primes.size(); j++) {
        if (primes[i] * primes[j] > m || a * primes[j] > b * primes[i])
          continue;
        if (ma < primes[i] * primes[j]) {
          p = primes[i];
          q = primes[j];
          ma = primes[i] * primes[j];
        }
      }
    }
    cout << p << " " << q << endl;
  }
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;

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

  // sieve of eratosthenes
  vector<int> pnums(1000000, 0);
  for (int i = 2; i < 1000000; i++)
    pnums[i] = 1;
  for (int i = 2; i * i < 1000000; i++) {
    if (pnums[i]) {
      for (int j = i * i; j < 1000000; j += i) {
        pnums[j] = 0;
      }
    }
  }
  vector<int> primes;
  for (int i = 2; i < 10000; i++)
    if (pnums[i])
      primes.push_back(i);
  int m, a, b;
  while (cin >> m >> a >> b, m || a || b) {
    int p = 0, q = 0, ma = p * q;
    for (int i = 0; i < (int)primes.size(); i++) {
      for (int j = i; j < (int)primes.size(); j++) {
        if (primes[i] * primes[j] > m || a * primes[j] > b * primes[i])
          continue;
        if (ma < primes[i] * primes[j]) {
          p = primes[i];
          q = primes[j];
          ma = primes[i] * primes[j];
        }
      }
    }
    cout << p << " " << q << endl;
  }
}
Problem p00811 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int stoi(string x) {
  stringstream ss;
  ss << x;
  int tmp;
  ss >> tmp;
  return tmp;
}
string itos(int x) {
  stringstream ss;
  ss << x;
  return ss.str();
}
vector<int> ta;
bool is(int n) {
  if (n == 2)
    return true;
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
void init() {
  int i = 2;
  while (ta.size() <= 100000) {
    if (is(i))
      ta.push_back(i);
    i++;
  }
}
int m, a, b, a1 = 0, a2 = 0;
int main() {
  init();
  // for(int i=0;i<10;i++)cout<<ta[i]<<" ";cout<<endl;
  while (true) {
    cin >> m >> a >> b;
    if (m + a + b == 0)
      break;
    a1 = 0, a2 = 0;
    for (int i = 0; ta[i] <= m; i++) {
      for (int j = 0; ta[j] <= m; j++) {
        // if(i>j)continue;
        int p = ta[j], q = ta[i];
        double a_b = (double)a / (double)b, p_q = (double)p / (double)q;
        if (p * q <= m && a_b <= p_q && p_q <= 1.0 && a1 * a2 < p * q) {
          a1 = min(q, p), a2 = max(p, q);
        }
      }
    }
    cout << a1 << " " << a2 << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int stoi(string x) {
  stringstream ss;
  ss << x;
  int tmp;
  ss >> tmp;
  return tmp;
}
string itos(int x) {
  stringstream ss;
  ss << x;
  return ss.str();
}
vector<int> ta;
bool is(int n) {
  if (n == 2)
    return true;
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
void init() {
  int i = 2;
  while (ta.size() <= 100000) {
    if (is(i))
      ta.push_back(i);
    i++;
  }
}
int m, a, b, a1 = 0, a2 = 0;
int main() {
  init();
  // for(int i=0;i<10;i++)cout<<ta[i]<<" ";cout<<endl;
  while (true) {
    cin >> m >> a >> b;
    if (m + a + b == 0)
      break;
    a1 = 0, a2 = 0;
    for (int i = 0; ta[i] <= m; i++) {
      for (int j = 0; ta[j] <= m; j++) {
        // if(i>j)continue;
        int p = ta[j], q = ta[i];
        double a_b = (double)a / (double)b, p_q = (double)p / (double)q;
        if (p * q <= m && a_b <= p_q && p_q <= 1.0 && a1 * a2 < p * q) {
          a1 = min(q, p), a2 = max(p, q);
        }
        if (p * q > m)
          break;
      }
    }
    cout << a1 << " " << a2 << endl;
  }
  return 0;
}
Problem p00811 (C++)

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

bool is_prime[100001];
void prime(int n) {
  for (int i = 0; i <= n; i++)
    is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i] == true) {
      for (int j = 2 * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
}

int main() {
  int m, a, b;
  prime(100000);
  while (cin >> m >> a >> b, m) {
    int p = 0, q = 0;
    for (int i = 1; i * i <= m; i++) {
      if (!is_prime[i])
        continue;
      for (int j = i; j <= m; j++) {
        if (!is_prime[j])
          continue;
        if (q * p < i * j && i * j <= m && a * j <= i * b && i * b <= b * j) {
          p = j;
          q = i;
        }
      }
    }
    cout << q << " " << p << endl;
  }
  return 0;
}

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

bool is_prime[100001];
void prime(int n) {
  for (int i = 0; i <= n; i++)
    is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i] == true) {
      for (int j = 2 * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
}

int main() {
  int m, a, b;
  prime(100000);
  while (cin >> m >> a >> b, m) {
    int p = 0, q = 0;
    for (int i = 1; i * i <= m; i++) {
      if (!is_prime[i])
        continue;
      for (int j = i; j <= m / i + 1; j++) {
        if (!is_prime[j])
          continue;
        if (q * p < i * j && i * j <= m && a * j <= i * b && i * b <= b * j) {
          p = j;
          q = i;
        }
      }
    }
    cout << q << " " << p << endl;
  }
  return 0;
}
Problem p00811 (C++)

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

const static int M_MAX = 999999;

int main() {
  int era[M_MAX + 1] = {0};

  vector<int> primes;
  era[0] = era[1] = 1;
  for (int i = 2; i <= M_MAX; i++) {
    if (era[i]) {
      continue;
    }
    primes.push_back(i);
    for (int j = i + i; j <= M_MAX; j += i) {
      era[j] = 1;
    }
  }
  int pLen = primes.size();

  while (1) {
    int m, a, b;
    cin >> m >> a >> b;
    if (!m && !a && !b) {
      return EXIT_SUCCESS;
    }

    int ansP = -1, ansQ = -1;
    for (int i = 0; primes[i] <= m; i++) {
      for (int j = i; primes[j] <= m; j++) {
        int p = primes[i];
        int q = primes[j];
        if (p * q <= m && a * q <= b * p) {
          if (ansP == -1 || p * q > ansP * ansQ) {
            ansP = p;
            ansQ = q;
          }
        }
      }
    }
    cout << ansP << " " << ansQ << endl;
  }
}

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

const static int M_MAX = 999999;

int main() {
  int era[M_MAX + 1] = {0};

  vector<int> primes;
  era[0] = era[1] = 1;
  for (int i = 2; i <= M_MAX; i++) {
    if (era[i]) {
      continue;
    }
    primes.push_back(i);
    for (int j = i + i; j <= M_MAX; j += i) {
      era[j] = 1;
    }
  }
  int pLen = primes.size();

  while (1) {
    int m, a, b;
    cin >> m >> a >> b;
    if (!m && !a && !b) {
      return EXIT_SUCCESS;
    }

    int ansP = -1, ansQ = -1;
    for (int i = 0; i < pLen; i++) {
      for (int j = 0; j < i + 1; j++) {
        int p = primes[j];
        int q = primes[i];
        if (p * q > m) {
          break;
        }
        if (a * q <= b * p) {
          if (ansP == -1 || p * q > ansP * ansQ) {
            ansP = p;
            ansQ = q;
          }
        }
      }
    }
    cout << ansP << " " << ansQ << endl;
  }
}
Problem p00811 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

const double EPS = 1.0e-9;

int main() {
  vector<int> prime;
  prime.push_back(2);
  prime.push_back(3);

  for (int i = 4; i < 100000; i++) {
    bool flag = true;

    for (int j = 0; j < prime.size(); j++) {
      if (i % prime[j] == 0) {
        flag = false;
        break;
      }
    }

    if (flag == true) {
      prime.push_back(i);
    }
  }

  while (true) {
    int m, a, b;

    cin >> m >> a >> b;

    if (m == 0 && a == 0 && b == 0) {
      break;
    }

    int ansp, ansq;
    int maxarea = 0;

    rep(i, prime.size()) {
      for (int j = i; j < prime.size(); j++) {
        if (prime[i] * prime[j] <= m &&
            ((double)a / b <= (double)prime[i] / prime[j] ||
             fabs((double)a / b - (double)prime[i] / prime[j]) < EPS) &&
            (double)prime[i] / prime[j] <= 1) {
          if (prime[i] * prime[j] > maxarea) {
            maxarea = prime[i] * prime[j];
            ansp = prime[i];
            ansq = prime[j];
          }
        }
      }
    }

    cout << ansp << " " << ansq << endl;
  }

  return 0;
}

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

const double EPS = 1.0e-9;

int main() {
  vector<int> prime;
  prime.push_back(2);
  prime.push_back(3);

  for (int i = 4; i < 10000; i++) {
    bool flag = true;

    for (int j = 0; j < prime.size(); j++) {
      if (i % prime[j] == 0) {
        flag = false;
        break;
      }
    }

    if (flag == true) {
      prime.push_back(i);
    }
  }

  while (true) {
    int m, a, b;

    cin >> m >> a >> b;

    if (m == 0 && a == 0 && b == 0) {
      break;
    }

    int ansp, ansq;
    int maxarea = 0;

    rep(i, prime.size()) {
      for (int j = i; j < prime.size(); j++) {
        if (prime[i] * prime[j] <= m &&
            ((double)a / b <= (double)prime[i] / prime[j] ||
             fabs((double)a / b - (double)prime[i] / prime[j]) < EPS) &&
            (double)prime[i] / prime[j] <= 1) {
          if (prime[i] * prime[j] > maxarea) {
            maxarea = prime[i] * prime[j];
            ansp = prime[i];
            ansq = prime[j];
          }
        }
      }
    }

    cout << ansp << " " << ansq << endl;
  }

  return 0;
}
Problem p00811 (C++)

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

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

int main() {
  vector<int> ps;
  for (int x = 2; x <= 50000; ++x) {
    if (isPrime(x))
      ps.push_back(x);
  }
  for (int m, a, b; cin >> m >> a >> b && (m | a | b);) {
    int p = 0, q = 0;
    for (int i = 0; i < ps.size(); ++i) {
      for (int j = i; j < ps.size(); ++j) {
        if (ps[i] * ps[j] <= m && a * ps[j] <= b * ps[i] &&
            ps[i] * ps[j] > p * q) {
          p = ps[i];
          q = ps[j];
        }
      }
    }
    cout << p << " " << q << endl;
  }
  return 0;
}

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

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

int main() {
  vector<int> ps;
  for (int x = 2; x <= 50000; ++x) {
    if (isPrime(x))
      ps.push_back(x);
  }
  for (int m, a, b; cin >> m >> a >> b && (m | a | b);) {
    int p = 0, q = 0;
    for (int i = 0; i < ps.size(); ++i) {
      for (int j = i; j < ps.size() && ps[i] * ps[j] <= m; ++j) {
        if (ps[i] * ps[j] <= m && a * ps[j] <= b * ps[i] &&
            ps[i] * ps[j] > p * q) {
          p = ps[i];
          q = ps[j];
        }
      }
    }
    cout << p << " " << q << endl;
  }
  return 0;
}
Problem p00811 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int isPrime(int n) {
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return 0;
  return 1;
}
int main() {
  int m, a, b;
  int i, j, k;
  int memo[200001] = {};
  for (i = 2; i < 200001; i++)
    memo[i] = isPrime(i);
  cin >> m >> a >> b;
  while ((m + a + b) != 0) {
    int p, q, o = 0;
    for (i = 1; i * i <= m; i++) {

      for (j = i; j <= b * i / a; j++) {
        if (memo[i] * memo[j] == 1) {
          if (o < i * j && i * j <= m) {
            o = i * j;
            p = i;
            q = j;
          }
        }
      }
    }
    // cout << o << endl;
    cout << p << " " << q << endl;
    cin >> m >> a >> b;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int isPrime(int n) {
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return 0;
  return 1;
}
int main() {
  int m, a, b;
  int i, j, k;
  int memo[200001] = {};
  for (i = 2; i < 200001; i++)
    memo[i] = isPrime(i);
  cin >> m >> a >> b;
  while ((m + a + b) != 0) {
    int p, q, o = 0;
    for (i = 1; i * i <= m; i++) {

      for (j = i; j <= b * i / a; j++) {
        if (j < 100001) {
          if (memo[i] * memo[j] == 1) {
            if (o < i * j && i * j <= m) {
              o = i * j;
              p = i;
              q = j;
            }
          }
        }
      }
    }
    // cout << o << endl;
    cout << p << " " << q << endl;
    cin >> m >> a >> b;
  }
  return 0;
}
Problem p00811 (C++)

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

using namespace std;

int main() {
  int m, a, b;
  bool pp[100001];
  vector<int> prime;
  {
    memset(pp, true, sizeof(pp));
    pp[0] = pp[1] = false;
    for (int i = 2; i < 100001; i++) {
      if (pp[i]) {
        prime.push_back(i);
        for (int j = i + i; j < 100001; j += i)
          pp[j] = false;
      }
    }
  }

  for (; cin >> m >> a >> b, m;) {
    for (; m > 0;) {
      for (int i = 0; i < prime.size(); i++) {
        if (prime[i] > m)
          continue;
        if (m % prime[i] == 0 && pp[m / prime[i]] &&
            ((double)a / b) <= ((double)(m / prime[i]) / prime[i]) &&
            ((double)(m / prime[i]) / prime[i]) <= 1) {
          cout << m / prime[i] << " " << prime[i] << endl;
          m = -1;
          i = -1;
        }
      }
      m--;
    }
  }
}

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

using namespace std;

int main() {
  int m, a, b;
  bool pp[100001];
  vector<int> prime;
  {
    memset(pp, true, sizeof(pp));
    pp[0] = pp[1] = false;
    for (int i = 2; i < 100001; i++) {
      if (pp[i]) {
        prime.push_back(i);
        for (int j = i + i; j < 100001; j += i)
          pp[j] = false;
      }
    }
  }

  for (; cin >> m >> a >> b, m;) {
    for (; m > 0;) {
      for (int i = 0; i < prime.size(); i++) {
        if (prime[i] > m / 2)
          break;
        if (m % prime[i] == 0 && pp[m / prime[i]] &&
            ((double)a / b) <= ((double)(m / prime[i]) / prime[i]) &&
            ((double)(m / prime[i]) / prime[i]) <= 1) {
          cout << m / prime[i] << " " << prime[i] << endl;
          m = -1;
          i = -1;
        }
      }
      m--;
    }
  }
}
Problem p00816 (C++)

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

using namespace std;

int dp[7][1000000];
int pr[7][1000000];
int df[7][1000000];

int main() {
  while (true) {
    int n;
    string s;
    cin >> n >> s;
    if (n == 0 && s == "0") {
      break;
    }
    const int m = s.size();
    for (int i = 0; i <= m; ++i) {
      for (int j = 0; j <= n; ++j) {
        dp[i][j] = pr[i][j] = df[i][j] = 0;
      }
    }
    dp[0][0] = 1;
    for (int i = 0; i < m; ++i) {
      int x = 0;
      for (int j = i; j < m; ++j) {
        x = (x * 10) + s[j] - '0';
        if (x > n) {
          break;
        }
        for (int k = 0; k + x <= n; ++k) {
          if (dp[i][k] == 0) {
            continue;
          }
          dp[j + 1][k + x] += dp[i][k];
          pr[j + 1][k + x] = i;
          df[j + 1][k + x] = x;
        }
      }
    }
    int answer = -1;
    for (int i = n; answer < 0 && i >= 0; --i) {
      if (dp[m][i] != 0) {
        answer = i;
      }
    }
    if (answer < 0) {
      cout << "error" << endl;
    } else if (dp[m][answer] >= 2) {
      cout << "rejected" << endl;
    } else {
      cout << answer;
      vector<int> cut;
      int r = m, c = answer;
      do {
        const int d = df[r][c];
        cut.push_back(d);
        r = pr[r][c];
        c -= d;
      } while (r > 0);
      for (int i = cut.size() - 1; i >= 0; --i) {
        cout << " " << cut[i];
      }
      cout << endl;
    }
  }
  return 0;
}

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

using namespace std;

unsigned short dp[7][1000000];
unsigned char pr[7][1000000];
int df[7][1000000];

int main() {
  while (true) {
    int n;
    string s;
    cin >> n >> s;
    if (n == 0 && s == "0") {
      break;
    }
    const int m = s.size();
    for (int i = 0; i <= m; ++i) {
      for (int j = 0; j <= n; ++j) {
        dp[i][j] = pr[i][j] = df[i][j] = 0;
      }
    }
    dp[0][0] = 1;
    for (int i = 0; i < m; ++i) {
      int x = 0;
      for (int j = i; j < m; ++j) {
        x = (x * 10) + s[j] - '0';
        if (x > n) {
          break;
        }
        for (int k = 0; k + x <= n; ++k) {
          if (dp[i][k] == 0) {
            continue;
          }
          dp[j + 1][k + x] += dp[i][k];
          pr[j + 1][k + x] = i;
          df[j + 1][k + x] = x;
        }
      }
    }
    int answer = -1;
    for (int i = n; answer < 0 && i >= 0; --i) {
      if (dp[m][i] != 0) {
        answer = i;
      }
    }
    if (answer < 0) {
      cout << "error" << endl;
    } else if (dp[m][answer] >= 2) {
      cout << "rejected" << endl;
    } else {
      cout << answer;
      vector<int> cut;
      int r = m, c = answer;
      do {
        const int d = df[r][c];
        cut.push_back(d);
        r = pr[r][c];
        c -= d;
      } while (r > 0);
      for (int i = cut.size() - 1; i >= 0; --i) {
        cout << " " << cut[i];
      }
      cout << endl;
    }
  }
  return 0;
}
Problem p00816 (C++)

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

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

using namespace std;

int dig(int n) {
  if (n < 10)
    return 1;
  else
    return dig(n / 10) + 1;
}

int main() {
  while (1) {
    int t, n;
    cin >> t >> n;
    if (!n)
      break;
    int s = dig(n);
    string ns = to_string(n);
    vector<vector<int>> dp(s + 1, vector<int>(n + 1));
    dp[0][0] = 1;
    REP(i, s + 1) {
      REP(j, i) {
        auto sub = ns.substr(j, i - j);
        int m = stoi(sub);
        REP(k, n - m + 1) {
          if (dp[i][k + m] == 0 && dp[j][k] > 0)
            dp[i][k + m] = j + 1;
          else if ((dp[i][k + m] != 0 && dp[j][k] != 0) || dp[j][k] < 0)
            dp[i][k + m] = -1;
        }
      }
    }
    bool ok = false;
    for (int u = t; u > 0; --u) {
      if (dp[s][u] > 0) {
        vector<int> res;
        int i = s;
        int j = u;
        do {
          int oldi = i;
          i = dp[i][j] - 1;
          auto sub = ns.substr(i, oldi - i);
          res.push_back(stoi(sub));
          j -= stoi(sub);
        } while (i > 0);
        reverse(begin(res), end(res));
        cout << u;
        for (int v : res)
          cout << ' ' << v;
        cout << endl;
        ok = true;
        break;
      } else if (dp[s][u] < 0) {
        cout << "rejected" << endl;
        ok = true;
        break;
      }
    }
    if (!ok)
      cout << "error" << endl;
  }
  return 0;
}

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

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

using namespace std;

int dig(int n) {
  if (n < 10)
    return 1;
  else
    return dig(n / 10) + 1;
}

int main() {
  while (1) {
    int t, n;
    cin >> t >> n;
    if (!n)
      break;
    t = min(t, n);
    int s = dig(n);
    string ns = to_string(n);
    vector<vector<int>> dp(s + 1, vector<int>(n + 1));
    dp[0][0] = 1;
    REP(i, s + 1) {
      REP(j, i) {
        auto sub = ns.substr(j, i - j);
        int m = stoi(sub);
        REP(k, n - m + 1) {
          if (dp[i][k + m] == 0 && dp[j][k] > 0)
            dp[i][k + m] = j + 1;
          else if ((dp[i][k + m] != 0 && dp[j][k] != 0) || dp[j][k] < 0)
            dp[i][k + m] = -1;
        }
      }
    }
    bool ok = false;
    for (int u = t; u > 0; --u) {
      if (dp[s][u] > 0) {
        vector<int> res;
        int i = s;
        int j = u;
        do {
          int oldi = i;
          i = dp[i][j] - 1;
          auto sub = ns.substr(i, oldi - i);
          res.push_back(stoi(sub));
          j -= stoi(sub);
        } while (i > 0);
        reverse(begin(res), end(res));
        cout << u;
        for (int v : res)
          cout << ' ' << v;
        cout << endl;
        ok = true;
        break;
      } else if (dp[s][u] < 0) {
        cout << "rejected" << endl;
        ok = true;
        break;
      }
    }
    if (!ok)
      cout << "error" << endl;
  }
  return 0;
}
Problem p00820 (C++)

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

using namespace std;

vector<long long> sq;

// int memo[1<<15][200][4];

int dfs(long long sum, int pos, int num) {

  if (sum == 0)
    return 1;
  else if (num == 4 || sum < 0)
    return 0;

  //	if(memo[sum][pos][num]!=0) return memo[sum][pos][num];

  int res = 0;
  for (int i = pos; i < sq.size(); i++)
    res += dfs(sum - sq[i], i, num + 1);

  //	return memo[sum][pos][num]=res;
  return res;
}

int main() {

  for (int i = 1; i * i < (1 << 15); i++)
    sq.push_back(i * i);

  long long n;
  while (cin >> n, n) {
    cout << dfs(n, 0, 0) << endl;
  }

  return 0;
}

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

using namespace std;

vector<long long> sq;

// int memo[1<<15][200][4];

int dfs(long long sum, int pos, int num) {

  if (sum == 0)
    return 1;
  else if (num == 4 || sum < 0)
    return 0;

  //	if(memo[sum][pos][num]!=0) return memo[sum][pos][num];

  int res = 0;
  for (int i = pos; sq[i] <= sum; i++)
    res += dfs(sum - sq[i], i, num + 1);

  //	return memo[sum][pos][num]=res;
  return res;
}

int main() {

  for (int i = 1; i * i < (1 << 15); i++)
    sq.push_back(i * i);

  long long n;
  while (cin >> n, n) {
    cout << dfs(n, 0, 0) << endl;
  }

  return 0;
}
Problem p00820 (C++)

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

using namespace std;

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

namespace std {
template <> struct hash<std::vector<int>> {
  size_t operator()(const vector<int> &v) const {
    std::hash<int> hasher;
    size_t seed = 0;
    for (int i : v) {
      seed ^= hasher(i) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
    }
    return seed;
  }
};
} // namespace std

int main() {
  int n;
  unordered_set<vector<int>> used;
  while (cin >> n, n) {
    int ans = 0;
    used.clear();
    int nr = (int)sqrt(n) + 1;
    rep(i, nr) {
      int p = i * i;
      if (p > n)
        break;
      rep(j, nr) {
        int q = p + j * j;
        if (q > n)
          break;
        rep(k, nr) {
          int r = q + k * k;
          if (r > n)
            break;
          rep(l, nr) {
            int s = r + l * l;
            if (s > n)
              break;
            if (s == n) {
              vector<int> v = {i, j, k, l};
              sort(v.begin(), v.end());
              if (!used.count(v)) {
                used.insert(v);
                ans++;
              }
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
}


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

using namespace std;

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

namespace std {
template <> struct hash<std::vector<int>> {
  size_t operator()(const vector<int> &v) const {
    std::hash<int> hasher;
    size_t seed = 0;
    for (int i : v) {
      seed ^= hasher(i) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
    }
    return seed;
  }
};
} // namespace std

int main() {
  int n;
  unordered_set<vector<int>> used;
  while (cin >> n, n) {
    int ans = 0;
    used.clear();
    int nr = (int)sqrt(n) + 1;
    rep(i, nr) {
      int p = i * i;
      if (p > n)
        break;
      rep(j, nr) {
        int q = p + j * j;
        if (q > n)
          break;
        rep(k, nr) {
          int r = q + k * k;
          if (r > n)
            break;
          int l = sqrt(n - r);
          if (r + l * l != n)
            continue;
          vector<int> v = {i, j, k, l};
          sort(v.begin(), v.end());
          if (!used.count(v)) {
            used.insert(v);
            ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
}