Problem p03297 (C++)

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

using namespace std;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define fst first
#define snd second
#define SZ(u) ((int)(u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline T read() {
  register T sum = 0, fg = 1;
  register char ch(getchar());
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fg = -1;
  for (; isdigit(ch); ch = getchar())
    sum = (sum << 3) + (sum << 1) - '0' + ch;
  return sum * fg;
}

typedef long long LL;
typedef pair<int, int> pii;

LL A, B, C, D;

inline void input() {
  A = read<LL>(), B = read<LL>(), C = read<LL>(), D = read<LL>();
}

inline void solve() {
  if (B > D) {
    puts("No");
    return;
  }
  LL A0 = A - max(A - C, 0ll) / B * B;
  if (A0 == C)
    A0 += D;
  if (A0 < B) {
    puts("No");
    return;
  }
  A0 %= B;
  //	printf("A0: %lld\n", A0);
  LL delta = __gcd(D, B), mx = (A0 + (B - A0) / delta * delta) % B;
  if (mx == 0)
    mx = (B - delta) % B;
  //	printf("max: %lld\n", mx);
  if (mx > C) {
    puts("No");
    return;
  }
  puts("Yes");
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("B.in", "r", stdin);
  freopen("B.out", "w", stdout);
#endif

  int T = read<int>();
  while (T--) {
    input();
    solve();
  }

  return 0;
}


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

using namespace std;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define fst first
#define snd second
#define SZ(u) ((int)(u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline T read() {
  register T sum = 0, fg = 1;
  register char ch(getchar());
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fg = -1;
  for (; isdigit(ch); ch = getchar())
    sum = (sum << 3) + (sum << 1) - '0' + ch;
  return sum * fg;
}

typedef long long LL;
typedef pair<int, int> pii;

LL A, B, C, D;

inline void input() {
  A = read<LL>(), B = read<LL>(), C = read<LL>(), D = read<LL>();
}

inline void solve() {
  if (B > D) {
    puts("No");
    return;
  }
  LL A0 = A - max(A - C, 0ll) / B * B;
  if (A0 == C)
    A0 += D;
  if (A0 < B) {
    puts("No");
    return;
  }
  A0 %= B;
  //	printf("A0: %lld\n", A0);
  LL delta = __gcd(D, B), mx = (A0 + (B - A0) / delta * delta) % B;
  if (mx == 0)
    mx = (B - delta) % B;
  //	printf("max: %lld\n", mx);
  if (mx > C) {
    puts("No");
    return;
  }
  puts("Yes");
}

int main() {
  // #ifndef ONLINE_JUDGE
  //	freopen("B.in", "r", stdin);
  //	freopen("B.out", "w", stdout);
  // #endif

  int T = read<int>();
  while (T--) {
    input();
    solve();
  }

  return 0;
}