vp

Time limit: 0.1s Memory limit: 64MB Input: Output:

Initially, Andrei had two sequences VV and PP of NN elements each. The sequence VV contained natural numbers <M< M, while the sequence PP had numbers with the following property: Pi=(Pi1×Vi) (mod M)P_i = (P_{i-1} \times V_i) \ (\text{mod} \ M) for each ii, 2iN2 \leq i \leq N, and P1=V1P_1 = V_1.

One morning, Andrei realized that some numbers had been smudged and were unreadable!

Since Andrei wants both sequences VV and PP to be readable from the beginning to the end, he asks you to help him reconstruct the sequences.

Input data

The first line contains 22 natural numbers NN and MM, and the second and third lines of the input contain the sequences VV and PP, respectively. If a number at position ii is 1-1, it means that number is unreadable. It is guaranteed that the readable numbers respect the given property.

Output data

The first line will contain the readable sequence VV from start to finish, and the second line will contain the readable sequence PP from start to finish. Since Andrei is a thoughtful boy, he will accept any solution you find, as long as it respects the rule of the sequences.

Constraints and clarifications

  • 1N1051 \leq N \leq 10^5
  • 1M10001 \leq M \leq 1000
  • For 4242 points, N1000N \leq 1000
  • It is guaranteed that a solution exists
  • During the contest "RoAlgo Contest #2", there was no test with id 14. It was added later so that only the solution with the best complexity would score 100 points

Example 1

stdin

3 6
1 -1 4
-1 -1 -1

stdout

1 4 4
1 4 4

Explanation

  • 1=11 = 1
  • 4=(1×4)4 = (1 \times 4) (mod 66)
  • 4=(4×4)4 = (4 \times 4) (mod 66)

Therefore, these sequences respect the property.

Example 2

stdin

3 7
-1 -1 5
-1 -1 6

stdout

2 2 5
2 4 6

Explanation

  • 2=22 = 2
  • 4=(2×2)4 = (2 \times 2) (mod 77)
  • 6=(4×5)6 = (4 \times 5) (mod 77)

Therefore, these sequences respect the property.

Log in or sign up to be able to send submissions!