Given segment lengths and a target hand position for a robot arm with equal joint angles, recover the base angle and joint angle that reach it.
Medium6GeometryBinary searchBrute forceNo attempts yetTime limit2sMemory limit512 MBMoving a robot arm is harder than it looks. When the arm is built from several segments, the angle at every joint contributes to where the hand ends up. In an industrial robot the operator gives only the target position of the hand, and software works out the joint angles. That computation is called inverse kinematics.
The arm here has N segments, numbered 1 to N starting at the base. The base is anchored at the origin and segment 1 can point in any direction. Every joint between two consecutive segments is locked to the same angle.

Write θ for the base angle and φ for the common joint angle, both in degrees. Segment 1 points in the direction reached by turning θ anticlockwise from the positive x axis. At the joint between segment k and segment k+1, the direction of segment k+1 is reached by turning φ anticlockwise from the direction that points back along segment k. A value of φ=180 leaves the arm straight, and any smaller value bends it clockwise by the same amount at each joint. Segment k therefore points in the direction
θ−(k−1)(180−φ)
and the hand, at the far end of segment N, sits at
(∑k=1Nℓkcos(θ−(k−1)(180−φ)),∑k=1Nℓksin(θ−(k−1)(180−φ)))
where ℓk is the length of segment k.
In the picture the arm is anchored at (0,0), the segment lengths are 1, 3 and 1, the base angle is 90 degrees and the common joint angle is 90 degrees, so the hand lands at (3,0).
Given the segment lengths and a position for the hand, recover θ and φ.
Several pairs (θ,φ) can put the hand at the same point. Report the pair whose common joint angle φ is largest, which is the least bent arm that reaches the target.
The input holds a sequence of problems, at most 20 of them. The first line of each problem has N, X and Y separated by spaces. N is the number of segments, 2≤N≤30. X and Y are the required coordinates of the hand and are floating point values. The next N lines hold the segment lengths in order from the base, one floating point value per line. A line holding three zeros ends the input and is not a problem.
Every segment length is between 1 and 10 inclusive, and the coordinates satisfy −10≤X≤10 and −10≤Y≤10. The hand is never at the origin. Every problem has at least one solution, and the data avoids answers whose third decimal digit sits on a rounding boundary.
Print one line per problem with two values separated by a space: the base angle first, then the common joint angle. Both are in degrees, rounded to exactly 3 decimal digits. When more than one pair of angles reaches the target, print the pair whose common joint angle is largest. The base angle is reported in the range 0 (inclusive) to 360 (exclusive) and the common joint angle in the range 0 to 180 inclusive.