Problem:
Problem E:
Twirl Around
Let's think about a bar rotating clockwise as if it were a
twirling baton moving on a planar surface surrounded by a
polygonal wall (see Figure 1).
Figure 1.  A bar rotating in a polygon
Initially, an end of the bar (called "end A") is at (0,0), and
the other end (called "end B") is at (0,
L
) where
L
is the
length of the bar.  Initially, the bar is touching the wall only
at the end A.
The bar turns fixing a touching point as the center.
The center changes as a new point touches the wall.
Your task is to calculate the coordinates of the end A when the
bar has fully turned by the given count
R
.
Figure 2.  Examples of turning bars
In Figure 2, some examples are shown.  In cases (D) and
(E), the bar is stuck prematurely (cannot rotate clockwise anymore 
with any point touching the wall as the center)
before
R
rotations.  In such cases, you should answer the
coordinates of the end A in that (stuck) position.
You can assume the following:
When the bar's length
L
changes by ε (|ε| <
0.00001), the final (
x
,
y
) coordinates
will not change more than 0.0005.
Input
The input consists of multiple datasets.  The number of
datasets is no more than 100.  The end of the input is represented
by "0 0 0".
The format of each dataset is as follows:
L
R
N
X
1
Y
1
X
2
Y
2
...
X
N
Y
N
L
is the length of the bar.
The bar rotates 2π×
R
radians (if it is not
stuck prematurely).
N
is the number of vertices which make the polygon.
The vertices of the polygon are arranged in a counter-clockwise order.
You may assume that the polygon is
simple
, that is,
its border never crosses or touches itself.
N
,
X
i
and
Y
i
are
integer numbers;
R
and
L
are decimal fractions.
Ranges of those values are as follows:
1.0 ≤
L
≤ 500.0,
1.0 ≤
R
≤ 10.0,
3 ≤
N
≤ 100,
-1000 ≤
X
i
≤ 1000,
-1000 ≤
Y
i
≤ 1000,
X
1
≤ -1,
Y
1
= 0,
X
2
≥ 1,
Y
2
= 0.
Output
For each dataset, print one line containing
x-
and
y-
coordinates of the final position of the end A,
separated by a space.
The value may contain an error less than or equal to 0.001.
You may print any number of digits after the decimal point.
Sample Input
4.0 2.0 8
-1 0
5 0
5 -2
7 -2
7 0
18 0
18 6
-1 6
4.0 2.0 4
-1 0
10 0
10 12
-1 12
4.0 1.0 7
-1 0
2 0
-1 -3
-1 -8
6 -8
6 6
-1 6
4.0 2.0 6
-1 0
10 0
10 3
7 3
7 5
-1 5
5.0 2.0 6
-1 0
2 0
2 -4
6 -4
6 6
-1 6
6.0 1.0 8
-1 0
8 0
7 2
9 2
8 4
11 4
11 12
-1 12
0 0 0
Output for the Sample Input
16.0 0.0
9.999999999999998 7.4641016151377535
0.585786437626906 -5.414213562373095
8.0 0.0
6.0 0.0
9.52786404500042 4.0
Note that the above sample input corresponds to the
cases in Figure 2.  For convenience, in Figure 3, we will show an
animation and corresponding photographic playback for the case (C).
Figure 3.  Animation and photographic playback for case (C)

Submitted solutions:

code_snippet: #include<cstdio>
#include<cmath>
#include<utility>
#include<vector>
#include<complex>
#include<iostream>
#include<algorithm>
  
using namespace std;
  
typedef long double Real;
typedef complex<Real> Point;
typedef complex<Real> Vector;
typedef pair<Point,Point> Line;
typedef pair<Point,Point> Segment;
typedef vector<Point> Polygon;
  
const Real eps=1e-9;
const Real PI=acos(-1.0);
const Real inf=1e9;
  
const Real eps2=1e-6;
  
const Point NPoint=Point(NAN,NAN);
  
void print(Point p,char ch='\n'){
    printf("(%f %f)%c",p.real(),p.imag(),ch);
}
  
