Problem:
Problem Statement
You have a billiard table.
The playing area of the table is rectangular.
This billiard table is special as it has no pockets, and the playing area is completely surrounded with a cushion.
You succeeded in producing a ultra-precision billiards playing robot.
When you put some balls on the table, the machine hits one of those balls.
The hit ball stops after 10,000 unit distance in total moved.
When a ball collided with the cushion of the table, the ball takes the orbit like mirror reflection.
When a ball collided with the corner, the ball bounces back on the course that came.
Your mission is to predict which ball collides first with the ball which the robot hits .
Input
The input is a sequence of datasets.
The number of datasets is less than 100.
Each dataset is formatted as follows.
n
w
h
r
v_x
v_y
x_1
y_1
...
x_n
y_n
First line of a dataset contains an positive integer
n
, which represents the number of balls on the table (
2 \leq n \leq 11
).
The next line contains five integers (
w
,
h
,
r
,
v_x
,
v_y
) separated by a single space,
where
w
and
h
are the width and the length of the playing area of the table respectively (
4 \leq w, h \leq 1,000
),
r
is the radius of the balls (
1 \leq r \leq 100
).
The robot hits the ball in the vector (
v_x, v_y
) direction (
-10,000 \leq v_x, v_y \leq 10,000
and
(v_x, v_y) \neq (0, 0)
).
The following
n
lines give position of balls.
Each line consists two integers separated by a single space,
(x_i, y_i)
means the center position of the
i
-th ball on the table in the initial state (
r < x_i < w - r
,
r < y_i < h - r
).
(0, 0)
indicates the position of the north-west corner of the playing area,
and
(w, h)
indicates the position of the south-east corner of the playing area.
You can assume that, in the initial state, the balls do not  touch each other  nor the cushion.
The robot always hits the first ball in the list.
You can assume that the given values do not have errors.
The end of the input is indicated by a line containing a single zero.
Output
For each dataset, print the index of the ball which first collides with the  ball  the robot hits.
When the hit ball collides with no ball until it stops moving, print
-1
.
You can assume that no more than one ball  collides with the hit ball first, at the same time.
It is also guaranteed that, when
r
changes by
eps
(
eps < 10^{-9}
), 
the ball which collides first and the way of the collision do not change.
Sample Input
3
26 16 1 8 4
10 6
9 2
9 10
3
71 363 4 8 0
52 238
25 33
59 288
0
Output for the Sample Input
3
-1

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=1005,INF=1<<30;

const double eps=1e-10;
const double pi=acos((long double)-1.0L);
#define equals(a,b) (fabs((a)-(b))<eps)

double torad(int deg) {return (double)(deg)*pi/180.0;}
double todeg(double ang) {return ang*180.0/pi;}

class Point{
public:
    double x,y;
    
    Point(double x=0,double y=0):x(x),y(y){}
    
    Point operator + (Point p){return Point(x+p.x,y+p.y);}
    Point operator - (Point p){return Point(x-p.x,y-p.y);}
    Point operator * (double a){return Point(a*x,a*y);}
    Point operator / (double a){return Point(x/a,y/a);}
    
    double abs(){return sqrt(norm());}
    double norm(){return x*x+y*y;}
    
    bool operator < (const Point &p)const{
        return x+eps<p.x||(equals(x,p.x)&&y+eps<p.y);
    }
    
    bool operator == (const Point &p)const{
        return fabs(x-p.x)<eps&&fabs(y-p.y)<eps;
    }
};

typedef Point Vector;

double norm(Vector a){
    return a.x*a.x+a.y*a.y;
}

double abs(Vector a){
    return sqrt(norm(a));
}

double dot(Vector a,Vector b){
    return a.x*b.x+a.y*b.y;
}

double cross(Vector a,Vector b){
    return a.x*b.y-a.y*b.x;
}

struct Segment{
    Point p1,p2;
};

bool isOrthogonal(Vector a,Vector b){
    return equals(dot(a,b),0.0);
}

bool isOrthogonal(Point a1,Point a2,Point b1,Point b2){
    return isOrthogonal(a1-a2,b1-b2);
}

bool isOrthogonal(Segment s1,Segment s2){
    return equals(dot(s1.p2-s1.p1,s2.p2-s2.p1),0.0);
}

bool isParallel(Vector a,Vector b){
    return equals(cross(a,b),0.0);
}

bool isParallel(Point a1,Point a2,Point b1,Point b2){
    return isParallel(a1-a2,b1-b2);
}

bool isParallel(Segment s1,Segment s2){
    return equals(cross(s1.p2-s1.p1,s2.p2-s2.p1),0.0);
}

Point project(Segment s,Point p){
    Vector base=s.p2-s.p1;
    double r=dot(p-s.p1,base)/norm(base);
    return s.p1+base*r;
}

Point reflect(Segment s,Point p){
    return p+(project(s,p)-p)*2.0;
}

Point turn(Point p,Point c,double pi){
    double q=atan2(p.y-c.y,p.x-c.x);
    q+=pi;
    p=c+Point{cos(q)*abs(p-c),sin(q)*abs(p-c)};
    
    return p;
}
//pをcを中心としてpi回転させる(1周で2π)
//p=cのときnan

static const int counter_clockwise=1;
static const int clockwise=-1;
static const int online_back=2;
static const int online_front=-2;
static const int on_segment=0;

int ccw(Point p0,Point p1,Point p2){
    Vector a=p1-p0;
    Vector b=p2-p0;
    
    if(cross(a,b)>eps) return counter_clockwise;
    if(cross(a,b)<-eps) return clockwise;
    if(dot(a,b)<-eps) return online_back;
    if(a.norm()<b.norm()) return online_front;
    
    return on_segment;
}

bool intersect(Point p1,Point p2,Point p3,Point p4){
    return(ccw(p1,p2,p3)*ccw(p1,p2,p4)<=0&&ccw(p3,p4,p1)*ccw(p3,p4,p2)<=0);
}

bool intersect(Segment s1,Segment s2){
    return intersect(s1.p1,s1.p2,s2.p1,s2.p2);
}

bool overlap(Segment s1,Segment s2){
    int a=ccw(s1.p1,s1.p2,s2.p1),b=ccw(s1.p1,s1.p2,s2.p2);
    if(a&1||b&1) return 0;
    if(a==2){
        if(b==-2||(b==0&&!(s2.p2==s1.p1))) return 1;
        else return 0;
    }
    if(a==-2){
        if(b==2||(b==0&&!(s2.p2==s1.p2))) return 1;
        else return 0;
    }
    if(a==0){
        if(s1.p1==s2.p1){
            if(b!=2) return 1;
            else return 0;
        }
        else if(s1.p2==s2.p1){
            if(b!=-2) return 1;
            else return 0;
        }
        else return 1;
    }
    return 0;
}
//s1とs2の共通の線分(長さ0より大きい)があるかどうか

typedef Segment Line;

double getDistance(Point a,Point b){
    return abs(a-b);
}

