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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-10;
const double PI = acos(-1.0);

int dfs(int p, int q, int a, int n, int bunbo, int bunsi, int cnt, int prev) {
  if (cnt == n) {
    return (q * bunsi == p * bunbo);
  }
  int ret = (q * bunsi == p * bunbo);
  FOR(i, prev, a + 1) {
    int new_bunbo = bunbo * i;
    int new_bunsi = bunsi * i + bunbo;
    if (new_bunbo <= a) {
      ret += dfs(p, q, a, n, new_bunbo, new_bunsi, cnt + 1, i);
    }
  }
  return ret;
}
int main() {
  int p, q, a, n;
  while (cin >> p >> q >> a >> n, p | q | a | n) {
    cout << dfs(p, q, a, n, 1, 0, 0, 1) << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-10;
const double PI = acos(-1.0);

int dfs(int p, int q, int a, int n, int bunbo, int bunsi, int cnt, int prev) {
  if (cnt == n) {
    return (q * bunsi == p * bunbo);
  }
  int ret = (q * bunsi == p * bunbo);
  FOR(i, prev, a + 1) {
    int new_bunbo = bunbo * i;
    int new_bunsi = bunsi * i + bunbo;
    if (new_bunbo > a)
      break;
    ret += dfs(p, q, a, n, new_bunbo, new_bunsi, cnt + 1, i);
  }
  return ret;
}
int main() {
  int p, q, a, n;
  while (cin >> p >> q >> a >> n, p | q | a | n) {
    cout << dfs(p, q, a, n, 1, 0, 0, 1) << endl;
  }
}
Problem p00712 (C++)

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

int p, q, a, n;

int dfs(int A, int B, int C, int D, int MAX) {

  int res = 0;

  if (B * p == A * q) {
    // cout<<A<<' '<<B<<' '<<C<<' '<<D<<' '<<MAX<<endl;
    return 1;
  }
  if (D == n)
    return 0;

  for (int i = MAX; i * C <= a; i++) {
    int t = __gcd(B, i);
    int bb = B * i / t;
    int aa = bb / B * A + bb / i;
    // cout<<A<<' '<<B<<' '<<i<<' '<<aa<<' '<<bb<<endl;
    res += dfs(aa, bb, C * i, D + 1, i);
  }

  return res;
}

signed main() {
  while (cin >> p >> q >> a >> n, n) {
    cout << dfs(0, 1, 1, 0, 1) << endl;
  }
}


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

int p, q, a, n;

int dfs(int A, int B, int C, int D, int MAX) {

  int res = 0;

  if (B * p == A * q) {
    // cout<<A<<' '<<B<<' '<<C<<' '<<D<<' '<<MAX<<endl;
    return 1;
  }

  if (B * p < A * q)
    return 0;

  if (D == n)
    return 0;

  for (int i = MAX; i * C <= a; i++) {
    int t = __gcd(B, i);
    int bb = B * i / t;
    int aa = bb / B * A + bb / i;
    // cout<<A<<' '<<B<<' '<<i<<' '<<aa<<' '<<bb<<endl;
    res += dfs(aa, bb, C * i, D + 1, i);
  }

  return res;
}

signed main() {
  while (cin >> p >> q >> a >> n, n) {
    cout << dfs(0, 1, 1, 0, 1) << endl;
  }
}
Problem p00712 (C++)

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

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int p, q, a, n, cnt;

void dfs(int v, int l, int r, int now) {
  if (v != 0) {
    int d = gcd(r, l);
    if (l / d == p && r / d == q) {
      cnt++;
      return;
    }
  }
  if (v < n) {
    for (int i = a / r; i >= now; i--) {
      if ((double)p / q < (double)(l * i + r) / (r * i))
        return;
      dfs(v + 1, l * i + r, r * i, i);
    }
  }
}

int main(void) {
  while (1) {
    scanf("%d %d %d %d", &p, &q, &a, &n);
    if (p + q + a + n == 0)
      break;
    int d = gcd(p, q);
    p /= d;
    q /= d;
    cnt = 0;
    dfs(0, 0, 1, 1);
    printf("%d\n", cnt);
  }
  return 0;
}

Fixed submission:
#include <cstdio>

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int p, q, a, n, cnt;

void dfs(int v, int l, int r, int now) {
  double dsum = (double)l / r;
  dsum += (double)(n - v + 1) / now;
  if ((double)p / q > dsum)
    return;
  if (v != 0) {
    int d = gcd(r, l);
    if (l / d == p && r / d == q) {
      cnt++;
      return;
    }
  }
  if (v < n) {
    for (int i = a / r; i >= now; i--) {
      if ((double)p / q < (double)(l * i + r) / (r * i))
        return;
      dfs(v + 1, l * i + r, r * i, i);
    }
  }
}

int main(void) {
  while (1) {
    scanf("%d %d %d %d", &p, &q, &a, &n);
    if (p + q + a + n == 0)
      break;
    int d = gcd(p, q);
    p /= d;
    q /= d;
    cnt = 0;
    dfs(0, 0, 1, 1);
    printf("%d\n", cnt);
  }
  return 0;
}
Problem p00712 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;

int p, q, a, n;
map<vector<int>, int> dp;

int gcd(int a, int b) { return a ? gcd(b % a, a) : b; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

int dfs(int x, int y, int z, int w, int last) {
  vi arg = {x, y, z, w, last};
  // if(dp.count(arg))return dp[arg];
  if (z == n) {
    if (x == 0)
      return 1;
    else
      return 0;
  }
  if (x < 0)
    return 0;
  if (x == 0)
    return 1;
  // x/y - (n-z)*(1/last)>0
  if (x * last > (n - z) * y)
    return 0;

  int ret = 0;
  for (int i = last; i * w <= a; i++) {
    // x/y - 1/i
    int bunbo = lcm(y, i);
    int bunshi = bunbo / y * x - bunbo / i * 1;
    int g = gcd(bunbo, bunshi);
    bunbo /= g;
    bunshi /= g;
    ret += dfs(bunshi, bunbo, z + 1, i * w, i);
  }

  // return dp[arg]=ret;
  return ret;
}

signed main() {
  while (cin >> p >> q >> a >> n, p or q or a or n) {
    dp.clear();
    int g = gcd(p, q);
    p /= g;
    q /= g;
    cout << dfs(p, q, 0, 1, 1) << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;

int p, q, a, n;
map<vector<int>, int> dp;

int gcd(int a, int b) { return a ? gcd(b % a, a) : b; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

int dfs(int x, int y, int z, int w, int last) {
  vi arg = {x, y, z, w, last};
  // if(dp.count(arg))return dp[arg];
  if (z == n) {
    if (x == 0)
      return 1;
    else
      return 0;
  }
  if (x < 0)
    return 0;
  if (x == 0)
    return 1;
  // x/y - (n-z)*(1/last)>0
  if (x * last > (n - z) * y)
    return 0;

  int ret = 0;
  for (int i = last; i * w <= a; i++) {
    // x/y - 1/i
    // int bunbo=lcm(y,i);
    // int bunshi=bunbo/y*x - bunbo/i*1;
    // int g=gcd(bunbo,bunshi);
    // bunbo/=g;
    // bunshi/=g;

    ret += dfs(x * i - y, y * i, z + 1, i * w, i);
  }

  // return dp[arg]=ret;
  return ret;
}

signed main() {
  while (cin >> p >> q >> a >> n, p or q or a or n) {
    dp.clear();
    int g = gcd(p, q);
    p /= g;
    q /= g;
    cout << dfs(p, q, 0, 1, 1) << endl;
  }
}
Problem p00712 (C++)

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

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

int P, Q, A, N;

int calc(int p, int q, int n, int a, int t) {
  if (p * Q == P * q)
    return 1;
  if (n == N)
    return 0;
  if (p * Q > P * q)
    return 0;
  int ret = 0;
  for (int i = t; i * a <= A; i++) {
    int np = p * i + q, nq = i * q;
    int g = __gcd(np, nq);
    np /= g;
    nq /= g;
    ret += calc(np, nq, n + 1, i * a, i);
  }
  return ret;
}

int main() {
  // while(cin >> P >> Q >> A >> N) {
  while (scanf("%d %d %d %d", &P, &Q, &A, &N) != EOF) {
    if (P == 0 && Q == 0 && A == 0 && N == 0)
      break;
    // cout << calc(0, 1, 0, 1, 1) << endl;
    printf("%d\n", calc(0, 1, 0, 1, 1));
  }
}

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

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

int P, Q, A, N;

int calc(int p, int q, int n, int a, int t) {
  if (p * Q == P * q)
    return 1;
  if (n == N)
    return 0;
  if (p * Q > P * q)
    return 0;
  int ret = 0;
  for (int i = t; i * a <= A; i++) {
    int np = p * i + q, nq = i * q;
    // int g = __gcd(np, nq);
    // np /= g;
    // nq /= g;
    ret += calc(np, nq, n + 1, i * a, i);
  }
  return ret;
}

int main() {
  // while(cin >> P >> Q >> A >> N) {
  while (scanf("%d %d %d %d", &P, &Q, &A, &N) != EOF) {
    if (P == 0 && Q == 0 && A == 0 && N == 0)
      break;
    // cout << calc(0, 1, 0, 1, 1) << endl;
    printf("%d\n", calc(0, 1, 0, 1, 1));
  }
}
Problem p00712 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
int lcm(int x, int y) { return x * y / gcd(x, y); }

int p, q, a, n;
using T = tuple<int, int, int, int, int>;
int f(int x, int y, int z, int c, int a_) {
  if (c == n)
    return 0;
  if (x * z > (n - c) * y)
    return 0;
  int ret = 0;
  rep(i, z, a + 1) {
    if (i * a_ > a)
      break;
    if (x * i > y) {
      int nx = x * i - y, ny = y * i;
      int g = gcd(nx, ny);
      nx /= g;
      ny /= g;
      ret += f(nx, ny, i, c + 1, i * a_);
    } else if (x * i == y) {
      ret += 1;
    }
  }
  return ret;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  for (; cin >> p >> q >> a >> n && p;) {
    cout << f(p, q, 1, 0, 1) << endl;
  }
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
int lcm(int x, int y) { return x * y / gcd(x, y); }

int p, q, a, n;
using T = tuple<int, int, int, int, int>;
int f(int x, int y, int z, int c, int a_) {
  if (c == n - 1 && x != 1)
    return 0;
  if (c == n)
    return 0;
  if (x * z > (n - c) * y)
    return 0;
  int ret = 0;
  rep(i, z, a + 1) {
    if (i * a_ > a)
      break;
    if (x * i > y) {
      int nx = x * i - y, ny = y * i;
      int g = gcd(nx, ny);
      nx /= g;
      ny /= g;
      ret += f(nx, ny, i, c + 1, i * a_);
    } else if (x * i == y) {
      ret += 1;
    }
  }
  return ret;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  for (; cin >> p >> q >> a >> n && p;) {
    cout << f(p, q, 1, 0, 1) << endl;
  }
  return 0;
}
Problem p00712 (C++)

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

using namespace std;

int GCD(int m, int n) {
  if (n == 0)
    return m;
  return GCD(n, m % n);
}

int dfs(int p, int q, int a, int n, int temp) {
  if (a == 0 || n == 0)
    return 0;
  int res = 0;
  int gcd = GCD(p, q);
  p /= gcd;
  q /= gcd;
  for (int i = max((q + p - 1) / p, temp); i <= a; i++) {
    if (p == 1 && q == i) {
      res++;
    } else
      res += dfs(p * i - q, q * i, a / i, n - 1, i);
  }
  return res;
}

int main() {
  int p, q, a, n;
  while (cin >> p >> q >> a >> n, p || q || a || n) {
    cout << dfs(p, q, a, n, 1) << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int GCD(int m, int n) {
  if (n == 0)
    return m;
  return GCD(n, m % n);
}

int dfs(int p, int q, int a, int n, int temp) {
  if (a < temp || n == 0)
    return 0;
  int res = 0;
  int gcd = GCD(p, q);
  p /= gcd;
  q /= gcd;
  for (int i = max((q + p - 1) / p, temp); i <= a; i++) {
    if (p == 1 && q == i) {
      res++;
    } else
      res += dfs(p * i - q, q * i, a / i, n - 1, i);
  }
  return res;
}

int main() {
  int p, q, a, n;
  while (cin >> p >> q >> a >> n, p || q || a || n) {
    cout << dfs(p, q, a, n, 1) << endl;
  }
  return 0;
}
Problem p00712 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define mp make_pair
#define fi first
#define sc second
ll p, q, a, n;

typedef ll GCD_TYPE;
GCD_TYPE gcd(GCD_TYPE a, GCD_TYPE b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll dfs(ll p, ll q, ll nowa, ll nown, ll j) {
  if (p <= 0) {
    return p == 0;
  }
  if (nown >= n) {
    return 0;
  }
  if (q * (n - nown) < p * j) {
    return 0;
  }
  ll g = gcd(p, q);
  p /= g;
  q /= g;

  ll ret = 0;
  for (ll i = j; i * nowa <= a; i++) {
    ll l = q * i / gcd(q, i);

    ret += dfs((p * l / q) - (l / i), l, nowa * i, nown + 1, i);
  }
  return ret;
}

int main() {

  while (1) {
    cin >> p >> q >> a >> n;
    if (p == 0) {
      return 0;
    }
    cout << dfs(p, q, 1, 0, 1) << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef int ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define mp make_pair
#define fi first
#define sc second
ll p, q, a, n;

typedef ll GCD_TYPE;
GCD_TYPE gcd(GCD_TYPE a, GCD_TYPE b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll dfs(ll p, ll q, ll nowa, ll nown, ll j) {
  if (p <= 0) {
    return p == 0;
  }
  if (nown >= n) {
    return 0;
  }
  if (q * (n - nown) < p * j) {
    return 0;
  }
  ll g = gcd(p, q);
  p /= g;
  q /= g;

  ll ret = 0;
  for (ll i = j; i * nowa <= a; i++) {
    ll l = q * i / gcd(q, i);

    ret += dfs((p * l / q) - (l / i), l, nowa * i, nown + 1, i);
  }
  return ret;
}

int main() {

  while (1) {
    cin >> p >> q >> a >> n;
    if (p == 0) {
      return 0;
    }
    cout << dfs(p, q, 1, 0, 1) << endl;
  }

  return 0;
}
Problem p00712 (C++)

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

using namespace std;

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

int cont;
void saiki(double rest, int a, int n, int st, vector<int> &bin) {

  if (fabs(rest) < 0.0000001) {
    cont++;
    return;
  }
  if (bin.size() == n)
    return;

  int sum = 1;
  rep(i, bin.size()) sum *= bin[i];

  for (int i = st;; i++) {
    if (sum * i <= a) {
      bin.push_back(i);
      saiki(rest - 1.0 / i, a, n, i, bin);
      bin.pop_back();
    } else {
      break;
    }
  }
}

int main() {
  while (1) {
    int p, q, a, n;
    cin >> p >> q >> a >> n;
    cont = 0;

    if (n == 0)
      break;

    vector<int> bin;
    int st = 1;
    double rest = (double)p / q;
    saiki(rest, a, n, st, bin);

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

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

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

int cont;
void saiki(double rest, int a, int n, int st, vector<int> &bin) {

  if (fabs(rest) < 0.0000001) {
    cont++;
    return;
  }
  if (bin.size() == n)
    return;
  if (rest < 0)
    return;

  int sum = 1;
  rep(i, bin.size()) sum *= bin[i];

  for (int i = st;; i++) {
    if (sum * i <= a) {
      bin.push_back(i);
      saiki(rest - 1.0 / i, a, n, i, bin);
      bin.pop_back();
    } else {
      break;
    }
  }
}

int main() {
  while (1) {
    int p, q, a, n;
    cin >> p >> q >> a >> n;
    cont = 0;

    if (n == 0)
      break;

    vector<int> bin;
    int st = 1;
    double rest = (double)p / q;
    saiki(rest, a, n, st, bin);

    printf("%d\n", cont);
  }
}
Problem p00712 (C++)

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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x, n) bitset<n>(x)
#define PI 3.14159265358979323846

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

//-----------------------------------------------------------------------------

int p, q, a, n, num[7], ans;

void dfs(int x, int y, int z) {
  int sum = 0;
  REP(i, x) sum += y / num[i];
  if (p * y == sum * q)
    ans++;
  if (p * y < sum * q)
    return;
  if (x == n)
    return;
  FOR(i, z, a + 1) {
    if (i * y <= a) {
      num[x] = i;
      dfs(x + 1, y * i, i);
    }
  }
}

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

  while (true) {
    cin >> p >> q >> a >> n;
    if (p == 0 && q == 0)
      break;
    ans = 0;
    dfs(0, 1, 1);
    cout << ans << endl;
  }

  return 0;
}


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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x, n) bitset<n>(x)
#define PI 3.14159265358979323846

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

//-----------------------------------------------------------------------------

int p, q, a, n, num[7], ans;

void dfs(int x, int y, int z) {
  int sum = 0;
  REP(i, x) sum += y / num[i];
  if (p * y == sum * q)
    ans++;
  if (p * y < sum * q)
    return;
  if (x == n)
    return;
  FOR(i, z, a + 1) {
    if (i * y <= a) {
      num[x] = i;
      dfs(x + 1, y * i, i);
    } else
      break;
  }
}

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

  while (true) {
    cin >> p >> q >> a >> n;
    if (p == 0 && q == 0)
      break;
    ans = 0;
    dfs(0, 1, 1);
    cout << ans << endl;
  }

  return 0;
}
Problem p00712 (C++)

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

int P, Q, A, N;
int P_, Q_;

int gcd(int a, int b) { return a % b ? gcd(b, a % b) : b; }

int solve(int a, int n, int sum, int b) {
  int res = 0;

  int div = gcd(sum, a);
  if (P_ == sum / div && Q_ == a / div)
    return 1;
  if (P_ * a < Q_ * sum)
    return 0;
  if (n >= N)
    return res;

  for (int i = b; a * i <= A; i++)
    res += solve(a * i, n + 1, sum * i + a, i);
  return res;
}

int main() {
  while (cin >> P >> Q >> A >> N, P | Q | A | N) {
    //		cout << P << " " << Q << " " << A << " " << N << endl;
    int div = gcd(P, Q);
    // cout << div << endl;
    P_ = P / div;
    Q_ = Q / div;

    int res = 0;
    for (int i = 1; i <= A; i++) {
      int tmp;
      tmp = solve(i, 1, 1, i);
      if (tmp > 0)
        res += tmp;
    }
    cout << res << endl;
  }
}

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

int P, Q, A, N;
int P_, Q_;

int gcd(int a, int b) { return a % b ? gcd(b, a % b) : b; }

int solve(int a, int n, int sum, int b) {
  int res = 0;

  /*
  int div = gcd(sum , a);
  if(P_ == sum / div && Q_ == a / div)
          return 1;
  // */
  if (sum / P_ == a / Q_ && sum % P_ + a % Q_ == 0)
    return 1;
  if (P_ * a < Q_ * sum)
    return 0;
  if (n >= N)
    return res;

  for (int i = b; a * i <= A; i++)
    res += solve(a * i, n + 1, sum * i + a, i);
  return res;
}

int main() {
  while (cin >> P >> Q >> A >> N, P | Q | A | N) {
    //		cout << P << " " << Q << " " << A << " " << N << endl;
    int div = gcd(P, Q);
    // cout << div << endl;
    P_ = P / div;
    Q_ = Q / div;

    int res = 0;
    for (int i = 1; i <= A; i++) {
      int tmp;
      tmp = solve(i, 1, 1, i);
      if (tmp > 0)
        res += tmp;
    }
    cout << res << endl;
  }
}
Problem p00712 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl;
int p, q, a, n, ans;
double eps = 1e-12;
void dfs(int pro, int cnt, double sum, int mx) {
  if (abs(sum - (double)p / q) < eps)
    ans++;
  if (cnt == n)
    return;
  rep1(i, mx) {
    if (i * pro > a)
      break;
    dfs(i * pro, cnt + 1, sum + 1.0 / i, min(mx, i));
  }
}
int main() {
  while (true) {
    cin >> p >> q >> a >> n;
    if (p == 0)
      break;
    ans = 0;
    dfs(1, 0, 0, a);
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl;
int p, q, a, n, ans;
double eps = 1e-12;
void dfs(int pro, int cnt, double sum, int mx) {
  if (abs(sum - (double)p / q) < eps)
    ans++;
  if (sum + eps > (double)p / q)
    return;
  if (cnt == n)
    return;
  rep1(i, mx) {
    if (i * pro > a)
      break;
    dfs(i * pro, cnt + 1, sum + 1.0 / i, min(mx, i));
  }
}
int main() {
  while (true) {
    cin >> p >> q >> a >> n;
    if (p == 0)
      break;
    ans = 0;
    dfs(1, 0, 0, a);
    cout << ans << endl;
  }
}
Problem p00712 (C++)

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

using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
struct flac {
  int p, q;
  flac(int p, int q) : p(p), q(q) {}
};

flac sumf(flac f, flac g) { return flac(f.p * g.q + f.q * g.p, f.q * g.q); }
bool operator==(flac f, flac g) { return (f.p * g.q == f.q * g.p); }
bool operator<(flac f, flac g) { return (f.p * g.q < f.q * g.p); }

int solve(flac sum, int &a, int n, flac &target, int bf) {
  if (sum == target)
    return 1;
  if (n == 0)
    return 0;
  int para = sum.q;
  int ret = 0;
  FOR(i, bf, a) {
    if (i * para > a)
      break;
    ret += solve(sumf(sum, flac(1, i)), a, n - 1, target, i);
  }
  return ret;
}

int main(void) {
  int p, q, a, n;
  while (cin >> p >> q >> a >> n) {
    if (!p && !q && !a && !n)
      break;
    flac tar = flac(p, q);
    cout << solve(flac(0, 1), a, n, tar, 1) << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
struct flac {
  int p, q;
  flac(int p, int q) : p(p), q(q) {}
};

flac sumf(flac f, flac g) { return flac(f.p * g.q + f.q * g.p, f.q * g.q); }
bool operator==(flac f, flac g) { return (f.p * g.q == f.q * g.p); }
bool operator<(flac f, flac g) { return (f.p * g.q < f.q * g.p); }

int solve(flac sum, int &a, int n, flac &target, int bf) {
  if (sum == target)
    return 1;
  if (target < sum)
    return 0;
  if (n == 0)
    return 0;
  int para = sum.q;
  int ret = 0;
  FOR(i, bf, a) {
    if (i * para > a)
      break;
    ret += solve(sumf(sum, flac(1, i)), a, n - 1, target, i);
  }
  return ret;
}

int main(void) {
  int p, q, a, n;
  while (cin >> p >> q >> a >> n) {
    if (!p && !q && !a && !n)
      break;
    flac tar = flac(p, q);
    cout << solve(flac(0, 1), a, n, tar, 1) << endl;
  }
  return 0;
}
Problem p00712 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int memo[12001][8];
int b_pow(int a, int b) {
  if (memo[a][b])
    return memo[a][b];
  else if (b == 0)
    return 1;
  else if (b == 1)
    return a;
  else
    return memo[a][b] = b_pow(a, b / 2) * b_pow(a, (b + 1) / 2);
}
int ans, A;

int dfs(int p, int q, int k, int a, int t) {
  if (a <= 0 || a > A)
    return 0;
  if (p == 0)
    return 1;
  if (t * a > A || k == 0 || p == 0)
    return 0;
  int ans = 0;
  for (int i = 0; i <= k; i++) {
    if (p * t >= i * q) {
      int w = p * t - i * q;
      int x = q * t;
      if (w != 0) {
        int g = __gcd(w, x);
        w /= g, x /= g;
      }
      ans += dfs(w, x, k - i, a * b_pow(t, i), t + 1);
    } else {
      break;
    }
  }
  return ans;
}
int main() {
  int a, b, c, d;
  while (cin >> a >> b >> c >> d, a || b || c || d) {
    A = c;
    cout << dfs(a, b, d, 1, 1) << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int memo[12001][8];
int b_pow(int a, int b) {
  if (memo[a][b])
    return memo[a][b];
  else if (b == 0)
    return 1;
  else if (b == 1)
    return a;
  else
    return memo[a][b] = b_pow(a, b / 2) * b_pow(a, (b + 1) / 2);
}
int ans, A;

int dfs(int p, int q, int k, int a, int t) {
  if (a <= 0 || a > A || p * t > k * q)
    return 0;
  if (p == 0)
    return 1;
  if (t * a > A || k == 0 || p == 0)
    return 0;
  int ans = 0;
  for (int i = 0; i <= k; i++) {
    if (p * t >= i * q) {
      int w = p * t - i * q;
      int x = q * t;
      if (w != 0) {
        int g = __gcd(w, x);
        w /= g, x /= g;
      }
      ans += dfs(w, x, k - i, a * b_pow(t, i), t + 1);
    } else {
      break;
    }
  }
  return ans;
}
int main() {
  int a, b, c, d;
  while (cin >> a >> b >> c >> d, a || b || c || d) {
    A = c;
    cout << dfs(a, b, d, 1, 1) << endl;
  }
}
Problem p00719 (C++)

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

#pragma warning(disable : 4996)

#define INF 1000000000.0

using namespace std;

int n, m, p, a, b, x, y, z, t[8];

double dp[128][30];

vector<pair<int, int>> G[30];

int main() {
  while (true) {
    scanf("%d", &n);
    scanf("%d", &m);
    scanf("%d", &p);
    scanf("%d", &a);
    scanf("%d", &b);

    if (n == 0 && m == 0 && p == 0 && a == 0 && b == 0)
      break;

    for (int i = 0; i < m; i++)
      G[i].clear();

    for (int i = 0; i < n; i++)
      scanf("%d", &t[i]);

    for (int i = 0; i < p; i++) {
      scanf("%d", &x);
      scanf("%d", &y);
      scanf("%d", &z);

      G[x - 1].push_back(make_pair(y - 1, z));
      G[y - 1].push_back(make_pair(x - 1, z));
    }

    for (int i = 0; i < (1 << n); i++) {
      for (int j = 0; j < m; j++) {
        dp[i][j] = INF;
      }
    }

    dp[0][a - 1] = 0.0;

    for (int i = 1; i < (1 << n); i++) {
      for (int j = 0; j < n; j++) {
        if (i & (1 << j)) {
          for (int k = 0; k < m; k++) {
            for (int l = 0; l < G[k].size(); l++) {
              int node = G[k][l].first;

              double dist = G[k][l].second * 1.0 / t[j];

              dp[i][node] = min(dp[i][node], dp[i - (1 << j)][k] + dist);
            }
          }
        }
      }
    }

    double ret = INF;

    for (int i = 0; i < (1 << n); i++) {
      ret = min(ret, dp[i][b - 1]);
    }

    if (ret != INF) {
      printf("%.12f\n", ret);
    } else {
      printf("Impossible\n");
    }
  }

  return 0;
}

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

#pragma warning(disable : 4996)

#define INF 1000000000.0

using namespace std;

int n, m, p, a, b, x, y, z, t[8];

double dp[256][30];

vector<pair<int, int>> G[30];

int main() {
  while (true) {
    scanf("%d", &n);
    scanf("%d", &m);
    scanf("%d", &p);
    scanf("%d", &a);
    scanf("%d", &b);

    if (n == 0 && m == 0 && p == 0 && a == 0 && b == 0)
      break;

    for (int i = 0; i < m; i++)
      G[i].clear();

    for (int i = 0; i < n; i++)
      scanf("%d", &t[i]);

    for (int i = 0; i < p; i++) {
      scanf("%d", &x);
      scanf("%d", &y);
      scanf("%d", &z);

      G[x - 1].push_back(make_pair(y - 1, z));
      G[y - 1].push_back(make_pair(x - 1, z));
    }

    for (int i = 0; i < (1 << n); i++) {
      for (int j = 0; j < m; j++) {
        dp[i][j] = INF;
      }
    }

    dp[0][a - 1] = 0.0;

    for (int i = 1; i < (1 << n); i++) {
      for (int j = 0; j < n; j++) {
        if (i & (1 << j)) {
          for (int k = 0; k < m; k++) {
            for (int l = 0; l < G[k].size(); l++) {
              int node = G[k][l].first;

              double dist = G[k][l].second * 1.0 / t[j];

              dp[i][node] = min(dp[i][node], dp[i - (1 << j)][k] + dist);
            }
          }
        }
      }
    }

    double ret = INF;

    for (int i = 0; i < (1 << n); i++) {
      ret = min(ret, dp[i][b - 1]);
    }

    if (ret != INF) {
      printf("%.12f\n", ret);
    } else {
      printf("Impossible\n");
    }
  }

  return 0;
}
Problem p00719 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, double> P;
int main() {
  int n, m, p, a, b;
  while (cin >> n >> m >> p >> a >> b, n) {
    a--, b--;

    double t[8], tp;
    vector<P> e[31];

    for (int i = 0; i < n; i++)
      cin >> t[i];
    for (int j = 0, x, y; j < p; j++) {
      cin >> x >> y >> tp;
      x--, y--;
      e[x].push_back(P(y, tp)), e[y].push_back(P(x, tp));
    }

    double dp[1 << 8][30];
    for (int i = 0; i < 1 << n; i++)
      for (int j = 0; j < m; j++)
        dp[i][j] = 1e4;
    dp[0][a] = 0;

    for (int i = 0; i < 1 << n; i++)
      for (int j = 0; j < m; j++)
        for (int k = 0; k < n; k++)
          if (!(i & 1 << k))
            for (int l = 0; l < e[k].size(); l++)
              dp[i + (1 << k)][e[j][l].first] =
                  min(dp[i + (1 << k)][e[j][l].first],
                      dp[i][j] + e[j][l].second / t[k]);

    double ans = 1e4;
    for (int i = 0; i < 1 << n; i++)
      ans = min(ans, dp[i][b]);
    if (ans == 1e4)
      cout << "Impossible" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, double> P;
int main() {
  int n, m, p, a, b;
  while (cin >> n >> m >> p >> a >> b, n) {
    a--, b--;

    double t[8], tp;
    vector<P> e[31];

    for (int i = 0; i < n; i++)
      cin >> t[i];
    for (int j = 0, x, y; j < p; j++) {
      cin >> x >> y >> tp;
      x--, y--;
      e[x].push_back(P(y, tp)), e[y].push_back(P(x, tp));
    }

    double dp[1 << 8][30];
    for (int i = 0; i < 1 << n; i++)
      for (int j = 0; j < m; j++)
        dp[i][j] = 1e4;
    dp[0][a] = 0;

    for (int i = 0; i < 1 << n; i++)
      for (int j = 0; j < m; j++)
        for (int k = 0; k < n; k++)
          if (!(i & 1 << k))
            for (int l = 0; l < e[j].size(); l++)
              dp[i + (1 << k)][e[j][l].first] =
                  min(dp[i + (1 << k)][e[j][l].first],
                      dp[i][j] + e[j][l].second / t[k]);

    double ans = 1e4;
    for (int i = 0; i < 1 << n; i++)
      ans = min(ans, dp[i][b]);
    if (ans == 1e4)
      cout << "Impossible" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
Problem p00722 (C++)

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

using namespace std;

const int MAX_SIZE = 100000;

int prime[MAX_SIZE];
bool isPrime[MAX_SIZE];
int p;

int main() {

  fill(isPrime, isPrime + MAX_SIZE, true);
  p = 0;
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i <= MAX_SIZE; i++) {
    if (isPrime[i]) {
      prime[p++] = i;
    } else
      continue;
    for (int j = 2 * i; j <= MAX_SIZE; j += i) {
      isPrime[j] = false;
    }
  }

  int a, d, n;
  while (cin >> a >> d >> n && !(a == 0 && d == 0 && n == 0)) {
    int cnt = 0;
    for (int i = 0;; i++) {
      int num = a + i * d;
      if (isPrime[num]) {
        cnt++;
      }
      if (cnt == n) {
        cout << num << endl;
        break;
      }
    }
  }

  return 0;
}

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

using namespace std;

const int MAX_SIZE = 1000000;

int prime[MAX_SIZE];
bool isPrime[MAX_SIZE];
int p;

int main() {

  fill(isPrime, isPrime + MAX_SIZE, true);
  p = 0;
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i <= MAX_SIZE; i++) {
    if (isPrime[i]) {
      prime[p++] = i;
    } else
      continue;
    for (int j = 2 * i; j <= MAX_SIZE; j += i) {
      isPrime[j] = false;
    }
  }

  int a, d, n;
  while (cin >> a >> d >> n && !(a == 0 && d == 0 && n == 0)) {
    int cnt = 0;
    for (int i = 0;; i++) {
      int num = a + i * d;
      if (isPrime[num]) {
        cnt++;
      }
      if (cnt == n) {
        cout << num << endl;
        break;
      }
    }
  }

  return 0;
}
Problem p00722 (C++)

Buggy submission (status: Memory Limit Exceeded):


#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
#define bv vector<bool>
using namespace std;
typedef pair<int, int> PA;
using namespace std;
#define MAX 99999999
int main() {
  // ?´???°??????sqrt(max)??§??????
  vector<bool> check(MAX + 1);
  rep(i, MAX + 1) check[i] = true;
  vector<int> Primenumber(MAX + 1, 0); // Primearray
  int counter = 0;                     // Primearray counter;
  for (int i = 2; i < MAX + 1; i++) {
    if (check[i]) {
      for (int j = 2; i * j < MAX; j++)
        check[i * j] =
            false; //?´???°??????Primearray???????´??????????????????°?????¨???false???
      Primenumber[counter] = i;
      counter++;
    }
  }
  int a, d, n;
  while (cin >> a >> d >> n, a + d + n) {
    int count = 0;
    int ans = 0;
    while (true) {
      if (check[a] && a != 1)
        count++;
      if (count == n) {
        ans = a;
        break;
      }
      a += d;
    }
    cout << ans << endl;
  }
}

Fixed submission:


#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
#define bv vector<bool>
using namespace std;
typedef pair<int, int> PA;
using namespace std;
#define MAX 999999
int main() {
  // ?´???°??????sqrt(max)??§??????
  vector<bool> check(MAX + 1);
  rep(i, MAX + 1) check[i] = true;
  vector<int> Primenumber(MAX + 1, 0); // Primearray
  int counter = 0;                     // Primearray counter;
  for (int i = 2; i < MAX + 1; i++) {
    if (check[i]) {
      for (int j = 2; i * j < MAX; j++)
        check[i * j] =
            false; //?´???°??????Primearray???????´??????????????????°?????¨???false???
      Primenumber[counter] = i;
      counter++;
    }
  }
  int a, d, n;
  while (cin >> a >> d >> n, a + d + n) {
    int count = 0;
    int ans = 0;
    while (true) {
      if (check[a] && a != 1)
        count++;
      if (count == n) {
        ans = a;
        break;
      }
      a += d;
    }
    cout << ans << endl;
  }
}
Problem p00722 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main(void) {
  for (;;) {
    int a, d, n;
    cin >> a >> d >> n;
    if (a == 0 && d == 0 && n == 0) {
      return 0;
    }
    int ans = -1;
    for (int i = 0; ((a + d * i <= 1000000) && (ans == -1)); i++) {
      bool prime = true;
      for (int j = 2; j <= 10000; j++) {
        if ((a + d * i) % j == 0) {
          prime = false;
        }
      }
      if (prime) {
        if (n == 1) {
          ans = a + d * i;
        } else {
          n--;
        }
      }
    }
    cout << ans << endl;
  }
}


Fixed submission:
#include <iostream>
using namespace std;
int main(void) {
  for (;;) {
    int a, d, n;
    cin >> a >> d >> n;
    if (a == 0 && d == 0 && n == 0) {
      return 0;
    }
    int ans = -1;
    for (int i = 0; ((a + d * i <= 1000000) && (ans == -1)); i++) {
      bool prime = true;
      if (a + d * i == 1) {
        prime = false;
      } else {
        for (int j = 2; j * j <= a + d * i; j++) {
          if ((a + d * i) % j == 0) {
            prime = false;
          }
        }
      }
      if (prime) {
        if (n == 1) {
          ans = a + d * i;
        } else {
          n--;
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00722 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <fstream>
#include <iostream>
#include <list>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; ++i)
#define LL long long
using namespace std;

#define int LL

bool func(int x) {
  if (x < 2)
    return false;

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

  return true;
}

signed main() {
  // ifstream cin("Ain.txt");
  // ofstream cout("Aout.txt");

  int a, d, n;

  do {
    cin >> a >> d >> n;

    a -= d;

    while (n) {
      a += d;
      if (func(a))
        n--;
    }
    cout << a << endl;
  } while (!(a && d && n));
}

Fixed submission:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <list>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; ++i)
#define LL long long
using namespace std;

#define int LL

bool func(int x) {
  if (x < 2)
    return false;

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

  return true;
}

signed main() {
  // ifstream cin("Ain.txt");
  // ofstream cout("Aout.txt");

  int a, d, n;

  do {
    cin >> a >> d >> n;

    if (!(a && d && n))
      break;

    a -= d;

    while (n) {
      a += d;
      if (func(a))
        n--;
    }
    cout << a << endl;
  } while (!(a && d && n));
}
Problem p00722 (C++)

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

int main();
bool prime_chk(int num);

int main() {

  int a[100] = {0};
  int d[100] = {0};
  int n[100] = {0};
  int result[100] = {0};
  int n_series;
  int s_series;
  int nth_prime_num;
  int i, i_dataset;

  bool prime_flg;

  // データ入力
  i_dataset = 0;
  while (1) {
    scanf("%d %d %d", &a[i_dataset], &d[i_dataset], &n[i_dataset]);

    if (a[i_dataset] == 0 && d[i_dataset] == 0 && n[i_dataset] == 0)
      break; // 0 0 0が入力されると処理終了
    else
      i_dataset++; // データ数を記録
  }

  // 計算開始

  for (i = 0; i < i_dataset; i++) {

    nth_prime_num = 0;
    n_series = 1;

    while (1) {
      if (n_series == 1)
        s_series = a[i];
      else
        s_series = a[i] + (n_series - 1) * d[i];

      prime_flg = prime_chk(s_series);
      if (prime_flg == true)
        nth_prime_num++;

      if (nth_prime_num == n[i]) {
        result[i] = s_series;
        break;
      }

      n_series++;
    }
  }

  for (i = 0; i < i_dataset; i++) {
    printf("%d\n", result[i]); // 結果出力
  }

  return 0;
}

bool prime_chk(int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false; // 偶数はあらかじめ除く

  double sqrt_num = sqrt(num);
  for (int i = 3; i <= sqrt_num; i += 2) {
    if (num % i == 0) {
      // 素数ではない
      return false;
    }
  }

  // 素数である
  return true;
}


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

int main();
bool prime_chk(int num);

int main() {

  int a[1000] = {0};
  int d[1000] = {0};
  int n[1000] = {0};
  int result[1000] = {0};
  int n_series;
  int s_series;
  int nth_prime_num;
  int i, i_dataset;

  bool prime_flg;

  // データ入力
  i_dataset = 0;
  while (1) {
    scanf("%d %d %d", &a[i_dataset], &d[i_dataset], &n[i_dataset]);

    if (a[i_dataset] == 0 && d[i_dataset] == 0 && n[i_dataset] == 0)
      break; // 0 0 0が入力されると処理終了
    else
      i_dataset++; // データ数を記録
  }

  // 計算開始

  for (i = 0; i < i_dataset; i++) {

    nth_prime_num = 0;
    n_series = 1;

    while (1) {
      if (n_series == 1)
        s_series = a[i];
      else
        s_series = a[i] + (n_series - 1) * d[i];

      prime_flg = prime_chk(s_series);
      if (prime_flg == true)
        nth_prime_num++;

      if (nth_prime_num == n[i]) {
        result[i] = s_series;
        break;
      }

      n_series++;
    }
  }

  for (i = 0; i < i_dataset; i++) {
    printf("%d\n", result[i]); // 結果出力
  }

  return 0;
}

bool prime_chk(int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false; // 偶数はあらかじめ除く

  double sqrt_num = sqrt(num);
  for (int i = 3; i <= sqrt_num; i += 2) {
    if (num % i == 0) {
      // 素数ではない
      return false;
    }
  }

  // 素数である
  return true;
}
Problem p00722 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
using namespace std;

#define INF 1 << 21
#define MOD 1000000007
#define MAX 1000000

int main() {
  int p[MAX];
  memset(p, 1, sizeof(p));
  p[0] = 0;
  p[1] = 0;
  for (int i = 2; i <= sqrt(MAX) + 1; i++) {
    if (p[i] == 1) {
      for (int j = 2 * i; j <= MAX; j += i) {
        p[j] = 0;
      }
    }
  }

  int a, d, n;
  while (1) {
    cin >> a >> d >> n;
    if (a == 0 && d == 0 && n == 0)
      return 0;
    while (1) {
      if (p[a] == 1)
        n--;
      if (n == 0) {
        cout << a << endl;
        break;
      }
      a += d;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
using namespace std;

#define INF 1 << 21
#define MOD 1000000007
#define MAX 1000000

int main() {
  char p[MAX];
  memset(p, 1, sizeof(p));
  p[0] = 0;
  p[1] = 0;
  for (int i = 2; i <= sqrt(MAX) + 1; i++) {
    if (p[i] == 1) {
      for (int j = 2 * i; j <= MAX; j += i) {
        p[j] = 0;
      }
    }
  }

  int a, d, n;
  while (1) {
    cin >> a >> d >> n;
    if (a == 0 && d == 0 && n == 0)
      return 0;
    while (1) {
      if (p[a] == 1)
        n--;
      if (n == 0) {
        cout << a << endl;
        break;
      }
      a += d;
    }
  }
}
Problem p00722 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char *argv[]) {
  const int N = 1000001;
  vector<bool> prime(N, true);
  prime[0] = false;
  prime[1] = false;
  for (int i = 2; i < N; i++) {
    if (prime[i]) {
      for (int j = i + i; j < N; j += i) {
        prime[j] = false;
      }
    }
  }
  for (;;) {
    int a, d, n;
    cin >> a >> d >> n;
    if (a == 0)
      break;
    for (;;) {
      while (!prime[a]) {
        a += d;
      }
      if (n == 0)
        break;
      a += d;
    }
    cout << a << endl;
  }
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char *argv[]) {
  const int N = 1000001;
  vector<bool> prime(N, true);
  prime[0] = false;
  prime[1] = false;
  for (int i = 2; i < N; i++) {
    if (prime[i]) {
      for (int j = i + i; j < N; j += i) {
        prime[j] = false;
      }
    }
  }
  for (;;) {
    int a, d, n;
    cin >> a >> d >> n;
    if (a == 0)
      break;
    for (;;) {
      while (!prime[a]) {
        a += d;
      }
      n--;
      if (n == 0)
        break;
      a += d;
    }
    cout << a << endl;
  }
  return 0;
}
Problem p00722 (C++)

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

using namespace std;

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

  while (cin >> a >> b >> n && n) {
    int count = 0, flag = 0;
    for (int i = a; i < 1000000; i += b) {
      for (int j = 2; j < i; j++) {
        if (i % j == 0 && i != j) {
          flag++;
          break;
        }
      }
      if (!flag && i >= 2)
        count++;
      if (count == n) {
        cout << i << endl;
        break;
      }
      flag = 0;
    }
  }
}

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

using namespace std;

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

  while (cin >> a >> b >> n && n) {
    int count = 0, flag = 0;
    for (int i = a; i <= 1000000; i += b) {
      for (int j = 2; j <= sqrt(i); j++) {
        if (i % j == 0 && i != j) {
          flag++;
          break;
        }
      }
      if (!flag && i >= 2)
        count++;
      if (count == n) {
        cout << i << endl;
        break;
      }
      flag = 0;
    }
  }
}