Problem p03297 (C++)

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

using namespace std;

#ifdef DEBUG_MODE
#define DBG(n) n;
#else
#define DBG(n) ;
#endif
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \
  {                                                                            \
    for (int W = 0; W < (n); W++)                                              \
      cerr << v[W] << ' ';                                                     \
    cerr << endl << endl;                                                      \
  }
#define SHOW2d(v, i, j)                                                        \
  {                                                                            \
    for (int aaa = 0; aaa < i; aaa++) {                                        \
      for (int bbb = 0; bbb < j; bbb++)                                        \
        cerr << v[aaa][bbb] << ' ';                                            \
      cerr << endl;                                                            \
    }                                                                          \
    cerr << endl;                                                              \
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define MOD 1000000007

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

int main() {

  int n;
  cin >> n;

  REP(i, n) {
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    a -= b;
    if (a < 0 || d < b) {
      DBG(cout << 1 << endl;)
      cout << "No" << endl;
      continue;
    }
    if (b <= c) {
      DBG(cout << 2 << endl;)
      cout << "Yes" << endl;
      continue;
    }
    ll tmp1 = ((a - c) / b);
    a -= tmp1 * b;
    if (a > c)
      a -= b;
    if (a < 0) {
      DBG(cout << 3 << endl;)
      cout << "No" << endl;
      continue;
    }

    if (d % b == 0) {
      DBG(cout << 4 << endl;)
      cout << "Yes" << endl;
      continue;
    }

    set<ll> ans;
    ans.insert(a);

    while (1) {
      a += d % b;
      if (a > c && a < b) {
        cout << "No" << endl;
        break;
      }
      if (a >= b)
        a -= b;
      if (ans.count(a)) {
        cout << "Yes" << endl;
        break;
      }
      ans.insert(a);
    }
  }

  return 0;
}


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

using namespace std;

#ifdef DEBUG_MODE
#define DBG(n) n;
#else
#define DBG(n) ;
#endif
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \
  {                                                                            \
    for (int W = 0; W < (n); W++)                                              \
      cerr << v[W] << ' ';                                                     \
    cerr << endl << endl;                                                      \
  }
#define SHOW2d(v, i, j)                                                        \
  {                                                                            \
    for (int aaa = 0; aaa < i; aaa++) {                                        \
      for (int bbb = 0; bbb < j; bbb++)                                        \
        cerr << v[aaa][bbb] << ' ';                                            \
      cerr << endl;                                                            \
    }                                                                          \
    cerr << endl;                                                              \
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define MOD 1000000007

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

int main() {

  int n;
  cin >> n;

  REP(i, n) {
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    a -= b;
    if (a < 0 || d < b) {
      DBG(cout << 1 << endl;)
      cout << "No" << endl;
      continue;
    }
    if (b <= c) {
      DBG(cout << 2 << endl;)
      cout << "Yes" << endl;
      continue;
    }
    ll tmp1 = ((a - c) / b);
    a -= tmp1 * b;
    if (a > c)
      a -= b;
    if (a < 0) {
      DBG(cout << 3 << endl;)
      cout << "No" << endl;
      continue;
    }

    if (d % b == 0) {
      DBG(cout << 4 << endl;)
      cout << "Yes" << endl;
      continue;
    }

    set<ll> ans;
    ans.insert(a);

    while (1) {
      ll seica = max((c - a) / (d % b) - 1, 1LL);
      a += seica * (d % b);
      if (a > c && a < b) {
        cout << "No" << endl;
        break;
      }
      if (a >= b)
        a -= b;
      if (ans.count(a)) {
        cout << "Yes" << endl;
        break;
      }
      ans.insert(a);
    }
  }

  return 0;
}