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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>

using namespace std;

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

const int INFTY = 1 << 29;
const double EPS = 1e-6;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

const double PI = acos(-1);

int Signum(double x) { return abs(x) < EPS ? 0 : x < 0 ? -1 : 1; }

struct Point {
  double x, y;
  Point() {}
  Point(double x, double y) : x(x), y(y) {}
};
bool operator==(Point a, Point b) {
  return abs(a.x - b.x) < EPS && abs(a.y - b.y) < EPS;
}
bool operator!=(Point a, Point b) {
  return abs(a.x - b.x) >= EPS || abs(a.y - b.y) >= EPS;
}
Point operator+(Point a, Point b) { return Point(a.x + b.x, a.y + b.y); }
Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); }
Point operator*(double c, Point p) { return Point(c * p.x, c * p.y); }
Point operator/(Point p, double c) { return Point(p.x / c, p.y / c); }
double Abs(Point p) { return sqrt(p.x * p.x + p.y * p.y); }
double Abs2(Point p) { return p.x * p.x + p.y * p.y; }
double Arg(Point p) { return atan2(p.y, p.x); }
double Dot(Point a, Point b) { return a.x * b.x + a.y * b.y; }
double Cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }
Point Rot(Point p, double t) {
  return Point(cos(t) * p.x - sin(t) * p.y, sin(t) * p.x + cos(t) * p.y);
}

int CCW(Point a, Point b, Point c) {
  Point d1 = b - a, d2 = c - a;
  if (int sign = Signum(Cross(d1, d2)))
    return sign; // 1:ccw,-1:cw
  if (Dot(d1, d2) < -EPS)
    return -2; // c-a-b
  if (Abs2(d1) < Abs2(d2) - EPS)
    return 2; // a-b-c
  return 0;   // a-c-b
}

struct Circle {
  Point center;
  double radius;
  Circle() {}
  Circle(Point c, double r) : center(c), radius(r) {}
  Circle(double x, double y, double r) : center(x, y), radius(r) {}
};

pair<Point, Point> InterCC(Circle c1, Circle c2) {
  Point p1 = c1.center, p2 = c2.center;
  double r1 = c1.radius, r2 = c2.radius;

  double d = Abs(p1 - p2);
  double a = (d * d + r1 * r1 - r2 * r2) / (2 * d);
  double s = sqrt(r1 * r1 - a * a);

  return mp(p1 + a / d * (p2 - p1) + s * Rot((p2 - p1) / d, PI / 2),
            p1 + a / d * (p2 - p1) - s * Rot((p2 - p1) / d, PI / 2));
}

int main() {
  for (int n, r, q; scanf("%d%d%d", &n, &r, &q), n | r | q;) {
    vector<Point> ps(n);
    rep(i, n) scanf("%lf%lf", &ps[i].x, &ps[i].y);

    int u = -1;
    rep(i, n) if (abs(Abs(ps[i]) - r) < EPS) u = i;

    rep(_, q) {
      double nt = INFTY;
      int v = -1;
      rep(i, n) if (i != u) {
        pair<Point, Point> cp =
            InterCC(Circle(0, 0, r), Circle(ps[u], Abs(ps[i] - ps[u])));
        Point q = CCW(ps[u], ps[i], cp.first) == 1 ? cp.first : cp.second;
        double t = Arg(q - ps[u]) - Arg(ps[i] - ps[u]);
        if (t < EPS)
          t += 2 * PI;
        if (t < nt - EPS ||
            t < nt + EPS && Abs(q - ps[u]) > Abs(ps[v] - ps[u]) - EPS) {
          nt = t;
          v = i;
        }
      }
      rep(i, n) if (i != u) ps[i] = Rot(ps[i] - ps[u], nt) + ps[u];

      printf("%.12f %.12f\\n", ps[v].x, ps[v].y);
      u = v;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>

using namespace std;

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

const int INFTY = 1 << 29;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

const double PI = acos(-1);

struct Point {
  double x, y;
  Point() {}
  Point(double x, double y) : x(x), y(y) {}
};
bool operator==(Point a, Point b) {
  return abs(a.x - b.x) < EPS && abs(a.y - b.y) < EPS;
}
bool operator!=(Point a, Point b) {
  return abs(a.x - b.x) >= EPS || abs(a.y - b.y) >= EPS;
}
Point operator+(Point a, Point b) { return Point(a.x + b.x, a.y + b.y); }
Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); }
double Abs(Point p) { return sqrt(p.x * p.x + p.y * p.y); }
Point Rot(Point p, double t) {
  return Point(cos(t) * p.x - sin(t) * p.y, sin(t) * p.x + cos(t) * p.y);
}

struct Line {
  Point pos, dir;
  Line() {}
  Line(Point p, Point d) : pos(p), dir(d) {}
  Line(double px, double py, double dx, double dy) : pos(px, py), dir(dx, dy) {}
};
typedef Line Segment;

int main() {
  for (int n, r, q; scanf("%d%d%d", &n, &r, &q), n | r | q;) {
    vector<Point> ps(n);
    rep(i, n) scanf("%lf%lf", &ps[i].x, &ps[i].y);

    reverse(all(ps));
    rep(i, n) if (abs(Abs(ps[i]) - r) < EPS) {
      rotate(ps.begin(), i + all(ps));
      break;
    }

    rep(_, q) {
      double mn = 2 * PI;
      int ni = -1;
      repi(i, 1, n) {
        double l = Abs(ps[i] - ps[0]);
        double t = 2 * asin(l / (2 * r));
        Point q = Rot(ps[0], -t);
        double s = 2 * asin(Abs(q - ps[i]) / (2 * l));
        vector<Point> qs(n);
        rep(j, n) qs[j] = Rot(ps[j] - ps[0], s) + ps[0];

        bool ok = true;
        rep(j, n) if (Abs(qs[j]) > r + EPS) ok = false;
        if (ok && (ni == -1 || mn > s))
          mn = s, ni = i;
      }
      rep(i, n) ps[i] = Rot(ps[i] - ps[0], mn) + ps[0];
      rotate(ps.begin(), ni + all(ps));
      printf("%.12f %.12f\\n", ps[0].x, ps[0].y);
    }
  }

  return 0;
}