ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules

2242. Root Calculator

Time limit: 2.0 second
Memory limit: 256 MB
Vadim has a favorite odd prime number P. He does all calculations only modulo P, even when he should not. Recently, he heard about quadratic irrationals. These are numbers of the form a + b · √c. Naturally, Vadim wanted to write a calculator that can process such numbers. Your task is to help him do that.
As is known, a calculator first receives one number, and then operations are performed on it. You need to implement 5 operations: addition, subtraction, multiplication, and division of quadratic irrationals, as well as exponentiation. Note that all these calculations must be performed modulo P. If for a number a there are x1 and x2 such that x12 ≡ x22 ≡ a (mod P), then Vadim considers the smaller of them to be the correct square root. All subsequent calculations will be performed based on this result. Vadim also knows that 00 = 1, so the calculator must compute exactly this.
Vadim assures you that all results will have the form a + b · √c, where a, b, c ∈ [0; P − 1], and the same number may have several representations. Among all such representations, you must output the one with the smallest c. If there are several such representations, output the one with the smallest b.

Input

The first line contains three integers A, B, and C — the initial quadratic irrational A + B · √C (0 ≤ A, B, C ≤ 109).
The second line contains two integers Q and P — the number of operations on the calculator and Vadim’s favorite prime number (1 ≤ Q ≤ 105, 3 ≤ P ≤ 109 + 9, P is prime).
The next Q lines describe operations in one of the following formats:
  • + a b c — add a + b · √c to the current number (0 ≤ a, b, c ≤ 109)
  • - a b c — subtract a + b · √c from the current number (0 ≤ a, b, c ≤ 109)
  • * a b c — multiply the current number by a + b · √c (0 ≤ a, b, c ≤ 109)
  • / a b c — divide the current number by a + b · √c (0 ≤ a, b, c ≤ 109, a + b · √c  ≠ 0 (mod P))
  • ^ n — raise the current number to the power n (0 ≤ n ≤ 109)
All numbers a, b, c, and n are integers.

Output

After each operation, print three integers a, b, and c describing the current value modulo P. If there are several answers, print the one with the smallest c. If there are several such representations, print the one with the smallest b.

Sample

inputoutput
6 1 2
6 101
+ 0 1 8
/ 3 0 3
- 0 1 2
* 2 3 2
^ 2
+ 13 100 5408
6 3 2
2 1 2
2 0 0
4 6 2
88 48 2
0 100 2
Problem Author: Vadim Barinov
Problem Source: The 2026 ICPC UrFU Qualification