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

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
typedef long long ll;
template <typename T> void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> void chkmin(T &x, T y) { x = min(x, y); }
template <typename T> void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -f;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  x *= f;
}
void exgcd(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1, y = 0;
    return;
  }
  int q = a / b, r = a % b;
  exgcd(b, r, y, x);
  y -= q * x;
}
int inv(int x, int p) {
  int a = 0, b = 0;
  exgcd(x, p, a, b);
  return (a % p + p) % p;
}
struct info {
  int x, y, c;
} q[MAXN];
int top, a, b, c, x, y;
void work(int c, int d) {
  q[top = 0] = (info){0, 0, 0};
  int x, a = 1, b = 0;
  while (d != 0) {
    info tmp = q[top];
    q[++top] = (info){tmp.x + c / d * a, tmp.y + c / d * d, c / d};
    for (int t = 1; t <= 2 && d != 0; t++) {
      if (t == 1)
        b += c / d * a;
      else
        a += c / d * b;
      x = c % d, c = d, d = x;
    }
  }
}
ll func(ll x, ll y) {
  if (x < 0)
    return -1;
  else
    return x / y;
}
int main() {
  int T;
  read(T);
  while (T--) {
    read(a), read(x), read(b), read(y), read(c);
    int g = __gcd(a, b);
    a /= g, b /= g, c /= __gcd(c, g);
    g = __gcd(a, c), a /= g, c /= g, y /= g;
    g = __gcd(b, c), b /= g, c /= g, x /= g;
    a %= c, b %= c;
    if (c == 1) {
      printf("%d\\n", x + y);
      continue;
    }
    int d = 1ll * a * inv(b, c) % c;
    work(c, d);
    if (q[top].x != c)
      q[++top] = (info){c, c, 1};
    int ans = 0;
    for (int i = 1; i <= top; i++) {
      int lx = q[i - 1].x, rx = q[i].x;
      int ly = c - q[i - 1].y, ry = c - q[i].y;
      int dx = (rx - lx) / q[i].c, dy = (ly - ry) / q[i].c;
      int l = ans + 1, r = x + y;
      while (l <= r) {
        int mid = (0ll + l + r) / 2;
        ll s = func(x - 1ll * mid * lx, dx);
        ll t = func(y - 1ll * mid * ry, dy);
        if (s >= 0 && t >= 0 && s + t >= 1ll * q[i].c * mid)
          ans = mid, l = mid + 1;
        else
          r = mid - 1;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
typedef long long ll;
template <typename T> void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> void chkmin(T &x, T y) { x = min(x, y); }
template <typename T> void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -f;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  x *= f;
}
void exgcd(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1, y = 0;
    return;
  }
  int q = a / b, r = a % b;
  exgcd(b, r, y, x);
  y -= q * x;
}
int inv(int x, int p) {
  int a = 0, b = 0;
  exgcd(x, p, a, b);
  return (a % p + p) % p;
}
struct info {
  int x, y, c;
} q[MAXN];
int top, a, b, c, x, y;
void work(int c, int d) {
  q[top = 0] = (info){0, 0, 0};
  int len = 1, x, a = 1, b = 0;
  while (d != 0) {
    info tmp = q[top];
    q[++top] = (info){tmp.x + c / d * a, tmp.y + c / d * d, c / d};
    for (int t = 1; t <= 2 && d != 0; t++) {
      if (t == 1)
        b += c / d * a;
      else
        a += c / d * b;
      x = c % d, c = d, d = x;
    }
  }
}
ll func(ll x, ll y) {
  if (x < 0)
    return -1;
  else
    return x / y;
}
int main() {
  int T;
  read(T);
  while (T--) {
    read(a), read(x), read(b), read(y), read(c);
    int g = __gcd(a, b);
    a /= g, b /= g, c /= __gcd(c, g);
    g = __gcd(a, c), a /= g, c /= g, y /= g;
    g = __gcd(b, c), b /= g, c /= g, x /= g;
    a %= c, b %= c;
    if (c == 1) {
      printf("%d\\n", x + y);
      continue;
    }
    int d = 1ll * a * inv(b, c) % c;
    work(c, d);
    if (q[top].x != c)
      q[++top] = (info){c, c, 1};
    int ans = 0;
    for (int i = 1; i <= top; i++) {
      int lx = q[i - 1].x, rx = q[i].x;
      int ly = c - q[i - 1].y, ry = c - q[i].y;
      int dx = (rx - lx) / q[i].c, dy = (ly - ry) / q[i].c;
      int l = ans + 1, r = x + y;
      while (l <= r) {
        int mid = (l + r) / 2;
        ll s = func(x - 1ll * mid * lx, dx);
        ll t = func(y - 1ll * mid * ry, dy);
        if (s >= 0 && t >= 0 && s + t >= 1ll * q[i].c * mid)
          ans = mid, l = mid + 1;
        else
          r = mid - 1;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}