template<class T> bool eq(T a,T b){
    return abs(a-b)<eps;
}
template<class T> int sgn(T a){
    if(eq(a,(Real)0.0)) return 0;
    if(a>0) return 1;
    return -1;
}
template<class T> bool eq2(T a,T b){
    return abs(a-b)<eps2;
}
template<class T> int sgn2(T a){
    if(eq2(a,0.0)) return 0;
    if(a>0) return 1;
    return -1;
}
  
bool onSeg(Point a,Point b,Point c){
    return eq(abs(a-b),abs(a-c)+abs(c-b));
}
  
bool onSeg(Segment s,Point p){
    return onSeg(s.first,s.second,p);
}
  
Real doP(Vector a,Vector b){
    return (conj(a)*b).real();
}
  
Real crP(Vector a,Vector b){
    return (conj(a)*b).imag();
}
  
Vector proj(Vector p,Vector b){
    return b*doP(p,b)/norm(b);
}
  
Point perp(Line l,Point a){
    Point p=l.first,q=l.second;
    return p+proj(a-p,q-p);
}
  
bool isPara(Vector a,Vector b){
    return eq(crP(a,b),(Real)0);
}
  
bool isPara(Line l1,Line l2){
    return isPara(l1.second-l1.first,l2.second-l2.first);
}
  
Real arg(Point p){
    return atan2(p.imag(),p.real());
}
  
Real normalize(Real x){//[-PI,PI)
    while(x<-PI) x+=PI*2;
    while(x>=PI) x-=PI*2;
    return x;
}
  
struct Circle:vector<Real>{
    Point c;
    Real r;
    Circle(){}
    Circle(Point c,Real r):c(c),r(r){}
};
  
Point getP(Point c,Real r,Real ang){
    return c+r*Point(cos(ang),sin(ang));
}
  
void iCL(Circle &c_,Line l){//tangent: not intersect
    //lが中心を通る場合!!!
    Point c=c_.c;
    Real r=c_.r;
    Point p=l.first,q=l.second;
    Point h=perp(l,c);
    Real d=abs(h-c);
    if(sgn(d-r)>=0) return;
    Real theta=arg(h-c);
    Real phi=acos(d/r);
    c_.push_back(normalize(theta-phi));
    c_.push_back(normalize(theta+phi));
}
  
void iCS(Circle &c_,Segment s){
//  printf("iCS\n");
//  print(c_.c,' ');
//  printf("%f\n",c_.r);
//  print(s.first,' ');
//  print(s.second);
    Point c=c_.c;
    Real r=c_.r;
    Point p=s.first,q=s.second;
    Point h=perp(s,c);
    Real d=abs(h-c);
    if(sgn(d-r)>=0) return;
    if(eq(d,(Real)0)){
        Point dir=s.second-s.first;
        dir/=abs(dir);
        Point p1=c+r*dir;
        Point p2=c-r*dir;
//      printf("in iCS\n");
//      print(p1);
//      print(p2);
        if(onSeg(p,q,p1)) c_.push_back(arg(dir));
        if(onSeg(p,q,p2)) c_.push_back(arg(dir)-PI);
        return;
    }
    Real theta=arg(h-c);
    Real phi=acos(d/r);
    Point p1=getP(c,r,theta-phi);
    if(onSeg(p,q,p1)) c_.push_back(theta-phi);
    Point p2=getP(c,r,theta+phi);
    if(onSeg(p,q,p2)) c_.push_back(theta+phi);
}
  
Segment shrink(Segment s){
    Segment res;
    res.first=s.first+(s.second-s.first)*eps;
    res.second=s.second+(s.first-s.second)*eps;
    return res;
}
  
Polygon poly;
  
vector<Point> getTouchingPoints(Segment s){
    vector<Point> res;
    for(int i=0;i<poly.size();i++){
        if(onSeg(s,poly[i])) res.push_back(poly[i]);
    }
    for(int i=0;i<poly.size();i++){
        Point p=poly[i],q=poly[i+1==poly.size()?0:i+1];
        if(onSeg(p,q,s.first)) res.push_back(s.first);
        if(onSeg(p,q,s.second)) res.push_back(s.second);
    }
    return res;
}
  
Real getAngDis(Real from,Real to,bool rev=false){
    if(rev) return getAngDis(to,from);
    Real res=to-from;
    while(res<0) res+=PI*2;
    return res;
}
  
