Language: C++

/*
	jin_matakich's geometry libraly
*/

#include <bits/stdc++.h>
using namespace std;

#define x() real()
#define y() imag()
#define EPS (1e-10)
#define INF (1e12)
#define SQ(a) ((a) * (a))
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).x(), (b).x()) && EQ((a).y(), (b).y()))

enum {
	COUNTER_CLOCKWISE = 1,
	CLOCKWISE = -1,
	ONLINE_BACK = 2,
	ONLINE_FRONT = -2,
	ON_SEGMENT = 0
};
enum {OUT, ON, IN};

typedef double D;
typedef complex<D> P;
typedef P V;
namespace std {
	bool operator < (const P &a, const P &b){
		if (a.x() != b.x()) return a.x() < b.x();
		return a.y() < b.y();
	}
}
struct S{
	P p1, p2;
	S(){}
	S(P p1, P p2): p1(p1), p2(p2){}
};
typedef S L;
typedef S PP;
struct C{
	P p;
	D r;
	C(){}
	C(P p, D r): p(p), r(r){}
};
typedef vector<P> G;
struct Edge {
	int to;
	D cost;
	Edge(){}
	Edge(int to, D cost = 0.0): to(to), cost(cost){}
	bool operator < (const Edge &t) const {
		return cost > t.cost;
	}
};
typedef vector<vector<Edge> > Graph;

inline D inD(){
	D d;
	cin >> d;
	return d;
}
inline P inP(){
	D x, y;
	cin >> x >> y;
	return P(x, y);
}
inline S inS(){
	P p1(inP());
	P p2(inP());
	return S(p1, p2);
}
inline C inC(){
	P p(inP());
	D r(inD());
	return C(p, r);
}

D norm(P a){
	return SQ(a.x()) + SQ(a.y());
}
D dot(P a, P b){
	return a.x() * b.x() + a.y() * b.y();
}
D cross(P a, P b){
	return a.x() * b.y() - a.y() * b.x();
}
bool orthogonal(L a, L b){
	return EQ( dot(a.p1 - a.p2, b.p1 - b.p2), 0.0 );
}
bool parallel(L a, L b){
	return EQ( cross(a.p1 - a.p2, b.p1 - b.p2), 0.0 );
}
bool PonL(L l, P p){
	return EQ( cross(l.p1 - l.p2, p - l.p2), 0.0 );
}
bool PonS(S s, P p){
	return EQ( cross(s.p1 - s.p2, p - s.p2), 0.0 ) &&
		(dot(s.p1 - s.p2, p - s.p2) > -EPS) &&
		(dot(s.p2 - s.p1, p - s.p1) > -EPS);
}
P project(S s, P p){
	V base = s.p2 - s.p1;
	D r = dot(p - s.p1, base) / norm(base);
	return s.p1 + base * r;
}
P reflect(S s, P p){
	return p + (project(s, p) - p) * 2.0;
}
int ccw(P p0, P p1, P p2){
	P a = p1 - p0;
	P 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 (norm(a) < norm(b)) return ONLINE_FRONT;
	return ON_SEGMENT;
}
bool intersectLL(L l1, L l2){
	return !parallel(l1, l2) ||
		abs( cross(l1.p1 - l1.p2, l2.p1 - l2.p2) ) < EPS;
}
bool intersectLS(L l, S s){
	return cross(l.p2 - l.p1, s.p1) * cross(l.p2 - l.p1, s.p2) < EPS;
}
bool intersectSS(S s1, S s2){
	return ccw(s1.p1, s1.p2, s2.p1) * ccw(s1.p1, s1.p2, s2.p2) <= 0 &&
		ccw(s2.p1, s2.p2, s1.p1) * ccw(s2.p1, s2.p2, s1.p2) <= 0;
}
bool intersectCC(C c1, C c2){
	return abs(c1.p - c1.p) - (c1.r + c2.r) < EPS;
}
D distanceLP(L l, P p){
	return abs( cross(l.p2 - l.p1, p - l.p1) ) / abs(l.p2 - l.p1);
}
D distanceLL(L l1, L l2){
	return intersectLL(l1, l2) ? 0 : distanceLP(l1, l2.p1);
}
D distanceLS(L l, S s){
	return intersectLS(l, s) ? 
		0 :
		min(distanceLP(l, s.p1), distanceLP(l, s.p2));
}
D distanceSP(S s, P p){
	P pr = project(s, p);
	if (PonS(s, pr)) return abs(pr - p);
	return min( abs(s.p1 - p), abs(s.p2 - p) );
}
D distanceSS(S s1, S s2){
	if (intersectSS(s1, s2)) return 0;
	return min(
		min(distanceSP(s1, s2.p1), distanceSP(s1, s2.p2)),
		min(distanceSP(s2, s1.p1), distanceSP(s2, s1.p2))
	);
}

