Powers of a matrix P61833


Statement
 

pdf   zip

Given a 2×22 \times 2 matrix MM of natural numbers, a natural number nn and a natural number mm, compute MnM^n. To avoid overflows, compute every element of MnmodmM^n \bmod m.

Input

Input consists of several cases, each with M11M_{11}, M12M_{12}, M21M_{21} and M22M_{22} in this order, followed by nn and mm. Assume that the elements of MM are not larger than 500500, 0n1090 \le n \le 10^9, and 2m10002 \le m \le 1000.

Output

For every case, print the elements of MnmodmM^n \bmod m in two lines following the format of the sample. Print a line with 10 dashes after every matrix.

Public test cases
  • Input

    2 7
    1 4
    2 100
    
    2 7
    1 4
    2 5
    
    15 2
    3 4
    0 1000
    
    500 499
    499 498
    123456789 1000
    

    Output

    11 42
    6 23
    ----------
    1 2
    1 3
    ----------
    1 0
    0 1
    ----------
    792 815
    815 422
    ----------
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Official solutions
    C++ Python
    User solutions
    C++ Python