Real getRotateAngle(Point p,Point q){
//  printf("getRotAng");
//  print(p,' ');
//  print(q);
    vector<Point> pts;
    Real r=abs(p-q);
    if(eq(r,(Real)0)) return -1;
    Circle c=Circle(p,r);
    for(int i=0;i<poly.size();i++){
        Point p=poly[i],q=poly[i+1==poly.size()?0:i+1];
        Segment s_=Segment(p,q);
        Segment s=shrink(s_);
        if(sgn(abs(s.first-c.c)-c.r)<0) pts.push_back(s.first);
        if(sgn(abs(s.second-c.c)-c.r)<0) pts.push_back(s.second);
        iCS(c,s);
    }
    for(int i=0;i<c.size();i++){
        pts.push_back(getP(c.c,c.r,c[i]));
    }
//  for(int i=0;i<pts.size();i++){
//      print(pts[i]);
//  }
    Point nxt;
    Real ang=PI*3;
    Real init_dir=arg(q-p);
    for(int i=0;i<pts.size();i++){
        if(eq(pts[i],p)) continue;
        Real dir=arg(pts[i]-p);
        Real cur=getAngDis(init_dir,dir,true);
        if(cur<ang){
            Real tmp=getAngDis(init_dir,arg(pts[i]-p),true);
            //if(sgn(tmp)<=0) continue;
            if(tmp<(eps2)) continue;
            ang=cur;
            nxt=pts[i];
        }
    }
    return getAngDis(init_dir,arg(nxt-p),true);
}
  
//bool checkNonzero(Segment s,Point p);
  
bool checkValidity(Segment s,Point c,Real ang);

Real getRotateAngle(Segment s,Point c){
//    bool flg=checkNonzero(s,c);
//    if(!flg){
//      printf("check = false\n");
//        return 0;
//    }
    Real res=PI*3;
    if(!eq(s.first,c)){
        Real cur=getRotateAngle(c,s.first);
        res=min(res,cur);
    }
    if(!eq(s.second,c)){
        Real cur=getRotateAngle(c,s.second);
        res=min(res,cur);
    }
	bool flg=checkValidity(s,c,res);
	if(!flg) return 0;
    return res;
}
  
Point rotatePoint(Point p,Point c,Real ang){
    return c+(p-c)*(Point(cos(-ang),sin(-ang)));
}
  
Segment rotateSegment(Segment s,Point c,Real ang){
    s.first=rotatePoint(s.first,c,ang);
    s.second=rotatePoint(s.second,c,ang);
    return s;
}
  
Point iLL(Line l1,Line l2){
    if(isPara(l1,l2)) return NPoint;
    Point a1=l1.first,a2=l1.second;
    Point b1=l2.first,b2=l2.second;
    Real num=crP(b1-a1,a2-a1);
    Real den=crP(a2-a1,b2-b1);
    return b1+(b2-b1)*(num/den);
}
  
Point iSS(Segment s1,Segment s2){
    Point p=iLL(s1,s2);
    if(isnan(p.real())) return p;
    if(onSeg(s1,p)&&onSeg(s2,p)) return p;
    return NPoint;
}
  
const Real eps_rot=1e-6;
  
Point rot(Point p,Point q,Real a){
    return p+(q-p)*(Point(cos(a),sin(a)));
}
  