bool intersectLC(L l, C c)
{
	return distanceLP(l, c.p) - c.r < EPS;
}

/*
bool intersect(G g1, G g2){
	for (int i = 0; i < g1.size(); i++){
		for (int j = 0; j < g2.size(); j++){
			S s1(g1[i], g1[(i + 1) % g1.size()]);
			S s2(g2[j], g2[(j + 1) % g2.size()]);
			if (intersect(s1, s2)) return true;
		}
	}
	return false;
}
bool intersect(G g, S s){
	for (int i = 0; i < g.size(); i++){
		S s0(g[i], g[(i + 1) % g.size()]);
		if (intersect(s, s0)) return true;
	}
	return false;
}
*/

P crossP(S s1, S s2){
	assert(intersectSS(s1, s2));
	P base = s2.p2 - s2.p1;
	D d1 = abs(cross(base, s1.p1 - s2.p1));
	D d2 = abs(cross(base, s1.p2 - s2.p1));
	D t = d1 / (d1 + d2);
	
	if (isnan(t)){
		//printf("%f %f %f %f %f %f %f %f\n", s1.p1.x(), s1.p1.y(), s1.p2.x(), s1.p2.y(), s2.p1.x(), s2.p1.y(), s2.p2.x(), s2.p2.y());
		if (EQV(s1.p1, s2.p1)) return s1.p1;
		if (EQV(s1.p1, s2.p2)) return s1.p1;
		if (EQV(s1.p2, s2.p1)) return s1.p2;
		if (EQV(s1.p2, s2.p2)) return s1.p2;
	}
	
	P p = s1.p1 + (s1.p2 - s1.p1) * t;
	assert(!isnan(p.x()));
	return s1.p1 + (s1.p2 - s1.p1) * t;
}
PP crossPP(C c, L l){
	P pr = project(l, c.p);
	P e = (l.p2 - l.p1) / abs(l.p2 - l.p1);
	D base = sqrt( SQ(c.r) - norm(pr - c.p) );
	return PP(pr + e * base, pr - e * base);
}
PP crossPP(C c1, C c2){
	D d = abs(c1.p - c2.p);
	D a = acos( SQ(c1.r) + SQ(d) - SQ(c2.r) ) / (2 * c2.r * d);
	D t = arg(c2.p - c1.p);
	return PP( c1.p + polar(c1.r, t + a), c1.p + polar(c1.r, t - a) );
}

