Circle Bounce
Time limit1sMemory limit1024 MB
A ball thrown from (-1,0) with rational slope a/b bounces perfectly inside a unit circle n times; output the x-coordinate of the n-th impact point modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Math, Geometry, Number theory, Combinatorics
- Solved
- No attempts yet
Problem
You are standing by the wall in a large, perfectly circular arena and you throw a tennis ball hard against some other part of the arena. After a given number of bounces, where does the tennis ball next strike the wall?
Map the arena as a unit circle centered at the origin, with you standing at the point . You throw the ball with a direction given by a slope in the coordinate plane of a rational fraction . Each bounce is perfect, losing no energy and bouncing from the wall with the same angle of reflection as the angle of incidence to a tangent to the wall at the point of impact.

After bounces, the ball strikes the circle again at some point which has rational coordinates that can be expressed as . Output the fraction modulo the prime .
It can be shown that the coordinate can be expressed as an irreducible fraction , where and are integers and . Output the integer equal to . In other words, output an integer such that and .
For example, if we throw the ball with slope and it bounces once, it first strikes the wall at coordinates . After bouncing, it next strikes the wall at coordinates . The modular inverse of with respect to the prime is , and the final result is thus .
Input
The single line of input will contain three integers , () and (), where is the slope of your throw, and is the number of bounces. Note that and are relatively prime.
Output
Output a single integer value as described above.
Note that Sample 2 corresponds to the example in the problem description.