bool inPoly_(Polygon poly,Point pt){//ON is IN
    bool in=false;
    for(int i=0;i<poly.size();i++){
        Point a=poly[i],b=poly[(i+1)%poly.size()];
        a-=pt,b-=pt;
//      if(a.imag()>b.imag()) swap(a,b);
        if(sgn(a.imag()-b.imag())>0) swap(a,b);
//      if(a.imag()<=0&&0<b.imag()){
        if(sgn(a.imag())<=0&&sgn(b.imag())>0){
//          if(crP(a,b)<0) in=!in;
            int s=sgn(crP(a,b));
            if(s==0) return true;//ON
            if(s==-1) in=!in;
        }
    }
    return in;
}
  /*
bool checkNonzero(Segment s,Point p){
//  printf("checkNonzero\n");
//  print(s.first,' ');
//  print(s.second);
/// print(p);
    Segment ns;
    ns.first=rot(p,s.first,-eps_rot);
    ns.second=rot(p,s.second,-eps_rot);
    vector<Point> ps;
    for(int i=0;i<poly.size();i++){
        Point v1=poly[i],v2=poly[i+1==poly.size()?0:i+1];
        Segment e=Segment(v1,v2);
        Point in=iSS(e,ns);
//      if(!isnan(in.real())) ps.push_back(in);
        if(!isnan(in.real())){
//          return false;
//          printf("inter:");
//          print(in,' ');
//          printf("d=%.9f\n",abs(p-in));
            if((!eq2(ns.first,in))&&(!eq2(ns.second,in))&&(!eq2(e.first,in))&&(!eq2(e.second,in))
                &&(!eq2(p,in))){
//              printf("this is invalid\n");
                return false;
            }else{
//              printf("ok\n");
            }
        }
    }
    ps.push_back(ns.first);
    ps.push_back(ns.second);
    for(int i=0;i<ps.size();i++){
        if(!inPoly_(poly,ps[i])){
//          printf("not inside");
//          print(ps[i]);
            return false;
        }
    }
    //printf("true\n");
    return true;
}*/

bool checkValidity(Point p,Point c,Real ang){
//	printf("checkValidity\n");
//	print(p,' ');
//	print(c,' ');
//	printf("%f\n",ang);
	ang/=2;
	if(eq(p,c)) return true;
	Point np=c+(p-c)*(Point(cos(-ang),sin(-ang)));
	//printf("np=");
	//print(np);
	if(inPoly_(poly,np)==false){
		//printf("this is invalid\n");
		return false;
	}
	Segment s=Segment(np,c);
	for(int i=0;i<poly.size();i++){
		Point v1=poly[i];
		Point v2=poly[i+1==poly.size()?0:i+1];
		Segment e=Segment(v1,v2);
		if(isPara(s,e)) continue;
		Point in=iSS(s,e);
		if(isnan(in.real())) continue;
		if(eq(in,np)||eq2(in,c)) continue;
//		if(abs(in-c)<(1e-1)) continue;
		//printf("intersect ");
	//	print(in);
	//	printf("this is invalid\n");
		return false;
	}
	return true;
}

bool checkValidity(Segment s,Point c,Real ang){
	bool flg1=checkValidity(s.first,c,ang);
	bool flg2=checkValidity(s.second,c,ang);
	return flg1&flg2;
}
  
typedef pair<Real,Point> P;
/*
Segment rotate(Segment s){
    vector<Point> cands=getTouchingPoints(s);
    vector<P> vec;
    for(int i=0;i<cands.size();i++){
        */
  
Real remRotateAng;
Segment seg;
  
Point solve(){
    int cnt_zero=0;
    while(remRotateAng>eps){
        if(cnt_zero>150) break;
        //printf("curseg=");
        //print(seg.first,' ');
        //print(seg.second);
        vector<Point> cands=getTouchingPoints(seg);
        //printf("cands::\n");
        //for(int i=0;i<cands.size();i++){
        //  print(cands[i]);
        //}
        vector<Real> vec;
        for(int i=0;i<cands.size();i++){
            vec.push_back(abs(seg.first-cands[i]));
        }
        sort(vec.begin(),vec.end());
        Point dir=seg.second-seg.first;
        dir/=(abs(dir));
        Point p1=seg.first+dir*vec.front();
        Point p2=seg.first+dir*vec.back();
        Real ang1=getRotateAngle(seg,p1);
        Real ang2=getRotateAngle(seg,p2);
//      printf("---\n");
//      print(p1,' ');
//      print(p2);
//      printf("%f %f\n",ang1,ang2);
//      printf("---\n");
        if(ang1>eps){
            Real rotateAng=min(ang1,remRotateAng);
            Segment nseg=rotateSegment(seg,p1,rotateAng);
            remRotateAng-=rotateAng;
            //printf("center=");
            //print(p1);
            //printf("ang=%f\n",rotateAng);
            seg=nseg;
        }else if(ang2>eps){
            Real rotateAng=min(ang2,remRotateAng);
            Segment nseg=rotateSegment(seg,p2,rotateAng);
            remRotateAng-=rotateAng;
            //printf("center=");
            //print(p2);
            //printf("ang=%f\n",rotateAng);
            seg=nseg;
        }else{
            cnt_zero++;
            //printf("zero\n");
            //print(seg.first,' ');
            //print(seg.second);
            //print(p1);
            //print(p2);
            break;
        }
        cnt_zero=0;
//      printf("remAng=%f\n",remRotateAng);
    }
    return seg.first;
}
  
