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

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

int w, h, n, A[11][11], ans, cnt;
bitset<100> B[11][11];

void dfs(int d, bitset<100> s, int sco) {
  if (d == n) {
    ans = min(ans, sco);
    return;
  }
  cnt++;
  if (cnt >= 100000000)
    return;
  if (ans <= sco)
    return;
  if (!A[d / w][d % w])
    dfs(d + 1, s, sco);
  else {
    int y = d / w;
    int x = d % w;
    if ((B[y][x] & s).any()) {
      bitset<100> p;
      p.set(y * 10 + x); //(y*100+x);
      if (!(s & p).any())
        dfs(d + 1, s, sco);
      dfs(d + 1, (B[y][x] | s) ^ B[y][x], sco + 1);
    } else
      dfs(d + 1, s, sco);
  }
}

int main() {
  while (cin >> w >> h, w) {
    memset(A, 0, sizeof(A));
    r(i, 11) r(j, 11) B[i][j] &= 0;
    n = h * w;
    bitset<100> b;
    r(i, h) r(j, w) cin >> A[i][j];
    r(i, h) r(j, w) if (A[i][j]) {
      int sum = 1;
      for (;; sum++) {
        int f = 0;
        for (int y = i; y < i + sum; y++) {
          for (int x = j; x < j + sum; x++) {
            if (A[y][x] == 0)
              f++;
          }
        }
        if (f) {
          sum--;
          break;
        }
      }
      bitset<100> tmp;
      for (int y = i, k = 0; k < sum; k++, y++) {
        for (int x = j, l = 0; l < sum; l++, x++) {
          tmp.set(y * 10 + x);
        }
      }
      b.set(i * 10 + j);
      B[i][j] = tmp;
    }
    ans = 100;
    cnt = 0;
    dfs(0, b, 0);
    cout << ans << endl;
  }
}


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

int w, h, n, A[11][11], ans, cnt;
bitset<100> B[11][11];

void dfs(int d, bitset<100> s, int sco) {
  if (d == n) {
    ans = min(ans, sco);
    return;
  }
  cnt++;
  if (cnt >= 30000000)
    return;
  if (ans <= sco)
    return;
  if (!A[d / w][d % w])
    dfs(d + 1, s, sco);
  else {
    int y = d / w;
    int x = d % w;
    if ((B[y][x] & s).any()) {
      bitset<100> p;
      p.set(y * 10 + x); //(y*100+x);
      if (!(s & p).any())
        dfs(d + 1, s, sco);
      dfs(d + 1, (B[y][x] | s) ^ B[y][x], sco + 1);
    } else
      dfs(d + 1, s, sco);
  }
}

int main() {
  while (cin >> w >> h, w) {
    memset(A, 0, sizeof(A));
    r(i, 11) r(j, 11) B[i][j] &= 0;
    n = h * w;
    bitset<100> b;
    r(i, h) r(j, w) cin >> A[i][j];
    r(i, h) r(j, w) if (A[i][j]) {
      int sum = 1;
      for (;; sum++) {
        int f = 0;
        for (int y = i; y < i + sum; y++) {
          for (int x = j; x < j + sum; x++) {
            if (A[y][x] == 0)
              f++;
          }
        }
        if (f) {
          sum--;
          break;
        }
      }
      bitset<100> tmp;
      for (int y = i, k = 0; k < sum; k++, y++) {
        for (int x = j, l = 0; l < sum; l++, x++) {
          tmp.set(y * 10 + x);
        }
      }
      b.set(i * 10 + j);
      B[i][j] = tmp;
    }
    ans = 100;
    cnt = 0;
    dfs(0, b, 0);
    cout << ans << endl;
  }
}
Problem p00710 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <exception>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define LL long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<LL, LL> LP;
typedef pair<int, P> PP;
typedef pair<LL, LP> LPP;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

