Problem:
Watchdog Corporation
In Northern Kyushu, you are running a company
which rents watchdogs in response to the customers' order.
The distinguishing point of your service is that you also
install fences to enhance the performance of watchdogs.
The dog is put on a leash, which is tied up to a peg.
You are installing fences at the
border of the dog's reach, so that strangers approaching the
fence are taken care of by your dog.
There may be a rectangular building near the dog; the dog cannot
enter the building, but can go around it as long as the length of
the leash permits, as in Figure F-1.  You do not need fences along the
building's wall.
Figure F-1: Example fence layouts (some parts of the fences are omitted.)
Given the length of the leash and the position and the size of the
building, your program should calculate the length of the fence.
Input
The input consists of multiple datasets each consisting of 
five integers in a line of the following format.
len
x
1
y
1
x
2
y
2
len
indicates the length of the leash.
x
1
,
y
1
,
x
2
and
y
2
indicate the size and position of the
building in that a point with coordinate (
x
,
y
)
such that
x
1
<
x
<
x
2
and
y
1
<
y
<
y
2
is 
contained in the building.  This means that
the building's walls are parallel to either the X-axis or Y-axis.
The peg is located on the origin.
You may assume that 0 <
len
≤ 100, −100
≤
x
1
<
x
2
≤ 100 and
−100 ≤
y
1
<
y
2
≤ 100.
You may also assume that the peg is located apart from the
building (that is, neither inside nor on the border of the building).
The end of the input is indicated by a line containing five zeros.
Output
For each dataset, output the total length of the fences as a
single fractional number in
a line.  There may not be any other characters in the output.
The output should not contain an error greater than 0.00001.
Sample Input
3 3 0 6 5
5 3 0 6 5
6 3 0 6 5
7 3 0 6 5
9 3 0 6 5
10 3 0 6 5
12 3 0 6 5
100 3 0 6 5
4 -3 4 5 8
5 -3 4 5 8
6 -3 4 5 8
64 -30 40 50 50
7 -3 4 5 8
10 -3 4 5 8
11 -3 4 5 8
14 -3 4 5 8
35 -3 4 5 8
100 -3 4 5 8
10 5 9 12 12
13 5 9 12 12
15 5 9 12 12
18 5 9 12 12
19 5 9 12 12
20 5 9 12 12
21 5 9 12 12
100 5 9 12 12
100 -5 1 -3 5
100 0 1 100 2
100 -1 99 100 100
10 -1 1 1 2
84 1 -77 5 -42
27 -12 -7 3 -4
0 0 0 0 0
Output for the Sample Input
18.84955592
26.77945045
31.69103413
39.54501577
55.51851918
63.83954229
75.38181750
628.05343108
25.13274123
24.98091545
29.43519428
318.90944272
35.02723766
55.44758990
64.23914179
89.33649537
219.11188064
627.48648370
62.83185307
76.33420961
88.61222855
112.17417345
121.59895141
126.16196589
132.25443447
628.23333565
628.18890261
626.17695473
613.16456638
62.61625003
527.84509612
168.07337509

Submitted solutions:

code_snippet: #include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <unordered_map>
#include <iterator>
#include <assert.h>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) ((int)container.size())
#define mp(a,b) make_pair(a, b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}
template<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}
template<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}

