This page is still under construction.

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

Faulty Factorial

Time limit3sMemory limit512 MB

Summary
Given n, prime p, and target r mod p, find the faulty factorial (one factor reduced below its index) with remainder r, printing the smallest such (index, value).
Level

Hard8 of 10

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

Problem

The factorial of a natural number is the product of all positive integers up to that number. For example, the factorial of 44 is 1×2×3×4=241 \times 2 \times 3 \times 4 = 24. A faulty factorial of length nn has the same shape as the factorial of nn, but exactly one of the multiplied integers is strictly smaller than it should be. The reduced value is still at least 11. For example, 1×2×2×4=161 \times 2 \times 2 \times 4 = 16 is a faulty factorial of length 44.

You are given the length nn, a prime modulus pp, and a target remainder rr. Find a faulty factorial of length nn that leaves remainder rr when divided by pp.

Input

The first line contains the length nn of the faulty factorial, the prime modulus pp, and the target remainder rr, separated by spaces (2≤n≤10182 \le n \le 10^{18}, 2≤p<1072 \le p < 10^7, 0≤r<p0 \le r < p). The number pp is prime.

Output

If no faulty factorial meets the requirement, print -1 -1. Otherwise print the index kk of the fault and the value vv at that index, separated by a space (2≤k≤n2 \le k \le n, 1≤v<k1 \le v < k).

If several pairs work, print the lexicographically smallest pair (k,v)(k, v). That is, take the smallest kk, and among the answers with that kk take the smallest vv.

Note

The answer for the first example describes the faulty factorial 1×2×2×4=161 \times 2 \times 2 \times 4 = 16. Dividing 1616 by 55 leaves remainder 11.

Examples2

  1. Example 1

    Input
    4 5 1
    
    Expected output
    3 2
    
  2. Example 2

    Input
    4 127 24
    
    Expected output
    -1 -1