#P7466. Red Particle Reflection

    ID: 20661 Type: Default 1000ms 256MiB

Red Particle Reflection

Red Particle Reflection

In a physics experiment you study a rare unstable particle called the red particle. A red particle travels in a straight line until it encounters a mirror. In the experiment, several one‐time reflection mirrors are arranged on an infinite two‐dimensional plane. A one–time mirror reflects the particle on its first encounter (from any direction) and then thereafter becomes transparent. Note that if a particle reaches an intersection point of two mirrors (i.e. it meets two mirrors at the same time), the particle decays immediately.

Your lab supervisor has fixed the positions of the mirrors. Starting from a given point S, you wish to count how many distinct launching directions will eventually bring the red particle to a target point T following the laws of reflection (i.e. the angle of incidence equals the angle of reflection) and obeying the one–time reflection rule. Note that if the red particle would hit a mirror that has not yet reflected it before reaching T, then it will be reflected and will not go along the direct S→T line. In other words, a valid launching direction must be such that the particle exactly reaches T under the rule that every mirror it encounters (for the first time) reflects it, and if it hits a mirror intersection (where two or more mirrors meet simultaneously) it decays.

The theoretical solution uses the mirror–image method. For any valid sequence of reflections (i.e. an ordered subset of mirrors that are hit in that order), if one reflects the target point T across these mirrors (in reverse order) to get an image T*, then the unique launching direction is along the ray from S to T*. (Any two sequences that yield the same launching direction are counted only once.)

You are given the equation of each mirror in the form of a linear equation in \(ax+by+c=0\) (in \(\LaTeX\) format). The starting point S and the target point T are also given. In this problem the mirrors may cause reflections, but in some configurations the direct path from S to T (which does not hit any mirror) is valid. (Note that if S→T would normally cross a mirror then the mirror would reflect the particle rather than allow it to go straight.)

Your task is to output the number of distinct launching directions from S that will end at T.

inputFormat

The input begins with an integer n (\(0 \le n \le 10\)) indicating the number of mirrors. The next line contains four real numbers: Sx Sy Tx Ty, the coordinates of the starting point S and the target point T. Each of the next n lines contains three real numbers a b c representing a mirror with equation \(ax+by+c=0\). It is guaranteed that each mirror is an infinite straight line. All real numbers are given with at most 6 digits after the decimal point.

Note: In some test cases the mirrors are arranged so that the direct ray from S to T does not hit any mirror (because it reaches T before any mirror is encountered). In such cases that launching direction is valid. In general a valid launching direction is one for which the particle exactly reaches T obeying the one–time reflection rule.

outputFormat

Output a single integer—the number of distinct launching directions from S that bring the red particle to T.

sample

0
0 0 1 0
1