int main() {
  int n, r;

  while (cin >> n >> r, n + r) {
    int huda[51];
    REP(i, n) { huda[i] = n - i; }

    while (r--) {
      int p, c;
      int tmp[c];
      cin >> p >> c;

      REP(i, c) { tmp[i] = huda[p + i - 1]; }

      REPR(i, p - 2) { huda[i + c] = huda[i]; }

      REP(i, c) { huda[i] = tmp[i]; }
    }

    cout << huda[0] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <exception>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define LL long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<LL, LL> LP;
typedef pair<int, P> PP;
typedef pair<LL, LP> LPP;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

int main() {
  int n, r;

  while (cin >> n >> r, n + r) {
    int huda[51];
    REP(i, n) { huda[i] = n - i; }

    while (r--) {
      int p, c;
      int tmp[51];
      cin >> p >> c;

      REP(i, c) { tmp[i] = huda[p + i - 1]; }

      REPR(i, p - 2) { huda[i + c] = huda[i]; }

      REP(i, c) { huda[i] = tmp[i]; }
    }

    cout << huda[0] << endl;
  }
}
Problem p00710 (C++)

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

using namespace std;

int main() {
  vector<int> cards;
  vector<int>::iterator cardsIterator;
  vector<int>::iterator shuffuleIterator;
  int n, r, p, c;

  while (cin >> n >> r, n && r) {
    cards.clear(); // 初期化
    // カードの山生成
    for (int i = 0; i < n; ++i) {
      cards.push_back(i + 1);
    }
    // シャッフル
    while (cin >> p >> c) {
      cardsIterator = cards.end();
      cardsIterator -= p + (c - 1);
      while (c--) {
        cards.push_back(*cardsIterator);
        cards.erase(cardsIterator);
      }
      if (r == 0) {
        break;
      }
    }
    // 一番上のカード表示
    cout << cards.back() << endl;
  }
}

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

using namespace std;

int main() {
  vector<int> cards;
  vector<int>::iterator cardsIterator;
  vector<int>::iterator shuffuleIterator;
  int n, r, p, c;

  while (cin >> n >> r, n && r) {
    cards.clear(); // 初期化
    // カードの山生成
    for (int i = 0; i < n; ++i) {
      cards.push_back(i + 1);
    }
    // シャッフル
    while (cin >> p >> c, r--) {
      cardsIterator = cards.end();
      cardsIterator -= p + (c - 1);
      while (c--) {
        cards.push_back(*cardsIterator);
        cards.erase(cardsIterator);
      }
      if (r == 0) {
        break;
      }
    }
    // 一番上のカード表示
    cout << cards.back() << endl;
  }
}
Problem p00710 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;

int main(void) {
  int ans[100], cnt = 0;
  while (1) {
    int n, r;
    cin >> n >> r;
    if (n == 0)
      break;
    int p[50], c[50];
    irep(i, r) cin >> p[i] >> c[i]; //?????????????????????
    int pos = 0;
    rep(i, r) {
      if (pos < c[i]) {
        pos += p[i] - 1;
      } else if (pos < c[i] + p[i] - 1) {
        pos -= c[i];
      }
    }
    ans[cnt] = n - pos;
    cnt++;
  }
  rep(i, cnt) cout << ans[i] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;

int main(void) {
  int ans[1000], cnt = 0;
  while (1) {
    int n, r;
    cin >> n >> r;
    if (n == 0)
      break;
    int p[50], c[50];
    irep(i, r) cin >> p[i] >> c[i]; //?????????????????????
    int pos = 0;
    rep(i, r) {
      if (pos < c[i]) {
        pos += p[i] - 1;
      } else if (pos < c[i] + p[i] - 1) {
        pos -= c[i];
      }
    }
    ans[cnt] = n - pos;
    cnt++;
  }
  rep(i, cnt) cout << ans[i] << endl;
  return 0;
}
Problem p00710 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define N 10
using namespace std;
/*
Hanafuda Shuffle
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1129&lang=jp

*/
int array[N];
void makearray(int n) {
  for (int i = 0; i < N; i++) {
    array[i] = n;
    if (n > 0)
      n -= 1;
  }
}
void shuffle(int p, int c) {
  int sarray[N];
  // copy unshuffled deck
  for (int i = p - 1 + c; i < N; i++) {
    sarray[i] = array[i];
  }
  // copy the c part
  int j = 0;
  for (int i = p - 1; i < p - 1 + c; i++) {
    sarray[j] = array[i];
    j++;
  }
  // copy the p part
  j = c;
  for (int i = 0; i < p - 1; i++) {
    sarray[j] = array[i];
    j++;
  }
  for (int i = 0; i < N; i++) {
    array[i] = sarray[i];
  }
}

int main() {
  int n, r, p, c;
  while (1) {
    cin >> n >> r;
    makearray(n);
    if (n == 0 && r == 0)
      break;
    for (int i = 0; i < r; i++) {
      cin >> p >> c;
      shuffle(p, c);
    }
    cout << array[0] << endl;
  }
}

Fixed submission:
#include <iostream>
#define N 50
using namespace std;
/*
Hanafuda Shuffle
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1129&lang=jp

*/
int array[N];
void makearray(int n) {
  for (int i = 0; i < N; i++) {
    array[i] = n;
    if (n > 0)
      n -= 1;
  }
}
void shuffle(int p, int c) {
  int sarray[N];
  // copy unshuffled deck
  for (int i = p - 1 + c; i < N; i++) {
    sarray[i] = array[i];
  }
  // copy the c part
  int j = 0;
  for (int i = p - 1; i < p - 1 + c; i++) {
    sarray[j] = array[i];
    j++;
  }
  // copy the p part
  j = c;
  for (int i = 0; i < p - 1; i++) {
    sarray[j] = array[i];
    j++;
  }
  for (int i = 0; i < N; i++) {
    array[i] = sarray[i];
  }
}

int main() {
  int n, r, p, c;
  while (1) {
    cin >> n >> r;
    makearray(n);
    if (n == 0 && r == 0)
      break;
    for (int i = 0; i < r; i++) {
      cin >> p >> c;
      shuffle(p, c);
    }
    cout << array[0] << endl;
  }
}
Problem p00711 (C++)

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

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int W, H;
char board[30][30];

int dfs(int y, int x) {
  board[y][x] = '#';
  int ret = 1;
  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i], nx = x + dx[i];
    if (ny >= 0 && ny < H && nx >= 0 && nx < W && board[ny][nx] == '.') {
      ret += dfs(y, x);
    }
  }
  return ret;
}

int main() {
  while (cin >> W >> H, W) {
    for (int i = 0; i < H; i++) {
      cin >> board[i];
    }

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (board[i][j] == '@') {
          cout << dfs(i, j) << endl;
        }
      }
    }
  }

  return 0;
}


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

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int W, H;
char board[30][30];

int dfs(int y, int x) {
  board[y][x] = '#';
  int ret = 1;
  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i], nx = x + dx[i];
    if (ny >= 0 && ny < H && nx >= 0 && nx < W && board[ny][nx] == '.') {
      ret += dfs(ny, nx);
    }
  }
  return ret;
}

int main() {
  while (cin >> W >> H, W) {
    for (int i = 0; i < H; i++) {
      cin >> board[i];
    }

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (board[i][j] == '@') {
          cout << dfs(i, j) << endl;
        }
      }
    }
  }

  return 0;
}
Problem p00711 (C++)

