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

#include <bits/stdc++.h>
#define re register
int gcd(re int a, re int b) {
  if (!b)
    return a;
  return gcd(b, a % b);
}
void exgcd(re int a, re int b, re int &x, re int &y) {
  if (!b) {
    x = 1;
    y = 0;
    return;
  }
  exgcd(b, a % b, y, x);
  y -= a / b * x;
}
int a, b, c, d, x, y, x1, yk1, x2, y2, c1, d1, c2, d2, rd;
long long sol() {
  re int l = 0, r = (yk1 - y2) / d2 - 1, xx, yy, xx1, yy1;
  for (re int mid = (l + r + 1) / 2; l < r; mid = (l + r + 1) / 2) {
    yy = yk1 - mid * d2, xx = 1ll * rd * yy % c;
    if (xx == 0 && yy == 0)
      xx = c;
    if (1ll * x * yy >= 1ll * xx * y)
      l = mid;
    else
      r = mid - 1;
  }
  yy = yk1 - l * d2;
  xx = 1ll * rd * yy % c;
  yy1 = yy - d2;
  xx1 = 1ll * rd * yy1 % c;
  if (xx == 0 && yy == 0)
    xx = c;
  if (xx1 == 0 && yy1 == 0)
    xx1 = c;
  re long long xl1 = xx - xx1, yl1 = yy - yy1, xxl, yyl;
  l = 0, r = 1e9;
  for (re int mid = (l + r + 1) / 2; l < r; mid = (l + r + 1) / 2) {
    xxl = x + 1ll * mid * xl1, yyl = y + 1ll * mid * yl1;
    if ((long double)xxl * yy >= (long double)xx * yyl)
      l = mid;
    else
      r = mid - 1;
  }
  xxl = x + l * xl1, yyl = y + l * yl1; // printf("****%lld %lld %d %d %d %d
                                        // %d\\n",xxl,yyl,xx,yy,xx1,yy1,l);
  re long long ans =
      std::min(xx ? xxl / xx : (1ll << 60), yy ? yyl / yy : (1ll << 60));
  xxl += xl1;
  yyl += yl1;
  if (xxl >= 0 && yyl >= 0)
    ans = std::max(ans, std::min(xx ? xxl / xx : (1ll << 60),
                                 yy ? yyl / yy : (1ll << 60)));
  return ans;
}
int main() {
  re int T;
  scanf("%d", &T);
  for (; T--;) {
    scanf("%d%d%d%d%d", &a, &x, &b, &y, &c);
    re 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;
    exgcd(b, c, x1, yk1);
    if (c == 1) {
      printf("%d\\n", x + y);
      continue;
    }
    d = 1ll * a * (x1 + c) % c; // printf("**%d %d %d\\n",a,b,c);
    exgcd(d, c, x1, yk1);
    rd = (c - x1) % c;
    c1 = c, d1 = d;
    while (c1 != 0) {
      x1 = 1ll * rd * c1 % c;
      yk1 = c1;
      c2 = c1, d2 = d1;
      if (d1)
        c1 %= d1;
      else
        c1 = 0;
      if (c1)
        d1 %= c1;
      else
        d1 = 0;
      if (d1 == 0)
        d1 = c1;
      re int x2 = 1ll * rd * c1 % c, y2 = c1;
      if (x2 == 0 && y2 == 0)
        x2 = c;
      // printf("****%d %d %d %d %d %d\\n",x1,yk1,x2,y2,d,d2);
      if (1ll * x * yk1 >= 1ll * x1 * y && 1ll * x2 * y >= 1ll * x * y2) {
        printf("%lld\\n", sol());
        break;
      }
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define re register
int gcd(re int a, re int b) {
  if (!b)
    return a;
  return gcd(b, a % b);
}
void exgcd(re int a, re int b, re int &x, re int &y) {
  if (!b) {
    x = 1;
    y = 0;
    return;
  }
  exgcd(b, a % b, y, x);
  y -= a / b * x;
}
int a, b, c, d, x, y, x1, yk1, x2, y2, c1, d1, c2, d2, rd;
long long sol() {
  re int l = 0, r = (yk1 - y2) / d2 - 1, xx, yy, xx1, yy1;
  for (re int mid = (l + r + 1) / 2; l < r; mid = (l + r + 1) / 2) {
    yy = yk1 - mid * d2, xx = 1ll * rd * yy % c;
    if (1ll * x * yy >= 1ll * xx * y)
      l = mid;
    else
      r = mid - 1;
  }
  yy = yk1 - l * d2;
  xx = 1ll * rd * yy % c;
  yy1 = yy - d2;
  xx1 = 1ll * rd * yy1 % c;
  re long long xl1 = xx - xx1, yl1 = yy - yy1, xxl, yyl;
  l = 0, r = 1e9;
  for (re int mid = (l + r + 1) / 2; l < r; mid = (l + r + 1) / 2) {
    xxl = x + 1ll * mid * xl1, yyl = y + 1ll * mid * yl1;
    if ((long double)xxl * yy >= (long double)xx * yyl)
      l = mid;
    else
      r = mid - 1;
  }
  xxl = x + l * xl1, yyl = y + l * yl1; // printf("****%lld %lld %d %d %d %d
                                        // %d\\n",xxl,yyl,xx,yy,xx1,yy1,l);
  re long long ans =
      std::min(xx ? xxl / xx : (1ll << 60), yy ? yyl / yy : (1ll << 60));
  xxl += xl1;
  yyl += yl1;
  if (xxl >= 0 && yyl >= 0)
    ans = std::max(ans, std::min(xx ? xxl / xx : (1ll << 60),
                                 yy ? yyl / yy : (1ll << 60)));
  return ans;
}
int main() {
  re int T;
  scanf("%d", &T);
  for (; T--;) {
    scanf("%d%d%d%d%d", &a, &x, &b, &y, &c);
    re 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;
    exgcd(b, c, x1, yk1);
    d = 1ll * a * (x1 + c) % c; // printf("**%d %d %d c:%d d:%d
                                // %lld\\n",a,b,gcd(a,b),c,d,1ll*(x1+c)*b%c);
    exgcd(d, c, x1, yk1);
    rd = (c - x1) % c;
    c1 = c, d1 = d;
    while (c1 != 0) {
      x1 = 1ll * rd * c1 % c;
      yk1 = c1;
      c2 = c1, d2 = d1;
      if (d1)
        c1 %= d1;
      else
        c1 = 0;
      if (c1)
        d1 %= c1;
      else
        d1 = 0;
      if (d1 == 0)
        d1 = c1;
      re int x2 = 1ll * rd * c1 % c, y2 = c1;
      // printf("****%d %d %d %d %d %d\\n",x1,y1,x2,y2,d,d2);
      if (1ll * x * yk1 >= 1ll * x1 * y && 1ll * x2 * y >= 1ll * x * y2) {
        printf("%lld\\n", sol());
        break;
      }
    }
  }
}