double getDistanceLP(Line l,Point p){
    return abs(cross(l.p2-l.p1,p-l.p1)/abs(l.p2-l.p1));
}

double getDistanceSP(Segment s,Point p){
    if(dot(s.p2-s.p1,p-s.p1)<0.0) return abs(p-s.p1);
    if(dot(s.p1-s.p2,p-s.p2)<0.0) return abs(p-s.p2);
    return getDistanceLP(s,p);
}

double getDistance(Segment s1,Segment s2){
    if(intersect(s1,s2)) return 0.0;
    return min({getDistanceSP(s1,s2.p1),getDistanceSP(s1,s2.p2),getDistanceSP(s2,s1.p1),getDistanceSP(s2,s1.p2)});
}

Point getCrossPointS(Segment s1,Segment s2){
    //if(ccw(s1.p1,s1.p2,s2.p1)==0&&ccw(s1.p1,s1.p2,s2.p2)==0) return s1.p1;
    Vector base=s2.p2-s2.p1;
    double d1=abs(cross(base,s1.p1-s2.p1));
    double d2=abs(cross(base,s1.p2-s2.p1));
    double t=d1/(d1+d2);
    return s1.p1+(s1.p2-s1.p1)*t;
}//同じ時壊れます

Point getCrossPointL(Line l1,Line l2){
    //if(ccw(s1.p1,s1.p2,s2.p1)==0&&ccw(s1.p1,s1.p2,s2.p2)==0) return s1.p1;
    
    Vector v1=l1.p2-l1.p1,v2=l2.p2-l2.p1;
    
    return l1.p1+v1*cross(v2,l2.p1-l1.p1)/cross(v2,v1);
}

Segment ParallelSegment(Segment s,double d){
    Vector v={-(s.p2-s.p1).y,(s.p2-s.p1).x};
    v=v/abs(v);
    
    s.p1=s.p1+v*d;
    s.p2=s.p2+v*d;
    
    return s;
}

class Circle{
public:
    Point c;
    double r;
    Circle(Point c=Point(),double r=0.0):c(c),r(r){}
};

Point CircleCenter(Point a,Point b,Point c){
    Point u=a-b,v=a-c;
    double m1=(norm(a)-norm(b))/2.0,m2=(norm(a)-norm(c))/2.0;
    
    Point res;
    if(cross(u,v)==0.0){
        res.x=1e9;
        res.y=1e9;
        
        return res;
    }
    res.x=(m1*v.y-m2*u.y)/cross(u,v);
    res.y=(m1*v.x-m2*u.x)/cross(v,u);
    
    return res;
}

pair<Point,Point> segCrossPpoints(Circle c,Line l){
    //assert(intersect(c,l));
    Vector pr=project(l,c.c);
    Vector e=(l.p2-l.p1)/abs(l.p2-l.p1);
    double base=sqrt(c.r*c.r-norm(pr-c.c));
    return make_pair(pr+e*base,pr-e*base);
}

double arg(Vector p){return atan2(p.y,p.x);}
Vector polar(double a,double r){return Point(cos(r)*a,sin(r)*a);}

pair<Point,Point> getCrossPoints(Circle c1,Circle c2){
    //assert(intersect(c1,c2));
    double d=abs(c1.c-c2.c);
    double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d));
    double t=arg(c2.c-c1.c);
    return make_pair(c1.c+polar(c1.r,t+a),c1.c+polar(c1.r,t-a));
}

typedef vector<Point> Polygon;

/*
 IN 2
 ON 1
 OUT 0
 */

int contains(Polygon g,Point p){
    int n=int(g.size());
    bool x=false;
    for(int i=0;i<n;i++){
        Point a=g[i]-p,b=g[(i+1)%n]-p;
        if(a.y>b.y) swap(a,b);
        if(a.y<eps&&eps<b.y&&cross(a,b)>eps) x=!x;
    }
    return (x?2:0);
}

Polygon andrewScan(Polygon s,bool ok){
    Polygon u,l;
    sort(all(s));
    
    if(int(s.size())<3) return s;
    int n=int(s.size());
    
    u.push_back(s[0]);
    u.push_back(s[1]);
    
    l.push_back(s[n-1]);
    l.push_back(s[n-2]);
    
    if(ok){
        for(int i=2;i<n;i++){
            for(int j=int(u.size());j>=2&&ccw(u[j-2],u[j-1],s[i])==counter_clockwise;j--){
                u.pop_back();
            }
            u.push_back(s[i]);
        }
        
        for(int i=int(s.size())-3;i>=0;i--){
            for(int j=int(l.size());j>=2&&ccw(l[j-2],l[j-1],s[i])==counter_clockwise;j--){
                l.pop_back();
            }
            l.push_back(s[i]);
        }
    }
    
    if(!ok){
        for(int i=2;i<n;i++){
            for(int j=int(u.size());j>=2&&ccw(u[j-2],u[j-1],s[i])!=clockwise;j--){
                u.pop_back();
            }
            u.push_back(s[i]);
        }
        
        for(int i=int(s.size())-3;i>=0;i--){
            for(int j=int(l.size());j>=2&&ccw(l[j-2],l[j-1],s[i])!=clockwise;j--){
                l.pop_back();
            }
            l.push_back(s[i]);
        }
    }
    
    reverse(all(l));
    
    for(int i=int(u.size())-2;i>=1;i--) l.push_back(u[i]);
    
    return l;
}//ok==1なら辺の上も含める

Polygon convex_cut(const Polygon& P, const Line& l) {
    Polygon Q;
    for(int i=0;i<si(P);i++){
        Point A=P[i],B=P[(i+1)%si(P)];
        if(ccw(l.p1,l.p2,A)!=-1)Q.push_back(A);
        if(ccw(l.p1,l.p2,A)*ccw(l.p1,l.p2,B)<0) Q.push_back(getCrossPointL(Line{A,B},l));
    }
    return Q;
}

double area(Point a,Point b,Point c){
    b=b-a;
    c=c-a;
    return abs(b.x*c.y-b.y*c.x)/2.0;
}

double area(Polygon &P){
    if(si(P)==0) return 0.0;
    double res=0;
    Point c={0.0,0.0};
    for(int i=0;i<si(P);i++){
        c=c+P[i];
    }
    c=c/si(P);
    
    for(int i=0;i<si(P);i++){
        res+=area(c,P[i],P[(i+1)%si(P)]);
    }
    
    return res;
}