Buggy submission (status: Runtime Error):
#include <cassert>
#include <iostream>
#define REP(i, l, n) for (int i = l; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define MAX 20
int ans, baseX, baseY;
int w, h;
char tile[MAX][MAX];
void seek(int x, int y);
using namespace std;
int main() {
  while (1) {
    cin >> w;
    cin >> h;
    if (w == 0 && h == 0)
      return 0;
    rep(i, h) {
      rep(j, w) {
        cin >> tile[i][j];
        if (tile[i][j] == '@') {
          baseX = i;
          baseY = j;
        }
      }
      tile[i][w] = '#';
    }
    rep(i, w) tile[h][i] = '#';
    ans = 1;
    seek(baseX, baseY);
    cout << ans << endl;
  }
  return 0;
}

void seek(int x, int y) {
  if (tile[x + 1][y] == '.') {
    tile[x + 1][y] = '#';
    ans++;
    seek(x + 1, y);
  }
  if (tile[x][y + 1] == '.') {
    tile[x][y + 1] = '#';
    ans++;
    seek(x, y + 1);
  }
  if (tile[x - 1][y] == '.') {
    tile[x - 1][y] = '#';
    ans++;
    seek(x - 1, y);
  }
  if (tile[x][y - 1] == '.') {
    tile[x][y - 1] = '#';
    ans++;
    seek(x, y - 1);
  }
}

Fixed submission:
#include <cassert>
#include <iostream>
#define REP(i, l, n) for (int i = l; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define MAX 21
int ans, baseX, baseY;
int w, h;
char tile[MAX][MAX];
void seek(int x, int y);
using namespace std;
int main() {
  while (1) {
    cin >> w;
    cin >> h;
    if (w == 0 && h == 0)
      return 0;
    rep(i, h) {
      rep(j, w) {
        cin >> tile[i][j];
        if (tile[i][j] == '@') {
          baseX = i;
          baseY = j;
        }
      }
      tile[i][w] = '#';
    }
    rep(i, w) tile[h][i] = '#';
    ans = 1;
    seek(baseX, baseY);
    cout << ans << endl;
  }
  return 0;
}

void seek(int x, int y) {
  if (tile[x + 1][y] == '.') {
    tile[x + 1][y] = '#';
    ans++;
    seek(x + 1, y);
  }
  if (tile[x][y + 1] == '.') {
    tile[x][y + 1] = '#';
    ans++;
    seek(x, y + 1);
  }
  if (tile[x - 1][y] == '.') {
    tile[x - 1][y] = '#';
    ans++;
    seek(x - 1, y);
  }
  if (tile[x][y - 1] == '.') {
    tile[x][y - 1] = '#';
    ans++;
    seek(x, y - 1);
  }
}
Problem p00711 (C++)

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

vector<string> f;
void init() { f.clear(); }

int input() {
  int w, h;
  cin >> w >> h;
  if (w == 0 && h == 0)
    return false;
  string s;
  for (int i = 0; i < h; i++) {
    cin >> s;
    f.push_back(s);
  }
  return true;
}

int vx[] = {0, 0, 1, -1};
int vy[] = {1, -1, 0, 0};

int rec(int x, int y) {
  if (f[x][y] != '.')
    return 0;
  f[x][y] = '#';
  int res = 1;
  for (int i = 0; i < 4; i++) {
    res += rec(x + vx[i], y + vy[i]);
  }
  return res;
}

int solve() {
  int x, y;
  for (int i = 0; i < f.size(); i++) {
    for (int j = 0; j < f[i].size(); j++) {
      if (f[i][j] == '@') {
        x = i;
        y = j;
        f[i][j] = '.';
      }
    }
  }
  return rec(x, y);
}

int main() {
  while (init(), input()) {
    cout << solve() << endl;
  }
}

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

vector<string> f;
void init() { f.clear(); }

int input() {
  int w, h;
  cin >> w >> h;
  if (w == 0 && h == 0)
    return false;
  string s;
  for (int i = 0; i < h; i++) {
    cin >> s;
    f.push_back(s);
  }
  return true;
}

int vx[] = {0, 0, 1, -1};
int vy[] = {1, -1, 0, 0};

int rec(int x, int y) {
  if (x < 0 || f.size() <= x)
    return 0;
  if (y < 0 || f[x].size() <= y)
    return 0;
  if (f[x][y] != '.')
    return 0;
  f[x][y] = '#';
  int res = 1;
  for (int i = 0; i < 4; i++) {
    res += rec(x + vx[i], y + vy[i]);
  }
  return res;
}

int solve() {
  int x, y;
  for (int i = 0; i < f.size(); i++) {
    for (int j = 0; j < f[i].size(); j++) {
      if (f[i][j] == '@') {
        x = i;
        y = j;
        f[i][j] = '.';
      }
    }
  }
  return rec(x, y);
}

int main() {
  while (init(), input()) {
    cout << solve() << endl;
  }
}
Problem p00711 (C++)

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

#define rep(i, N) for (int i = 0; i < (int)(N); i++)
#define REP(i, N, k) for (int i = k; i < (int)(N); i++)

#define vi vector<int>
#define pb push_back

#define pii pair<int, int>
#define fi first
#define se second
int main() {

  ifstream in("b_in.txt");
  cin.rdbuf(in.rdbuf());

  int w, h;
  while (cin >> w >> h, w) {
    int sx, sy;
    string s[30];
    rep(i, h) cin >> s[i];
    rep(y, h) rep(x, w) if (s[y][x] == '@') s[y][x] = '.', sx = x, sy = y;

    queue<pii> que;
    que.push(pii(sx, sy));
    bool u[30][30] = {};
    u[sy][sx] = true;
    int cnt = 1;
    while (!que.empty()) {
      int x, y;
      tie(x, y) = que.front();
      que.pop();
      rep(i, 4) {
        const int dx[] = {0, 1, 0, -1, 0};
        int nx = x + dx[i];
        int ny = y + dx[i + 1];
        if (nx < 0 || w <= nx || ny < 0 || h <= ny)
          continue;
        if (s[ny][nx] == '#')
          continue;
        if (u[ny][nx])
          continue;
        u[ny][nx] = true;
        cnt += 1;
        que.push(pii(nx, ny));
      }
    }
    cout << cnt << endl;
  }

  return 0;
}


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