int main(){
    int tnum=0;
    while(true){
        double L,R;
        int N;
        scanf("%lf%lf%d",&L,&R,&N);
        if(N==0) break;
        tnum++;
        //printf("test case %d\n",tnum);
        poly.clear();
        for(int i=0;i<N;i++){
            double x,y;
            scanf("%lf%lf",&x,&y);
            poly.push_back(Point(x,y));
        }
        remRotateAng=PI*2.0*R;
        seg=Segment(Point(0,0),Point(0,L));
        Point ans=solve();
        //fprintf(stderr,"%.9f %.9f\n",ans.real(),ans.imag());
        double ansx=ans.real(),ansy=ans.imag();
        //printf("-----\n");
   //     fprintf(stderr,"%.9f %.9f\n",ansx,ansy);
		printf("%9f %9f\n",ansx,ansy);
  //    	cout << ans.real() << " " << ans.imag() << "\n";
    //  break;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1316
final_rank: 1

code_snippet: #include <iostream>
#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>
#include <iomanip>
#include <fstream>
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 long long ll;
typedef pair<int,int> pii;

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()]
#define prev(g, i) g[(i+g.size()-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) { }
};
bool contain(const C &c1, const C &c2) {
  double d = abs(c1.p - c2.p);
  return d < c1.r - c2.r + EPS;
}
vector<L> tangentCP(const C &c, const P &p) {
  vector<L> ret;
  P vect = c.p - p;
  double d = abs(vect);
  double l = sqrt(d*d-c.r*c.r);
  if (::isnan(l)) { return ret; }
  P v1 = vect * P(l / d,  c.r / d);
  P v2 = vect * P(l / d, -c.r / d);
  ret.push_back(L(p, p+v1));
  if (l > EPS)
    ret.push_back(L(p, p+v2));
  return ret;
}
vector<L> tangentCC(const C &c1, const C &c2) {
  vector<L> ret;
  if (abs(c1.p - c2.p) - (c1.r + c2.r) > -EPS) {
    P center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);
    ret = tangentCP(c1, center);
  }
  if (fabs(c1.r - c2.r) > EPS) {
    P out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);
    vector<L> nret = tangentCP(c1, out);
    ret.insert(ret.end(), nret.begin(), nret.end());
  } else {
    P vect = c2.p - c1.p;
    vect /= abs(vect);
    P q1 = c1.p + vect * P(0, 1) * c1.r;
    P q2 = c1.p + vect * P(0, -1) * c1.r;
    ret.push_back(L(q1, q1 + vect));
    ret.push_back(L(q2, q2 + vect));
  }
  return ret;
}
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;
}
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;
}


G g;
double l,r;
int N;

struct Res {
  P ct;
  L s;
  double ang;
  Res(const P &ct, const L &s, double ang) :
    ct(ct),s(s),ang(ang) {}
  Res() {}
};

