#P4276. Laser Reflection Angle Challenge

    ID: 17522 Type: Default 1000ms 256MiB

Laser Reflection Angle Challenge

Laser Reflection Angle Challenge

In this problem, several UFOs (represented as convex polygons with at most 5 edges) are deployed on a plane. Before the game, each UFO is rotated clockwise about its centroid by (k \times 90^\circ) (where (k) is an integer between 0 and 3). A laser is fired from the origin ((0, 0)) at a chosen angle (\theta) (in degrees, measured from the positive x-axis). Each UFO has active edges (all edges initially active). When the laser beam hits an active edge, it reflects according to the law of reflection (angle of incidence equals angle of reflection) and its energy increases by (w) (a positive real number). After an edge reflects the beam once, it becomes transparent for any subsequent hits.

For simplicity, you may assume that for every integer (r) between 0 and (S) (where (S) is the total number of edges across all UFOs), there exists at least one initial angle that will yield exactly (r) reflections. The final laser energy is (E_{final} = r \times w). To keep the party fun but not destructive, the host requires that the final energy be as close as possible to a given integer target (E). Your task is to determine an emission angle (\theta) (with exactly two decimal places) such that (E_{final}) is as close as possible to (E). If there are multiple choices, output the smallest such angle.

The relation to compute the answer is as follows:

  1. Let (S = \sum_{i=1}^{n} m_i), where (m_i) is the number of edges of the (i)-th UFO.

  2. Choose an integer (r) (with (0 \le r \le S)) that minimizes (|r \times w - E|). If multiple (r) yield the same absolute difference, choose the smallest (r).

  3. The answer angle is then computed as:

[ \theta = \frac{r \times 360}{S} ]

Output (\theta) with exactly two decimal places.

inputFormat

The first line of input contains three numbers: an integer (E) (the target energy), a real number (w) (the energy increment per reflection, given with two decimal places), and an integer (n) (the number of UFOs).\n\nThis is followed by (n) lines. Each of these lines contains two integers: (m) (the number of edges of the UFO, where (3 \le m \le 5)) and (k) (the rotation factor, where (0 \le k \le 3)). Note that the rotation value does not influence the number of edges.

outputFormat

Output a single line containing the laser emission angle (\theta) (in degrees) that results in a final laser energy as close as possible to (E). The angle must be printed with exactly two decimal places.

sample

10 2.00 2
3 1
4 0
257.14