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

#include <iostream>
#include <vector>
#define MAX 1000000
using namespace std;
int main() {
  vector<int> a(MAX);
  vector<int> box(MAX);
  int n, sum, k = 0, l, r, mid;
  for (int i = 0; i < MAX; i++)
    a[i] = 1;
  a[0] = 0;
  a[1] = 0;
  for (int i = 0; i * i < MAX; i++)
    if (a[i]) {
      for (int j = i * 2; j < MAX; j += i)
        a[j] = 0;
    }
  for (int i = 0; i < MAX; i++)
    if (a[i])
      box[k++] = i;
  while (cin >> n, n) {
    sum = 0;
    for (int i = 0; box[i] < n; i++) {
      if (box[i] * 2 < n)
        continue;
      l = 0;
      r = i;
      while (r - l > 1) {
        mid = (r + l) / 2;
        if (box[mid] >= n - box[i]) {
          r = mid;
        } else {
          l = mid;
        }
      }
      if (box[r] + box[i] == n) {
        sum++;
      }
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
#define MAX 500000
using namespace std;
int main() {
  vector<int> a(MAX);
  vector<int> box(MAX);
  int n, sum, k = 0;
  for (int i = 0; i < MAX; i++)
    a[i] = 1;
  a[0] = 0;
  a[1] = 0;
  for (int i = 0; i * i < MAX; i++)
    if (a[i]) {
      for (int j = i * 2; j < MAX; j += i)
        a[j] = 0;
    }
  for (int i = 0; i < MAX; i++)
    if (a[i])
      box[k++] = i;
  while (cin >> n, n) {
    sum = 0;
    for (int i = 0; box[i] < n; i++)
      for (int j = 0; box[j] <= box[i]; j++) {
        if (box[j] + box[i] == n)
          sum++;
        if (box[j] + box[i] > n)
          break;
      }
    cout << sum << endl;
  }
  return 0;
}
Problem p00185 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int prime[200];
int main() {
  int count = 0;
  for (int i = 3; i < 1000; i += 2) {
    bool t = true;
    for (int j = 0; j < count; j++)
      if (i % prime[j] == 0)
        t = false;
    if (t == true) {
      prime[count] = i;
      count++;
    }
  }
  int n;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    n /= 2;
    int ans = 0, k = n * 2 - 3;
    bool t;
    for (int i = 3; i <= n; i += 2) {
      t = true;
      for (int j = 0; j < count; j++) {
        if (i % prime[j] == 0) {
          if (i != prime[j]) {
            t = false;
            break;
          }
        }
        if (k % prime[j] == 0) {
          if (k != prime[j]) {
            t = false;
            break;
          }
        }
      }
      if (t)
        ans++;
      k -= 2;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int prime[200];
int main() {
  int count = 0;
  for (int i = 3; i < 1000; i += 2) {
    bool t = true;
    for (int j = 0; j < count; j++)
      if (i % prime[j] == 0)
        t = false;
    if (t == true) {
      prime[count] = i;
      count++;
    }
  }
  int n;
  while (true) {
    cin >> n;
    n /= 2;
    int ans = 0, k = n * 2 - 3;
    bool t;
    for (int i = 3; i <= n; i += 2) {
      t = true;
      for (int j = 0; j < count; j++) {
        if (i % prime[j] == 0) {
          if (i != prime[j]) {
            t = false;
            break;
          }
        }
        if (k % prime[j] == 0) {
          if (k != prime[j]) {
            t = false;
            break;
          }
        }
      }
      if (t)
        ans++;
      k -= 2;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00185 (C++) — buggy submission, verdict: Runtime Error

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

#define MAX 1000000
vector<bool> prime(MAX + 1, true);
void init() {
  prime[0] = prime[1] = 0;
  for (int i = 2; i * i <= MAX; i++) {
    if (prime[i]) {
      for (int j = i * i; j <= MAX; j += i) {
        prime[j] = false;
      }
    }
  }
}

int main() {
  init();

  int n;
  while (cin >> n, n) {
    int ans = 0;
    for (int i = 1; i <= n / 2; i++) {
      if (prime[i] & prime[n - i])
        ans++;
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

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

int memo[100000];
int dummy_prime(int n, int i) {
  return memo[n] ? memo[n]
                 : memo[n] = i > 1 ? n % i ? dummy_prime(n, i - 1) : -1 : 1;
}
int isprime(int n) {
  return memo[n] ? memo[n] : memo[n] = n > 1 ? dummy_prime(n, sqrt(n)) : -1;
}
int main() {
  int n, result;

  while (cin >> n && n) {
    result = 0;
    for (int i = 2; i <= n - i; i++)
      if (isprime(i) == 1 && isprime(n - i) == 1)
        result++;
    cout << result << endl;
  }
}
Problem p00185 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>

using namespace std;

int const N = 1000000;

int ps[N + 1];
int cnt[N + 1] = {};

bool isp[N + 1] = {};

int idx = 0;
inline void add(int n) { ps[idx++] = n; }

void init() {
  for (int i = 0; i <= N; i++)
    isp[i] = true;
  add(2);
  for (int i = 4; i <= N; i += 2)
    isp[i] = false;
  for (int i = 3; i <= N; i += 2) {
    if (!isp[i])
      continue;
    add(i);
    for (int j = i + i; j <= N; j += i)
      isp[j] = false;
  }
}

int main() {
  init();

  int n;
  while (cin >> n, n) {
    int ans = 0;
    for (int i = 0; i < idx; i++) {
      if (n - ps[i] < 0 || ps[i] > n - ps[i])
        break;
      if (isp[n - ps[i]])
        ans++;
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;

int const N = 1000000;

int ps[N + 1];
int cnt[N + 1] = {};

bool isp[N + 1] = {};

int idx = 0;
inline void add(int n) { ps[idx++] = n; }

void init() {
  for (int i = 0; i <= N; i++)
    isp[i] = true;
  add(2);
  for (int i = 4; i <= N; i += 2)
    isp[i] = false;
  for (int i = 3; i <= N; i += 2) {
    if (!isp[i])
      continue;
    add(i);
    for (int j = i + i; j <= N; j += i)
      isp[j] = false;
  }

  for (int i = 0; i < idx; i++) {
    for (int j = i; j < idx; j++) {
      int c = ps[i] + ps[j];
      if (c > N)
        break;
      cnt[c]++;
    }
  }
}

int main() {
  init();

  int n;
  while (cin >> n, n) {
    cout << cnt[n] << endl;
  }
}
Problem p00185 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
bool us[1000010] = {false};
bool p[1000010] = {0};
int main() {
  /*Eratosthenes*/
  p[2] = true;
  for (int i = 0; i <= 1000010; i += 2) {
    us[i] = true;
  }

  int cnt = 1;
  for (int i = 3; i <= 1000010; i += 2) {
    int q = 0;
    if (us[i] == true)
      continue;
    else if (us[i] == false) {
      p[i] = true;
      us[i] = true;
      q = i;
      for (int j = q + q; j <= 1000010; j += q) {
        us[j] = true;
      }
    }
  }

  int n;
  while (scanf("%d", &n), n) {
    int x = 0;
    for (int i = 0; i <= n / 2; i++) {
      if (p[i] && p[n - i]) {
        x++;
      }
    }
    printf("%d\\n", x);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
bool us[1000010] = {false};
int p[1000010] = {0};
int main() {
  /*Eratosthenes*/
  p[0] = 2;
  for (int i = 0; i <= 1000010; i += 2) {
    us[i] = true;
  }

  int cnt = 1;
  for (int i = 3; i <= 1000010; i += 2) {
    int q = 0;
    if (us[i] == true)
      continue;
    else if (us[i] == false) {
      p[cnt] = i;
      cnt++;
      us[i] = true;
      q = i;
      for (int j = q + q; j <= 1000010; j += q) {
        us[j] = true;
      }
    }
  }

  int n;
  while (scanf("%d", &n), n) {
    int x = 0;
    memset(us, false, sizeof(us));
    for (int i = 0; i < n / 2; i++) {
      if (us[i] == true)
        continue;
      if (us[i] >= n)
        break;
      for (int j = i; j < n / 2; j++) {
        if (p[i] + p[j] > n)
          break;
        if (p[j] == true) {
          if (p[i] + p[j] < n)
            continue;
          else
            break;
        }
        if (binary_search(p, p + (n / 2), (n - p[i]))) {
          x++;
          us[i] = true;
          us[j] = true;
          break;
        }
      }
    }
    printf("%d\\n", x);
  }
  return 0;
}
Problem p00185 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
#define sup 1000000
using namespace std;
std::vector<int> prm;
/*
inline bool isinthevector(std::vector<int> v,int n)
{
        std::vector<int>::iterator i=find(v.begin(), v.end(),n);
        return i!=v.end();
}
*/

inline bool isprmfunc(int n) {
  for (int i = 0; prm[i] * prm[i] <= n; ++i) {
    if (n % prm[i] == 0)
      return false;
  }
  return true;
}

int main(int argc, char const *argv[]) {
  prm.push_back(2);
  prm.push_back(3);
  bool isprm = true;
  for (int n = 5; n <= sup; n += 2, isprm = true) {
    for (int i = 0; prm[i] * prm[i] <= n; ++i) {
      if (n % prm[i] == 0) {
        isprm = false;
        break;
      }
    }
    if (isprm == true) {
      prm.push_back(n);
    }
  }

  int n;
  while (cin >> n, n) {
    int cnt = 0;
    for (int i = 0; 2 * prm[i] <= n; ++i) {
      if (isprmfunc(n - prm[i])) {
        ++cnt;
      }
    }
    cout << cnt << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define sup 1000000
using namespace std;

bool isinthevector(std::vector<int> v, int n) {
  std::vector<int>::iterator i = find(v.begin(), v.end(), n);
  return i != v.end();
}

int main(int argc, char const *argv[]) {
  std::vector<int> prm;
  prm.push_back(2);
  prm.push_back(3);
  bool isprm;
  for (int n = 5; n <= sup; n += 2, isprm = true) {
    for (int i = 0; prm[i] * prm[i] <= n; ++i) {
      if (n % prm[i] == 0) {
        isprm = false;
        break;
      }
    }
    if (isprm == true) {
      prm.push_back(n);
    }
  }

  int n;
  while (cin >> n, n) {
    int cnt = 0;
    for (int i = 0; 2 * prm[i] <= n; ++i) {
      if (isinthevector(prm, n - prm[i])) {
        ++cnt;
      }
    }
    cout << cnt << "\\n";
  }
  return 0;
}
Problem p00185 (C++) — buggy submission, verdict: Runtime Error

#define SIZE 1000001
#include <iostream>
using namespace std;

int main() {
  int a, d, n;
  bool prime[SIZE];

  for (int i = 0; i < SIZE; i++) {
    prime[i] = 1;
  }
  prime[0] = prime[1] = 0;

  for (int i = 2; i < SIZE; i++) {
    if (prime[i]) {
      for (int j = 2; j * i < SIZE; j++) {
        prime[j * i] = 0;
      }
    }
  }

  int N;
  while (cin >> N, N) {
    int cnt = 0;
    for (int i = 0; i <= N / 2; i++) {
      if (prime[i] && prime[N - i]) {
        cnt++;
      }
    }

    cout << cnt << endl;
  }
}

Error: 0

Fixed submission:

#define SIZE 100001
#include <iostream>
using namespace std;

int main() {
  int a, d, n;
  bool prime[SIZE];

  for (int i = 0; i < SIZE; i++) {
    prime[i] = 1;
  }
  prime[0] = prime[1] = 0;

  for (int i = 2; i < SIZE; i++) {
    if (prime[i]) {
      for (int j = 2; j * i < SIZE; j++) {
        prime[j * i] = 0;
      }
    }
  }

  int N;
  while (cin >> N, N) {
    int cnt = 0;
    for (int i = 0; i <= N / 2; i++) {
      if (prime[i] && prime[N - i]) {
        cnt++;
      }
    }

    cout << cnt << endl;
  }
}
Problem p00185 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  bool prime[1000001] = {true, true};
  vector<int> ps;
  for (int i = 4; i <= 1000000; i += 2)
    prime[i] = true;
  for (int i = 3; i * i <= 1000000; i += 2) {
    for (int j = i * 2; j <= 1000000; j += i)
      prime[j] = true;
  }
  for (int i = 0; i <= 1000000; i++)
    if (!prime[i])
      ps.push_back(i);
  int n;
  while (cin >> n, n) {
    int cnt = 0;
    for (int i = 0; i < ps.size(); i++) {
      if (ps[i] > n / 2)
        break;
      if (upper_bound(ps.begin(), ps.end(), n - ps[i]) -
          lower_bound(ps.begin(), ps.end(), n - ps[i]))
        cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  bool prime[1000001] = {true, true};
  vector<int> ps;
  for (int i = 4; i <= 1000000; i += 2)
    prime[i] = true;
  for (int i = 3; i * i <= 1000000; i += 2) {
    for (int j = i * 2; j <= 1000000; j += i)
      prime[j] = true;
  }
  for (int i = 0; i <= 1000000; i++)
    if (!prime[i])
      ps.push_back(i);
  int n;
  while (cin >> n, n) {
    int cnt = 0;
    for (int i = 0; i < ps.size(); i++) {
      if (ps[i] > n / 2)
        break;
      if (find(ps.begin(), ps.end(), n - ps[i]) != ps.end())
        cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00185 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

#define MAX 1000001

int main(void) {
  int i, j;
  bool p[MAX];

  for (i = 0; i < MAX; i++) {
    p[i] = true;
  }
  for (i = 2; (i * i) < MAX; i++) {
    if (p[i]) {
      for (j = i * 2; j < MAX; j += i) {
        p[j] = false;
      }
    }
  }

  int n;
  while (cin >> n, n) {
    int res = 0;
    for (i = 2; (i * 2) <= n; i++) {
      if (p[i] && p[n - i])
        res++;
    }
    cout << res << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

#define MAX (1000000 / 2) + 100

int main(void) {
  bool p[MAX];
  for (int i = 3; i < MAX; i++) {
    if (i % 2 == 0)
      p[i] = false;
    else
      p[i] = true;
  }
  p[0] = p[1] = false;
  p[2] = true;
  for (int i = 3; i < MAX; i += 2) {
    if (p[i]) {
      for (int j = i * 2; j < MAX; j += i) {
        p[j] = false;
      }
    }
  }

  int n;
  while (cin >> n, n) {
    int res = 0;
    for (int i = 2; i <= n / 2; i++) {
      if (p[i] && p[n - i])
        res++;
    }
    cout << res << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

bool f[10000002];
int N = 1000000;
int main() {
  for (int i = 3; i <= N; i += 2) {
    f[i] = true;
    for (int j = 3; j * j <= i; j += 2)
      if (i % j == 0)
        f[i] = false;
  }
  int x;
  while (1) {

    int ans = 0;
    cin >> x;
    if (x == 0)
      break;
    for (int i = 3; i <= x / 2; i += 2)
      if (f[x - i] == true && f[i] == true)
        ans++; // cout<<i<<" "<<x-i<<endl;//

    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
#define NN (1000001)

bool f[(1000001)];
void Cs() {
  for (int i = 3; i < 1000001; i += 2)
    f[i] = true;

  for (int i = 3; i < 1000001; i += 2)
    for (int j = 3; j <= sqrt((double)(i)); j += 2) {
      if (i % j == 0) {
        f[i] = false;
        break;
      }
    }
}

int main() {

  int c = 0, x;
  Cs();

  while (1) {
    scanf("%d", &x);
    if (x == 0)
      break;
    for (int i = 3; i < x; i += 2)
      for (int j = x - 1; j >= i; j -= 2)
        if ((f[i] == true && f[j] == true) && i + j == x) {
          printf("%d+%d\\n", i, j);
          c++;
        }

    printf("%d\\n", c);
    c = 0;
  }
  return 0;
}
Problem p00185 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  bool pri[1000001];
  vector<int> price;
  fill(pri, pri + 1000001, true);
  pri[0] = pri[1] = false;
  for (int i = 2; i < 1000001; i++) {
    if (pri[i] == 0)
      continue;
    price.push_back(i);
    for (int j = i + i; j < 1000001; j += i) {
      pri[j] = false;
    }
  }
  int n;
  for (; cin >> n, n;) {
    int count = 0;

    for (int i = 0; i < price.size(); i++) {
      if (n - price[i] < 2)
        break;
      if (price[i] > (n - price[i]))
        break;
      if (pri[n - price[i]]) {
        count++;
      }
    }
    cout << count << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  bool pri[1000001];
  vector<int> price;
  fill(pri, pri + 1000001, true);
  pri[0] = pri[1] = false;
  for (int i = 2; i < 1000001; i++) {
    if (pri[i] == 0)
      continue;
    price.push_back(i);
    for (int j = i + i; j < 1000001; j += i) {
      pri[j] = false;
    }
  }
  int n;
  for (; cin >> n, n;) {
    int count = 0;

    for (int i = 0; i < price.size(); i++) {
      for (int j = i; j < price.size(); j++) {
        if (price[i] + price[j] == n)
          count++;
        if (price[j] >= n)
          break;
      }
      if (price[i] >= n)
        break;
    }
    cout << count << endl;
  }
}
Problem p00185 (C++) — buggy submission, verdict: Time Limit Exceeded


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

#define MAX 1000000
#define sqrtMAX sqrt(MAX)

int main(void) {
  int n;
  vector<int> b(MAX + 1);
  int i, j;
  int count;

  // ツエツδ可トツスツテツネツスツづ個づ督づゥツつ「
  b[0] = 1;
  b[1] = 1;
  for (i = 4; i <= MAX; i += 2)
    b[i] = 1;
  for (i = 3; i < sqrtMAX; i += 2)
    for (j = i + i; j <= MAX; j += i)
      b[j] = 1;

  while (scanf("%d", &n) != EOF) {
    if (n == 0)
      break;
    count = 0;
    for (i = 0; i <= n / 2; i++)
      if (!b[i] && !b[n - i])
        count++;
    printf("%d\\n", count);
  }
  return 0;
}

Error: TLE

Fixed submission:


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

#define MAX 1000000
#define sqrtMAX sqrt(MAX)

int main(void) {
  int n;
  vector<int> b(MAX + 1);
  vector<int> p(MAX + 1);
  vector<int> c(MAX + 1);
  int i, j;
  int count;

  // ツエツδ可トツスツテツネツスツづ個づ督づゥツつ「
  b[0] = 1;
  b[1] = 1;
  for (i = 4; i <= MAX; i += 2)
    b[i] = 1;
  for (i = 3; i < sqrtMAX; i += 2)
    for (j = i + i; j <= MAX; j += i)
      b[j] = 1;

  // ツ素ツ青板づーツ板イツつォツ出ツつキ
  count = 0;
  for (i = 0; i <= MAX; i++) {
    if (b[i])
      continue;
    p[count++] = i;
  }

  for (i = 0; i < count; i++) {
    for (j = i; j < count; j++) {
      if (p[i] + p[j] > MAX)
        break;
      c[p[i] + p[j]]++;
    }
  }

  while (scanf("%d", &n) != EOF) {
    if (n == 0)
      break;
    printf("%d\\n", c[n]);
  }
  return 0;
}
Problem p00185 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int f[1000000], s[80000];

int main() {
  int n, i, j, k = 0;
  for (i = 2; i < 1000000; i++)
    if (f[i] == 0) {
      f[i] = 1;
      s[k++] = i;
      for (j = i + i; j < 1000000; j += i)
        f[j] = 2;
    }
  while (cin >> n && n > 0) {
    for (i = 0, j = 0; s[i] <= n / 2; i++)
      if (f[n - s[i]] == 1)
        j++;
    cout << j << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int f[500000], s[42000];

int main() {
  int n, i, j, k = 0;
  for (i = 2; i < 500000; i++)
    if (f[i] == 0) {
      f[i] = 1;
      s[k++] = i;
      for (j = i + i; j < 500000; j += i)
        f[j] = 2;
    }
  while (cin >> n && n > 0) {
    for (i = 0, j = 0; s[i] <= n / 2; i++)
      if (f[n - s[i]] == 1)
        j++;
    cout << j << endl;
  }
  return 0;
}
Problem p00186 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {
  int q1, b, c1, c2, q2;
  while (cin >> q1, q1) {
    cin >> b >> c1 >> c2 >> q2;
    int aizu = min(b / c1, q2);
    if (aizu == 0) {
      cout << "NA" << endl;
      continue;
    }
    int money = b - aizu * c1;
    int ordinal = money / c2;

    bool possible = true;
    while (aizu + ordinal < q1) {
      aizu--;
      if (aizu == 0) {
        cout << "NA" << endl;
        possible = false;
        break;
      }
      money = b - aizu * c1;
      ordinal = money / c2;
      money = b - aizu * c1 - ordinal * c2;
    }
    if (possible)
      cout << aizu << ' ' << ordinal << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int q1, b, c1, c2, q2;
  while (cin >> q1, q1) {
    cin >> b >> c1 >> c2 >> q2;
    int aizu = min(b / c1, q2);
    int money = b - aizu * c1;
    int ordinal = money / c2;
    while (aizu + ordinal < q1) {
      aizu--;
      money = b - aizu * c1;
      ordinal = money / c2;
      money = b - aizu * c1 - ordinal * c2;
    }
    if (aizu < 1)
      cout << "NA" << endl;
    else
      cout << aizu << ' ' << ordinal << endl;
  }
  return 0;
}
Problem p00186 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>

using namespace std;

void failed() { cout << "NA" << endl; }

int main() {

  int Q1, B, C1, C2, Q2;

  while (cin >> Q1) {

    if (Q1 == 0)
      break;

    cin >> B >> C1 >> C2 >> Q2;

    assert(Q2 >= 0);

    /*
    if(B/C1 == 0) {
      failed(); continue;
    }
    */

    int AizuCh = min(B / C1, Q2);
    if (AizuCh <= 0) {
      failed();
      continue;
    }
    int NCh = 0;
    for (;;) {

      NCh = (B - AizuCh * C1) / C2;

      /*
      if(B - (NCh+1)*C2 - AizuCh*C1 >= 0) {
        NCh = (B - AizuCh*C1)/C2;
      }
      */
      if (AizuCh + NCh >= Q1) {
        cout << AizuCh << ' ' << NCh << endl;
        break;
      }
      /*
      else if(B - (NCh+1)*C2 - (AizuCh-1)*C1 >= 0) {
        NCh ++, AizuCh --;
      }
      */
      else
        AizuCh--;

      if (AizuCh <= 0) {
        failed();
        break;
      }
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>

using namespace std;

void failed() { cout << "NA" << endl; }

int main() {

  int Q1, B, C1, C2, Q2;

  while (cin >> Q1) {

    if (Q1 == 0)
      break;

    cin >> B >> C1 >> C2 >> Q2;

    assert(Q2 >= 1);

    /*
    if(B/C1 == 0) {
      failed(); continue;
    }
    */

    int AizuCh = min(B / C1, Q2);
    if (AizuCh <= 0) {
      failed();
      continue;
    }
    int NCh = 0;
    for (;;) {

      NCh = (B - AizuCh * C1) / C2;

      /*
      if(B - (NCh+1)*C2 - AizuCh*C1 >= 0) {
        NCh = (B - AizuCh*C1)/C2;
      }
      */
      if (AizuCh + NCh >= Q1) {
        cout << AizuCh << ' ' << NCh << endl;
        break;
      }
      /*
      else if(B - (NCh+1)*C2 - (AizuCh-1)*C1 >= 0) {
        NCh ++, AizuCh --;
      }
      */
      else
        AizuCh--;

      if (AizuCh <= 0) {
        failed();
        break;
      }
    }
  }

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

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

int main(void) {
  int q1, b, c1, c2, q2;

  while (1) {
    scanf("%d", &q1);
    if (q1 == 0)
      break;
    scanf("%d %d %d %d", &b, &c1, &c2, &q2);

    if (c1 <= c2) {
      int x = min(b / c1, q2);
      int y = (b - (x * c1)) / c2;
      if ((x + y) < q1) {
        printf("NA\\n");
      } else {
        printf("%d %d\\n", x, y);
      }
    } else {
      int x = min(min((b - (c2 * q1)) / (c1 - c2), q2), (b / c1));
      int y = (b - (x * c1)) / c2;
      if (x <= 0) {
        printf("NA\\n");
      } else {
        printf("%d %d\\n", x, y);
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

int main(void) {
  long q1, b, c1, c2, q2;

  while (1) {
    scanf("%ld", &q1);
    if (q1 == 0)
      break;
    scanf("%ld %ld %ld %ld", &b, &c1, &c2, &q2);

    if ((b < (c2 * (q1 - 1)) + c1) && (c2 < c1)) {
      printf("NA\\n");
      continue;
    }

    long aizu = 0, normal = 0;

    aizu = b / c1;
    b = b % c1;
    if (q2 < aizu) {
      long over = aizu - q2;
      aizu -= over;
      b += (over * c1);
    }
    //		printf("%ld %ld\\n", aizu, b);

    normal = b / c2;
    b = b % c2;
    //		printf("%ld %ld\\n", normal, b);

    while ((aizu + normal) < q1) {
      aizu--;
      b += c1;
      normal += (b / c2);
      b %= c2;
    }

    printf("%ld %ld\\n", aizu, normal);
  }

  return (0);
}