Sum in field#12318

Sum in field

1 second
128 megabytes
Very hard

For the given integers a, b, c, n, p, compute the value of the sum:

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

Print the result as two numbers and such that

x + y \sqrt{c} = \sum_{i = 0}^n (a + b \sqrt{c}​)^i~ 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^9

Output

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

Examples

In the first example:

\sum_{i = 0}^2 (2 + \sqrt{5}​)^i~mod~17 = (1 + (2 + \sqrt{5}) + (9 + 4 \sqrt{5}))~mod~17 = 12 + 5 \sqrt{5}

In the second example:

\sum_{i = 0}^{10} (3 + 2 \sqrt{5}​)^i~ mod~17 = 15 + 6 \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 .