Faulty Factorial
Time limit3sMemory limit512 MB
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 is . A faulty factorial of length has the same shape as the factorial of , but exactly one of the multiplied integers is strictly smaller than it should be. The reduced value is still at least . For example, is a faulty factorial of length .
You are given the length , a prime modulus , and a target remainder . Find a faulty factorial of length that leaves remainder when divided by .
Input
The first line contains the length of the faulty factorial, the prime modulus , and the target remainder , separated by spaces (, , ). The number is prime.
Output
If no faulty factorial meets the requirement, print -1 -1. Otherwise print the index of the fault and the value at that index, separated by a space (, ).
If several pairs work, print the lexicographically smallest pair . That is, take the smallest , and among the answers with that take the smallest .
Note
The answer for the first example describes the faulty factorial . Dividing by leaves remainder .