Under construction

Still being built. Things may change

Circle Bounce

Time limit1sMemory limit1024 MB

Summary
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 (−1,0)(-1, 0). You throw the ball with a direction given by a slope in the coordinate plane of a rational fraction a/ba/b. 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 nn bounces, the ball strikes the circle again at some point pp which has rational coordinates that can be expressed as (r/s,t/u)(r/s, t/u). Output the fraction r/sr/s modulo the prime M=1,000,000,007M = 1{,}000{,}000{,}007.

It can be shown that the xx coordinate can be expressed as an irreducible fraction r/sr/s, where rr and ss are integers and s≢0(modM)s \not\equiv 0 \pmod M. Output the integer equal to r⋅s−1(modM)r\cdot s^{-1} \pmod M. In other words, output an integer kk such that 0≤k<M0 \le k < M and k⋅s≡r(modM)k\cdot s \equiv r \pmod M.

For example, if we throw the ball with slope 1/21/2 and it bounces once, it first strikes the wall at coordinates (3/5,4/5)(3/5, 4/5). After bouncing, it next strikes the wall at coordinates (7/25,−24/25)(7/25, -24/25). The modular inverse of 2525 with respect to the prime MM is 280,000,002280{,}000{,}002, and the final result is thus 7⋅280,000,002(modM)=960,000,0077\cdot 280{,}000{,}002 \pmod M = 960{,}000{,}007.

Input

The single line of input will contain three integers aa, bb (1≤a,b≤109,gcd⁡(a,b)=11 \le a,b \le 10^9, \gcd(a,b)=1) and nn (1≤n≤10121 \le n \le 10^{12}), where a/ba/b is the slope of your throw, and nn is the number of bounces. Note that aa and bb are relatively prime.

Output

Output a single integer value as described above.

Note that Sample 2 corresponds to the example in the problem description.

Examples4

  1. Example 1

    Input
    1 1 3
    
    Expected output
    1000000006
    
  2. Example 2

    Input
    1 2 1
    
    Expected output
    960000007
    
  3. Example 3

    Input
    11 63 44
    
    Expected output
    22
    
  4. Example 4

    Input
    163 713 980
    
    Expected output
    0