This page is still under construction.

Parts of this page are still being built. What you see may change.

Cut the Cake

Time limit20sMemory limit128 MB

Summary
Count into how many regions the given infinite lines divide a circle.
Level

Medium5 of 10

Topics
Geometry, Combinatorics
Solved
No attempts yet

Problem

You are given a circle and a list of lines. Count how many parts the lines cut the circle into.

Every line extends infinitely in both directions. A line that never meets the circle does not cut it.

Input

The input has several test cases. Each test case begins with four integers rr (1≤r≤10001 \le r \le 1000), xx, yy (−1000≤x,y≤1000-1000 \le x, y \le 1000), and nn (0≤n≤10000 \le n \le 1000). Here rr is the radius of the circle, (x,y)(x, y) is its center, and nn is the number of lines.

Each of the next nn lines contains four integers x1x_1, y1y_1, x2x_2, y2y_2 (−1000≤x1,y1,x2,y2≤1000-1000 \le x_1, y_1, x_2, y_2 \le 1000). These four integers describe the line through (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2). What matters is the whole infinite line, not the segment between the two points.

In every test case, no more than two lines meet at any point inside the circle, no line is tangent to the circle, and no two lines are the same line.

The input ends with a line of four zeros.

Output

For each test case, print a single integer on its own line: the number of parts the circle is cut into. Print no spaces and no blank lines.

Examples1

  1. Example 1

    Input
    16 1 7 4
    -15 -9 14 -11
    -4 30 -3 -20
    -20 12 -10 7
    17 10 31 0
    0 0 0 0
    
    Expected output
    5