Hide

Problem A
Factorial Encoding

Languages en is
/problems/hropmerkingardulkodun/file/statement/en/img-0001.png
Image by Jahnke and Emde, commons.wikimedia.org

There is an open conjecture that says for a prime $p$ and $0 < x < p$ there will always exist two values $0 \leq a, b < p$ such that $x \equiv a!b! \pmod{p}$.

You happened upon a paper that talked about using this conjecture for encryption. The idea would be that everyone would be privy to $x, p$ but $a, b$ would be secret. You kindly inform the authors that if $p > 10^{12}$ it will be infeasible to calculate $a!$ and $b!$ in a reasonable time frame. They simply responded that then the solution is to just stick to $p \leq 10^{12}$. You still want to show them this is a bad idea.

The best way to do this is of course to make a program that takes in $x, p$ and prints these secret $a, b$!1

Note, that due to technical reasons, this problem is interactive. This means that if you try to read more input than is available, your program may hang and go over the time limit. Make sure to flush your output buffer for this reason.

Input

The first and only line of input contains two integers $x, p$ separated by a space, satisfying $0 < x < p$ and $1 \leq p \leq 10^{12}$. Furthermore $p$ will always be a prime.

Output

Print two integers $0 \leq a, b < p$ separated by a space such that $x \equiv a!b! \pmod{p}$. If there is more than one solution any one of them will be accepted.

Explanation of sample

In the first sample we are given $x = 6$ and $p = 101$. There are several solutions and one of them is $a = 0$ and $b = 3$. Then $a! = 1$ and $b! = 6$ so $a!b! = 6$. Another option would be $a = 16$ and $b = 4$. Then $a! = 20922789888000$ and $b! = 24$. Therefore $a!b! = 502146957312000$ which gives us a remainder of $6$ when divided by $101$.

Sample Input 1 Sample Output 1
6 101
0 3
Sample Input 2 Sample Output 2
101 999983
44758 144580

Footnotes

  1. This exclamation point is not a factorial.

Please log in to submit a solution to this problem

Log in