Field modulo#12317

Field modulo

1 second
128 megabytes
Medium

For the given integers a, b, c, n, p, find the value of the expression:

(a + b \sqrt{c}​)^n~ mod~p

Print the result as two numbers and such that

x + y \sqrt{c} = (a + b \sqrt{c}​)^n~ mod~p

Input

Five integers a, b, c, n, p are given. It is known that:

  • p < 10^9 is a prime number,

  • 0 \le a, b < p,

  • 1 \le c < p,

  • 0 \le n \le 10^{18}

Output

Print two integers and — the coefficients of and \sqrt{c} respectively, modulo .

Examples

In the first example:

(2 + \sqrt{5}​)^{2}~ mod~17 = 9 + 4 \sqrt{5}

In the second example:

(3 + 2 \sqrt{5}​)^{10}~ mod~17 = 5 + 11 \sqrt{5}

Example 1

Loading
Loading

Example 2

Loading
Loading

Note

You should work in the extended field:

Z_p(\sqrt{c}) = \{ x + y \sqrt{c}~|~x, y \in Z_p \}

Operation rules:

  • Addition

    (x_1 + y_1 \sqrt{c}) + (x_2 + y_2 \sqrt{c}) = (x_1 + x_2) + (y_1 + y_2) \sqrt{c}
  • Multiplication

    (x_1 + y_1 \sqrt{c}) \cdot (x_2 + y_2 \sqrt{c}) = (x_1 x_2 + c y_1 y_2) + (x_1 y_2 + x_2 y_1) \sqrt{c}

All operations are performed modulo .