Language: C++

#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;
  return p + (proj(p,l) - p) * x;
}

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(){
  int F1=0, F2=0;
  
  if(vy<0){
    vy=abs(vy);
    L N = L(P(0, h), P(w, h));
    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);
    F1=1;
  }
  
  if(vx<0){
    vx=abs(vx);
    L E = L(P(w, 0), P(w, h));
    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());
    F2=1;
  }
  

  
  int ans=-3;
  double mindis=1e9;
  
  L l = L(p, P(p.real()+vx, p.imag()+vy));
  int cnt=0;
  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(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;
    
    if(f1){
      mirror(N);
      recWS=P(recWS.real(),recWS.imag()+h);
    }
    
    if(f2){
      mirror(E);
      recWS=P(recWS.real()+w,recWS.imag());
    }
    
    //if(!f1&&!f2&&(F1||F2)) assert(cnt);
    //if(!F1&&!F2) assert(f1||f2);
    cnt++;
  }
  
  cout<<ans+2<<endl;
  
}

int main(){
  
  while(1){
    
    cin>>n;
    if(!n) break;
    
    n--;
    
    cin>>w>>h>>r>>vx>>vy;
    
    int x, y;
    cin>>x>>y;

    p = P(x-r, y-r);
    assert(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;
    
    recWS=P(0,0);
    
    solve();
    
  }
  
  return 0;
}
#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;
  return p + (proj(p,l) - p) * x;
}

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(){
  int F1=0, F2=0;
  
  if(vy<0){
    vy=abs(vy);
    L N = L(P(0, h), P(w, h));
    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);
    F1=1;
  }
  
  if(vx<0){
    vx=abs(vx);
    L E = L(P(w, 0), P(w, h));
    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());
    F2=1;
  }
  

  
  int ans=-3;
  double mindis=1e9;
  
  L l = L(p, P(p.real()+vx, p.imag()+vy));
  int cnt=0;
  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(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;
    
    if(f1){
      mirror(N);
      recWS=P(recWS.real(),recWS.imag()+h);
    }
    
    if(f2){
      mirror(E);
      recWS=P(recWS.real()+w,recWS.imag());
    }
    
    //if(!f1&&!f2&&(F1||F2)) assert(cnt);
    //if(!F1&&!F2) assert(f1||f2);
    cnt++;
  }
  
  cout<<ans+2<<endl;
  
}

