This page is still under construction.

Parts of this page are still being built. What you see may change.

Quaternion inverse

Time limit1sMemory limit256 MB

Summary
Given a prime M and up to 100000 quaternions with components modulo M, output the modular inverse of each or zeros if none exists.
Level

Medium5 of 10

Topics
Number theory, Math
Solved
No attempts yet

Problem

A quaternion extends the complex numbers. It is written with three imaginary units ii, jj, kk that satisfy i2=j2=k2=ijk=−1i^2 = j^2 = k^2 = ijk = -1, so it has four real components. This problem deals only with the quaternions in the set below, called restricted quaternions.

a+bi+cj+dk(a,b,c,d are integers with 0≤a,b,c,d<M)a + bi + cj + dk \qquad (a, b, c, d \text{ are integers with } 0 \le a, b, c, d < M)

Because of the relations among ii, jj, kk, the product of two ordinary quaternions is the following.

(a1+b1i+c1j+d1k)(a2+b2i+c2j+d2k)=(a1a2−b1b2−c1c2−d1d2)+(a1b2+b1a2+c1d2−d1c2)i+(a1c2−b1d2+c1a2+d1b2)j+(a1d2+b1c2−c1b2+d1a2)k\begin{aligned} &(a_1 + b_1i + c_1j + d_1k)(a_2 + b_2i + c_2j + d_2k) \\ &= (a_1a_2 - b_1b_2 - c_1c_2 - d_1d_2) \\ &+ (a_1b_2 + b_1a_2 + c_1d_2 - d_1c_2)i \\ &+ (a_1c_2 - b_1d_2 + c_1a_2 + d_1b_2)j \\ &+ (a_1d_2 + b_1c_2 - c_1b_2 + d_1a_2)k \end{aligned}

The product of two restricted quaternions is defined as the product of the same two quaternions taken as ordinary quaternions, with every integer component replaced by its remainder modulo MM.

Given MM and a restricted quaternion AA, find the restricted quaternion BB with AB=1AB = 1.

Input

The first line has two natural numbers MM and TT (1≤T≤1000001 \le T \le 100000), separated by a space. MM is prime, so its only divisors are 1 and itself, and 2≤M≤1000002 \le M \le 100000.

Each of the next TT lines has four integers aa, bb, cc, dd (0≤a,b,c,d<M0 \le a, b, c, d < M) separated by spaces, and they describe A=a+bi+cj+dkA = a + bi + cj + dk.

Output

For each AA, print on one line the four integers aa, bb, cc, dd of the restricted quaternion B=a+bi+cj+dkB = a + bi + cj + dk with AB=1AB = 1, separated by spaces. At most one such BB exists, so the answer is unique. If there is no such BB, print 0 four times, separated by spaces.

Examples2

  1. Example 1

    Input
    5 3
    2 3 2 1
    3 1 3 2
    3 2 4 1
    
    Expected output
    4 4 1 3
    1 3 4 1
    0 0 0 0
    
  2. Example 2

    Input
    2 16
    0 0 0 0
    0 0 0 1
    0 0 1 0
    0 0 1 1
    0 1 0 0
    0 1 0 1
    0 1 1 0
    0 1 1 1
    1 0 0 0
    1 0 0 1
    1 0 1 0
    1 0 1 1
    1 1 0 0
    1 1 0 1
    1 1 1 0
    1 1 1 1
    
    Expected output
    0 0 0 0
    0 0 0 1
    0 0 1 0
    0 0 0 0
    0 1 0 0
    0 0 0 0
    0 0 0 0
    0 1 1 1
    1 0 0 0
    0 0 0 0
    0 0 0 0
    1 0 1 1
    0 0 0 0
    1 1 0 1
    1 1 1 0
    0 0 0 0