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

#include <cstdio>
#include <iostream>
#include <vector>
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
inline char gc() {
  //	return getchar();
  static char buf[100000], *l = buf, *r = buf;
  return l == r && (r = (l = buf) + fread(buf, 1, 100000, stdin), l == r)
             ? EOF
             : *l++;
}
template <class T> void rd(T &x) {
  scanf("%d", &x);
  return;
  x = 0;
  int f = 1, ch = gc();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = gc();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 - '0' + ch;
    ch = gc();
  }
  x *= f;
}
template <class T> inline bool Cmax(T &x, T y) { return x < y ? x = y, 1 : 0; }
typedef long long ll;
int T, A, X, B, Y, C, D;
vector<int> pos, cnt;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int exgcd(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  int d = exgcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}
inline int inver(int a, int mod) {
  int x, y, d = exgcd(a, mod, x, y);
  if (x < 0)
    x += mod;
  return x;
}
inline ll lowdiv(ll a, ll b) { return a / b - (a % b && (a ^ b) < 0); }
void init() {
  int d = gcd(A, B);
  A /= d, B /= d, C /= gcd(d, C);
  for (int _ = 0; _ < 2; ++_) {
    d = gcd(A, C);
    C /= d, A /= d;
    int t = gcd(B, d);
    B /= t, Y /= d / t;
    swap(A, B), swap(X, Y);
  }
}
void getpos() {
  pos.clear(), cnt.clear();
  int W = C, H = D, now = 0, r = inver(D, C);
  while (W) {
    int d = W / H;
    pos.push_back((ll)now * r % C), cnt.push_back(d);
    now += d * H;
    W %= H;
    if (W == 0)
      break;
    H %= W;
    if (H == 0)
      H = W;
  }
  pos.push_back(C);
}
inline int gety(int x) { return x == 0 ? C : (C - (ll)x * D % C) % C; }
int main() {
  //	freopen("1.in","r",stdin);
  //	freopen("1.out","w",stdout);
  rd(T);
  for (int kase = 1; kase <= T; ++kase) {
    rd(A), rd(X), rd(B), rd(Y), rd(C);
    init();
    if (C == 1) {
      printf("%d\\n", X + Y);
      continue;
    }
    D = (ll)A * inver(B, C) % C;
    getpos();
    int an = 0;
    for (int i = 0; i < cnt.size(); ++i) {
      int xl = pos[i], xr = pos[i + 1], yl = gety(xl), yr = gety(xr);
      int dx = (xr - xl) / cnt[i], dy = (yl - yr) / cnt[i];
      int l = 0, r = X + Y, re = -1;
      while (l <= r) {
        int mid = ((ll)l + r) >> 1;
        ll p = lowdiv(X - (ll)xl * mid, dx), q = lowdiv(Y - (ll)yr * mid, dy);
        if (p >= 0 && q >= 0 && p + q >= (ll)cnt[i] * mid)
          re = mid, l = mid + 1;
        else
          r = mid - 1;
      }
      Cmax(an, re);
    }
    printf("%d\\n", an);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <vector>
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
inline char gc() {
  //	return getchar();
  static char buf[100000], *l = buf, *r = buf;
  return l == r && (r = (l = buf) + fread(buf, 1, 100000, stdin), l == r)
             ? EOF
             : *l++;
}
template <class T> void rd(T &x) {
  scanf("%d", &x);
  return;
  x = 0;
  int f = 1, ch = gc();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = gc();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 - '0' + ch;
    ch = gc();
  }
  x *= f;
}
template <class T> inline bool Cmax(T &x, T y) { return x < y ? x = y, 1 : 0; }
typedef long long ll;
int T, A, X, B, Y, C, D;
vector<int> pos, cnt;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int exgcd(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  int d = exgcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}
inline int inver(int a, int mod) {
  int x, y, d = exgcd(a, mod, x, y);
  if (x < 0)
    x += mod;
  return x;
}
inline ll lowdiv(ll a, ll b) { return a / b - (a % b && (a ^ b) < 0); }
void init() {
  int d = gcd(A, B);
  A /= d, B /= d, C /= gcd(d, C);
  for (int _ = 0; _ < 2; ++_) {
    d = gcd(A, C);
    C /= d, A /= d;
    int t = gcd(B, d);
    B /= t, Y /= d / t;
    swap(A, B), swap(X, Y);
  }
}
void getpos() {
  pos.clear(), cnt.clear();
  int W = C, H = D, now = 0, r = inver(D, C);
  while (W) {
    int d = W / H;
    pos.push_back((ll)now * r % C), cnt.push_back(d);
    now += d * H;
    W %= H;
    if (W == 0)
      break;
    H %= W;
    if (H == 0)
      H = W;
  }
  pos.push_back(C);
}
inline int gety(int x) { return x == 0 ? C : (C - (ll)x * D % C) % C; }
int main() {
  //	freopen("1.in","r",stdin);
  //	freopen("1.out","w",stdout);
  rd(T);
  for (int kase = 1; kase <= T; ++kase) {
    rd(A), rd(X), rd(B), rd(Y), rd(C);
    init();
    if (C == 1) {
      printf("%d\\n", X + Y);
      continue;
    }
    D = (ll)A * inver(B, C) % C;
    getpos();
    int an = 0;
    for (int i = 0; i < cnt.size(); ++i) {
      int xl = pos[i], xr = pos[i + 1], yl = gety(xl), yr = gety(xr);
      int dx = (xr - xl) / cnt[i], dy = (yl - yr) / cnt[i];
      int l = 0, r = X + Y, re = -1;
      while (l <= r) {
        int mid = (l + r) >> 1;
        ll p = lowdiv(X - (ll)xl * mid, dx), q = lowdiv(Y - (ll)yr * mid, dy);
        if (p >= 0 && q >= 0 && p + q >= (ll)cnt[i] * mid)
          re = mid, l = mid + 1;
        else
          r = mid - 1;
      }
      Cmax(an, re);
    }
    printf("%d\\n", an);
  }
  return 0;
}