bool win(const Res &a, const Res &b, const P &ct) {
  if (abs(a.ang-b.ang)>EPS) return a.ang > b.ang;
  return abs(a.ct-ct) < abs(b.ct-ct);
}
bool sameang(const P &a, const P &b) {
  return abs(cross(a,b))<EPS && dot(a,b)>0;
}
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));
}
double angle2_2(const P &a, const P &b) {
  double ret = abs(arg(b)-arg(a));
  return ret>PI ? 2*PI-ret : ret;
}
// テヲツ卍づィツィツ暗・ツ崢榲」ツつ甘・ツ?エテ」ツ?ッテ・ツ青ォテ」ツ?ソテ」ツ??・ツ債甘ヲツ卍づィツィツ暗・ツ崢榲」ツつ甘・ツ?エテ」ツ?ッテ・ツ青ォテ」ツ?セテ」ツ?ェテ」ツ??
int quadrant(const P &a) {
  if (a.real() > 0 && a.imag() >= 0) return 0;
  if (a.real() <= 0 && a.imag() > 0) return 1;
  if (a.real() < 0 && a.imag() <= 0) return 2;
  return 3;
}
// テ」ツδュテ」ツδ静」ツつケテ」ツδ暗」ツ?ェティツァツ津・ツコツヲテヲツッツ氾ィツシツ?
bool cmpAngle(const P &a, const P&b) {
  int q1 = quadrant(a);
  int q2 = quadrant(b);
  if (q1 != q2) return q1 < q2;
  return cross(a,b)>0;
}
// テ」ツδ凖」ツつッテ」ツδ暗」ツδォテ」ツ?ョテ・ツ崢榲ィツサツ「
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));
}

double signedLength(const P &p1, const P &p2, const P &p) {
  P pc = projection(L(p1,p2),p);
  double res = abs(pc-p1);
  if (dot(pc-p1,p2-p1)>0) return res;
  else return -res;
}

Res find_next(const P &ct, const L& s) {
  Res res;
  res.ang = INF;
  // vertex
  REP(i,N) {
    if (ct==g[i]) continue;
    REP(j,2) {
      if (s[j]==ct) continue;
      if (abs(g[i]-ct)<abs(s[j]-ct)) {
        if (sameang(g[i]-ct,s[j]-ct)) {
          if (ccw(ct,s[j],next(g,i)) == -1 ||
              ccw(ct,s[j],prev(g,i)) == -1) {
            // stuck
            Res t(g[i], s, 0);
            if (win(res,t,ct)) res = t;
          }
        } else {
          double ang = angle(g[i]-ct,s[j]-ct);
          Res t(g[i],L(ct+rotate(s[0]-ct,-ang),ct+rotate(s[1]-ct,-ang)),ang);
          if (win(res,t,ct)) res = t;
        }
      }
    }
  }
  // edge
  REP(i,N) {
    REP(j,2) {
      if (s[j]==ct) continue;
      vector<P> ps = crosspointSC(line(g,i),C(ct,abs(s[j]-ct)));
      FOR(it, ps) {
        if (sameang(*it-ct,s[j]-ct)) {
          P a = g[i];
          P b = next(g,i);
          P c = prev(g,i);
          if (abs(signedLength(ct,s[j],a) - signedLength(ct,s[j],b)) < EPS) continue;
          if (signedLength(ct,s[j],a) > signedLength(ct,s[j],b)) {
            swap(a,b);
            c = g[(i+2)%g.size()];
          }
          if (ccw(ct,*it,a) == 1) continue;
          if (ccw(ct,*it,a) != -1) {
            continue;
            if (ccw(ct,*it,c) == 1) continue;
          }
          // cout << ct << " " << s[j] << " " << a << " " << b << " :" << signedLength(ct,s[j],a) << " " << signedLength(ct,s[j],b) << endl;
          // cout << *it << endl;
          // cout << ccw(ct,*it,a) << endl;
          
          // cout << *it << endl;
          // stuck
          Res t(*it, s, 0);
          if (win(res,t,ct)) res = t;
        } else {
          // cout << *it << endl;
          double ang = angle(*it-ct,s[j]-ct);
          Res t(*it,L(ct+rotate(s[0]-ct,-ang),ct+rotate(s[1]-ct,-ang)),ang);
          // cout << res.ang << " " << ang << endl;
          if (win(res,t,ct)) res = t;
        }      
      }
    }
  }
  return res;
}
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);
}