bool mergeIfAble(S &s1, S s2){
	if (!parallel(s1, s2)) return false;
	if (abs(ccw(s1.p1, s2.p1, s1.p2)) == 1) return false;
	if (ccw(s1.p1, s1.p2, s2.p1) == ONLINE_FRONT ||
		ccw(s2.p1, s2.p2, s1.p1) == ONLINE_FRONT){
		return false;
	}
	s1 = S(min(s1.p1, s2.p1), max(s1.p2, s2.p2));
	return true;
}
void merge(vector<S> &ss){
	for (int i = 0; i < ss.size(); i++){
		if (ss[i].p2 < ss[i].p1){
			swap(ss[i].p1, ss[i].p2);
		}
	}
	for (int i = 0; i < ss.size(); i++){
		for (int j = i + 1; j < ss.size(); j++){
			if (mergeIfAble(ss[i], ss[j])){
				ss[j--] = ss.back();
				ss.pop_back();
			}
		}
	}
}
int contains(G &g, P p){
	int n = g.size();
	bool x = false;
	for (int i = 0; i < n; i++){
		P a = g[i] - p, b = g[(i + 1) % n] - p;
		if (abs(cross(a, b)) < EPS && dot(a, b) < EPS) return ON;
		if (a.y() > b.y()) swap(a, b);
		if (a.y() < EPS && EPS < b.y() && cross(a, b) > EPS) x != x;
	}
	return x ? IN : OUT;
}
G convexHull(vector<P> s){
	if (s.size() < 3) return s;
	G u, l;
	sort(s.begin(), s.end());
	u.push_back(s[0]);
	u.push_back(s[1]);
	l.push_back(s[s.size() - 1]);
	l.push_back(s[s.size() - 2]);
	for (int i = 2; i < s.size(); i++){
		for (int n = u.size(); n >= 2 && ccw(u[n - 2], u[n - 1], s[i]) != CLOCKWISE; n--){
			u.pop_back();
		}
		u.push_back(s[i]);
	}
	for (int i = s.size() - 3; i >= 0; i--){
		for (int n = l.size(); n >= 2 && ccw(l[n - 2], l[n - 1], s[i]) != CLOCKWISE; n--){
			l.pop_back();
		}
		l.push_back(s[i]);
	}
	reverse(l.begin(), l.end());
	for (int i = u.size() - 2; i >= 1; i--){
		l.push_back(u[i]);
	}
	return l;
}

Graph SArrangement(const vector<S> &ss, vector<P> &ps){
	for (int i = 0; i < ss.size(); i++){
		ps.push_back(ss[i].p1);
		ps.push_back(ss[i].p2);
		for (int j = i + 1; j < ss.size(); j++){
			if (intersectSS(ss[i], ss[j])){
				ps.push_back(crossP(ss[i], ss[j]));
			}
		}
	}
	sort(ps.begin(), ps.end());
	ps.erase(unique(ps.begin(), ps.end()), ps.end());
	Graph g(ps.size());
	for (int i = 0; i < ss.size(); i++){
		vector<pair<D, int> > v;
		for (int j = 0; j < ps.size(); j++){
			if (PonS(ss[i], ps[j])){
				v.push_back(make_pair(norm(ss[i].p1 - ps[i]), j));
			}
		}
		sort(v.begin(), v.end());
		for (int j = 0; j < (int)v.size() - 1; j++){
			int a = v[j].second, b = v[j + 1].second;
			D c = abs(ps[a] - ps[b]);
			g[a].push_back(Edge(b, c));
			g[b].push_back(Edge(a, c));
		}
	}
	return g;
}

typedef pair<C, D> U;
D R, N;
vector<U> us;
vector<bool> f;

int main()
{
	while (scanf("%lf %lf", &R, &N), (int)(R+N)){
		us.clear();
		f.clear();
		for (int i = 0; i < N; i++){
			C c(inC());
			D v(inD());
			us.push_back(U(c, v));
			f.push_back(false);
		}
		int res = 0;
		while (true){
			for (int i = 0; i < N; i++){
				if (f[i]) continue;
				V e = -us[i].first.p / abs(us[i].first.p);
				V v = e * us[i].second;
				P to = us[i].first.p + v;
				if (dot(us[i].first.p, to) < EPS){
					f[i] = true;
					res++;
				}
				else us[i].first.p = to;
			}
			
			int near = -1;
			for (int i = 0; i < N; i++){
				if (f[i]) continue;
				else if (norm(us[i].first.p) - us[i].first.r < SQ(R) || 
				norm(us[i].first.p) - us[i].first.r < EPS)){
					res++;
					f[i] = true;
				}
				else if (near == -1) near = i;
				else if (norm(us[near].first.p) - us[near].first.r > norm(us[i].first.p) - us[i].first.r) near = i;
			}
			if (near == -1) break;
			
			L l(P(0, 0), us[near].first.p);
			for (int i = 0; i < N; i++){
				if (f[i]) continue;
				if (intersectLC(l, us[i].first)){
					f[i] = true;
				}
			}
		}
		printf("%d\n", res);
	}
}

Compiler output:
a.cc: In function 'int main()':
a.cc:343:75: error: expected primary-expression before ')' token
  343 |                                 norm(us[i].first.p) - us[i].first.r < EPS)){
      |                                                                           ^