namespace geom{
#define X real()
#define Y imag()
#define at(i) ((*this)[i])
#define SELF (*this)
	enum {TRUE = 1, FALSE = 0, BORDER = -1};
	typedef int BOOL;
	typedef long double R;
	const R INF = 1e12;
	R EPS = 1e-6;
	const R PI = 3.1415926535897932384626;
	inline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }
	inline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;}
	typedef complex<R> P;
	inline R norm(const P &p){return p.X*p.X+p.Y*p.Y;}
	inline R inp(const P& a, const P& b){return (conj(a)*b).X;}
	inline R outp(const P& a, const P& b){return (conj(a)*b).Y;}
	inline P unit(const P& p){return p/abs(p);}
	inline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);}
	inline int ccw(const P &s, const P &t, const P &p, int adv=0){
		int res = sig(outp(t-s, p-s));
		if(res || !adv) return res;
		if(sig(inp(t-s, p-s)) < 0) return -2;	// p-s-t
		if(sig(inp(s-t, p-t)) < 0) return 2;	// s-t-p
		return 0;								// s-p-t
	}
	
	
	struct L : public vector<P>{	// line
		L(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);}
		L(){}
		P dir()const {return at(1) - at(0);}
		BOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));}
	};
	struct S : public L{	// segment
		S(const P &p1, const P &p2):L(p1, p2){}
		S(){}
		BOOL online(const P &p)const {
			if(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER;
			return !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1)));
		}
	};
	struct C : public P{
		C(){}
		C(const P& p, const R r):P(p), r(r){}
		R r;
		BOOL inside(const P& p)const { return less(norm(p-SELF), r*r);}
	};
	struct F : public C{
		R s, t;
		F(const C &c, R ss, R tt):C(c), s(ss), t(tt){
			if(PI < s) s -= 2*PI;
			if(PI < t) t -= 2*PI;
		}
		BOOL inside(const P& p)const {
			P v = p - SELF;
			if(!sig(norm(v))) return BORDER;
			R a = arg(v);
			if(t < s){
				if((!less(s, a) && !less(a, t)) || !less(norm(v), r*r)) return FALSE;
				return less(s, a) | less(a, t) | less(norm(v), r*r);
			}else{
				if(!less(s, a) || !less(a, t) || !less(norm(v), r*r)) return FALSE;
				return less(s, a) | less(a, t) | less(norm(v), r*r);
			}
		}
	};
	P crosspoint(const L &l, const L &m);
	struct G : public vector<P>{
		G(size_type size=0):vector(size){}
		S edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));}
		BOOL contains(const P &p)const {
			R sum = .0;
			REP(i, size()){
				if(S(at(i), at((i+1)%size())).online(p)) return BORDER;	// online
				sum += arg((at(i) - p) / (at((i+1)%size()) - p));
			}
			return !!sig(sum);
		}
		R area()const {
			R sum = 0;
			REP(i, size()) sum += outp(at(i), at((i+1)%size()));
			return abs(sum / 2.);
		}
		P gp()const {
			P r(.0, .0);
			REP(i, size()){
				const S &s = edge(i);
				r += (s[0]+s[1])*outp(s[0], s[1]);
			}
			return r / (6*area());
		}
		G convex_hull(bool online = false) {
			if(size() < 2) return *this;
			sort(ALL(*this));
			G r;
			r.resize((int)size()*2);
			int k=0;
			for(int i=0;i<size();r[k++]=at(i++))
				while(k>1 && ccw(r[k-2], r[k-1], at(i)) < 1-online) k--;
			int t = k;
			for(int i=(int)size()-1;i>=0;r[k++]=at(i--))
				while(k>t && ccw(r[k-2], r[k-1], at(i)) < 1-online) k--;
			r.resize(k-1);
			return r;
		}
		G cut(const L &l)const {
			G g;
			REP(i, size()){
				const S &s = edge(i);
				if(ccw(l[0], l[1], s[0], 0) >= 0) g.push_back(s[0]);
				if(ccw(l[0], l[1], s[0], 0) * ccw(l[0], l[1], s[1], 0) < 0)
					g.push_back(crosspoint(s, l));
			}
			return g;
		}
		G Voronoi(const vector<P> &p, const int t)const {
			G g = *this;
			REP(i, p.size())if(i!=t){
				const P m = (p[t]+p[i])*(R)0.5;
				g = g.cut(L(m, m+(p[i]-p[t])*P(0, 1)));
			}
			return g;
		}
	};

	inline P proj(const P &s, const L &t){return t[0] + proj(s-t[0], t[1]-t[0]);}
	inline P reflect(const P &s, const L &t){return (R)2*proj(s, t) - s;}
	inline S reflect(const S &s, const L &t){return S(reflect(s[0], t), reflect(s[1], t));}
	BOOL intersect(const S &s, const S &t){
		const int p = ccw(t[0], t[1], s[0], 1) * ccw(t[0], t[1], s[1], 1);
		const int q = ccw(s[0], s[1], t[0], 1) * ccw(s[0], s[1], t[1], 1);
		return (p>0||q>0) ? FALSE : (!p||!q) ? BORDER : TRUE;
	}
	BOOL intersect(const S &s, const L &l){
		if(l.online(s[0]) || l.online(s[1])) return BORDER;
		return (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0);
	}
	R dist2(const L &l, const P &p){return norm(outp(l.dir(), p - l[0])) / norm(l.dir());}
	R dist2(const S &s, const P &p){
		if(inp(p-s[0],  s.dir()) < EPS) return norm(p - s[0]);
		if(inp(p-s[1], -s.dir()) < EPS) return norm(p - s[1]);
		return dist2((const L &)s, p);
	}
	R dist2(const S &s, const L &l){
		return intersect(s, l) ? .0 : min(dist2(l, s[0]), dist2(l, s[1]));
	}
	R dist2(const S &s, const S &t){
		return intersect(s, t) ? .0 : min(min(dist2(s, t[0]), dist2(t, s[0])), 
									   	  min(dist2(s, t[1]), dist2(t, s[1])));
	}
	template <class T> R dist2(const G &g, const T& t){ // todo: 内部に完全に含まれる場合
		R res = INF;
		REP(i, g.size()) res = min(res, dist2(g.edge(i), t));
		return res;
	}
	template<class S, class T> R dist(const S& s, const T& t){return sqrt(dist2(s, t));}
	inline BOOL intersect(const C &a, const C &b){
		return less((a.r-b.r)*(a.r-b.r), norm(a-b)) + less(norm(a-b), (a.r+b.r)*(a.r+b.r)) - 1;
	}
	inline BOOL intersect(const C &c, const L &l){
		return less(dist2(l, c), c.r*c.r);
	}
	inline BOOL intersect(const C &c, const S &s){
		int d = less(dist2(s, c), c.r*c.r);
		if(d != TRUE) return d;
		int p = c.inside(s[0]), q = c.inside(s[1]);
		return (p<0 || q<0) ? BORDER : p&q;
	}
	
	/*
	 * CCWでs[0]->s[1]にかけてが共通部分
	 */
	inline S crosspoint(const C &c1, const C &c2){
		if(!intersect(c1, c2)) return S();
		R d = abs(c1 - c2);
		R x = (c1.r*c1.r - c2.r*c2.r + d*d) / (2*d);
		R h = sqrt(max((R)0., c1.r*c1.r - x*x));
		P u = unit(c2-c1);
		return S(c1 + u*x + u*P(0,-1)*h, c1 + u*x + u*P(0,1)*h);
	}
	inline P crosspoint(const L &l, const L &m){
		R A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]);
		if(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line
		if(abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!!
		return m[0] + B / A * (m[1] - m[0]);
	}
	inline S crosspoint(const C &c, const L &l){
		R d2 = dist2(l, c);
		if(c.r*c.r+EPS < d2) return S();
		P m = proj(c, l);
		P u = unit(l[1]-l[0]);
		R d = sqrt(max((R).0, c.r*c.r - d2));
		return S(m+u*d, m-u*d);
	}
	inline vector<P> crosspoint(const C &c, const S &s){
		vector<P> res = crosspoint(c, (const L&)s);
		RREP(i, res.size()){
			if(inp(res[i]-s[0], s.dir())*inp(res[i]-s[1], -s.dir())<-EPS)
				res.erase(res.begin() + i);
		}
		return res;
	}

#undef SELF
#undef at
}