int main() {
  fp = fopen("data.js","w");
  while(cin>>l>>r>>N, l||r||N) {
    g.clear();
    double tang = r*2*PI;
    REP(i,N) g.push_back(pIN());
    REP(i,N) outputLine(line(g,i));
    L s(P(0,0),P(0,l));
    P ct(0,0);
    double ang = 0;
    outputLine(s);
    int counter = 0;
    REP(i,1000) {
      Res ret = find_next(ct,s);
      // cout << ang << " " << ret.ang << " " << tang << endl;
      if (ang+ret.ang>=tang) {
        double ag = tang-ang;
        s[0] = ct+rotate(s[0]-ct,-ag);
        s[1] = ct+rotate(s[1]-ct,-ag);
        outputLine(s);
        break;
      }
      if (ang == 0) {
        if (counter++ == 10) break;
      } else counter = 0;
        
      // cout << ang << " " << s  << ":" << ret.ang << " " << ret.s<< " " << ret.ct << endl;
      outputLine(ret.s);
      outputCircle(C(ct,l));
      ang += ret.ang;
      s = ret.s;
      ct = ret.ct;
    }
    printf("%.10f %.10f\n", s[0].real(), s[0].imag());
  }
}
accuracy: 1
time_ms: 100
memory_kb: 1376
final_rank: 2

code_snippet: #include <iostream>
#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>
#include <iomanip>
#include <fstream>
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 long long ll;
typedef pair<int,int> pii;

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()]
#define prev(g, i) g[(i+g.size()-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) { }
};
bool contain(const C &c1, const C &c2) {
  double d = abs(c1.p - c2.p);
  return d < c1.r - c2.r + EPS;
}
vector<L> tangentCP(const C &c, const P &p) {
  vector<L> ret;
  P vect = c.p - p;
  double d = abs(vect);
  double l = sqrt(d*d-c.r*c.r);
  if (::isnan(l)) { return ret; }
  P v1 = vect * P(l / d,  c.r / d);
  P v2 = vect * P(l / d, -c.r / d);
  ret.push_back(L(p, p+v1));
  if (l > EPS)
    ret.push_back(L(p, p+v2));
  return ret;
}
vector<L> tangentCC(const C &c1, const C &c2) {
  vector<L> ret;
  if (abs(c1.p - c2.p) - (c1.r + c2.r) > -EPS) {
    P center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);
    ret = tangentCP(c1, center);
  }
  if (fabs(c1.r - c2.r) > EPS) {
    P out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);
    vector<L> nret = tangentCP(c1, out);
    ret.insert(ret.end(), nret.begin(), nret.end());
  } else {
    P vect = c2.p - c1.p;
    vect /= abs(vect);
    P q1 = c1.p + vect * P(0, 1) * c1.r;
    P q2 = c1.p + vect * P(0, -1) * c1.r;
    ret.push_back(L(q1, q1 + vect));
    ret.push_back(L(q2, q2 + vect));
  }
  return ret;
}
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;
}
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;
}


G g;
double l,r;
int N;

struct Res {
  P ct;
  L s;
  double ang;
  Res(const P &ct, const L &s, double ang) :
    ct(ct),s(s),ang(ang) {}
  Res() {}
};

bool win(const Res &a, const Res &b, const P &ct) {
  if (abs(a.ang-b.ang)>EPS) return a.ang > b.ang;
  return abs(a.ct-ct) < abs(b.ct-ct);
}
bool sameang(const P &a, const P &b) {
  return abs(cross(a,b))<EPS && dot(a,b)>0;
}
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));
}
double angle2_2(const P &a, const P &b) {
  double ret = abs(arg(b)-arg(a));
  return ret>PI ? 2*PI-ret : ret;
}
// テヲツ卍づィツィツ暗・ツ崢榲」ツつ甘・ツ?エテ」ツ?ッテ・ツ青ォテ」ツ?ソテ」ツ??・ツ債甘ヲツ卍づィツィツ暗・ツ崢榲」ツつ甘・ツ?エテ」ツ?ッテ・ツ青ォテ」ツ?セテ」ツ?ェテ」ツ??
int quadrant(const P &a) {
  if (a.real() > 0 && a.imag() >= 0) return 0;
  if (a.real() <= 0 && a.imag() > 0) return 1;
  if (a.real() < 0 && a.imag() <= 0) return 2;
  return 3;
}
// テ」ツδュテ」ツδ静」ツつケテ」ツδ暗」ツ?ェティツァツ津・ツコツヲテヲツッツ氾ィツシツ?
bool cmpAngle(const P &a, const P&b) {
  int q1 = quadrant(a);
  int q2 = quadrant(b);
  if (q1 != q2) return q1 < q2;
  return cross(a,b)>0;
}
// テ」ツδ凖」ツつッテ」ツδ暗」ツδォテ」ツ?ョテ・ツ崢榲ィツサツ「
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));
}