#define rep(i, N) for (int i = 0; i < (int)(N); i++)
#define REP(i, N, k) for (int i = k; i < (int)(N); i++)

#define vi vector<int>
#define pb push_back

#define pii pair<int, int>
#define fi first
#define se second
int main() {

  //    ifstream in("b_in.txt");
  //    cin.rdbuf(in.rdbuf());

  int w, h;
  while (cin >> w >> h, w) {
    int sx, sy;
    string s[30];
    rep(i, h) cin >> s[i];
    rep(y, h) rep(x, w) if (s[y][x] == '@') s[y][x] = '.', sx = x, sy = y;

    queue<pii> que;
    que.push(pii(sx, sy));
    bool u[30][30] = {};
    u[sy][sx] = true;
    int cnt = 1;
    while (!que.empty()) {
      int x, y;
      tie(x, y) = que.front();
      que.pop();
      rep(i, 4) {
        const int dx[] = {0, 1, 0, -1, 0};
        int nx = x + dx[i];
        int ny = y + dx[i + 1];
        if (nx < 0 || w <= nx || ny < 0 || h <= ny)
          continue;
        if (s[ny][nx] == '#')
          continue;
        if (u[ny][nx])
          continue;
        u[ny][nx] = true;
        cnt += 1;
        que.push(pii(nx, ny));
      }
    }
    cout << cnt << endl;
  }

  return 0;
}
Problem p00711 (C++)

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

const int MAX_SIZE = 20;
typedef pair<int, int> P;

int calc(char f[][MAX_SIZE], queue<P> q, int W, int H) {
  int n = 0;
  while (q.size()) {
    n++;
    P p = q.front();
    q.pop();
    int di[] = {-1, 1, 0, 0};
    int dj[] = {0, 0, -1, 1};
    for (int k = 0; k < 4; ++k) {
      int i = p.first + di[k];
      int j = p.second + dj[k];
      if (i >= 0 && j >= 0 && i < H && j < W && f[i][j] == '.') {
        f[i][j] = '#';
        q.push(P(i, j));
      }
    }
  }
  return n;
}
int main() {
  int W, H;
  char f[MAX_SIZE][MAX_SIZE];
  while (true) {
    queue<P> q;
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    for (int i = 0; i < H; ++i) {
      cin >> f[i];
      for (int j = 0; j < W; ++j) {
        if (f[i][j] == '@') {
          q.push(P(i, j));
        }
      }
    }
    cout << calc(f, q, W, H) << endl;
  }
}

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

const int MAX_SIZE = 21;
typedef pair<int, int> P;

int calc(char f[][MAX_SIZE], queue<P> q, int W, int H) {
  int n = 0;
  while (q.size()) {
    n++;
    P p = q.front();
    q.pop();
    int di[] = {-1, 1, 0, 0};
    int dj[] = {0, 0, -1, 1};
    for (int k = 0; k < 4; ++k) {
      int i = p.first + di[k];
      int j = p.second + dj[k];
      if (i >= 0 && j >= 0 && i < H && j < W && f[i][j] == '.') {
        f[i][j] = '#';
        q.push(P(i, j));
      }
    }
  }
  return n;
}
int main() {
  int W, H;
  char f[MAX_SIZE][MAX_SIZE];
  while (true) {
    queue<P> q;
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    for (int i = 0; i < H; ++i) {
      cin >> f[i];
      for (int j = 0; j < W; ++j) {
        if (f[i][j] == '@') {
          q.push(P(i, j));
        }
      }
    }
    cout << calc(f, q, W, H) << endl;
  }
}
Problem p00711 (C++)

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

using namespace std;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int paint(vector<string> &field, int x, int y) {
  int cnt = 0;

  for (int i = 0; i < 4; ++i) {
    int xx = x + dx[i];
    int yy = y + dy[i];

    if (xx < 0 || field[i].size() <= xx || yy < 0 || field.size() <= yy)
      continue;

    if (field[yy][xx] == '.') {
      field[yy][xx] = '#';
      cnt += paint(field, xx, yy) + 1;
    }
  }
  return cnt;
}

int main() {
  while (true) {
    int W, H;
    cin >> W >> H;

    if (!W && !H)
      break;

    int sx, sy;
    vector<string> field(H);
    for (int i = 0; i < H; ++i) {
      cin >> field[i];
      for (int x = 0; x < W; ++x) {
        if (field[i][x] == '@') {
          sx = x;
          sy = i;
        }
      }
    }

    cout << paint(field, sx, sy) + 1 << endl;
  }
  return 0;
}

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

using namespace std;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int paint(vector<string> &field, int x, int y) {
  int cnt = 0;

  for (int i = 0; i < 4; ++i) {
    int xx = x + dx[i];
    int yy = y + dy[i];

    if (xx < 0 || field[0].size() <= xx || yy < 0 || field.size() <= yy)
      continue;

    if (field[yy][xx] == '.') {
      field[yy][xx] = '#';
      cnt += paint(field, xx, yy) + 1;
    }
  }
  return cnt;
}