using namespace geom;

int f = 0;
namespace std{
	bool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;}
	bool operator==(const P &a, const P &b){return abs(a-b) < EPS;}
	istream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;}
	istream& operator>>(istream &is, L &l){l.resize(2);return is >> l[0] >> l[1];}
	istream& operator>>(istream &is, C &c){return is >> (P &)c >> c.r;}
	const R B = 200;
	const R Z = 30;
	ostream& operator<<(ostream &os, const C &c){return os << "circle("<<B+Z*(c.X)<<", "<<1000-B-Z*(c.Y)<<", "<<Z*(c.r)<<")";}
	ostream& operator<<(ostream &os, const P &p){return os << C(p, 2./Z);}
	ostream& operator<<(ostream &os, const S &s){return os << "line("<<B+Z*(s[0].X)<<", "<<1000-B-Z*(s[0].Y)<<", "<<B+Z*(s[1].X)<<", "<<1000-B-Z*(s[1].Y)<<")";}
	ostream& operator<<(ostream &os, const G &g){REP(i, g.size()) cout << g.edge(i) << endl;return os;}

}

int n;
G g;

R dist(P p){
	if(g.contains(p) == TRUE) return INF;
	if([&](){
		REP(j, 4) if(intersect(g.edge(j), S(p, P(0, 0))) == TRUE) return 0;
		return 1;
	}()) return abs(p);
	vector<vector<R>> d(6, vector<R>(6, INF));
	REP(i, 6) d[i][i] = 0;
	REP(i, 4){
		d[i][(i+1)&3] = d[(i+1)&3][i] = abs(g.edge(i).dir());
		if([&](){
			REP(j, 4){
				if(intersect(g.edge(j), S(p, g[i])) == TRUE) return 0;
				if(g.contains((p + g[i])*(R).5) == TRUE) return 0;
			}
			return 1;
		}()) d[i][4] = d[4][i] = abs(g[i] - p);
		if([&](){
			REP(j, 4) if(intersect(g.edge(j), S(P(0, 0), g[i])) == TRUE) return 0;
			return 1;
		}()) d[i][5] = d[5][i] = abs(P(0, 0) - g[i]);
	}
	REP(k, 6)REP(i, 6)REP(j, 6) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
	return d[4][5];
}
R len;
P p1, p2;
int main(){
	ios::sync_with_stdio(false);
	while(cin >> len >> p1 >> p2, len > EPS){
		g.resize(4);
		g[0] = p1;
		g[1] = P(p2.X, p1.Y);
		g[2] = p2;
		g[3] = P(p1.X, p2.Y);
		vector<C> c;
		c.emplace_back(P(0, 0), len);
		REP(i, 4)if(dist(g[i]) < len)
			c.emplace_back(g[i], len - dist(g[i]));
		vector<P> p;
		REP(i, c.size()){
			REP(j, i){
				auto x = crosspoint(c[i], c[j]);
				p.insert(p.end(), ALL(x));
			}
			REP(j, 4){
				auto x = crosspoint(c[i], g.edge(j));
				p.insert(p.end(), ALL(x));
			}
		}
		sort(ALL(p));UNIQUE(p);
		R ans = .0;
		REP(i, c.size()){
			vector<R> args;
			REP(j, p.size()) if(!sig(norm(c[i] - p[j]) - c[i].r*c[i].r))
				args.push_back(arg((p[j]-c[i])));
			sort(ALL(args));
			if(args.empty()) ans += 2*PI * c[i].r;
			REP(j, args.size()){
				R s = args[j];
				R t = (j+1 == args.size()) ? args[0]+2*PI : args[j+1];
				if(!sig(dist(c[i]+polar(c[i].r, s)) - len) && 
				   !sig(dist(c[i]+polar(c[i].r, t)) - len) && 
				   !sig(dist(c[i]+polar(c[i].r, (s+t)*.5)) - len)){
				   ans += (t-s) * c[i].r;
				}
			}
		}
		printf("%.6f\n", (double)ans);
	}
	return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 1328
final_rank: 3

code_snippet: #include <bits/stdc++.h>

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) begin(x),end(x)

