Problem p03297 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define llu unsigned long long
#define ld double
#define llu unsigned long long
#define rep(i, x, y) for (ll i = x; i < y; ++i)
#define For(i, x, y) for (ll i = x; i <= y; ++i)
#define FOr(i, x, y) for (ll i = x; i >= y; --i)
#define pi acos(-1)
#define mk make_pair<ll, ll>
#define pa pair<ll, ll>
#define lf else if
#define min(x, y) ((x) < (y) ? (x) : (y))
#define sqr(x) ((x) * (x))
#define abs(x) ((x) > 0 ? (x) : -(x))
#define Mul(x, y) (x = (x) * (y) % mod)
#define Mul1(x, y) (x = (x) * (y) % mod1)
#define Add(x, y) (x = (x + (y)) % mod)
#define Add1(x, y) (x = (x + (y)) % mod1)
#define E(x) return writeln(x), 0
#define p(x) printf("~%d~\n", x)
#define pp(x, y) printf("~~%d %d~~\n", x, y)
#define ppp(x, y, z) printf("~~~%d %d %d~~~\n", x, y, z)
#define pppp(x, y, z, a) printf("~~~%d %d %d %d~~~\n", x, y, z, a)
#define f_in(x) freopen(x ".in", "r", stdin)
#define f_out(x) freopen(x ".out", "w", stdout)
#define open(x) f_in(x), f_out(x)
#define fi first
#define se second
typedef complex<double> E;
namespace SHENZHEBEI {
#ifdef LOCAL
struct _ {
  _() { freopen("cf.in", "r", stdin); }
} _;
#endif
#define NEG 1
inline char gc() { return getchar(); }
#if NEG
inline ll read() {
  ll x = 0, f = 1;
  char ch = gc();
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = gc())
    x = x * 10 - 48 + ch;
  return x * f;
}
inline void write(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
#else
inline ll read() {
  ll x = 0;
  char ch = gc();
  for (; !isdigit(ch); ch = gc())
    ;
  for (; isdigit(ch); ch = gc())
    x = x * 10 - 48 + ch;
  return x;
}
inline void write(ll x) {
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
#endif
inline char readchar() {
  char ch = gc();
  for (; isspace(ch); ch = gc())
    ;
  return ch;
}
inline ll readstr(char *s) {
  char ch = gc();
  int cur = 0;
  for (; isspace(ch); ch = gc())
    ;
  for (; !isspace(ch); ch = gc())
    s[cur++] = ch;
  s[cur] = '\0';
  return cur;
}
inline void writeln(ll x) {
  write(x);
  puts("");
}
inline ld getreal() {
  static ld lbc;
  scanf("%lf", &lbc);
  return lbc;
}
} // namespace SHENZHEBEI
using namespace SHENZHEBEI;
const ll N = 1000;
ll A, B, C, D;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
void work() {
  A = read(), B = read(), C = read(), D = read();
  if ((B > D) || (A < B))
    return puts("No"), void(0);
  ll d = gcd(B, D);
  //	writeln(C-(d-A%d)%d);
  //	A=C-(d-A%d)%d+gcd(B,D);
  if (A >= C)
    A -= (A - C) / d * d;
  for (; A > C; A -= d)
    ;
  for (; A <= C; A += d)
    ;
  if (A < B)
    puts("No");
  else
    puts("Yes");
}
int main() {
  for (ll T = read(); T--;)
    work();
}
/*
1

<=C
的%10=1的最大的数
%10	=	1
%10	=	1
%10	=	1
for(;A>C;A-=gcd(B,D));

5-
10-
d=3
A=100 (3-1)
C=99
min()
刚开始可以构造出的最小值<=C+gcd(B,D)-B
*/

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define llu unsigned long long
#define ld double
#define llu unsigned long long
#define rep(i, x, y) for (ll i = x; i < y; ++i)
#define For(i, x, y) for (ll i = x; i <= y; ++i)
#define FOr(i, x, y) for (ll i = x; i >= y; --i)
#define pi acos(-1)
#define mk make_pair<ll, ll>
#define pa pair<ll, ll>
#define lf else if
#define min(x, y) ((x) < (y) ? (x) : (y))
#define sqr(x) ((x) * (x))
#define abs(x) ((x) > 0 ? (x) : -(x))
#define Mul(x, y) (x = (x) * (y) % mod)
#define Mul1(x, y) (x = (x) * (y) % mod1)
#define Add(x, y) (x = (x + (y)) % mod)
#define Add1(x, y) (x = (x + (y)) % mod1)
#define E(x) return writeln(x), 0
#define p(x) printf("~%d~\n", x)
#define pp(x, y) printf("~~%d %d~~\n", x, y)
#define ppp(x, y, z) printf("~~~%d %d %d~~~\n", x, y, z)
#define pppp(x, y, z, a) printf("~~~%d %d %d %d~~~\n", x, y, z, a)
#define f_in(x) freopen(x ".in", "r", stdin)
#define f_out(x) freopen(x ".out", "w", stdout)
#define open(x) f_in(x), f_out(x)
#define fi first
#define se second
typedef complex<double> E;
namespace SHENZHEBEI {
#ifdef LOCAL
struct _ {
  _() { freopen("cf.in", "r", stdin); }
} _;
#endif
#define NEG 1
inline char gc() { return getchar(); }
#if NEG
inline ll read() {
  ll x = 0, f = 1;
  char ch = gc();
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = gc())
    x = x * 10 - 48 + ch;
  return x * f;
}
inline void write(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
#else
inline ll read() {
  ll x = 0;
  char ch = gc();
  for (; !isdigit(ch); ch = gc())
    ;
  for (; isdigit(ch); ch = gc())
    x = x * 10 - 48 + ch;
  return x;
}
inline void write(ll x) {
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
#endif
inline char readchar() {
  char ch = gc();
  for (; isspace(ch); ch = gc())
    ;
  return ch;
}
inline ll readstr(char *s) {
  char ch = gc();
  int cur = 0;
  for (; isspace(ch); ch = gc())
    ;
  for (; !isspace(ch); ch = gc())
    s[cur++] = ch;
  s[cur] = '\0';
  return cur;
}
inline void writeln(ll x) {
  write(x);
  puts("");
}
inline ld getreal() {
  static ld lbc;
  scanf("%lf", &lbc);
  return lbc;
}
} // namespace SHENZHEBEI
using namespace SHENZHEBEI;
const ll N = 1000;
ll A, B, C, D;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
void work() {
  A = read(), B = read(), C = read(), D = read();
  if ((B > D) || (A < B))
    return puts("No"), void(0);
  ll d = gcd(B, D);
  //	writeln(C-(d-A%d)%d);
  //	A=C-(d-A%d)%d+gcd(B,D);
  if (A >= C)
    A -= (A - C) / d * d;
  if (A < C)
    A += (C - A) / d * d;
  for (; A > C; A -= d)
    ;
  for (; A <= C; A += d)
    ;
  if (A < B)
    puts("No");
  else
    puts("Yes");
}
int main() {
  for (ll T = read(); T--;)
    work();
}
/*
1

<=C
的%10=1的最大的数
%10	=	1
%10	=	1
%10	=	1
for(;A>C;A-=gcd(B,D));

5-
10-
d=3
A=100 (3-1)
C=99
min()
刚开始可以构造出的最小值<=C+gcd(B,D)-B
*/