int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    while(1){
        int N;cin>>N;
        if(N==0) break;
        double w,h,r,vx,vy;cin>>w>>h>>r>>vx>>vy;
        vector<Point> P(N);
        for(int i=0;i<N;i++) cin>>P[i].x>>P[i].y;
        
        if(vx<0){
            vx*=-1;
            for(int i=0;i<N;i++) P[i].x=w-P[i].x;
        }
        
        if(vy<0){
            vy*=-1;
            for(int i=0;i<N;i++) P[i].y=h-P[i].y;
        }
        
        Segment s;
        s.p1=P[0];
        s.p2=s.p1+Point{vx,vy}/abs({vx,vy})*10000;
        
        pair<double,int> ans(INF,-1);
        
        w-=2*r;
        h-=2*r;
        
        for(int i=1;i<N;i++){
            for(int q=1;q<=4;q++){
                double save=P[i].y;
                for(int t=0;t<10000;t++,P[i].x+=2*w){
                    int sum=0;
                    for(int j=0;j<10;j++,P[i].y+=2*h){
                        double d=getDistanceSP(s,P[i]);
                        if(d<2*r+eps){
                            Circle c(P[i],2*r);
                            auto a=segCrossPpoints(c,s);
                            
                            //chmin(ans,mp(getDistance(P[0],a.fi),i+1));
                            
                            double mi=min(getDistance(P[0],a.fi),getDistance(P[0],a.se));
                            
                            chmin(ans,mp(mi,i+1));
                        }
                        
                        if(vx==0) break;
                    }
                    if(vx!=0){
                        P[i].y-=20*h;
                        
                        while(P[i].y<P[0].y+(P[i].x-P[0].x)/vx*vy){
                            P[i].y+=2*h;
                            sum++;
                        }
                        if(sum) P[i].y-=2*h;
                    }
                }
                P[i].x-=20000*w;
                P[i].y=save;
                
                P[i].x=2*(w+r)-P[i].x;
                if(q==2) P[i].y=2*(h+r)-P[i].y;
            }
        }
        
        cout<<ans.se<<endl;
    }
}
accuracy: 1
time_ms: 840
memory_kb: 3164
final_rank: 5

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

//constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-9;

//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

long double f(long double a, long double b, long double c) {
	long double ret = MOD;
	long double d = b * b - 4 * a*c;
//	cout << d << endl;
	if (d > -EPS) {
		d = sqrt(d);
		long double e = (-b + d) / 2 / a;
		if (e >= 0 && e <= 20000000)ret = min(ret, e);
		e = (-b - d) / 2 / a;
		if (e >= 0 && e <= 20000000)ret = min(ret, e);
	}
	return ret;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N;
	vector<int>ret;
	while (N) {
		cin >> W >> H >> R;
		long double vy, vx;
		cin >> vx >> vy;
		vector<long double>x(N);
		vector<long double>y(N);
		for (int i = 0; i < N; i++) {
			cin >> x[i] >> y[i];
		}
		long double d = sqrt(vx*vx + vy * vy);
		vx /= d, vy /= d;
		//	cout << vx << " " << vy << endl;
		int dif = 10000 / min(W, H) * 2  + 10;
		vector<long double>near(N, MOD);
		for (int i = -dif; i <= dif; i++) {
			for (int j = -dif; j <= dif; j++) {
				for (int k = 1; k < N; k++) {
					long double cx = x[k], cy = y[k];
					if ((i + 10000) & 1) {
						cx = W - cx;
					}
					if ((j + 10000) & 1) {
						cy = H - cy;
					}
					cx += i * (W - 2 * R);
					cy += j * (H - 2 * R);
					//if (f(1, vx*(x[0] - cx) * 2 + vy * (y[0] - cy) * 2, cx*cx + cy * cy + x[0] * x[0] + y[0] * y[0] - cx * x[0] * 2 - cy * y[0] * 2 - R * R * 4) <= 30000) {
					//	cout << cx << " " << cy << " " << f(1, vx*(x[0] - cx) * 2 + vy * (y[0] - cy) * 2, cx*cx + cy * cy + x[0] * x[0] + y[0] * y[0] - cx * x[0] * 2 - cy * y[0] * 2 - R * R * 4) << endl;
					//}
				//	cout << cx << " " << cy << endl;
					near[k] = min(near[k], f(1, vx*(x[0] - cx) * 2 + vy * (y[0] - cy) * 2, cx*cx + cy * cy + x[0] * x[0] + y[0] * y[0] - cx * x[0] * 2 - cy * y[0] * 2 - R * R * 4));
				}
			}
		}
		int ans = -2;
		for (int i = 1; i < N; i++) {
			//		cout << i << " " << near[i] << endl;
			if (near[i] <= 10000 + EPS) {
				if (ans == -2)ans = i;
				else {
					if (near[ans] > near[i])ans = i;
				}
			}
		}
		ret.push_back(ans + 1);
		cin >> N;
	}
	for (auto i : ret)cout << i << endl;
}
accuracy: 1
time_ms: 4140
memory_kb: 3236
final_rank: 7