int main() {
  while (true) {
    int W, H;
    cin >> W >> H;

    if (!W && !H)
      break;

    int sx, sy;
    vector<string> field(H);
    for (int i = 0; i < H; ++i) {
      cin >> field[i];
      for (int x = 0; x < W; ++x) {
        if (field[i][x] == '@') {
          sx = x;
          sy = i;
        }
      }
    }

    cout << paint(field, sx, sy) + 1 << endl;
  }
  return 0;
}
Problem p00711 (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<int, int> P;
typedef pair<ll, P> PP;

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

int sx, sy, h, w;
string fld[20];
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};

int bfs() {
  bool used[h][w];
  REP(i, h) REP(j, w) used[i][j] = false;
  int cnt = 1;
  queue<P> q;
  used[sy][sx] = true;
  q.push(P(sx, sy));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    REP(i, 4) {
      int nx = p.fi + dx[i], ny = p.se + dy[i];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h || used[ny][nx] ||
          fld[ny][nx] == '#')
        continue;
      used[ny][nx] = true;
      cnt++;
      q.push(P(nx, ny));
    }
  }
  return cnt;
}

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

  while (true) {
    cin >> w >> h;
    REP(i, h) {
      cin >> fld[i];
      REP(j, w) if (fld[i][j] == '@') sx = j, sy = i;
    }
    cout << bfs() << 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<int, int> P;
typedef pair<ll, P> PP;

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

int sx, sy, h, w;
string fld[20];
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};

int bfs() {
  bool used[h][w];
  REP(i, h) REP(j, w) used[i][j] = false;
  int cnt = 1;
  queue<P> q;
  used[sy][sx] = true;
  q.push(P(sx, sy));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    REP(i, 4) {
      int nx = p.fi + dx[i], ny = p.se + dy[i];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h || used[ny][nx] ||
          fld[ny][nx] == '#')
        continue;
      used[ny][nx] = true;
      cnt++;
      q.push(P(nx, ny));
    }
  }
  return cnt;
}

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

  while (true) {
    cin >> w >> h;
    if (w == 0)
      break;
    REP(i, h) {
      cin >> fld[i];
      REP(j, w) if (fld[i][j] == '@') sx = j, sy = i;
    }
    cout << bfs() << endl;
  }

  return 0;
}
Problem p00711 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int check(int j) {
  if (j == 2 || j == 4 || j == 3)
    return j;
  else
    return 3;
}
int main() {
  int board[22][22];
  int w, h;
  char t;
  while (1) {
    cin >> w >> h;
    for (int i = 0; i <= 21; i++) {
      for (int j = 0; j <= 21; j++) {
        board[j][i] = 4;
      }
    } // initialization
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> t;
        if (t == '.')
          board[j][i] = 1;
        else if (t == '#')
          board[j][i] = 2;
        else
          board[j][i] = 3;
      }
    } // input end
    int cmem = 2000;
    int c = 0;
    while (cmem - c != 0) {
      cmem = c;
      for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
          if (board[j][i] == 3) {
            board[j - 1][i] = check(board[j - 1][i]);
            board[j + 1][i] = check(board[j + 1][i]);
            board[j][i - 1] = check(board[j][i - 1]);
            board[j][i + 1] = check(board[j][i + 1]);
          }
        }
      }
      c = 0;
      for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
          if (board[j][i] == 3)
            c += 1;
        }
      }
    }

    cout << c << endl;
  } // while loop end
  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int check(int j) {
  if (j == 2 || j == 4 || j == 3)
    return j;
  else
    return 3;
}
int main() {
  int board[22][22];
  int w, h;
  char t;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i <= 21; i++) {
      for (int j = 0; j <= 21; j++) {
        board[j][i] = 4;
      }
    } // initialization
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> t;
        if (t == '.')
          board[j][i] = 1;
        else if (t == '#')
          board[j][i] = 2;
        else
          board[j][i] = 3;
      }
    } // input end
    int cmem = 2000;
    int c = 0;
    while (cmem - c != 0) {
      cmem = c;
      for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
          if (board[j][i] == 3) {
            board[j - 1][i] = check(board[j - 1][i]);
            board[j + 1][i] = check(board[j + 1][i]);
            board[j][i - 1] = check(board[j][i - 1]);
            board[j][i + 1] = check(board[j][i + 1]);
          }
        }
      }
      c = 0;
      for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
          if (board[j][i] == 3)
            c += 1;
        }
      }
    }

    cout << c << endl;
  } // while loop end
  return 0;
}
Problem p00711 (C++)

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

using namespace std;
vector<int> ans;
typedef pair<int, int> P;

int main() {
  int W, H;
  cin >> W >> H;
  int tate[4] = {0, 1, 0, -1};
  int yoko[4] = {1, 0, -1, 0};
  while (W) {
    int count = 0;
    queue<P> zahyou;
    vector<vector<int>> room(H, vector<int>(W));
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        char a;
        cin >> a;
        if (a == '.')
          room[i][j] = 1;
        else if (a == '@') {
          zahyou.push(P(i, j));
          room[i][j] = 1;
        } else
          room[i][j] = 0;
      }
    }
    while (zahyou.size()) {
      pair<int, int> now = zahyou.front();
      zahyou.pop();
      if (room[now.first][now.second] == 1) {
        count++;
        room[now.first][now.second] = 0;
        for (int i = 0; i < 4; i++) {
          zahyou.push(P(now.first + tate[i], now.second + yoko[i]));
        }
      }
    }
    ans.push_back(count);
    cin >> W >> H;
  }
  for (int i = 0; i < ans.size(); i++)
    cout << ans[i] << endl;
}


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

using namespace std;
vector<int> ans;
typedef pair<int, int> P;

