This page is still under construction.

Parts of this page are still being built. What you see may change.

Modular Reverse Engineering

Time limit2sMemory limit512 MB

Summary
Given v, x, and prime m, find the smallest p with a matching q so that p/q lies in [x, x+1) and p/q equals v modulo m.
Level

Medium7 of 10

Topics
Number theory, Math, Binary search, Implementation
Solved
No attempts yet

Problem

Sometimes a competitive programming problem whose output is a rational number pq\frac{p}{q} asks you to output the quantity pq−1 mod mpq^{-1} \bmod m instead, for some prime modulus mm. This quantity is hard to debug when your program gives the wrong answer, because computing q−1q^{-1} by hand is difficult and the value can be very large even for small rationals. For example, 1⋅2−1 mod 97=491 \cdot 2^{-1} \bmod 97 = 49.

To debug your solution quickly, you want to write a program that recovers pp and qq from pq−1 mod mpq^{-1} \bmod m. The possible values of pp and qq are not unique, but one piece of information narrows them down: pq\frac{p}{q}, read as an ordinary rational number, lies in the range [x,x+1)[x, x + 1) for some integer xx.

Given the three values vv, xx, and mm, find the minimum possible pp and the corresponding qq such that (0≤p,q<m0 \leq p, q < m) pq−1≡v mod mpq^{-1} \equiv v \bmod m and x≤pq<x+1x \leq \frac{p}{q} < x + 1.

Input

The input is a single line with three space-separated integers vv, xx, and mm, where 3≤m≤1063 \leq m \leq 10^6, 1≤v<m1 \leq v < m, 0≤x<m0 \leq x < m, and mm is prime.

Output

Print the minimal integer pp and the corresponding qq such that 0≤p,q<m0 \leq p,q < m, pq−1≡v mod mpq^{-1} \equiv v \bmod m, and x≤pq<x+1x \leq \frac{p}{q} < x + 1. If several such pairs exist, print the pair with the minimum value of pp. If no such pp and qq exist, print a single −1-1 instead.

Examples2

  1. Example 1

    Input
    3 1 17
    
    Expected output
    10 9
    
  2. Example 2

    Input
    3 2 17
    
    Expected output
    -1