code_snippet: using namespace std;
#define _USE_MATH_DEFINES
#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<iomanip>
const double EPS=1e-10;
bool eq(double a,double b){return abs(a-b)<EPS;}
struct Point{
	double x,y;
	Point(double x_=0,double y_=0):x(x_),y(y_){}
	Point operator-()const{return Point(-x,-y);}
	Point operator+(const Point&p)const{return Point(x+p.x,y+p.y);}
	Point operator-(const Point&p)const{return Point(x-p.x,y-p.y);}
	Point operator*(const double k)const{return Point(x*k,y*k);}
	Point operator/(const double k)const{return Point(x/k,y/k);}
	bool operator<(const Point&p)const{return eq(x,p.x)?y<p.y:x<p.x;}
	bool operator==(const Point&p)const{return eq(x,p.x)&&eq(y,p.y);}
};
istream&operator>>(istream&is,Point&p){return is>>p.x>>p.y;}
ostream&operator<<(ostream&os,const Point&p){return os<<fixed<<setprecision(9)<<p.x<<' '<<p.y;}
struct Line{
	Point p1,p2;
	Line(Point p1_=Point(),Point p2_=Point()):p1(p1_),p2(p2_){}
};
struct Segment:Line{
	Segment(Point p1_=Point(),Point p2_=Point()):Line(p1_,p2_){}
};
struct Circle{
	Point o;
	double r;
	Circle(Point o_=Point(),double r_=0):o(o_),r(r_){}
};
using Polygon=vector<Point>;
//function list begin
Point vec(const Line&);
double norm(const Point&);
double norm(const Line&);
double abs(const Point&);
double abs(const Line&);
double arg(const Point&);
double arg(const Line&);
double arg(const Point&,const Point&,const Point&);//a->b->c
int argtype(const Point&);//(-pi,0]->0,(0,pi]->1
bool argless(const Point&,const Point&);//sorting points with arg
double dot(const Point&,const Point&);
double cross(const Point&,const Point&);
Point polar(const double,const double);
Point rotate(const Point&,const double);
enum{ONLINE_FRONT=-2,CLOCKWISE=-1,ON_SEGMENT=0,COUNTER_CLOCKWISE=1,ONLINE_BACK=2};
int ccw(const Point&,const Point&);
int ccw(const Point&,const Point&,const Point&);
int ccw(const Line&,const Point&);
bool orthogonal(const Point&,const Point&);
bool orthogonal(const Line&,const Line&);
bool parallel(const Point&,const Point&);
bool parallel(const Line&,const Line&);
bool intersect(const Line&,const Point&);
bool intersect(const Line&,const Line&);
bool intersect(const Segment&,const Point&);
bool intersect(const Segment&,const Segment&);
bool intersect(const Line&,const Segment&);
bool intersect(const Segment&,const Line&);
bool intersect(const Circle&,const Point&);
int intersect(const Circle&,const Line&);//count contacts
int intersect(const Circle&,const Segment&);
int intersect(const Circle&,const Circle&);//count common tangents
double distance(const Point&,const Point&);
double distance(const Line&,const Point&);
double distance(const Line&,const Line&);
double distance(const Segment&,const Point&);
double distance(const Segment&,const Segment&);
double distance(const Line&,const Segment&);
double distance(const Segment&,const Line&);
double distance(const Circle&,const Point&);
double distance(const Circle&,const Line&);
double distance(const Circle&,const Segment&);
double distance(const Circle&,const Circle&);
Point projection(const Line&,const Point&);
Point reflection(const Line&,const Point&);
Point crosspoint(const Line&,const Line&);
pair<Point,Point>crosspoint(const Circle&,const Line&);
pair<Point,Point>crosspoint(const Circle&,const Segment&);
pair<Point,Point>crosspoint(const Circle&,const Circle&);
pair<Point,Point>tangent(const Circle&,const Point&);
vector<Line>tangent(const Circle&,const Circle&);
bool is_convex(const Polygon&);
Polygon convex_hull(Polygon,bool=false);
enum{OUT,ON,IN};
int contain(const Polygon&,const Point&);
int contain(const Circle&,const Point&);
int contain(const Circle&,const Segment&);
Polygon convex_cut(const Polygon&,const Line&);
double diameter(Polygon);
double area(const Polygon&);
double area(const Polygon&,const Line&);
double area(const Polygon&,const Circle&);
//function list end
Point vec(const Line&s){return s.p2-s.p1;}
double norm(const Point&p){return p.x*p.x+p.y*p.y;}
double norm(const Line&s){return norm(vec(s));}
double abs(const Point&p){return hypot(p.x,p.y);}
double abs(const Line&s){return abs(vec(s));}
double arg(const Point&p){return atan2(p.y,p.x);}
double arg(const Line&s){return arg(vec(s));}
double arg(const Point&a,const Point&b,const Point&c){
	double A=arg(b-a),B=arg(c-b);
	double theta=abs(A-B);
	return min(theta,2*M_PI-theta);
}
int argtype(const Point&a)
{
	return a.y<-EPS?0:a.y>EPS?1:a.x<0?1:0;
}
bool argless(const Point&a,const Point&b)
{
	int at=argtype(a),bt=argtype(b);
	return at!=bt?at<bt:ccw(a,b)>0;
}
double dot(const Point&a,const Point&b){return a.x*b.x+a.y*b.y;}
double cross(const Point&a,const Point&b){return a.x*b.y-a.y*b.x;}
Point polar(const double r,const double theta){return Point(cos(theta),sin(theta))*r;}
Point rotate(const Point&p,const double theta){
	return Point(p.x*cos(theta)-p.y*sin(theta),p.x*sin(theta)+p.y*cos(theta));
}
int ccw(const Point&a,const Point&b)
{
	return cross(a,b)>EPS?COUNTER_CLOCKWISE
		:cross(a,b)<-EPS?CLOCKWISE
		:dot(a,b)<0?ONLINE_BACK
		:norm(a)<norm(b)?ONLINE_FRONT
		:ON_SEGMENT;
}
int ccw(const Point&a,const Point&b,const Point&c){return ccw(b-a,c-a);}
int ccw(const Line&s,const Point&p){return ccw(s.p1,s.p2,p);}
bool orthogonal(const Point&a,const Point&b){return eq(dot(a,b),0);}
bool orthogonal(const Line&s,const Line&t){return orthogonal(vec(s),vec(t));}
bool parallel(const Point&a,const Point&b){return eq(cross(a,b),0);}
bool parallel(const Line&s,const Line&t){return parallel(vec(s),vec(t));}
bool intersect(const Line&s,const Point&p){return eq(cross(vec(s),p-s.p1),0);}
bool intersect(const Line&s,const Line&t){return !parallel(s,t)||intersect(s,t.p1);}
bool intersect(const Segment&s,const Point&p){return ccw(s,p)==ON_SEGMENT;}
bool intersect(const Segment&s,const Segment&t){
	return ccw(s,t.p1)*ccw(s,t.p2)<=0&&ccw(t,s.p1)*ccw(t,s.p2)<=0;
}
bool intersect(const Line&s,const Segment&t){
	return cross(vec(s),t.p1-s.p1)*cross(vec(s),t.p2-s.p1)<EPS;
}
bool intersect(const Segment&s,const Line&t){return intersect(t,s);}
bool intersect(const Circle&c,const Point&p){return eq(distance(c.o,p),c.r);}
int intersect(const Circle&c,const Line&s){
	double d=distance(s,c.o);
	return eq(d,c.r)?1:d<c.r?2:0;
}
int intersect(const Circle&c,const Segment&s){
	Point h=projection(s,c.o);
	double d1=distance(c.o,s.p1),d2=distance(c.o,s.p2);
	return distance(c.o,h)>c.r+EPS?0
		:d1<c.r-EPS&&d2<c.r-EPS?0
		:d1<c.r-EPS&&d2>c.r-EPS||d1>c.r-EPS&&d2<c.r-EPS?1
		:intersect(s,h)?eq(distance(c.o,h),c.r)?1:2
		:0;
}
int intersect(const Circle&a,const Circle&b){
	double d=distance(a.o,b.o);
	return eq(d,a.r+b.r)?3:d>a.r+b.r?4:eq(d,abs(a.r-b.r))?1:d>abs(a.r-b.r)?2:0;
}
double distance(const Point&a,const Point&b){return abs(a-b);}
double distance(const Line&s,const Point&p){return distance(p,projection(s,p));}
double distance(const Line&s,const Line&t){return intersect(s,t)?0:distance(s,t.p1);}
double distance(const Segment&s,const Point&p){
	return distance(p,
		dot(vec(s),p-s.p1)<0?s.p1
		:dot(-vec(s),p-s.p2)<0?s.p2
		:projection(s,p)
	);
}
double distance(const Segment&s,const Segment&t){
	return intersect(s,t)?0:min({
		distance(s,t.p1),distance(s,t.p2),
		distance(t,s.p1),distance(t,s.p2)
	});
}
double distance(const Line&s,const Segment&t){
	return intersect(s,t)?0:min(distance(s,t.p1),distance(s,t.p2));
}
double distance(const Segment&s,const Line&t){return distance(t,s);}
double distance(const Circle&c,const Point&p){return abs(distance(c.o,p)-c.r);}
double distance(const Circle&c,const Line&s){return max(distance(s,c.o)-c.r,0.);}
double distance(const Circle&c,const Segment&s){
	return intersect(c,s)?0
		:contain(c,s)?c.r-max(distance(c.o,s.p1),distance(c.o,s.p2))
		:distance(s,c.o)-c.r;
}
double distance(const Circle&a,const Circle&b){return max(distance(a.o,b.o)-a.r-b.r,0.);}
Point projection(const Line&s,const Point&p){
	return s.p1+vec(s)*dot(p-s.p1,vec(s))/norm(s);
}
Point reflection(const Line&s,const Point&p){return projection(s,p)*2-p;}
Point crosspoint(const Line&s,const Line&t){
	double d1=abs(cross(vec(s),t.p1-s.p1));
	double d2=abs(cross(vec(s),t.p2-s.p1));
	return t.p1+vec(t)*(d1/(d1+d2));
}
pair<Point,Point>crosspoint(const Circle&c,const Line&s){
	Point h=projection(s,c.o);
	Point e=vec(s)/abs(s)*sqrt(c.r*c.r-norm(h-c.o));
	return minmax(h-e,h+e);
}
pair<Point,Point>crosspoint(const Circle&c,const Segment&s){
	pair<Point,Point>p=crosspoint(c,Line(s));
	return intersect(c,s)==2?p
		:intersect(s,p.first)?make_pair(p.first,p.first)
		:make_pair(p.second,p.second);
}
pair<Point,Point>crosspoint(const Circle&a,const Circle&b){
	double d=distance(a.o,b.o);
	double alpha=acos((a.r*a.r+d*d-b.r*b.r)/(2*a.r*d));
	double theta=arg(b.o-a.o);
	return minmax(a.o+polar(a.r,theta+alpha),a.o+polar(a.r,theta-alpha));
}
pair<Point,Point>tangent(const Circle&c,const Point&p){
	return crosspoint(c,Circle(p,sqrt(norm(c.o-p)-c.r*c.r)));
}
vector<Line>tangent(const Circle&a,const Circle&b){
	vector<Line>ret;
	double g=distance(a.o,b.o);
	if(eq(g,0))return ret;
	Point u=(b.o-a.o)/g;
	Point v=rotate(u,M_PI/2);
	for(int s:{-1,1}){
		double h=(a.r+b.r*s)/g;
		if(eq(h*h,1))ret.emplace_back(a.o+(h>0?u:-u)*a.r,a.o+(h>0?u:-u)*a.r+v);
		else if(1-h*h>0){
			Point U=u*h,V=v*sqrt(1-h*h);
			ret.emplace_back(a.o+(U+V)*a.r,b.o-(U+V)*b.r*s);
			ret.emplace_back(a.o+(U-V)*a.r,b.o-(U-V)*b.r*s);
		}
	}
	return ret;
}
bool is_convex(const Polygon&P){
	for(int i=0;i<P.size();i++)
		if(ccw(P[i],P[(i+1)%P.size()],P[(i+2)%P.size()])==CLOCKWISE)return false;
	return true;
}
Polygon convex_hull(Polygon P,bool ONSEG){
	if(P.size()<=2)return P;
	sort(P.begin(),P.end());
	Polygon ret(2*P.size());
	int k=0,t;
	if(ONSEG){
		for(const Point&p:P){
			while(k>=2&&ccw(ret[k-2],ret[k-1],p)==CLOCKWISE)k--;
			ret[k++]=p;
		}
		t=k;
		for(int i=P.size()-2;i>=0;i--){
			while(k>=t+1&&ccw(ret[k-2],ret[k-1],P[i])==CLOCKWISE)k--;
			ret[k++]=P[i];
		}
	}
	else{
		for(const Point&p:P){
			while(k>=2&&ccw(ret[k-2],ret[k-1],p)!=COUNTER_CLOCKWISE)k--;
			ret[k++]=p;
		}
		t=k;
		for(int i=P.size()-2;i>=0;i--){
			while(k>=t+1&&ccw(ret[k-2],ret[k-1],P[i])!=COUNTER_CLOCKWISE)k--;
			ret[k++]=P[i];
		}
	}
	ret.resize(k-1);
	int mi=0;
	for(int i=1;i<k-1;i++)
		if(eq(ret[mi].y,ret[i].y)?ret[mi].x>ret[i].x:ret[mi].y>ret[i].y)mi=i;
	rotate(ret.begin(),ret.begin()+mi,ret.end());
	return ret;
}
int contain(const Polygon&P,const Point&p){
	bool in=false;
	for(int i=0;i<P.size();i++){
		Segment s(P[i],P[(i+1)%P.size()]);
		if(intersect(s,p))return ON;
		else{
			Point a=s.p1-p,b=s.p2-p;
			if(a.y>b.y)swap(a,b);
			if(a.y<EPS&&EPS<b.y&&cross(a,b)>EPS)in=!in;
		}
	}
	return in?IN:OUT;
}
int contain(const Circle&c,const Point&p){
	double d=distance(c.o,p);
	return eq(d,c.r)?ON:d<c.r?IN:OUT;
}
int contain(const Circle&c,const Segment&s){
	double d1=distance(c.o,s.p1),d2=distance(c.o,s.p2);
	return d1<c.r+EPS&&d2<c.r+EPS?eq(d1,c.r)||eq(d2,c.r)?ON:IN:OUT;
}
Polygon convex_cut(const Polygon&P,const Line&s){
	Polygon ret;
	for(int i=0;i<P.size();i++){
		Segment t(P[i],P[(i+1)%P.size()]);
		if(ccw(s,t.p1)!=CLOCKWISE)ret.push_back(t.p1);
		if(!parallel(s,t)&&!intersect(s,t.p1)
			&&!intersect(s,t.p2)&&intersect(s,t))ret.push_back(crosspoint(s,t));
	}
	return ret;
}
double diameter(Polygon P){
	if(!is_convex(P))P=convex_hull(P);
	int mi=0,Mi=0;
	for(int i=1;i<P.size();i++){
		if(P[i].x<P[mi].x)mi=i;
		if(P[i].x>P[Mi].x)Mi=i;
	}
	double ret=0;
	int sm=mi,sM=Mi;
	while(mi!=sM||Mi!=sm){
		ret=max(ret,norm(P[mi]-P[Mi]));
		if(cross(P[(mi+1)%P.size()]-P[mi],P[(Mi+1)%P.size()]-P[Mi])<0)mi=(mi+1)%P.size();
		else Mi=(Mi+1)%P.size();
	}
	return sqrt(ret);
}
double area(const Polygon&P){
	double ret=0;
	for(int i=0;i<P.size();i++)ret+=cross(P[i],P[(i+1)%P.size()]);
	return ret/2;
}
double area(const Polygon&P,const Line&s){return area(convex_cut(P,s));}
double area(const Polygon&P,const Circle&c){
	double ret=0;
	for(int i=0;i<P.size();i++)
	{
		Segment s(P[i],P[(i+1)%P.size()]);
		if(contain(c,s))ret+=cross(s.p1-c.o,s.p2-c.o);
		else if(!intersect(c,s)){
			double a=arg(s.p2-c.o)-arg(s.p1-c.o);
			if(a>M_PI)a-=2*M_PI;
			if(a<-M_PI)a+=2*M_PI;
			ret+=c.r*c.r*a;
		}
		else
		{
			pair<Point,Point>p=crosspoint(c,s);
			Point tmp[4]={s.p1,p.first,p.second,s.p2};
			if(intersect(c,Segment(s.p1,p.first))==2)swap(tmp[1],tmp[2]);
			for(int j=0;j<3;j++)
			{
				Segment t(tmp[j],tmp[j+1]);
				if(contain(c,t))ret+=cross(t.p1-c.o,t.p2-c.o);
				else{
					double a=arg(t.p2-c.o)-arg(t.p1-c.o);
					if(a>M_PI)a-=2*M_PI;
					if(a<-M_PI)a+=2*M_PI;
					ret+=c.r*c.r*a;
				}
			}
		}
	}
	return ret/2;
}
main()
{
	int N;
	while(cin>>N,N)
	{
		int W,H,R,vx,vy;
		cin>>W>>H>>R>>vx>>vy;
		Point v{(double)vx,(double)vy};
		Line L{Point(0.,0.),v};
		v=v/abs(v);
		int X[11],Y[11];
		for(int i=0;i<N;i++)cin>>X[i]>>Y[i];
		int id=-1;
		double dist=1e150;
		for(int i=1;i<N;i++)
		{
			vector<int>Xs,Ys;
			for(int t=-10000;t<=10000;t++)
			{
				int x=t*W+(t%2==0?X[i]:W-X[i]);
				int y=t*H+(t%2==0?Y[i]:H-Y[i]);
				x-=2*R*t;
				y-=2*R*t;
				Xs.push_back(x);
				Ys.push_back(y);
			}
			for(int k=0;k<=10100+2*R;k++)
			{
				Point now=v*k;
				vector<int>::iterator ix=lower_bound(Xs.begin(),Xs.end(),(int)now.x+X[0]);
				vector<int>::iterator iy=lower_bound(Ys.begin(),Ys.end(),(int)now.y+Y[0]);
				for(int iix=-1;iix<=1;iix++)for(int iiy=-1;iiy<=1;iiy++)
				{
					double x=ix[iix],y=iy[iiy];
					Point p{x-X[0],y-Y[0]};
					double d=distance(L,p);
					if(d>2*R)continue;
					double l=dot(p,v)-sqrt(4*R*R-d*d);
					if(0<l&&l<10000+EPS)
					{
						if(dist>l)
						{
							dist=l;
							id=i+1;
						}
					}
				}
			}
		}
		cout<<id<<endl;
	}
}
accuracy: 1
time_ms: 710
memory_kb: 3236
final_rank: 6