double signedLength(const P &p1, const P &p2, const P &p) {
  P pc = projection(L(p1,p2),p);
  double res = abs(pc-p1);
  if (dot(pc-p1,p2-p1)>0) return res;
  else return -res;
}

Res find_next(const P &ct, const L& s) {
  Res res;
  res.ang = INF;
  // vertex
  REP(i,N) {
    if (ct==g[i]) continue;
    REP(j,2) {
      if (s[j]==ct) continue;
      if (abs(g[i]-ct)<abs(s[j]-ct)) {
        if (sameang(g[i]-ct,s[j]-ct)) {
          if (ccw(ct,s[j],next(g,i)) == -1 ||
              ccw(ct,s[j],prev(g,i)) == -1) {
            // stuck
            Res t(g[i], s, 0);
            if (win(res,t,ct)) res = t;
          }
        } else {
          double ang = angle(g[i]-ct,s[j]-ct);
          Res t(g[i],L(ct+rotate(s[0]-ct,-ang),ct+rotate(s[1]-ct,-ang)),ang);
          if (win(res,t,ct)) res = t;
        }
      }
    }
  }
  // edge
  REP(i,N) {
    REP(j,2) {
      if (s[j]==ct) continue;
      vector<P> ps = crosspointSC(line(g,i),C(ct,abs(s[j]-ct)));
      FOR(it, ps) {
        if (sameang(*it-ct,s[j]-ct)) {
          P a = g[i];
          P b = next(g,i);
          P c = prev(g,i);
          if (abs(signedLength(ct,s[j],a) - signedLength(ct,s[j],b)) < EPS) continue;
          if (signedLength(ct,s[j],a) > signedLength(ct,s[j],b)) {
            swap(a,b);
            c = g[(i+2)%g.size()];
          }
          if (ccw(ct,*it,a) == 1) continue;
          if (ccw(ct,*it,a) != -1) {
            continue;
            if (ccw(ct,*it,c) == 1) continue;
          }
          // cout << ct << " " << s[j] << " " << a << " " << b << " :" << signedLength(ct,s[j],a) << " " << signedLength(ct,s[j],b) << endl;
          // cout << *it << endl;
          // cout << ccw(ct,*it,a) << endl;
          
          // cout << *it << endl;
          // stuck
          Res t(*it, s, 0);
          if (win(res,t,ct)) res = t;
        } else {
          // cout << *it << endl;
          double ang = angle(*it-ct,s[j]-ct);
          Res t(*it,L(ct+rotate(s[0]-ct,-ang),ct+rotate(s[1]-ct,-ang)),ang);
          // cout << res.ang << " " << ang << endl;
          if (win(res,t,ct)) res = t;
        }      
      }
    }
  }
  return res;
}
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);
}


int main() {
  fp = fopen("data.js","w");
  while(cin>>l>>r>>N, l||r||N) {
    g.clear();
    double tang = r*2*PI;
    REP(i,N) g.push_back(pIN());
    REP(i,N) outputLine(line(g,i));
    L s(P(0,0),P(0,l));
    P ct(0,0);
    double ang = 0;
    outputLine(s);
    REP(i,1000) {
      Res ret = find_next(ct,s);
      // cout << ang << " " << ret.ang << " " << tang << endl;
      if (ang+ret.ang>=tang) {
        double ag = tang-ang;
        s[0] = ct+rotate(s[0]-ct,-ag);
        s[1] = ct+rotate(s[1]-ct,-ag);
        outputLine(s);
        break;
      }
      // cout << ang << " " << s  << ":" << ret.ang << " " << ret.s<< " " << ret.ct << endl;
      outputLine(ret.s);
      outputCircle(C(ct,l));
      ang += ret.ang;
      s = ret.s;
      ct = ret.ct;
    }
    printf("%.10f %.10f\n", s[0].real(), s[0].imag());
  }
}
accuracy: 1
time_ms: 100
memory_kb: 1376
final_rank: 2