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

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int, int>
#define pll pair<ll, ll>
#define IT iterator
#define PB push_back
#define fi first
#define se second
#define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--)
#define CLR(a, v) memset(a, v, sizeof(a));
#define CPY(a, b) memcpy(a, b, sizeof(a));
#define debug puts("wzpakking")
#define y1 ysghysgsygsh
#define int long long
using namespace std;
int A, B, C, X, Y, H, G, D;
int top, q1[555], q2[555], q3[555];
int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
void exgcd(int x, int y, int &a, int &b) {
  if (!y) {
    a = 1;
    b = 0;
    return;
  }
  exgcd(y, x % y, b, a);
  b -= x / y * a;
}
int getinv(int x, int y) {
  int a, b;
  exgcd(x, y, a, b);
  return (a % y + y) % y;
}
void get_seq(int C, int D) {
  top = q1[0] = q2[0] = 0;
  int len = 1, x, a = 1, b = 0;
  for (; D;) {
    // cout<<"debug "<<C<<' '<<D<<' '<<a<<endl;
    ++top;
    q1[top] = q1[top - 1] + C / D * a;
    q2[top] = q2[top - 1] + C / D * D;
    q3[top] = C / D;
    For(T, 1, 2) {
      if (T == 1)
        b += C / D * a;
      else
        a += C / D * b;
      x = C % D;
      C = D;
      D = x;
      if (!D)
        break;
    }
  }
}
ll Mydiv(ll x, ll y) { return x < 0 ? -1 : x / y; }
void solve() {
  scanf("%lld%lld%lld%lld%lld", &A, &X, &B, &Y, &C);
  G = gcd(A, B);
  A /= G;
  B /= G;
  C /= gcd(C, G);
  G = gcd(A, C);
  A /= G;
  C /= G;
  H = gcd(G, B);
  B /= H;
  Y /= G / H;
  G = gcd(B, C);
  B /= G;
  C /= G;
  H = gcd(G, A);
  A /= H;
  X /= G / H;
  A %= C;
  B %= C;
  if (C == 1)
    return cout << X + Y << endl, void(0);
  D = 1ll * A * getinv(B, C) % C;
  get_seq(C, D);
  if (q1[top] != C)
    q1[++top] = C, q2[top] = C, q3[top] = 1;
  // For(i,1,top) cout<<q1[i]<<' '<<q2[i]<<' '<<q3[i]<<endl;
  int ans = 0;
  For(i, 1, top) {
    int lx = q1[i - 1], rx = q1[i];
    int ly = C - q2[i - 1], ry = C - q2[i];
    int dx = (rx - lx) / q3[i], dy = (ly - ry) / q3[i];
    int l = ans + 1, r = X + Y + 1;
    // cout<<lx<<' '<<ry<<' '<<dx<<' '<<dy<<' '<<ans<<endl;
    while (l <= r) {
      int mid = (l + r) / 2;
      ll p = Mydiv(X - 1ll * mid * lx, dx);
      ll q = Mydiv(Y - 1ll * mid * ry, dy);
      if (p >= 0 && q >= 0 && p + q >= 1ll * q3[i] * mid)
        ans = max(ans, mid), l = mid + 1;
      else
        r = mid - 1;
    }
  }
  cout << ans << endl;
}
signed main() {
  int T;
  scanf("%lld", &T);
  while (T--)
    solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int, int>
#define pll pair<ll, ll>
#define IT iterator
#define PB push_back
#define fi first
#define se second
#define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--)
#define CLR(a, v) memset(a, v, sizeof(a));
#define CPY(a, b) memcpy(a, b, sizeof(a));
#define debug puts("wzpakking")
#define y1 ysghysgsygsh
using namespace std;
int A, B, C, X, Y, H, G, D;
int top, q1[55], q2[55], q3[55];
int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
void exgcd(int x, int y, int &a, int &b) {
  if (!y) {
    a = 1;
    b = 0;
    return;
  }
  exgcd(y, x % y, b, a);
  b -= x / y * a;
}
int getinv(int x, int y) {
  int a, b;
  exgcd(x, y, a, b);
  return (a % y + y) % y;
}
void get_seq(int C, int D) {
  top = q1[0] = q2[0] = 0;
  int len = 1, x, a = 1, b = 0;
  for (; D;) {
    // cout<<"debug "<<C<<' '<<D<<' '<<a<<endl;
    ++top;
    q1[top] = q1[top - 1] + C / D * a;
    q2[top] = q2[top - 1] + C / D * D;
    q3[top] = C / D;
    For(T, 1, 2) {
      if (T == 1)
        b += C / D * a;
      else
        a += C / D * b;
      x = C % D;
      C = D;
      D = x;
      if (!D)
        break;
    }
  }
}
ll Mydiv(ll x, ll y) { return x < 0 ? -1 : x / y; }
void solve() {
  scanf("%d%d%d%d%d", &A, &X, &B, &Y, &C);
  G = gcd(A, B);
  A /= G;
  B /= G;
  C /= gcd(C, G);
  G = gcd(A, C);
  A /= G;
  C /= G;
  H = gcd(G, B);
  B /= H;
  Y /= G / H;
  G = gcd(B, C);
  B /= G;
  C /= G;
  H = gcd(G, A);
  A /= H;
  X /= G / H;
  A %= C;
  B %= C;
  if (C == 1)
    return cout << X + Y << endl, void(0);
  D = 1ll * A * getinv(B, C) % C;
  get_seq(C, D);
  if (q1[top] != C)
    q1[++top] = C, q2[top] = C, q3[top] = 1;
  // For(i,1,top) cout<<q1[i]<<' '<<q2[i]<<' '<<q3[i]<<endl;
  int ans = 0;
  For(i, 1, top) {
    int lx = q1[i - 1], rx = q1[i];
    int ly = C - q2[i - 1], ry = C - q2[i];
    int dx = (rx - lx) / q3[i], dy = (ly - ry) / q3[i];
    int l = ans + 1, r = X + Y + 1;
    // cout<<lx<<' '<<ry<<' '<<dx<<' '<<dy<<' '<<ans<<endl;
    while (l <= r) {
      int mid = (l + r) / 2;
      ll p = Mydiv(X - 1ll * mid * lx, dx);
      ll q = Mydiv(Y - 1ll * mid * ry, dy);
      if (p >= 0 && q >= 0 && p + q >= 1ll * q3[i] * mid)
        ans = max(ans, mid), l = mid + 1;
      else
        r = mid - 1;
    }
  }
  cout << ans << endl;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--)
    solve();
}