code_snippet: #include<bits/stdc++.h>
#define double long double
using namespace std;

typedef complex<double> P;
typedef pair<P,P> L;

const double EPS=1e-8;

double dot(P a, P b){ return real(conj(a)*b); }

double cross(P a, P b){ return imag(conj(a)*b); }

bool isIntersectSP(L s, P p){ 
  return abs(s.first-p) + abs(s.second-p) - abs(s.second-s.first) < EPS; 
}

P proj(P p, L l){
    return l.first + dot(p - l.first, l.second - l.first) / norm(l.second - l.first) * (l.second - l.first);
}

P reflect(P p, L l){
  double x=2;
  P res = p + (proj(p,l) - p) * x;
  int a=res.real()+0.5, b=res.imag()+0.5;
  res=P((double)a, (double)b);
  return res;
}

struct Circle{
  P c; //?????????
  double r; //??????
  Circle(P c=P(0,0), double r=0):c(c),r(r){}
};

int ccw(P a, P b, P c){
  b -= a;
  c -= a;
  if(cross(b,c) > EPS) return 1;   // ????????????(b-a)????????´???????????????(c-a)????????¨?????????
  if(cross(b,c) < -EPS) return -1; // ????????????(b-a)????????´???????????????(c-a)????????¨?????????
  if(dot(b,c) < -EPS) return 2;
  if(norm(b) < norm(c)) return -2;
  return 0;
}