int main() {
  int W, H;
  cin >> W >> H;
  int tate[4] = {0, 1, 0, -1};
  int yoko[4] = {1, 0, -1, 0};
  while (W) {
    int count = 0;
    queue<P> zahyou;
    vector<vector<int>> room(H, vector<int>(W));
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        char a;
        cin >> a;
        if (a == '.')
          room[i][j] = 1;
        else if (a == '@') {
          zahyou.push(P(i, j));
          room[i][j] = 1;
        } else
          room[i][j] = 0;
      }
    }
    while (zahyou.size()) {
      pair<int, int> now = zahyou.front();
      zahyou.pop();
      if (room[now.first][now.second] == 1) {
        count++;
        room[now.first][now.second] = 0;
        for (int i = 0; i < 4; i++) {
          if (now.first + tate[i] >= 0 && now.first + tate[i] < H &&
              now.second + yoko[i] >= 0 && now.second + yoko[i] < W &&
              room[now.first + tate[i]][now.second + yoko[i]] == 1)
            zahyou.push(P(now.first + tate[i], now.second + yoko[i]));
        }
      }
    }
    ans.push_back(count);
    cin >> W >> H;
  }
  for (int i = 0; i < ans.size(); i++)
    cout << ans[i] << endl;
}
Problem p00712 (C++)

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

using namespace std;

typedef long double ld;

const ld EPS = 1e-9;

ld p, q;
int a, n;
ld t;

int dfs(int m, ld s, int w, int c) {

  if (abs(s - t) < EPS) {
    return 1;
  }
  if (m == n)
    return 0;

  int ret = 0;
  for (int i = c; i >= 1; i--) {
    if (w * i <= a) {
      ld j = s + (ld(1.0) / ld(i));
      if (j <= t + EPS) {
        ret += dfs(m + 1, j, w * i, i);
      }
    }
  }
  return ret;
}