using namespace std;

typedef long double ld;
typedef complex<ld> P;
typedef vector<P> VP;
const ld eps = 1e-9, pi = acos((ld)-1.0), INF = 1e+8;

#define EQ(a,b) (abs((a)-(b))<eps)

ld dot (P a, P b) { return real(conj(a) * b); }
ld cross (P a, P b) { return imag(conj(a) * b); }

namespace std {
  bool operator<(const P &lhs, const P &rhs) {
    return lhs.real() == rhs.real() ? lhs.imag() < rhs.imag() : lhs.real() < rhs.real();
  }
}

// Line
class L{
public:
  P a, b;
  L (P aa, P bb) { a = aa; b = bb; }
  L (ld ax, ld ay, ld bx, ld by) { a = P(ax, ay); b = P(bx, by); }
};

// Circle
class C { public: P p; ld r; };

// counter clockwise
int ccw (P a, P b, P c) {
  b -= a; c -= a;
  if (cross(b, c) > eps) return 1;   // counter clockwise
  if (cross(b, c) < -eps) return -1; // clockwise
  if (dot(b, c) < 0) return 2;       // c--a--b on line
  if (norm(b) < norm(c)) return -2;  // a--b--c on line
  return 0;                          // a--c--b on line
}

bool isis_ss(L s, L t) {
  return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) < 0 &&
         ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) < 0;
}

P is_ll(L s, L t){
  P sv = s.b - s.a, tv = t.b - t.a;
  return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);
}

VP is_cc(C c1, C c2){
  VP res;
  ld d = abs(c1.p - c2.p);
  ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);
  ld dfr = c1.r * c1.r - rc * rc;
  if (abs(dfr) < eps) dfr = 0.0;
  else if (dfr < 0.0) return res; // no intersection
  ld rs = sqrt(dfr);
  P diff = (c2.p-c1.p)/d;
  res.push_back(c1.p + diff * P(rc, rs));
  if (dfr != 0.0) res.push_back(c1.p + diff * P(rc, -rs));
  return res;
}

P proj(L l, P p) {
  ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
  return l.a + t * (l.a - l.b);
}

ld dist_lp(L l, P p) {
  return abs(p - proj(l, p));
}

VP is_lc(C c, L l){
  VP res;
  ld d = dist_lp(l, c.p);
  if (d < c.r + eps){
    ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;
    P nor = (l.a - l.b) / abs(l.a - l.b);
    res.push_back(proj(l, c.p) + len * nor);
    res.push_back(proj(l, c.p) - len * nor);
  }
  return res;
}

bool isis_sp(L s, P p) {
  return abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps;
}

VP is_sc(C c, L l){
  VP v = is_lc(c, l), res;
  for (P p : v)
    if (isis_sp(l, p)) res.push_back(p);
  return res;
}

vector<L> house;
vector<P> hp;
ld dd[5][5];
ld x[2],y[2];

bool is_ok(P p, P o=P()) {
  REP(i,4) if (isis_ss(house[i], L(p, o))) return false;
  return true;
}

bool is_in_h(P p) {
  return x[0] < real(p) && real(p) < x[1] &&
    y[0] < imag(p) && imag(p) < y[1];
}

