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

#include <algorithm>
#include <bitset>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <type_traits>
#include <vector>
using namespace std;
#define ll long long int
#define all(v) begin(v), end(v)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
// typedef vector<int> V;
// typedef vector<VV> VVV;

template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

int euclid(int a, int b) {
  int temp;

  if (a < b) {
    temp = a;
    a = b;
    b = temp;
  }
  if (b < 1)
    return -1;

  if (a % b == 0)
    return b;
  return euclid(b, a % b);
}

int P, Q, N, A, cot = 0;

void dfs(int p, int q, int a, int n, int mini) {
  for (int i = mini; i * a <= A; i++) {
    if (n == N)
      break;
    if (((i * p - q) == 0)) {
      // cout << "N:" << n+1 << " mini:" << mini << " e:" << (i * p - q) <<
      // endl;
      cot++;
      continue;
    }

    if (i * p - q > 0)
      dfs((i * p - q), i * q, i * a, n + 1, i);
    // cout << "N:" << n+1 << " mini:" << mini << " i:" << i <<" e:"<< (i * p -
    // q) / e <<" "<< i * q / e<< endl;
  }
}

int main() {
  cin >> P >> Q >> A >> N;
  while (P != 0) {
    cot = 0;
    dfs(P, Q, 1, 0, 1);
    cout << cot << endl;
    cin >> P >> Q >> A >> N;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <type_traits>
#include <vector>
using namespace std;
#define ll long long int
#define all(v) begin(v), end(v)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
// typedef vector<int> V;
// typedef vector<VV> VVV;

template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

int euclid(int a, int b) {
  int temp;

  if (a < b) {
    temp = a;
    a = b;
    b = temp;
  }
  if (b < 1)
    return -1;

  if (a % b == 0)
    return b;
  return euclid(b, a % b);
}

int P, Q, N, A, cot = 0;

void dfs(int p, int q, int a, int n, int mini) {
  for (int i = mini; i * a <= A; i++) {
    if (n == N)
      break;
    if (((i * p - q) == 0)) {
      // cout << "N:" << n+1 << " mini:" << mini << " e:" << (i * p - q) <<
      // endl;
      cot++;
      continue;
    }

    int e = euclid(i * p - q, i * q);
    if (i * q / e > 0)
      dfs((i * p - q) / e, i * q / e, i * a, n + 1, i);
    // cout << "N:" << n+1 << " mini:" << mini << " i:" << i <<" e:"<< (i * p -
    // q) / e <<" "<< i * q / e<< endl;
  }
}

int main() {
  cin >> P >> Q >> A >> N;
  while (P != 0) {
    cot = 0;
    dfs(P, Q, 1, 0, 1);
    cout << cot << endl;
    cin >> P >> Q >> A >> N;
  }
}