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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head

ll inv(ll q, ll m) {
  q %= m;
  if (q < 0)
    q += m;
  if (q == 0)
    return 0;
  ll a1 = m, b1 = 0, a2 = q, b2 = 1, a3 = 0, b3 = 0, t = 0;
  while (a2 != 1) {
    t = a1 / a2, a3 = a1 - t * a2, b3 = b1 - t * b2 % m, a1 = a2, a2 = a3,
    b1 = b2, b2 = b3;
    if (b2 < 0)
      b2 += m;
  }
  return b2;
}

ll ceil(ll n, ll m) { return (n + m - 1) / m; }

ll calc(ll A, ll M, ll L,
        ll R) // minimum nonnegative integer x s.t. L <= Ax mod M <= R
{
  if (R == 0)
    return M;
  if (L == 0)
    return 0;
  if (2 * A > M) {
    swap(L, R);
    A = M - A;
    L = M - L;
    R = M - R;
  }
  ll cc_1 = ceil(L, A);
  if (A * cc_1 <= R)
    return cc_1;
  ll cc_2 = calc(A - M % A, A, L % A, R % A);
  return ceil(L + M * cc_2, A);
}

bool sol_ex(ll A, ll M, ll L, ll R) // Existence of x s.t. L <= Ax mod M <= R
{
  if (L == 0)
    return true;
  ll g = gcd(A, M);
  if ((L - 1) / g == R / g)
    return false;
  return true;
}

void solve() {
  ll a, x, b, y, c;
  scanf("%lld%lld%lld%lld%lld", &a, &x, &b, &y, &c);
  a %= c;
  b %= c;
  while (gcd(a, c) > 1 || gcd(b, c) > 1) {
    ll d = gcd(gcd(a, b), c);
    a /= d;
    b /= d;
    c /= d;
    if (gcd(a, c) == 1)
      swap(a, b), swap(x, y);
    d = gcd(a, c);
    ll dd = d / gcd(d, b);
    b = b * dd % c;
    y = y / dd;
    a /= d;
    b /= d;
    c /= d;
    if (a == 0 && b == 0) {
      printf("%lld\\n", x + y);
      return;
    }
  }
  b = a * inv(c - b, c) % c;
  PII px(0, c);
  vector<VI> g;
  while (px.se > 0) {
    int p = calc(b, c, 1, px.se - 1);
    int q = (ll)p * b % c;
    int dx = p - px.fi, dy = px.se - q;
    int cnt = px.se / dy;
    PII py(px.fi + cnt * dx, px.se - cnt * dy);
    g.pb({px.fi, px.se, py.fi, py.se, dx, -dy});
    px = py;
  }
  ll l = 0, r = 1ll << 33;
  while (l + 1 < r) {
    ll md = (l + r) >> 1;
    bool valid = 0;
    for (auto p : g) {
      ll x1 = p[0] * md, x2 = p[2] * md;
      if (x1 <= x && x <= x2) {
        ll cc = p[1] * md + ((x - x1) / p[4]) * p[5];
        if (y >= cc)
          valid = 1;
      }
    }
    if (g.back()[2] * md <= x)
      valid = 1;
    if (valid)
      l = md;
    else
      r = md;
  }
  printf("%lld\\n", l);
}

int main() {
  int _;
  for (scanf("%d", &_); _; _--) {
    solve();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head

ll inv(ll q, ll m) {
  q %= m;
  if (q < 0)
    q += m;
  if (q == 0)
    return 0;
  ll a1 = m, b1 = 0, a2 = q, b2 = 1, a3 = 0, b3 = 0, t = 0;
  while (a2 != 1) {
    t = a1 / a2, a3 = a1 - t * a2, b3 = b1 - t * b2 % m, a1 = a2, a2 = a3,
    b1 = b2, b2 = b3;
    if (b2 < 0)
      b2 += m;
  }
  return b2;
}

ll ceil(ll n, ll m) { return (n + m - 1) / m; }

ll calc(ll A, ll M, ll L,
        ll R) // minimum nonnegative integer x s.t. L <= Ax mod M <= R
{
  if (R == 0)
    return M;
  if (L == 0)
    return 0;
  if (2 * A > M) {
    swap(L, R);
    A = M - A;
    L = M - L;
    R = M - R;
  }
  ll cc_1 = ceil(L, A);
  if (A * cc_1 <= R)
    return cc_1;
  ll cc_2 = calc(A - M % A, A, L % A, R % A);
  return ceil(L + M * cc_2, A);
}

bool sol_ex(ll A, ll M, ll L, ll R) // Existence of x s.t. L <= Ax mod M <= R
{
  if (L == 0)
    return true;
  ll g = gcd(A, M);
  if ((L - 1) / g == R / g)
    return false;
  return true;
}

void solve() {
  int a, x, b, y, c;
  scanf("%d%d%d%d%d", &a, &x, &b, &y, &c);
  a %= c;
  b %= c;
  while (gcd(a, c) > 1 || gcd(b, c) > 1) {
    int d = gcd(gcd(a, b), c);
    a /= d;
    b /= d;
    c /= d;
    if (gcd(a, c) == 1)
      swap(a, b), swap(x, y);
    d = gcd(a, c);
    int dd = d / gcd(d, b);
    b = b * dd % c;
    y = y / dd;
    a /= d;
    b /= d;
    c /= d;
    if (a == 0 && b == 0) {
      printf("%d\\n", x + y);
      return;
    }
  }
  // printf("%lld %lld %lld %lld %lld\\n",a,x,b,y,c);
  b = a * inv(c - b, c) % c;
  PII px(0, c);
  vector<VI> g;
  while (px.se > 0) {
    int p = calc(b, c, 1, px.se - 1);
    int q = (ll)p * b % c;
    int dx = p - px.fi, dy = px.se - q;
    int cnt = px.se / dy;
    PII py(px.fi + cnt * dx, px.se - cnt * dy);
    g.pb({px.fi, px.se, py.fi, py.se, dx, -dy});
    px = py;
  }
  ll l = 0, r = 1ll << 33;
  while (l + 1 < r) {
    ll md = (l + r) >> 1;
    bool valid = 0;
    for (auto p : g) {
      ll x1 = p[0] * md, x2 = p[2] * md;
      if (x1 <= x && x <= x2) {
        ll cc = p[1] * md + ((x - x1) / p[4]) * p[5];
        if (y >= cc)
          valid = 1;
      }
    }
    if (g.back()[2] * md <= x)
      valid = 1;
    if (valid)
      l = md;
    else
      r = md;
  }
  printf("%lld\\n", l);
}

int main() {
  int _;
  for (scanf("%d", &_); _; _--) {
    solve();
  }
}