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

#include <bits/stdc++.h>
using namespace std;
double PI = acos(-1);
double eps = 1e-7;
typedef complex<double> P;

double modf(double x) {
  while (x > 2 * PI) {
    x -= 2 * PI;
  }
  while (x < 0) {
    x += 2 * PI;
  }
  return x;
}

double x, y, a, b;

double compute(double v, double m, double rate) {
  double R = v / b;

  P target = polar(R, m);
  double distA = abs(target - P(x, y)) / v;
  P base = target - P(x, y);
  P p = P(x, y) + (base * rate);
  double f = modf(arg(p)) - distA * b * rate;

  return f;
}

double check(double v, double m) {
  double left = 0, right = 1, midL, midR;

  for (int i = 0; i < 100; i++) {
    double dist = (right - left) / 3.0;
    midL = left + dist;
    midR = right - dist;

    double fl = compute(v, m, midL);
    double fr = compute(v, m, midR);

    if (fl < fr) {
      right = midR;
    } else {
      left = midL;
    }
  }

  return compute(v, m, left);
}

bool func(double v) {
  // R*b == v
  // R==v/b
  double R = v / b;
  if (abs(P(x, y)) < R)
    return true;

  double tx = x, ty = y;
  double left = 0, right = PI, midL, midR;

  if (x < 0) {
    left = PI * 0.5;
    right = PI * 1.5;
  }

  for (int i = 0; i < 100; i++) {
    double dist = (right - left) / 3.0;
    midL = left + dist;
    midR = right - dist;

    double fl = check(v, midL);
    double fr = check(v, midR);

    if (fl < fr) {
      left = midL;
    } else {
      right = midR;
    }
  }

  bool res = ((check(v, midL) >= 0) || (check(v, midR) >= 0));

  x = tx;
  y = ty;
  return res;
}

double solve() {

  P p = P(x, y) / polar(1.0, a);
  x = p.real();
  y = abs(p.imag());

  //  assert( x>=0 || y==0);

  double left = 0, right = 1e5, mid;

  for (int i = 0; i < 100; i++) {
    mid = (left + right) * 0.5;
    if (func(mid))
      right = mid;
    else
      left = mid;
  }

  return left;
}

int main() {
  cin >> x >> y >> a >> b;
  a = a / 180.0 * PI;
  b = b / 180.0 * PI;
  printf("%.10f\\n", (double)solve());
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
double PI = acos(-1);
double eps = 1e-7;
typedef complex<double> P;

double x, y, a, b;

double compute(double v, double m, double rate) {
  double R = v / b;

  P target = polar(R, m);
  double distA = abs(target - P(x, y)) / v;
  P base = target - P(x, y);
  P p = P(x, y) + (base * rate);
  double f = arg(p) - distA * b * rate;
  return f;
}

double check(double v, double m) {
  double left = 0, right = 1, midL, midR;

  for (int i = 0; i < 100; i++) {
    double dist = (right - left) / 3.0;
    midL = left + dist;
    midR = right - dist;

    double fl = compute(v, m, midL);
    double fr = compute(v, m, midR);

    if (fl < fr) {
      right = midR;
    } else {
      left = midL;
    }
  }

  return compute(v, m, left);
}

bool func(double v) {
  // R*b == v
  // R==v/b
  double R = v / b;
  if (abs(P(x, y)) < R)
    return true;

  double tx = x, ty = y;
  /*
  if(x<0){
    double k=(arg(P(x,y)) - PI*0.5)/b;
    if( k*v >= abs(P(x,y)) )return true;
    double dd=abs(P(x,y))-k*v;
    x=0,y=dd;
  }
  */

  double left = 0, right = PI, midL, midR;

  if (x < 0) {
    left = PI;
    right = PI * 1.9999999999;
  }

  for (int i = 0; i < 100; i++) {
    double dist = (right - left) / 3.0;
    midL = left + dist;
    midR = right - dist;

    double fl = check(v, midL);
    double fr = check(v, midR);

    if (fl < fr) {
      left = midL;
    } else {
      right = midR;
    }
  }

  bool res = ((check(v, midL) >= 0) || (check(v, midR) >= 0));

  x = tx;
  y = ty;
  return res;
}

double solve() {

  P p = P(x, y) / polar(1.0, a);
  x = p.real();
  y = abs(p.imag());

  assert(x >= 0 || y == 0);

  double left = 0, right = 1e5, mid;

  for (int i = 0; i < 100; i++) {
    mid = (left + right) * 0.5;
    if (func(mid))
      right = mid;
    else
      left = mid;
  }

  return left;
}

int main() {
  cin >> x >> y >> a >> b;
  a = a / 180.0 * PI;
  b = b / 180.0 * PI;
  printf("%.10f\\n", (double)solve());
  return 0;
}