vector<P> getIntersectCL(Circle c, L l){
  vector<P> res;
  P h = proj(c.c, l);
  double d = abs(c.c - h);
  if(d > c.r + EPS);
  else if(d > c.r - EPS) res.push_back(h);
  else{
    P v = l.second - l.first;
    v = (sqrt(c.r*c.r - d*d) / abs(v)) * v;
    res.push_back(h+v);
    res.push_back(h-v);
  }
  return res;
}

bool isIntersectLS(L l, L s){
  return cross(l.second-l.first, s.first-l.first) * cross(l.second-l.first, s.second-l.first) < EPS;
}

P crossPoint(L l, L m){
  double A = cross(l.second - l.first, m.second - m.first);
  double B = cross(l.second - l.first, l.second - m.first);
  if(fabs(A) < EPS && fabs(B) < EPS) return m.first;
  else if(fabs(A) >= EPS) return m.first + B / A * (m.second - m.first);
}

int n, w, h, r, vx, vy;
P p, ball[12], recWS;

void mirror(L l){
  for(int i=0;i<n;i++) ball[i] = reflect(ball[i],l);
}

bool isinrec(P point){
  
  double y1=recWS.imag(), y2=y1+h;
  double x1=recWS.real(), x2=x1+w;
  
  return y1<=point.imag() && point.imag()<=y2 && x1<=point.real() && point.real()<=x2;
}

void solve(){
  
  if(vy<0){
    vy=abs(vy);
    L N = L(P(0, h), P(w, h));
    p = reflect(p, N);
    p = P(p.real(),p.imag()-h);
    mirror(N);
    for(int i=0;i<n;i++) ball[i]=P(ball[i].real(),ball[i].imag()-h);
  }
  
  if(vx<0){
    vx=abs(vx);
    L E = L(P(w, 0), P(w, h));
    p = reflect(p, E);
    p = P(p.real()-w,p.imag());
    mirror(E);
    for(int i=0;i<n;i++) ball[i]=P(ball[i].real()-w,ball[i].imag());
  }
  
  int ans=-3;
  double mindis=1e9;
  
  L l = L(p, P(p.real()+vx, p.imag()+vy));
  
  while(1){
    
    if(abs(recWS-p)>1e4) break;
    
    for(int i=0;i<n;i++){
      
      Circle c = Circle(ball[i], r);
      vector<P> v = getIntersectCL(c, l);
      
      for(int j=0;j<v.size();j++){
	if(dot(l.second-p,v[j]-p)<0) continue;
	if(1e4<abs(p-v[j])) continue;
	
	if(isinrec(v[j])&&mindis>abs(p-v[j])){
	  mindis=abs(p-v[j]);
	  ans=i;
	}
	
      }
    }
    
    L N = L(P(recWS.real(), recWS.imag()+h), P(recWS.real()+w, recWS.imag()+h));
    L E = L(P(recWS.real()+w, recWS.imag()), P(recWS.real()+w, recWS.imag()+h));
    
    int f1=0, f2=0;
    
    if(isIntersectLS(l, N)) f1=1;
    if(isIntersectLS(l, E)) f2=1;
    /*
    P tmp = P(recWS.real()+w, recWS.imag()+h);
    if(abs(ccw(l.first, l.second, tmp))!=1) f1=1, f2=1;
    */
    if(f1){
      mirror(N);
      recWS=P(recWS.real(),recWS.imag()+h);
    }
    
    if(f2){
      mirror(E);
      recWS=P(recWS.real()+w,recWS.imag());
    }
    
  }
  
  cout<<ans+2<<endl;
  
}