int main() {

  while (cin >> p >> q >> a >> n && (p || q || a || n)) {
    t = p / q;
    cout << dfs(0, 0, 1, a) << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long double ld;

const ld EPS = 1e-9;

ld p, q;
int a, n;
ld t;

int dfs(int m, ld s, int w, int c) {

  if (abs(s - t) < EPS) {
    return 1;
  }
  if (m == n)
    return 0;

  int ret = 0;
  for (int i = min(a / w, c); i >= 1; i--) {
    if (w * i <= a) {
      ld j = s + (ld(1.0) / ld(i));
      if (j <= t + EPS) {
        ret += dfs(m + 1, j, w * i, i);
      }
    }
  }
  return ret;
}

int main() {

  while (cin >> p >> q >> a >> n && (p || q || a || n)) {
    t = p / q;
    cout << dfs(0, 0, 1, a) << endl;
  }

  return 0;
}
Problem p00712 (C++)

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

using namespace std;

int calc(int p, int q, int mind, int ca, int a, int cn, int n) {
  int s = 0;
  if (cn <= n) {
    mind = max(mind, q / p);
    int maxd = floor((double)(n - cn + 1) * q / p);
    for (int i = mind; i <= maxd && pow(i, floor((double)i * p / q)) * ca <= a;
         i++) {
      if (p * i - q == 0) {
        s++;
      } else if (p * i - q > 0) {
        int _p = p * i - q;
        int _q = q * i;
        for (int j = 2; j <= min(_p, _q); j++) {
          while (_p % j + _q % j == 0) {
            _p /= j;
            _q /= j;
          }
        }
        s += calc(_p, _q, i, ca * i, a, cn + 1, n);
      }
    }
  }
  return s;
}

int main() {
  int p, q, a, n;
  cin >> p >> q >> a >> n;
  while (p) {
    for (int i = 2; i <= min(p, q); i++) {
      while (p % i + q % i == 0) {
        p /= i;
        q /= i;
      }
    }
    cout << calc(p, q, 1, 1, a, 1, n) << endl;
    cin >> p >> q >> a >> n;
  }
}

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

using namespace std;

int calc(int p, int q, int mind, int ca, int a, int cn, int n) {
  int s = 0;
  if (cn <= n) {
    mind = max(mind, q / p);
    int maxd = floor((double)(n - cn + 1) * q / p);
    for (int i = mind; i <= maxd && pow(i, floor((double)i * p / q)) * ca <= a;
         i++) {
      if (p * i - q == 0) {
        s++;
      } else if (p * i - q > 0) {
        int _p = p * i - q;
        int _q = q * i;
        s += calc(_p, _q, i, ca * i, a, cn + 1, n);
      }
    }
  }
  return s;
}

int main() {
  int p, q, a, n;
  cin >> p >> q >> a >> n;
  while (p) {
    for (int i = 2; i <= min(p, q); i++) {
      while (p % i + q % i == 0) {
        p /= i;
        q /= i;
      }
    }
    cout << calc(p, q, 1, 1, a, 1, n) << endl;
    cin >> p >> q >> a >> n;
  }
}
Problem p00712 (C++)

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

using namespace std;

int p, q, a, n, answer;

void search(int pp, int qq, int start, int count) {
  if (count == n)
    return;
  for (int i = start; i <= a; ++i) {
    int ppp = pp * i + qq;
    int qqq = qq * i;
    if (qqq > a)
      return;
    if (!(qqq % q) && !(ppp % p) && qqq / q == ppp / p)
      ++answer;
    else
      search(ppp, qqq, i, count + 1);
  }
  return;
}

int gcd(int a, int b) {
  int c;
  while (b) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}

int main(void) {
  while (cin >> p >> q >> a >> n, p | q | a | n) {
    int gg = gcd(p, q);
    p /= gg;
    q /= gg;
    answer = 0;
    search(0, 1, 1, 0);
    cout << answer << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int p, q, a, n, answer;

void search(int pp, int qq, int start, int count) {
  if (count == n)
    return;
  for (int i = start; i <= a; ++i) {
    int ppp = pp * i + qq;
    int qqq = qq * i;
    if (qqq > a)
      return;
    if (ppp / qqq > p / q)
      continue;
    if (!(qqq % q) && !(ppp % p) && qqq / q == ppp / p)
      ++answer;
    else
      search(ppp, qqq, i, count + 1);
  }
  return;
}

int gcd(int a, int b) {
  int c;
  while (b) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}

int main(void) {
  while (cin >> p >> q >> a >> n, p | q | a | n) {
    int gg = gcd(p, q);
    p /= gg;
    q /= gg;
    answer = 0;
    search(0, 1, 1, 0);
    cout << answer << endl;
  }
  return 0;
}
Problem p00712 (C++)

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

using namespace std;

int p, q, a, n;
double EPS = 1e-9;

int solve(double sum, int mul, int prev, int num) {
  if (num > n)
    return 0;
  if (mul > a)
    return 0;

  if (fabs(sum - (double)p / (double)q) < EPS)
    return 1;
  if (sum > (double)p / (double)q)
    return 0;

  int ret = 0;
  for (int i = prev; i <= 800; i++) {
    ret += solve(sum + 1.0 / (double)i, mul * i, i, num + 1);
  }
  return ret;
}

int main() {
  while (1) {
    cin >> p >> q >> a >> n;
    if (p == 0 && q == 0 && a == 0 && n == 0)
      break;

    cout << solve(0.0, 1, 1, 0) << endl;
  }

  return 0;
}

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

using namespace std;

int p, q, a, n;
double EPS = 1e-9;

int solve(double sum, int mul, int prev, int num) {
  if (num > n)
    return 0;
  if (mul > a)
    return 0;

  if (fabs(sum - (double)p / (double)q) < EPS)
    return 1;
  if (sum > (double)p / (double)q)
    return 0;

  int ret = 0;
  for (int i = prev; mul * i <= a; i++) {
    ret += solve(sum + 1.0 / (double)i, mul * i, i, num + 1);
  }
  return ret;
}

int main() {
  while (1) {
    cin >> p >> q >> a >> n;
    if (p == 0 && q == 0 && a == 0 && n == 0)
      break;

    cout << solve(0.0, 1, 1, 0) << endl;
  }

  return 0;
}
Problem p00712 (C++)

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

int dfs(int p, int q, int a, int n, int pre) {
  if (p == 0)
    return 1;
  if (n == 0)
    return 0;
  int res = 0;
  for (int i = pre; i <= 12000; i++) {
    if ((a / i) == 0)
      break;
    res += dfs(i * p - q, i * q, a / i, n - 1, i);
  }
  return res;
}

int main() {
  int p, q, a, n;
  while (scanf("%d %d %d %d", &p, &q, &a, &n), p) {
    printf("%d\n", dfs(p, q, a, n, 1));
  }
}

Fixed submission:
#include <stdio.h>

int dfs(int p, int q, int a, int n, int pre) {
  if (p == 0)
    return 1;
  if (n == 0)
    return 0;
  int res = 0;
  for (int i = pre; i <= 6000; i++) {
    if ((a / i) == 0)
      break;
    res += dfs(i * p - q, i * q, a / i, n - 1, i);
  }
  return res;
}

int main() {
  int p, q, a, n;
  while (scanf("%d %d %d %d", &p, &q, &a, &n), p) {
    printf("%d\n", dfs(p, q, a, n, 1));
  }
}
Problem p00712 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
typedef long long ll;
ll p, q, a, n, an;

ll gcd(ll a, ll b) {
  if (b)
    return gcd(b, a % b);
  return a;
}

void dfs(ll d, ll s, ll t, ll k, ll j) {
  if (d > n || s * q > p * k)
    return;
  if (s * q == p * k) {
    an++;
    return;
  }
  for (int i = j; t * i <= a; i++) {
    ll cd = gcd(k, i);
    dfs(d + 1, s * i / cd + k / cd, t * i, k * i / cd, i);
  }
}

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

Fixed submission:
#include <iostream>
using namespace std;
typedef long long ll;
ll p, q, a, n, an;

ll gcd(ll a, ll b) {
  if (b)
    return gcd(b, a % b);
  return a;
}

void dfs(ll d, ll s, ll t, ll k, ll j) {
  if (d > n || s * q > p * k)
    return;
  if (s * q == p * k) {
    an++;
    return;
  }
  for (int i = j; t * i <= a; i++) {
    // ll cd=gcd(k,i);
    dfs(d + 1, s * i + k, t * i, k * i, i);
  }
}

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define ISEQ(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> P;

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

int calc(int p, int q, int a, int n, int d, int num, int prod) {
  if (p == 0)
    return 1;
  if (num == n)
    return 0;
  int res = 0;
  int g = gcd(p, q);
  p /= g;
  q /= g;
  for (int i = d; prod * i <= a; i++) {
    if (p * i - q < 0)
      continue;
    res += calc(p * i - q, i * q, a, n, i, num + 1, prod * i);
  }
  return res;
}

int main() {
  while (true) {
    int p, q, a, n;
    scanf("%d%d%d%d", &p, &q, &a, &n);
    if (p == 0 and q == 0 and a == 0 and n == 0)
      break;

    printf("%d\n", calc(p, q, a, n, 1, 0, 1));
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define ISEQ(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> P;

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

int calc(int p, int q, int a, int n, int d, int num, int prod) {
  if (p == 0)
    return 1;
  if (num == n)
    return 0;
  int res = 0;
  int g = gcd(p, q);
  p /= g;
  q /= g;
  for (int i = d; prod * i <= a; i++) {
    if (p * i - q < 0)
      continue;
    if (p * i - q * (n - num) > 0)
      break;
    res += calc(p * i - q, i * q, a, n, i, num + 1, prod * i);
  }
  return res;
}

int main() {
  while (true) {
    int p, q, a, n;
    scanf("%d%d%d%d", &p, &q, &a, &n);
    if (p == 0 and q == 0 and a == 0 and n == 0)
      break;

    printf("%d\n", calc(p, q, a, n, 1, 0, 1));
  }
}
Problem p00712 (C++)

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

using namespace std;

int p, q, n, a;

int ans;
int rec(int sum_u, int sum_l, int nn, int k) {
  if (sum_l > a || nn == 0)
    return -1;

  if (sum_u * q == sum_l * p) {
    ans++;
    return 0;
  }

  nn--;
  for (int x = k;; x++) {
    if (rec(sum_u * x + sum_l, sum_l * x, nn, x) < 0)
      break;
  }
  return 0;
}

int main() {
  while (cin >> p >> q >> a >> n, p) {
    ans = 0;
    rec(0, 1, n + 1, 1);
    cout << ans << endl;
  }
  return 0;
}

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

using namespace std;

int p, q, n, a;

int ans;
int rec(int sum_u, int sum_l, int nn, int k) {
  if (sum_l > a || nn == 0)
    return -1;

  if (sum_u * q == sum_l * p) {
    ans++;
    return 0;
  }
  if (p / double(q) < sum_u / double(sum_l))
    return 0;

  nn--;
  for (int x = k;; x++) {
    if (rec(sum_u * x + sum_l, sum_l * x, nn, x) < 0)
      break;
  }
  return 0;
}

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

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

using namespace std;
int ans = 0;
int p, q, a, n;
void dfs(int num, int seki, int deno, int nume, int value) {
  if (nume > 0) {
    int g = __gcd(deno, nume);
    deno /= g;
    nume /= g;
  }
  if (p * deno == q * nume) {
    ans++;
  }
  if (p * deno <= q * nume) {
    return;
  }
  if (num == n)
    return;
  for (int i = value; i * seki <= a; i++) {
    dfs(num + 1, seki * i, deno * i, nume * i + deno, i);
  }
}
int main() {

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

  return 0;
}

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

using namespace std;
int ans = 0;
int p, q, a, n;
void dfs(int num, int seki, int deno, int nume, int value) {
  if (nume > 0) {
    int g = __gcd(deno, nume);
    deno /= g;
    nume /= g;
  }
  if ((nume / (double)deno) + (double)(n - num) / (double)value <
      (double)p / (double)q - 1e-7)
    return;
  if (p * deno == q * nume) {
    ans++;
  }
  if (p * deno <= q * nume) {
    return;
  }
  if (num == n)
    return;
  for (int i = value; i * seki <= a; i++) {
    dfs(num + 1, seki * i, deno * i, nume * i + deno, i);
  }
}
int main() {

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

  return 0;
}
Problem p00712 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <cxxabi.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
#define rep(i, n) range(i, 0, n)
#define range(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define LINF ((ll)1ll < 60)
#define INF ((int)1 << 30)
#define EPS (1e-9)
#define MOD (1000000007)
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "[" << p.first << ", " << p.second << "]";
  return os;
};
template <class S> auto &operator<<(ostream &os, vector<S> t) {
  bool b = 1;
  for (auto s : t)
    os << (exchange(b, 0) ||
                   strlen(abi::__cxa_demangle(typeid(S).name(), 0, 0, 0)) > 20
               ? ""
               : " ")
       << s;
  return os << endl;
}

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

int ma;

int solve(int p, int q, int r, int a, int n) {
  int ans = 0;
  if (p == 0)
    return 1;
  if (n == 0)
    return 0;
  for (int nq = r; a * nq <= ma; nq++) {
    if (p * nq > n * q)
      break;
    // if(p*nq>=q){
    int P = p * nq - q;
    int Q = q * nq;
    ans += solve(P / gcd(P, Q), Q / gcd(P, Q), nq, a * nq, n - 1);
    //}
  }
  return ans;
}

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

  int p, q, n;
  while (cin >> p >> q >> ma >> n, p) {
    cout << solve(p, q, 1, 1, n) << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <cxxabi.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
#define rep(i, n) range(i, 0, n)
#define range(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define LINF ((ll)1ll < 60)
#define INF ((int)1 << 30)
#define EPS (1e-9)
#define MOD (1000000007)
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "[" << p.first << ", " << p.second << "]";
  return os;
};
template <class S> auto &operator<<(ostream &os, vector<S> t) {
  bool b = 1;
  for (auto s : t)
    os << (exchange(b, 0) ||
                   strlen(abi::__cxa_demangle(typeid(S).name(), 0, 0, 0)) > 20
               ? ""
               : " ")
       << s;
  return os << endl;
}

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

int ma;

int solve(int p, int q, int r, int a, int n) {
  int ans = 0;
  if (p == 0)
    return 1;
  if (n == 0)
    return 0;
  for (int nq = r; a * nq <= ma; nq++) {
    if (p * nq > n * q)
      break;
    if (p * nq >= q) {
      int P = p * nq - q;
      int Q = q * nq;
      ans += solve(P / gcd(P, Q), Q / gcd(P, Q), nq, a * nq, n - 1);
    }
  }
  return ans;
}

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

  int p, q, n;
  while (cin >> p >> q >> ma >> n, p) {
    cout << solve(p, q, 1, 1, n) << endl;
  }
  return 0;
}