ld dist(P p) {
  ld minl = INF;
  if(is_in_h(p)) return INF;
  REP(i,5) if(is_ok(p,hp[i])) minl=min(minl,dd[0][i]+abs(p-hp[i]));
  return minl;
}

int main() {
  random_device rg;
  mt19937 mt(rg());
  while(1){
    house.clear(); hp.clear();
    ld l;
    cin>>l>>x[0]>>y[0]>>x[1]>>y[1];
    if(!l) break;
    REP(i,2)REP(j,2) hp.emplace_back(x[i],y[j]);
    REP(i,5)REP(j,5) dd[i][j] = INF;
    REP(i,2){
      house.emplace_back(hp[i],hp[i+2]);
      house.emplace_back(hp[i*2],hp[i*2+1]);
      dd[i+1][i+3] = x[1]-x[0];
      dd[i*2+1][i*2+2] = y[1]-y[0];
    }
    REP(i,4) if(is_ok(hp[i])) dd[0][i+1] = abs(hp[i]);
    hp.insert(begin(hp),P());
    REP(i,5)REP(j,i) dd[i][j] = dd[j][i];
    REP(i,5) dd[i][i]=0;
    REP(k,5)REP(i,5)REP(j,5) dd[i][j]=min(dd[i][j],dd[i][k]+dd[k][j]);
    vector<C> vc;
    REP(i,5) vc.push_back((C){hp[i], l-dd[0][i]});
    VP crs;
    REP(i,5)REP(j,i){
      auto v=is_cc(vc[i],vc[j]);
      crs.insert(end(crs),ALL(v));
    }
    REP(i,5)REP(j,4){
      auto v=is_sc(vc[i],house[j]);
      crs.insert(end(crs),ALL(v));
    }
    ld res = 0;
    REP(i,5){
      vector<ld> va;
      REP(j,crs.size())
        if(abs(abs(crs[j]-hp[i])-vc[i].r) < eps)
          va.emplace_back(arg(crs[j]-hp[i]));
      sort(ALL(va));
      REP(j,va.size()-1){
        uniform_real_distribution<ld> dis(va[j],va[j+1]);
        ld rad = va[j+1]-va[j];
        P p=polar(vc[i].r, dis(mt))+hp[i];
        if (abs(dist(p)-l) < eps) res += rad*vc[i].r;
      }
      if(va.size()>=2){
        uniform_real_distribution<ld> dis(va.back(),va.front()+pi*2);
        ld rad = pi*2-(va.back()-va.front());
        assert(rad>=0);
        P p=polar(vc[i].r, dis(mt))+hp[i];
        if (abs(dist(p)-l) < eps) res += rad*vc[i].r;
      }
    }
    cout<<setprecision(8)<<fixed<<res<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 1364
final_rank: 4

code_snippet: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <cmath>
#include <complex>
#include <cfloat>
using namespace std;

typedef long double D;
typedef complex<D> P;

const D EPS = 1e-9;

D dot(P va, P vb){
	return real(va) * real(vb) + imag(va) * imag(vb);
}

D cross(P va, P vb){
	return real(va) * imag(vb) - imag(va) * real(vb);
}

bool check_angle(D ag, D rg[]){
	return rg[0] - EPS < ag && ag < rg[1] + EPS;
}

void intrcscs(P p1, D r1, D rg1[], P p2, D r2, D rg2[], vector<P> &ret){
	D d = abs(p2 - p1);
	D phi = arg(p2 - p1);
	
	D costheta = (d * d + r1 * r1 - r2 * r2) / (2.0 * d * r1);
	if(abs(costheta) > 1.0){
		if(abs(costheta) < 1.0 + EPS){
			costheta /= abs(costheta);
		}
		else{
			return;
		}
	}
	
	D theta = acos(costheta);

	P pt = p1 + polar(r1, phi + theta);
	if(check_angle(arg(pt - p1), rg1) && check_angle(arg(pt - p2), rg2)){
		ret.push_back(pt);
	}
	pt = p1 + polar(r1, phi - theta);
	if(check_angle(arg(pt - p1), rg1) && check_angle(arg(pt - p2), rg2)){
		ret.push_back(pt);
	}
}

void intrlsc(P p1, P p2, P pc, D r, vector<P> &ret){
	P v12 = p2 - p1;
	P v1c = pc - p1;

	D a = norm(v12);
	D b = -dot(v12, v1c);
	D c = norm(v1c) - r * r;

	D d = b * b - a * c;
	if(d < 0.0 && d > -EPS){
		d = 0.0;
	}
	if(d < 0.0){
		if(d > -EPS){
			d = 0.0;
		}
		else{
			return;
		}
	}

	d = sqrt(d);
	D t1 = (-b + d) / a;
	D t2 = (-b - d) / a;
	
	if(-EPS < t1 && t1 < 1.0 + EPS){
		ret.push_back(p1 + v12 * t1);
	}
	if(-EPS < t2 && t2 < 1.0 + EPS){
		ret.push_back(p1 + v12 * t2);
	}
}


struct info{
	D rem, range[2];
	P cen;

	info() : rem(-1.0){
		range[0] = range[1] = 0.0;
	}
	info(D rm, D rl, D rr, P c) : rem(rm), cen(c){
		range[0] = rl;
		range[1] = rr;
	}
};


D pi = acos(-1.0L);


D solve(D len){
	const D inf = 1e100;
	D x1, y1, x2, y2;
	cin >> x1 >> y1 >> x2 >> y2;

	info ropes[2], nropes[2];
	D add[2];
	vector<P> plg(4);

	plg[0] = P(x1, y1);
	plg[1] = P(x2, y1);
	plg[2] = P(x2, y2);
	plg[3] = P(x1, y2);
	
	P brot = conj(plg[0]) / abs(plg[0]);
	for(int i = 0; i < 4; ++i){
		plg[i] *= brot;
	}

	ropes[0] = info(len, -pi, pi, P());
	ropes[1] = info(len, -pi, pi, P());

	D ans = 0.0;
	while(max(ropes[0].rem, ropes[1].rem) > EPS){
		for(int k = 0; k < 2; ++k){
			info &rp = ropes[k];
			info &nx = nropes[k];
			nx = info();
			add[k] = 0.0;

			if(rp.rem < EPS){ continue; }

			vector<P> cand = plg;
			
			if(ropes[0].rem < len && ropes[1].rem < len){
				intrcscs(
					ropes[0].cen, ropes[0].rem, ropes[0].range,
					ropes[1].cen, ropes[1].rem, ropes[1].range, cand
				);
			}
			for(int i = 0; i < 4; ++i){
				intrlsc(plg[i], plg[(i + 1) & 3], rp.cen, rp.rem, cand);
			}
			for(int i = 0; i < 2; ++i){
				cand.push_back(ropes[i].cen);
			}

			int minidx = -1;
			D minangle = (k ? -inf : inf);
			D maxdist = -inf;

			for(int i = cand.size(); i--; ){
				D ds = abs(cand[i] - rp.cen);
				D ag = arg(cand[i] - rp.cen);
				
				bool update = false;
				if(	ds > EPS && ds < rp.rem + EPS && check_angle(ag, rp.range)){
					if(abs(minangle - ag) < EPS){
						update = maxdist < ds;
					}
					else{
						update = (minangle > ag) ^ k;
					}
				}

				if(update){
					minidx = i;
					minangle = ag;
					maxdist = ds;
				}
			}

			if(minidx == -1){
				return 2.0 * pi * len;
			}

			add[k] = rp.rem * abs(minangle - rp.range[k]);

			if(minidx < 4){
				nx.range[k] = minangle;
				nx.range[!k] = arg(plg[(minidx + (k ? 3 : 1)) & 3] - plg[minidx]);
				nx.rem = rp.rem - maxdist;
				nx.cen = cand[minidx];
			}
			else{
				nx.rem = 0.0;
				nx.range[0] = nx.range[1] = 0.0;
				nx.cen = cand[minidx];
			}
		}

		int updidx = (nropes[0].rem < nropes[1].rem);
		ans += add[updidx];
		ropes[updidx] = nropes[updidx];
	}
	return ans;
}


int main(){
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(8);

	D len;
	while(cin >> len, len != 0.0){
		D ans = solve(len);
		cout << ans << '\n';
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1332
final_rank: 1

code_snippet: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <cassert>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
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()
#define chmax(a,b) (a<(b)?(a=b,1):0)
#define chmin(a,b) (a>(b)?(a=b,1):0)
#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)
const int INF = 1<<29;
const double EPS = 1e-8;
const double PI = acos(-1);
typedef pair<int,int> pii;
typedef long long ll;

typedef complex<double> P;
P pIN() { double x,y; cin >> x >> y; return P(x,y); }
namespace std {
  bool operator < (const P& a, const P& b) {
    // if (abs(a-b)<EPS) return 0;
    return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
  }
bool operator==(const P &a, const P &b) {
  return abs(a-b)<EPS;
}

}
double cross(const P& a, const P& b) {
  return imag(conj(a)*b);
}
double dot(const P& a, const P& b) {
  return real(conj(a)*b);
}
int ccw(P a, P b, P c) {
  b -= a; c -= a;
  if (cross(b, c) > EPS)   return +1;       // counter clockwise
  if (cross(b, c) < -EPS)   return -1;       // clockwise
  if (dot(b, c) < -EPS)     return +2;       // c--a--b on line
  if (EPS < norm(b) && norm(b) < norm(c)-EPS) return -2; // a--b--c on line
  return 0;
}
struct L : public vector<P> {
  L(const P &a, const P &b) {
    push_back(a); push_back(b);
  }
  L() { resize(2); }
};
ostream &operator<<(ostream &os, const L &a) {
  os << a[0] << " -> " << a[1];
  return os;
}
typedef vector<P> G;
#define curr(g, i) g[i]
#define next(g, i) g[(i+1)%g.size()]
L line(const G &g, int i) {
  return L(g[i],g[(i+1)%g.size()]);
}
struct C {
  P p; double r;
  C(const P &p, double r) : p(p), r(r) { }
  C() {}
};

struct CS : C {
  P p1, p2;
  CS(const P &p, const P &p1, const P &p2):
    p1(p1),p2(p2) {
    this->p = p;
    this->r = abs(p1);
  }
  L l1() { return L(p,p+p1); }
  L l2() { return L(p,p+p2); }
};

P projection(const L &l, const P &p) {
  double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);
  return l[0] + t*(l[0]-l[1]);
}
P reflection(const L &l, const P &p) {
  return p + P(2,0) * (projection(l, p) - p);
}

vector<P> crosspointLC(const L &l, const C &c) {
  vector<P> ret;
  P center = projection(l, c.p);
  double d = abs(center - c.p);
  double t = sqrt(c.r * c.r - d * d);
  if (::isnan(t)) { return ret; }
  P vect = (l[1] - l[0]);
  vect /= abs(vect);
  ret.push_back(center - vect * t);
  if (t > EPS) {
    ret.push_back(center + vect * t);
  }
  return ret;
}

vector<P> crosspointCC(const C &c1, const C &c2) {
  vector<P> ret;
  double d = abs(c1.p - c2.p);
  if (max(c1.r, c2.r) - min(c1.r, c2.r) - d> -EPS) { return ret; }
  double x = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);
  P start = c1.p + (c2.p - c1.p) / d * x;
  P vect = (c1.p - c2.p) * P(0.0, 1.0);
  return crosspointLC(L(start, start + vect), c1);
}
// テ」ツδ凖」ツつッテ」ツδ暗」ツδォテッツスツ?」ツ?凝」ツつ嘉」ツ?ソテ」ツ?淌」ツδ凖」ツつッテ」ツδ暗」ツδォテッツスツづ」ツ?ョティツァツ津・ツコツヲテ」ツ?ョ[0,2pi)
double angle(const P &a, const P &b) {
  double ret = arg(b)-arg(a);
  return (ret>=0) ? ret : ret + 2*PI;
}
// テ」ツδ凖」ツつッテ」ツδ暗」ツδォaテ」ツ?ィテ」ツδ凖」ツつッテ」ツδ暗」ツδォbテ」ツ?ョテゥツ鳴禿」ツ?ョティツァツ津・ツコツヲ
double angle2(const P &a, const P &b) {
  return min(angle(a,b), angle(b,a));
}

bool intersectCSP(const CS &cs, const P &p) {
  return (angle(cs.p1, p-cs.p) <= angle(cs.p1, cs.p2)) &&
    (abs(cs.p-p) <= cs.r+EPS);
}
vector<P> crosspointCSCS(const CS &c1, const CS &c2) {
  vector<P> r = crosspointCC(c1,c2);
  vector<P> res;
  FOR(it, r)
    if (intersectCSP(c1, *it) &&
        intersectCSP(c2, *it))
      res.push_back(*it);
  return res;
}
P rotate(P p, double ang) {
  return p * P(cos(ang), sin(ang));
}
// テ・ツ篠淌ァツつケテ・ツ堕ィテ」ツつ甘」ツ?ョテァツ崢エテァツキツ堙」ツ?ョテ・ツ崢榲ィツサツ「
L rotate(L l, double ang) {
  return L(rotate(l[0], ang),rotate(l[1], ang));
}

FILE *fp;
double scale = 10;
P diff(300, 300);
double WH = 500;
void outputLine(const L &l) {
  if (!fp) return;
  P a = l[0];
  P b = l[1];
  a = a*P(scale,0)+diff;
  b = b*P(scale,0)+diff;
  fprintf(fp,"line(%f,%f,%f,%f);\n",a.real(),WH-a.imag(),b.real(),WH-b.imag());  
}
void outputCircle(C c) {
  if (!fp) return;
  c.p = c.p*P(scale,0) + diff;
  c.r *= scale;
  fprintf(fp, "circle(%f,%f,%f);\n", c.p.real(), WH-c.p.imag(), c.r);
}
void outputCS(const CS &c) {
  if (!fp) return;
  double ang = angle(c.p1,c.p2);
  const int N = 50;
  REP(i,N) {
    double a = ang*i/N;
    double b = ang*(i+1)/N;
    outputLine(L(c.p+rotate(c.p1,a), c.p+rotate(c.p1,b)));
  }
  outputLine(L(c.p,c.p+c.p1));
  outputLine(L(c.p,c.p+c.p2));
}
bool intersectSP(const L &s, const P &p) {
  return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality
}

vector<P> crosspointSC(const L &s, const C &c) {
  vector<P> ret;
  vector<P> nret = crosspointLC(s, c);
  FOR(it, nret)
    if (intersectSP(s, *it))
      ret.push_back(*it);
   return ret;
}
double len;
G g;
vector<CS> solve(int t) {
  L s(P(0,0),P(0,-len));
  vector<CS> res;
  REP(_,4) {
    double minAng = 100;
    P nxt1,nxt2;
    bool f = 0;    
    REP(i,4) {
      if (s[0]==g[i]) continue;
      if (abs(g[i]-s[0])<abs(s[1]-s[0])) {
        double ag = angle(s[1]-s[0],g[i]-s[0]);
        if (t == -1) ag = angle(g[i]-s[0],s[1]-s[0]);
        if (chmin(minAng, ag)) {
          nxt1 = g[i];
          nxt2 = g[i]-s[0];
          nxt2 *= P((abs(s[1]-s[0]) - abs(g[i]-s[0]))/abs(nxt2),0);
          nxt2 += nxt1;
          f = 0;
        }
      }
    }
    REP(i,4) {
      vector<P> ps = crosspointSC(line(g,i),
                                  C(s[0],abs(s[1]-s[0])));
      FOR(it, ps) {
        double ag = angle(s[1]-s[0],*it-s[0]);
        if (t == -1) ag = angle(*it-s[0],s[1]-s[0]);
        if (chmin(minAng, ag)) {
          nxt2 = *it;
          f = 1;
        }
      }
    }
    if (minAng == 100) {
      f = 1;
      nxt2 = P(0,len);
    }
    if (t == -1) 
      res.push_back(CS(s[0],nxt2-s[0],s[1]-s[0]));
    else
      res.push_back(CS(s[0],s[1]-s[0],nxt2-s[0]));
    if (f) break;
    s[0] = nxt1;
    s[1] = nxt2;
  }
  return res;
}

int main() {
  // fp = fopen("data.js", "w");
  double x1,x2,y1,y2;
  while(cin >> len, len) {
    cin >> x1 >> y1;
    cin >> x2 >> y2;
    if (y1 < 0) {
      y1 = -y1;
      y2 = -y2;
      swap(x1,x2);
      swap(y1,y2);
    }
    if (y1 < 0) {
      if (x1 < 0) {
        swap(x1,y1); y1 = -y1;
        swap(x2,y2); y2 = -y2;
      } else {
        swap(x1,y1); x1 = -x1;
        swap(x2,y2); x2 = -x2;
      }
    }
    if (y1 > y2) swap(y1,y2);
    if (x1 > x2) swap(x1,x2);
    g.resize(4);
    g[0] = P(x1,y1);
    g[1] = P(x2,y1);
    g[2] = P(x2,y2);
    g[3] = P(x1,y2);
    vector<CS> v1 = solve(1);
    vector<CS> v2 = solve(-1);

    outputLine(L(P(-1000,0),P(1000,0)));
    outputLine(L(P(0,-1000),P(0,1000)));
    REP(i,4) {
      outputLine(line(g,i));
    }
    REP(i,v1.size()) {
      outputCS(v1[i]);
    }
    REP(i,v2.size()) {
      outputCS(v2[i]);
    }
    
    
    int A=-1,B;
    P cp;
    int hoge = INF;
    REP(i,v1.size()) {
      REP(j,v2.size()) {
        vector<P> v = crosspointCSCS(v1[i],v2[j]);
        if (v.size() == 1 && hoge > i+j) {
          hoge = i+j;
          cp = v[0];
          A = i, B = j;
        }
      }
    }
    double ans = 0;
    if (A >= 0) {
      REP(i,A)
        ans += v1[i].r * angle(v1[i].p1, v1[i].p2);
      REP(i,B)
        ans += v2[i].r * angle(v2[i].p1, v2[i].p2);
      ans += v1[A].r * angle(v1[A].p1, cp-v1[A].p);
      ans += v2[B].r * angle(cp-v2[B].p, v2[B].p2);
    } else {
      REP(i,v1.size())
        ans += v1[i].r * angle(v1[i].p1, v1[i].p2);
      REP(i,v2.size())
        ans += v2[i].r * angle(v2[i].p1, v2[i].p2);
    }
    printf("%.8f\n", ans);
    
  }
}
accuracy: 1
time_ms: 10
memory_kb: 1356
final_rank: 2