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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

typedef double number;
typedef valarray<number> point;

number dot(const point &a, const point &b) { return (a * b).sum(); }
point cross(const point &a, const point &b) {
  return a.cshift(+1) * b.cshift(-1) - a.cshift(-1) * b.cshift(+1);
}
number dist2(const point &a, const point &b) { return dot(a - b, a - b); }
number norm(const point &a) { return sqrt(dot(a, a)); }
ostream &operator<<(ostream &stream, point a) {
  stream << "(";
  REP(i, a.size()) {
    if (i)
      stream << ", ";
    stream << a[i];
  }
  stream << ")";
  return stream;
}

// xNgÜíèÌñ] * axis :
// ñ]²ÌPÊxNg
point rotate(point axis, double ang, point a) {
  return a * cos(ang) + (1.0 - cos(ang)) * dot(a, axis) * axis +
         cross(axis, a) * sin(ang);
}

int main() {
  int n;
  while (cin >> n, n) {
    point nowvec(3);
    cin >> nowvec[0] >> nowvec[1] >> nowvec[2];
    vector<point> p(n, point(3));
    double r[n];
    REP(i, n) { cin >> p[i][0] >> p[i][1] >> p[i][2] >> r[i]; }
    point now(3);
    while (1) {
      double mik = INF;
      int hoge;
      REP(i, n) {
        double a = dot(nowvec, nowvec);
        double b = 2 * dot(nowvec, p[i] - now);
        double c = dot(p[i] - now, p[i] - now) - r[i] * r[i];

        double D = b * b - 4 * a * c;
        if (D < 0)
          continue;
        double k = (b - sqrt(b * b - 4 * a * c)) / (2 * a);
        if (k < 0)
          continue;
        if (mik > k) {
          mik = k;
          hoge = i;
        }
      }
      if (mik == INF)
        break;
      point vop(p[hoge] - now - nowvec * mik); // å«³rÌÍ¸
      // cout << mik << endl;
      // cout << "a " << norm(nowvec) << endl;
      // cout << r[hoge] << " " << norm(vop) << endl;
      point axis(cross(nowvec, vop));
      now = now + nowvec * mik;
      if (norm(axis) < 1e-7) {
        // cout << now << endl;
        nowvec = -nowvec;
        continue;
      }
      axis = axis / norm(axis);
      // cout << norm(nowvec*mik) << endl;
      double ang = acos(dot(vop, nowvec) / norm(vop) / norm(nowvec));
      nowvec = rotate(axis, ang * 2 + M_PI, nowvec);
    }
    printf("%.6f %.6f %.6f\\n", now[0], now[1], now[2]);
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

typedef double number;
typedef valarray<number> point;

number dot(const point &a, const point &b) { return (a * b).sum(); }
point cross(const point &a, const point &b) {
  return a.cshift(+1) * b.cshift(-1) - a.cshift(-1) * b.cshift(+1);
}
number dist2(const point &a, const point &b) { return dot(a - b, a - b); }
number norm(const point &a) { return sqrt(dot(a, a)); }
ostream &operator<<(ostream &stream, point a) {
  stream << "(";
  REP(i, a.size()) {
    if (i)
      stream << ", ";
    stream << a[i];
  }
  stream << ")";
  return stream;
}

// xNgÜíèÌñ] * axis :
// ñ]²ÌPÊxNg
point rotate(point axis, double ang, point a) {
  return a * cos(ang) + (1.0 - cos(ang)) * dot(a, axis) * axis +
         cross(axis, a) * sin(ang);
}

int main() {
  int n;
  while (cin >> n, n) {
    point nowvec(3);
    cin >> nowvec[0] >> nowvec[1] >> nowvec[2];
    point p[n];
    double r[n];
    REP(i, n) {
      p[i] = point(3);
      cin >> p[i][0] >> p[i][1] >> p[i][2] >> r[i];
    }
    point now(3);
    while (1) {
      double mik = INF;
      int hoge;
      REP(i, n) {
        double a = dot(nowvec, nowvec);
        double b = 2 * dot(nowvec, p[i] - now);
        double c = dot(p[i] - now, p[i] - now) - r[i] * r[i];

        double D = b * b - 4 * a * c;
        if (D < 0)
          continue;
        double k = (b - sqrt(b * b - 4 * a * c)) / (2 * a);
        if (k < 0)
          continue;
        if (mik > k) {
          mik = k;
          hoge = i;
        }
      }
      if (mik == INF)
        break;
      point vop = p[hoge] - now - nowvec * mik; // å«³rÌÍ¸
      // cout << mik << endl;
      // cout << "a " << norm(nowvec) << endl;
      // cout << r[hoge] << " " << norm(vop) << endl;
      point axis = cross(nowvec, vop);
      now = now + nowvec * mik;
      if (norm(axis) < 1e-7) {
        // cout << now << endl;
        nowvec = -nowvec;
        continue;
      }
      axis = axis / norm(axis);
      double ang = acos(dot(vop, nowvec) / norm(vop) / norm(nowvec));
      nowvec = rotate(axis, ang * 2 + M_PI, nowvec);
    }
    printf("%.6f %.6f %.6f\\n", now[0], now[1], now[2]);
  }
}