Problem p02656 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define db double
#define pint pair<int, int>
#define mk(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define eb(x, y) emplace_back(x, y)
#define fi first
#define se second
#define Rep(x, y, z) for (int x = y; x <= z; x++)
#define Red(x, y, z) for (int x = y; x >= z; x--)
using namespace std;
const int MAXN = 2e5 + 5;
char buf[1 << 12], *pp1 = buf, *pp2 = buf, nc;
int ny;
inline char gc() {
  return pp1 == pp2 &&
                 (pp2 = (pp1 = buf) + fread(buf, 1, 1 << 12, stdin), pp1 == pp2)
             ? EOF
             : *pp1++;
}
// inline char gc(){return getchar();}
inline int read() {
  int x = 0;
  for (ny = 1; nc = gc(), (nc < 48 || nc > 57) && nc != EOF;)
    if (nc == 45)
      ny = -1;
  if (nc < 0)
    return nc;
  for (x = nc - 48; nc = gc(), 47 < nc && nc < 58 && nc != EOF;
       x = (x << 3) + (x << 1) + (nc ^ 48))
    ;
  return x * ny;
}
int a, b, c, x, y, top;
inline void exgcd(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1, y = 0;
    return;
  }
  exgcd(b, a % b, y, x), y -= a / b * x;
}
inline int inv(int x, int p) {
  int a = 0, b = 0;
  exgcd(x, p, a, b);
  return (a % p + p) % p;
}
struct Node {
  int x, y, c;
} q[MAXN];
inline void work(int c, int d) {
  q[top = 0] = (Node){0, 0, 0};
  for (int x, a = 1, b = 0; d;) {
    Node tmp = q[top];
    q[++top] = (Node){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;
    }
  }
}
inline ll F(ll x, ll y) { return x < 0 ? -1 : x / y; }
int main() {
  //	freopen("std.in","r",stdin);
  //	freopen("std.out","w",stdout);
  for (int t = read(); t--;) {
    a = read(), x = read(), b = read(), y = read(), c = read();
    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) {
      cout << x + y << '\\n';
      continue;
    }
    int d = 1ll * a * inv(b, c) % c;
    work(c, d);
    if (q[top].x != c)
      q[++top] = (Node){c, c, 1};
    int ans = 0;
    Rep(i, 1, top) {
      int lx = q[i - 1].x, rx = q[i].x, ly = c - q[i - 1].y, ry = c - q[i].y,
          dx = (rx - lx) / q[i].c, dy = (ly - ry) / q[i].c, l = ans + 1,
          r = x + y;
      while (l <= r) {
        int mid = 0ll + l + r >> 1;
        ll s = F(x - 1ll * mid * lx, dx), t = F(y - 1ll * mid * ry, dy);
        if (s >= 0 && t >= 0 && s + t >= 1ll * mid * q[i].c)
          ans = mid, l = mid + 1;
        else
          r = mid - 1;
      }
    }
    cout << ans << '\\n';
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define db double
#define pint pair<int, int>
#define mk(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define eb(x, y) emplace_back(x, y)
#define fi first
#define se second
#define Rep(x, y, z) for (int x = y; x <= z; x++)
#define Red(x, y, z) for (int x = y; x >= z; x--)
using namespace std;
const int MAXN = 2e5 + 5;
char buf[1 << 12], *pp1 = buf, *pp2 = buf, nc;
int ny;
inline char gc() {
  return pp1 == pp2 &&
                 (pp2 = (pp1 = buf) + fread(buf, 1, 1 << 12, stdin), pp1 == pp2)
             ? EOF
             : *pp1++;
}
// inline char gc(){return getchar();}
inline int read() {
  int x = 0;
  for (ny = 1; nc = gc(), (nc < 48 || nc > 57) && nc != EOF;)
    if (nc == 45)
      ny = -1;
  if (nc < 0)
    return nc;
  for (x = nc - 48; nc = gc(), 47 < nc && nc < 58 && nc != EOF;
       x = (x << 3) + (x << 1) + (nc ^ 48))
    ;
  return x * ny;
}
int a, b, c, x, y, top;
inline void exgcd(int a, int b, int &x, int &y) {
  if (b == 0) {
    x = 1, y = 0;
    return;
  }
  exgcd(b, a % b, y, x), y -= a / b * x;
}
inline int inv(int x, int p) {
  int a = 0, b = 0;
  exgcd(x, p, a, b);
  return (a % p + p) % p;
}
struct Node {
  int x, y, c;
} q[MAXN];
inline void work(int c, int d) {
  q[top = 0] = (Node){0, 0, 0};
  for (int x, a = 1, b = 0; d;) {
    Node tmp = q[top];
    q[++top] = (Node){tmp.x + c / d * a, tmp.y + c / d * d, c / d};
    Rep(t, 1, 2) {
      if (t == 1)
        b += c / d * a;
      else
        a += c / d * b;
      x = c % d, c = d, d = x;
    }
  }
}
inline ll F(ll x, ll y) { return x < 0 ? -1 : x / y; }
int main() {
  //	freopen("std.in","r",stdin);
  //	freopen("std.out","w",stdout);
  for (int t = read(); t--;) {
    a = read(), x = read(), b = read(), y = read(), c = read();
    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) {
      cout << x + y << '\\n';
      continue;
    }
    int d = 1ll * a * inv(b, c) % c;
    work(c, d);
    if (q[top].x != c)
      q[++top] = (Node){c, c, 1};
    int ans = 0;
    Rep(i, 1, top) {
      int lx = q[i - 1].x, rx = q[i].x, ly = c - q[i - 1].y, ry = c - q[i].y,
          dx = (rx - lx) / q[i].c, dy = (ly - ry) / q[i].c, l = ans + 1,
          r = x + y;
      while (l <= r) {
        int mid = 0ll + l + r >> 1;
        ll s = F(x - 1ll * mid * lx, dx), t = F(y - 1ll * mid * ry, dy);
        if (s >= 0 && t >= 0 && s + t >= 1ll * mid * q[i].c)
          ans = mid, l = mid + 1;
        else
          r = mid - 1;
      }
    }
    cout << ans << '\\n';
  }
  return 0;
}