int main(){
  
  while(1){

    recWS=P(0,0);
    
    cin>>n;
    if(!n) break;
    
    n--;
    
    cin>>w>>h>>r>>vx>>vy;
    
    int x, y;
    cin>>x>>y;
    
    p = P(x-r, y-r);
    
    for(int i=0;i<n;i++){
      cin>>x>>y;
      ball[i] = P(x-r, y-r);
    }
    
    r*=2;
    w-=r, h-=r;
    
    solve();
    
  }
  
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3044
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
double eps = 1e-10;
struct P{ 
  ll x,y; 
  P(ll a=0,ll b=0): x(a),y(b) {}
};


int n;
ll w,h,x,y,r,W,H;
P A,V;
P t[20];

 
double calc(P p){
  double vx=V.x;
  double vy=V.y;
  double px=p.x-A.x;
  double py=p.y-A.y;
  double dot=vx*px+vy*py;
  if(dot<0)return 1.0/0.0;
  double cross= abs(px*vy-vx*py);
  return cross/sqrt(vx*vx+vy*vy);
}

double calc2(P p){
  double ax=A.x;
  double ay=A.y;
  double px=p.x;
  double py=p.y;
  return (ax-px)*(ax-px)+(ay-py)*(ay-py);
}


void func(){
  double xx=(double)(W-A.x) / (double)V.x;
  double yy=(double)(H-A.y) / (double)V.y;
  if( xx < yy - eps ){
    for(int i=0;i<n;i++)t[i].x=W+W-(t[i].x);
    W+=w;
  }else if( yy < xx - eps ){
    for(int i=0;i<n;i++)t[i].y=H+H-(t[i].y);
    H+=h;
  }else{
    for(int i=0;i<n;i++){
      t[i].x=W+w-t[i].x;
      t[i].y=H+h-t[i].y;
    }
    W+=w;
    H+=h;
  }
}

int solve(){
  for(int I=0;I<100000;I++){

    double mini=1e9;
    int id;
    
    for(int i=0;i<n;i++){
      double dist=calc(t[i]);
      if(dist>r+r)continue;
      double dist3=sqrt(abs( 4.0*(double)r*(double)r-dist*dist ));
      double dist4=sqrt(abs(calc2(t[i])-dist*dist))-dist3;
      if(dist4<mini)mini=dist4,id=i;
    }
    
    if(mini<1e9){
      if(mini>10000.0)break;
      /*
      cout<<mini<<endl;
      cout<<A.x<<' '<<A.y<<endl;
      cout<<t[0].x<<' '<<t[0].y<<endl;
      double dist=calc(t[0]);
      double dist3=sqrt(abs(4.0*(double)r*(double)r-dist*dist));
      double dist4=sqrt(abs(calc2(t[0])-dist*dist))-dist3;
      cout<<dist<<' '<<calc2(t[0])<<' '<<dist3<<' '<<dist4<<endl;
      */
      return id+2;
    }
    func();
  }
  return -1;
}

int main(){
  while(1){
    cin>>n;
    if(n==0)break;
    n--;
    cin>>w>>h>>r;
    W=w;H=h;
    cin>>V.x>>V.y>>A.x>>A.y;
    for(int i=0;i<n;i++)cin>>t[i].x>>t[i].y;
    if(V.x<0){
      V.x*=-1;
      for(int i=0;i<n;i++)t[i].x=w-t[i].x;
      A.x=w-A.x;
    }
    if(V.y<0){
      V.y*=-1;
      for(int i=0;i<n;i++)t[i].y=h-t[i].y;
      A.y=h-A.y;
    }
    W-=r;
    H-=r;
    h-=r;   h-=r;
    w-=r;   w-=r;

    cout<<solve()<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 1172
final_rank: 1

code_snippet: #include <cmath>
#include <cstdio>
#include <vector>
#include <algorithm>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const double EPS=1e-9;
const double INF=1e77;

struct point{
	double x,y;
	point operator+(const point &p)const{ return (point){x+p.x,y+p.y}; }
	point operator-(const point &p)const{ return (point){x-p.x,y-p.y}; }
};

point operator*(double c,const point &p){ return (point){c*p.x,c*p.y}; }

struct line{ point a,b; };

struct segment{
	point a,b;
	operator line()const{ return (line){a,b}; }
};

struct circle{
	point c;
	double r;
};

double dot(const point &a,const point &b){
	return a.x*b.x+a.y*b.y;
}

double abs2(const point &p){
	return p.x*p.x+p.y*p.y;
}

double abs(const point &p){
	return sqrt(abs2(p));
}

point proj(const point &p,const line &L){
	return L.a+dot(p-L.a,L.b-L.a)/abs2(L.b-L.a)*(L.b-L.a);
}

double dist(const point &a,const point &b){
	return abs(a-b);
}

bool cover(const segment &S,const point &p){
	return dist(S.a,p)+dist(p,S.b)<dist(S.a,S.b)+EPS;
}

int get_intersect(const circle &C,const segment &S,vector<point> &res){
	point h=proj(C.c,S);
	double d=dist(C.c,h);
	if(d>C.r+EPS) return 0;
	if(d>C.r-EPS){
		int cnt=0;
		if(cover(S,h)) res.push_back(h), cnt++;
		return cnt;
	}
	else{
		point v=S.b-S.a;
		v=(sqrt(C.r*C.r-d*d)/abs(v))*v;
		int cnt=0;
		if(cover(S,h+v)) res.push_back(h+v), cnt++;
		if(cover(S,h-v)) res.push_back(h-v), cnt++;
		return cnt;
	}
}

double solve(int h,int w,double r,double vx,double vy,point p,point q){
	if(vx<=0){
		vx*=-1;
		p.x=w-p.x;
		q.x=w-q.x;
	}
	if(vy<=0){
		vy*=-1;
		p.y=h-p.y;
		q.y=h-q.y;
	}

	double v=sqrt(vx*vx+vy*vy);
	vx/=v;
	vy/=v;

	segment S={p,p+(point){10000*vx,10000*vy}};
	int m=(10000*vy)/h+10;
	int n=(10000*vx)/w+10;
	circle C; C.r=2*r;
	vector<point> res;
	rep(i,m) rep(j,n) {
		C.c.x=j*w+(j%2==0?q.x:w-q.x);
		C.c.y=i*h+(i%2==0?q.y:h-q.y);
		get_intersect(C,S,res);
	}

	double d=INF;
	rep(i,res.size()) d=min(d,dist(p,res[i]));
	return d;
}

int main(){
	for(int n;scanf("%d",&n),n;){
		n--;
		int w,h,r,vx,vy; scanf("%d%d%d%d%d",&w,&h,&r,&vx,&vy);
		point p0,p[10];
		scanf("%lf%lf",&p0.x,&p0.y);
		rep(i,n) scanf("%lf%lf",&p[i].x,&p[i].y);

		w-=2*r;
		h-=2*r;
		p0.x-=r;
		p0.y-=r;
		rep(i,n){
			p[i].x-=r;
			p[i].y-=r;
		}

		int ans=-1;
		double d_ans=INF;
		rep(i,n){
			double d=solve(h,w,r,vx,vy,p0,p[i]);
			if(d_ans>d) d_ans=d, ans=i+2;
		}
		printf("%d\n",ans);
	}

	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 1188
final_rank: 2

code_snippet: #include <iostream>
#include <algorithm>
#include <cmath>
#include <complex>
#include <vector>
using namespace std;
typedef long long lli;
typedef complex<lli> P;
typedef complex<double> Pd;

const lli INF = 1LL<<60LL;
const int MAXN = 12;
const double inf = 1e20;
const double eps = 1e-9;

bool equals(double a, double b) {
  return abs(a-b) < eps;
}

int N;
lli W, H, R, Vx, Vy;
P Bs[MAXN];
vector<P> ps;
vector<int> indexs;

struct C {
  Pd o;
  double r;
};

double dot(Pd a, Pd b) {
  return a.real()*b.real() + a.imag()*b.imag();
}

double cross(Pd a, Pd b) {
  return a.real()*b.imag() - a.imag()*b.real();
}

Pd project(Pd s1, Pd s2, Pd p) {
  Pd base = s2 - s1;
  double t = dot(p - s1, base)/norm(base);
  return s1 + base*t;
}

double getDistanceLP(Pd s1, Pd s2, Pd p) {
  return abs(cross(s2 - s1, p - s1)/abs(s2 - s1));
}

int isIntersect(Pd s1, Pd s2, C c) {
  double d = getDistanceLP(s1, s2, c.o);
  if(equals(d, c.r)) return 1;
  else if(d < c.r) return 2;
  else return 0;
}

vector<Pd> getCrossP(Pd s1, Pd s2, C c) {
  vector<Pd> v;
  Pd p = project(s1, s2, c.o);
  if(isIntersect(s1, s2, c) == 0) return v;
  if(isIntersect(s1, s2, c) == 1) {
    v.push_back(p);
  } else {
    double h = abs(p-c.o);
    double d = sqrt(c.r*c.r - h*h);
    Pd base = s2 - s1;
    v.push_back(p + d*base/abs(base));
    v.push_back(p - d*base/abs(base));
  }
  return v;
}

double getDist(P p) {
  Pd pd(p.real(), p.imag());
  Pd s1 = Pd(Bs[0].real(), Bs[0].imag());
  Pd s2 = s1 + Pd(Vx, Vy);
  Pd n1 = s1;
  Pd n2 = n1 + Pd(-Vy, Vx);
  vector<Pd> v = getCrossP(s1, s2, (C){pd, 2*R});
  double res = inf;
  if(v.size() == 2) {
    for(int i = 0; i < v.size(); ++i) {
      if(cross(n2 - n1, v[i] - n1) > 0.0) continue;
      res = min(res, abs(v[i] - n1));
    }
  }
  return res;
}

int main() {
  while(cin >> N && N) {
    cin >> W >> H >> R >> Vx >> Vy;
    W = W - 2*R;
    H = H - 2*R;
    for(int i = 0; i < N; ++i) {
      cin >> Bs[i].real() >> Bs[i].imag();
      Bs[i].real() -= R;
      Bs[i].imag() -= R;
    }
    ps.clear();
    indexs.clear();
    vector<P> ptmp;
    vector<int> itmp;
    for(int i = 1; i < N; ++i) {
      lli dx1 = Bs[i].real();
      lli dx2 = W - dx1;
      for(lli x = Bs[i].real(); labs(x) < 13000LL; ) {
        ptmp.push_back(P(x, Bs[i].imag()));
        itmp.push_back(i);
        x += 2 * dx2;
        ptmp.push_back(P(x, Bs[i].imag()));
        itmp.push_back(i);
        x += 2 * dx1;
      }
      for(lli x = Bs[i].real(); labs(x) < 13000LL; ) {
        ptmp.push_back(P(x, Bs[i].imag()));
        itmp.push_back(i);
        x -= 2 * dx1;
        ptmp.push_back(P(x, Bs[i].imag()));
        itmp.push_back(i);
        x -= 2 * dx2;
      }
    }
    Pd s1 = Pd(Bs[0].real(), Bs[0].imag());
    Pd s2 = s1 + Pd(Vx, Vy);
    for(int i = 0; i < ptmp.size(); ++i) {
      lli dy1 = ptmp[i].imag();
      lli dy2 = H - dy1;
      for(lli y = ptmp[i].imag(); labs(y) < 13000LL; ) {
        do {
          Pd p(ptmp[i].real(), y);
          if(getDistanceLP(s1, s2, p) > 3*R) break;
          ps.push_back(P(ptmp[i].real(), y));
          indexs.push_back(itmp[i]);
        } while(0);
        y += 2 * dy2;

        do {
          Pd p(ptmp[i].real(), y);
          if(getDistanceLP(s1, s2, p) > 3*R) break;
          ps.push_back(P(ptmp[i].real(), y));
          indexs.push_back(itmp[i]);
        } while(0);
        y += 2 * dy1;
      }
      for(lli y = ptmp[i].imag(); labs(y) < 13000LL; ) {
        do {
          Pd p(ptmp[i].real(), y);
          if(getDistanceLP(s1, s2, p) > 3*R) break;
          ps.push_back(P(ptmp[i].real(), y));
          indexs.push_back(itmp[i]);
        } while(0);
        y -= 2 * dy1;
        do {
          Pd p(ptmp[i].real(), y);
          if(getDistanceLP(s1, s2, p) > 3*R) break;
          ps.push_back(P(ptmp[i].real(), y));
          indexs.push_back(itmp[i]);
        } while(0);
        y -= 2 * dy2;
      }
    }

    double mini = inf;
    int res = -1;
    for(int i = 0; i < ps.size(); ++i) {
      double dist = getDist(ps[i]);
      if(dist < mini) {
        mini = dist;
        res = indexs[i]+1;
      }
    }
    if(mini >= 10000) res = -1;
    cout << res << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 1110
memory_kb: 1540
final_rank: 3