Problem p00712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int a[] = {
    10, 2,  2,  4,  7,  18, 3,  7,  5,  25, 3,  19, 13, 30, 13, 2,  1,  1,  2,
    3,  44, 22, 14, 4,  13, 3,  1,  3,  2,  15, 23, 2,  2,  5,  3,  4,  3,  15,
    2,  1,  5,  6,  1,  1,  12, 6,  17, 1,  4,  5,  3,  6,  2,  3,  13, 4,  1,
    19, 3,  1,  5,  15, 10, 1,  16, 23, 2,  14, 1,  18, 9,  21, 1,  15, 2,  13,
    1,  1,  18, 3,  3,  17, 3,  18, 4,  8,  1,  24, 7,  3,  2,  22, 10, 5,  4,
    6,  5,  18, 1,  1,  3,  3,  16, 1,  2,  3,  15, 2,  5,  3,  13, 14, 4,  6,
    5,  5,  4,  41, 3,  11, 15, 2,  2,  2,  1,  9,  3,  17, 2,  2,  0,  8,  1,
    7,  10, 3,  3,  1,  3,  2,  6,  4,  32, 12, 10, 11, 4,  15, 8,  23, 11, 1,
    2,  1,  23, 2,  17, 1,  2,  7,  2,  1,  11, 6,  1,  7,  18, 4,  18, 4,  5,
    7,  1,  3,  20, 24, 13, 5,  2,  31, 4,  3,  22, 1,  8,  1,  1,  1,  3,  16,
    4,  2,  3,  6,  1,  9,  9,  13, 3,  7,  13, 1,  0,  1,  4,  16, 13, 26, 1,
    2,  15, 1,  4,  1,  24, 3,  1,  3,  5,  0,  2,  10, 13, 2,  3,  1,  8,  1,
    6,  9,  2,  1,  23, 18, 4,  3,  3,  14, 3,  4,  1,  1,  1,  3,  3,  8,  7,
    2,  1,  6,  18, 1,  21, 2,  5,  1,  8,  2,  21, 13, 1,  2,  11, 6,  7,  24,
    1,  1,  18, 1,  2,  2,  1,  6,  1,  9,  3,  7,  13, 34, 6,  2,  3,  18, 6,
    19, 3,  13, 4,  12, 3,  3,  17, 3,  1,  4,  18, 5,  6,  1,  5,  10, 22, 7,
    11, 2,  1,  1,  3,  25, 6,  1,  2,  2,  0,  23, 10, 9,  2,  1,  3,  2,  29,
    4,  4,  1,  10, 2,  4,  2,  1,  19, 18, 31, 0,  17, 29, 3,  31, 6,  1,  23,
    1,  21, 17, 1,  1,  20, 1,  6,  3,  4,  13, 6,  13, 5,  5,  16, 5,  9,  13,
    3,  1,  17, 1,  1,  15, 9,  3,  1,  6,  4,  7,  4,  1,  6,  1,  2,  2,  18,
    22, 1,  9,  1,  1,  1,  6,  4,  26, 4,  1,  23, 20, 7,  6,  1,  39, 1,  12,
    3,  1,  3,  5,  3,  4,  2,  16, 2,  2,  1,  4,  3,  19, 1,  6,  6,  2,  9,
    3,  12, 1,  10, 1,  17, 8,  1,  9,  2,  45, 6,  2,  17, 31, 1,  3,  11, 2,
    24, 1,  0,  0,  3,  8,  5,  2,  2,  2,  2,  8,  4,  19, 3,  3,  37, 6,  7,
    2,  21, 3,  3,  3,  9,  3,  13, 10, 2,  7,  7,  1,  1,  19, 12, 13, 3,  1,
    40, 1,  9,  10, 0,  1,  15, 1,  42, 3,  5,  3,  8,  1,  4,  2,  3,  3,  3,
    3,  18, 19, 3,  7,  1,  1,  19, 20, 4,  1,  27, 1,  12, 1,  1,  5,  2,  4,
    2,  1,  32, 1,  13, 5,  4,  3,  0,  4,  9,  4,  3,  23, 1,  13, 2,  3,  23,
    1,  18, 11, 4,  6,  13, 3,  13, 6,  2,  2,  4,  5,  41, 32, 3,  6,  4,  6,
    9,  6,  3,  1,  1,  1,  4,  24, 3,  1,  3,  10, 32, 1,  3,  7,  13, 19, 1,
    31, 2,  36, 2,  3,  6,  6,  20, 1,  1,  0,  25, 13, 24, 12, 10, 14, 5,  0,
    1,  8,  10, 25, 12, 32, 1,  41, 5,  8,  24, 8,  1,  16, 1,  20, 45, 1,  6,
    3,  1,  24, 27, 1,  16, 2,  1,  7,  38, 2,  5,  19, 15, 9,  3,  1,  3,  4,
    27, 3,  2,  3,  0,  1,  6,  13, 7,  10, 18, 2,  2,  1,  1,  2,  30, 3,  8,
    27, 3,  16, 4,  2,  14, 13, 26, 18, 13, 2,  3,  0,  11, 1,  9,  3,  14, 9,
    12, 1,  1,  22, 6,  12, 10, 9,  7,  9,  0,  1,  1,  4,  7,  2,  1,  5,  19,
    1,  12, 19, 4,  6,  3,  2,  10, 4,  7,  8,  2,  8,  1,  17, 0,  10, 4,  4,
    7,  1,  3,  3,  16, 13, 2,  1,  3,  10, 15, 5,  1,  17, 5,  11, 7,  3,  3,
    2,  2,  10, 22, 3,  1,  5,  2,  9,  4,  3,  2,  1,  10, 3,  24, 3,  7,  3,
    2,  6,  13, 3,  2,  22, 1,  32, 2,  4,  6,  2,  16, 4,  13, 10, 3,  21, 1,
    2,  11, 3,  3,  20, 3,  3,  1,  5,  15, 1,  18, 21, 1,  0,  24, 13, 5,  14,
    1,  13, 1,  6,  2,  2,  11, 5,  1,  4,  3,  9,  5,  5,  1,  19, 17, 5,  5,
    8,  15};

int main(void) {
  for (int i = 0; i < 800; i++) {
    cout << a[i] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
#define EQ(a, b) ((a)-EPS < (b) && (b) < (a) + EPS)

double pq;
int cnt, p, q, a, n;
inline void dfs(double sum, long long product, int prev, int ix) {
  if (product > a)
    return;
  if (EQ(sum, pq)) {
    cnt++;
    return;
  } else if (sum > pq)
    return;
  if (ix == n)
    return;

  for (int i = prev; i >= 1; i--) {
    dfs(sum + (1.0 / i), product * i, i, ix + 1);
  }
  return;
}

bool solve() {
  cin >> p >> q >> a >> n;
  cin.ignore();
  if (p + q + a + n == 0)
    return false;
  pq = 1. * p / q;
  cnt = 0;
  dfs(0.0, 1, a, 0);
  cout << cnt << endl;
  return true;
}

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