int main(){
  
  while(1){
    
    cin>>n;
    if(!n) break;
    
    n--;
    
    cin>>w>>h>>r>>vx>>vy;
    
    int x, y;
    cin>>x>>y;

    p = P(x-r, y-r);
    assert(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;
    
    recWS=P(0,0);
    
    solve();
    
  }
  
  return 0;
}

Compiler output:
a.cc:201:14: error: redefinition of 'const long double EPS'
  201 | const double EPS=1e-8;
      |              ^~~
a.cc:8:14: note: 'const long double EPS' previously defined here
    8 | const double EPS=1e-8;
      |              ^~~
a.cc:203:8: error: redefinition of 'long double dot(P, P)'
  203 | double dot(P a, P b){ return real(conj(a)*b); }
      |        ^~~
a.cc:10:8: note: 'long double dot(P, P)' previously defined here
   10 | double dot(P a, P b){ return real(conj(a)*b); }
      |        ^~~
a.cc:205:8: error: redefinition of 'long double cross(P, P)'
  205 | double cross(P a, P b){ return imag(conj(a)*b); }
      |        ^~~~~
a.cc:12:8: note: 'long double cross(P, P)' previously defined here
   12 | double cross(P a, P b){ return imag(conj(a)*b); }
      |        ^~~~~
a.cc:207:6: error: redefinition of 'bool isIntersectSP(L, P)'
  207 | bool isIntersectSP(L s, P p){
      |      ^~~~~~~~~~~~~
a.cc:14:6: note: 'bool isIntersectSP(L, P)' previously defined here
   14 | bool isIntersectSP(L s, P p){
      |      ^~~~~~~~~~~~~
a.cc:211:3: error: redefinition of 'P proj(P, L)'
  211 | P proj(P p, L l){
      |   ^~~~
a.cc:18:3: note: 'P proj(P, L)' previously defined here
   18 | P proj(P p, L l){
      |   ^~~~
a.cc:215:3: error: redefinition of 'P reflect(P, L)'
  215 | P reflect(P p, L l){
      |   ^~~~~~~
a.cc:22:3: note: 'P reflect(P, L)' previously defined here
   22 | P reflect(P p, L l){
      |   ^~~~~~~
a.cc:220:8: error: redefinition of 'struct Circle'
  220 | struct Circle{
      |        ^~~~~~
a.cc:27:8: note: previous definition of 'struct Circle'
   27 | struct Circle{
      |        ^~~~~~
a.cc:226:5: error: redefinition of 'int ccw(P, P, P)'
  226 | int ccw(P a, P b, P c){
      |     ^~~
a.cc:33:5: note: 'int ccw(P, P, P)' previously defined here
   33 | int ccw(P a, P b, P c){
      |     ^~~
a.cc:236:11: error: redefinition of 'std::vector<std::complex<long double> > getIntersectCL(Circle, L)'
  236 | vector<P> getIntersectCL(Circle c, L l){
      |           ^~~~~~~~~~~~~~
a.cc:43:11: note: 'std::vector<std::complex<long double> > getIntersectCL(Circle, L)' previously defined here
   43 | vector<P> getIntersectCL(Circle c, L l){
      |           ^~~~~~~~~~~~~~
a.cc:251:6: error: redefinition of 'bool isIntersectLS(L, L)'
  251 | bool isIntersectLS(L l, L s){
      |      ^~~~~~~~~~~~~
a.cc:58:6: note: 'bool isIntersectLS(L, L)' previously defined here
   58 | bool isIntersectLS(L l, L s){
      |      ^~~~~~~~~~~~~
a.cc:255:3: error: redefinition of 'P crossPoint(L, L)'
  255 | P crossPoint(L l, L m){
      |   ^~~~~~~~~~
a.cc:62:3: note: 'P crossPoint(L, L)' previously defined here
   62 | P crossPoint(L l, L m){
      |   ^~~~~~~~~~
a.cc:262:5: error: redefinition of 'int n'
  262 | int n, w, h, r, vx, vy;
      |     ^
a.cc:69:5: note: 'int n' previously declared here
   69 | int n, w, h, r, vx, vy;
      |     ^
a.cc:262:8: error: redefinition of 'int w'
  262 | int n, w, h, r, vx, vy;
      |        ^
a.cc:69:8: note: 'int w' previously declared here
   69 | int n, w, h, r, vx, vy;
      |        ^
a.cc:262:11: error: redefinition of 'int h'
  262 | int n, w, h, r, vx, vy;
      |           ^
a.cc:69:11: note: 'int h' previously declared here
   69 | int n, w, h, r, vx, vy;
      |           ^
a.cc:262:14: error: redefinition of 'int r'
  262 | int n, w, h, r, vx, vy;
      |              ^
a.cc:69:14: note: 'int r' previously declared here
   69 | int n, w, h, r, vx, vy;
      |              ^
a.cc:262:17: error: redefinition of 'int vx'
  262 | int n, w, h, r, vx, vy;
      |                 ^~
a.cc:69:17: note: 'int vx' previously declared here
   69 | int n, w, h, r, vx, vy;
      |                 ^~
a.cc:262:21: error: redefinition of 'int vy'
  262 | int n, w, h, r, vx, vy;
      |                     ^~
a.cc:69:21: note: 'int vy' previously declared here
   69 | int n, w, h, r, vx, vy;
      |                     ^~
a.cc:263:3: error: redefinition of 'P p'
  263 | P p, ball[12], recWS;
      |   ^
a.cc:70:3: note: 'P p' previously defined here
   70 | P p, ball[12], recWS;
      |   ^
a.cc:263:6: error: redefinition of 'P ball [12]'
  263 | P p, ball[12], recWS;
      |      ^~~~
a.cc:70:6: note: 'P ball [12]' previously defined here
   70 | P p, ball[12], recWS;
      |      ^~~~
a.cc:263:16: error: redefinition of 'P recWS'
  263 | P p, ball[12], recWS;
      |                ^~~~~
a.cc:70:16: note: 'P recWS' previously defined here
   70 | P p, ball[12], recWS;
      |                ^~~~~
a.cc:265:6: error: redefinition of 'void mirror(L)'
  265 | void mirror(L l){
      |      ^~~~~~
a.cc:72:6: note: 'void mirror(L)' previously defined here
   72 | void mirror(L l){
      |      ^~~~~~
a.cc:269:6: error: redefinition of 'bool isinrec(P)'
  269 | bool isinrec(P point){
      |      ^~~~~~~
a.cc:76:6: note: 'bool isinrec(P)' previously defined here
   76 | bool isinrec(P point){
      |      ^~~~~~~
a.cc:275:6: error: redefinition of 'void solve()'
  275 | void solve(){
      |      ^~~~~
a.cc:82:6: note: 'void solve()' previously defined here
   82 | void solve(){
      |      ^~~~~
a.cc:355:5: error: redefinition of 'int main()'
  355 | int main(){
      |     ^~~~
a.cc:162:5: note: 'int main()' previously defined here
  162 | int main(){
      |     ^~~~
a.cc: In function 'P crossPoint(L, L)':
a.cc:67:1: warning: control reaches end of non-void function [-Wreturn-type]
   67 | }
      | ^
a.cc: In function 'P crossPoint(L, L)':
a.cc:260:1: warning: control reaches end of non-void function [-Wreturn-type]
  260 | }
      | ^