Problem:


MathJax.Hub.Config({
tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}
});

四元数のかけ算

複素数を拡張したものに四元数と呼ばれるものがあります。物体の回転などを表現するのに便利なので、ロボットの腕の制御などに使える便利な数です。四元数は、４つの実数 $x$、$y$、$z$、$w$ と特別な数（虚数を拡張したもの）$i$、$j$、$k$ を用いて、$x + yi + zj + wk$ と表されます。このような四元数の和は次のように定義されます。

$(x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2)i + (z_1 + z_2)j + (w_1 + w_2)k$

1 + y1i+z1j+w1k) + (x2 + y2i + z2j + w2k) = (x1 + x2) + (y1 + y2)i + (z1 + z2)j + (w1 + w2)k

-->

一方、1、$i$、$j$、$k$ の間の積は次のように与えます。

この表は、２つの特別な数 $A$ と $B$ の積 $AB$ を表しています。例えば、 $i$ と $j$ の積 $ij$ は $k$ であり、 $j$  と $i$ の積 $ji$ は $-k$ であることを表しています。

一般の四元数の積は、この関係を満たすように計算されます。例えば、2つの四元数、$1+2i+3j+4k$ と$7+6i+7j+8k$ の積は次のように計算します。

$(1+2i+3j+4k) \times (7+6i+7j+8k)=$

$7+6i+7j+8k$

$+14i+12i^2+14ij+16ik$

$+21j+18ji+21j^2+24jk$

$+28k+24ki+28kj+32k^2$

上の表を適用することにより

$= -58+16i+36j+32k$

となります。

4つの係数$x$、$y$、$z$、$w$ が整数でかつすべてがゼロとはならない2つの四元数 ($x_1+y_1 i+z_1 j+w_1 k$) と($x_2+y_2 i+z_2 j+w_2 k$)を入力とし、その積を($x_3+y_3 i+z_3 j+w_3 k$)とする時の$x_3$、$y_3$、$z_3$、$w_3$ を出力するプログラムを作成してください。

入力

複数のデータセットが与えられます。入力の終わりはゼロひとつの行で示されます。各データセットは以下の形式で与えられます。

$n$

$data_1$

$data_2$

:

$data_n$

1行目に処理を行う四元数の組の個数 $n$ ($ n \leq 10$) が与えられます。続く $n$ 行に $i$ 番目の四元数の組の情報 $data_i$ が次の形式で与えられます。

$x_1$ $y_1$ $z_1$ $w_1$ $x_2$ $y_2$ $z_2$ $w_2$

与えられる係数はすべて-1000 以上1000 以下とします。データセットの数は 50 を超えません。

出力

データセットごとに、与えられた四元数の組の積を出力します。

入力例

2
1 2 3 4 7 6 7 8
5 6 7 8 3 2 3 4
0

出力例

-58 16 36 32
-50 32 28 48



Sample input:
2
1 2 3 4 7 6 7 8
5 6 7 8 3 2 3 4
0


Sample output:
-58 16 36 32
-50 32 28 48

Submission status: Wrong Answer

Solution code:
import sys
f = sys.stdin


index = [[0,1,2,3],
         [1,0,3,2],
         [2,3,0,1],
         [3,2,1,0]]
sign = [[1, 1, 1, 1],
        [1,-1, 1,-1],
        [1,-1,-1, 1],
        [1, 1,-1,-1]]
    
n = int(f.readline())
for _ in range(n):
    ab = list(map(int, f.readline().split()))
    ret = [0] * 4
    for i, a in enumerate(ab[:4]):
        for j, b in enumerate(ab[4:]):
            ret[index[i][j]] += sign[i][j] * a * b
    print(*ret)

Submission status: Wrong Answer

Solution code:
# Aizu Problem 0241: Quaternion Multiplication

import sys, math, os, bisect

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


n = int(input())
for k in range(n):
    a1, a2, a3, a4, b1, b2, b3, b4 = [int(__) for __ in input().split()]
    p1 = a1 * b1 - a2 * b2 - a3 * b3 - a4 * b4
    p2 = a1 * b2 + a2 * b1 - a4 * b3 + a3 * b4
    p3 = a1 * b3 - a2 * b4 + a3 * b1 + a4 * b2
    p4 = a1 * b4 + a2 * b3 - a3 * b2 + a4 * b1
    print(p1, p2, p3, p4)

Submission status: Wrong Answer

Solution code:
for _ in range(int(input())):
    x = tuple(map(int, input().split()))
    x1, x2 = x[:4], x[4:]
    a = [0,0,0,0]
    b = [[1,2,3,4],[2,-1,4,-3],[3,-4,-1,2],[4,3,-2,-1]]
    for i in range(4):
        for j in range(4):
            if b[i][j] > 0:a[b[i][j] - 1] += x1[i] * x2[j]
            else:a[-b[i][j] - 1] -= x1[i] * x2[j]
    print(*a)

Submission status: Wrong Answer

Solution code:
b = [[1,2,3,4],[2,-1,4,-3],[3,-4,-1,2],[4,3,-2,-1]]
for _ in range(int(input())):
    x = tuple(map(int, input().split()))
    x1, x2 = x[:4], x[4:]
    a = [0,0,0,0]
    for i in range(4):
        for j in range(4):
            if b[i][j] > 0:a[b[i][j] - 1] += x1[i] * x2[j]
            else:a[-b[i][j] - 1] -= x1[i] * x2[j]
    print(*a)

Submission status: Wrong Answer

Solution code:
for _ in range(int(input())):
    x1, y1, z1, w1, x2, y2, z2, w2 = map(int, input().split())
    print(x1 * x2 - y1 * y2 - z1 * z2 - w1 * w2, 
    x1 * y2 + y1 * x2 + z1 * w2 - w1 * z2,
    x1 * z2 - y1 * w2 + z1 * x2 + w1 * y2,
    x1 * w2 + y1 * z2 - z1 * y2 + w1 * x2)

Submission status: Accepted

Solution code:
while 1:
    n = int(input())
    if n == 0:break

    mark = (1,1,1,1,
            1,-1,1,-1,
            1,-1,-1,1,
            1,1,-1,-1)
    k = (0,1,2,3,
            1,0,3,2,
            2,3,0,1,
            3,2,1,0)

    for i in range(n):
        result = [0 for i in range(4)]
        inp = list(map(int,input().split()))
        
        for x_i,x in enumerate(inp[:4]):
            for y_i ,y in enumerate(inp[4:]):
                idx = x_i*4 + y_i 
                #print(mark[k[idx]])# * (x*y))

                result[k[idx]] += (mark[idx] * (x*y))

        print(' '.join(map(str,result)))

Submission status: Accepted

Solution code:
import sys
f = sys.stdin


index = [[0,1,2,3],
         [1,0,3,2],
         [2,3,0,1],
         [3,2,1,0]]
sign = [[1, 1, 1, 1],
        [1,-1, 1,-1],
        [1,-1,-1, 1],
        [1, 1,-1,-1]]
while True:    
    n = int(f.readline())
    if n == 0:
        break
    for _ in range(n):
        ab = list(map(int, f.readline().split()))
        ret = [0] * 4
        for i, a in enumerate(ab[:4]):
            for j, b in enumerate(ab[4:]):
                ret[index[i][j]] += sign[i][j] * a * b
        print(*ret)

Submission status: Accepted

Solution code:
while 1:
    n=int(input())
    if n==0:break
    for _ in range(n):
        x1,y1,z1,w1,x2,y2,z2,w2=map(int,input().split())
        print((x1*x2) - (y1*y2) -(z1*z2) -(w1*w2),
              (x1*y2) + (y1*x2) + (z1*w2) - (w1*z2),
              (x1*z2) - (y1*w2) + (z1*x2) + (w1*y2),
              (x1*w2) + (y1*z2) - (z1*y2) + (w1*x2))

Submission status: Accepted

Solution code:
# Aizu Problem 0241: Quaternion Multiplication

import sys, math, os, bisect

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


while True:
    n = int(input())
    if n == 0:
        break
    for k in range(n):
        a1, a2, a3, a4, b1, b2, b3, b4 = [int(__) for __ in input().split()]
        p1 = a1 * b1 - a2 * b2 - a3 * b3 - a4 * b4
        p2 = a1 * b2 + a2 * b1 - a4 * b3 + a3 * b4
        p3 = a1 * b3 - a2 * b4 + a3 * b1 + a4 * b2
        p4 = a1 * b4 + a2 * b3 - a3 * b2 + a4 * b1
        print(p1, p2, p3, p4)

Submission status: Accepted

Solution code:
# AOJ 0241: Quaternion Multiplication
# Python3 2018.6.25 bal4u

while True:
	n = int(input())
	if n == 0: break
	for i in range(n):
		x1, y1, z1, w1,	x2, y2, z2, w2 = map(int, input().split())
		x3 = x1*x2 - y1*y2 - z1*z2 - w1*w2
		y3 = x1*y2 + y1*x2 + z1*w2 - w1*z2
		z3 = x1*z2 - y1*w2 + z1*x2 + w1*y2
		w3 = x1*w2 + y1*z2 - z1*y2 + w1*x2
		print(x3, y3, z3, w3)

Submission status: Accepted

Solution code:
b = [[1,2,3,4],[2,-1,4,-3],[3,-4,-1,2],[4,3,-2,-1]]
while 1:
    n = int(input())
    if n == 0:break
    for _ in range(n):
        x = tuple(map(int, input().split()))
        x1, x2 = x[:4], x[4:]
        a = [0,0,0,0]
        for i in range(4):
            for j in range(4):
                if b[i][j] > 0:a[b[i][j] - 1] += x1[i] * x2[j]
                else:a[-b[i][j] - 1] -= x1[i] * x2[j]
        print(*a)

Submission status: Accepted

Solution code:
while True:
    n=input()
    if n==0:break
    for i in range(n):
        x1,y1,z1,w1,x2,y2,z2,w2=map(int,raw_input().split())
        x3=x1*x2-y1*y2-z1*z2-w1*w2
        y3=x1*y2+x2*y1+z1*w2-w1*z2
        z3=x1*z2-y1*w2+x2*z1+y2*w1
        w3=x1*w2+y1*z2-y2*z1+x2*w1
        print x3,y3,z3,w3

Submission status: Accepted

Solution code:
while 1:
	n=input()
	if n==0:break
	for i in range(n):
		a,b,c,d,x,y,z,w=map(int,raw_input().split())
		print a*x-b*y-c*z-d*w,a*y+b*x+c*w-d*z,a*z-b*w+c*x+d*y,a*w+b*z-c*y+d*x

Submission status: Accepted

Solution code:

while True :
    n = int(input())
    if n == 0 :
        break
    
    for i in range(n) :
        x1, y1, z1, w1, x2, y2, z2, w2 = map(int, input().split())
        print((x1*x2 - y1*y2 - z1*z2 - w1*w2), (x1*y2 + x2*y1 + z1*w2 - z2*w1), (x1*z2 - y1*w2 + x2*z1 + y2*w1), (x1*w2 + y1*z2 - y2*z1 + x2*w1))

Submission status: Accepted

Solution code:
while 1:
    N = int(input())
    if N == 0:
        break
    P = [
        [1, 2, 3, 4],
        [2, -1, 4, -3],
        [3, -4, -1, 2],
        [4, 3, -2, -1],
    ]
    for i in range(N):
        *X, = map(int, input().split())
        X0 = X[:4]; X1 = X[4:]
        Y = [0]*4
        for i, x0 in enumerate(X0):
            for j, x1 in enumerate(X1):
                z = P[i][j]
                if z > 0:
                    Y[z-1] += x0*x1
                else:
                    Y[-z-1] -= x0*x1
        print(*Y)

Submission status: Accepted

Solution code:
# from sys import exit
while(True):
    N = int(input())
    if N == 0:
        break
    for _ in range(N):

        a, b, c, d, A, B, C, D = [int(n) for n in input().split()]
        z = a*A - b*B - c*C - d*D
        i = a*B + b*A + c*D - d*C
        j = a*C - b*D + c*A + d*B
        k = a*D + b*C - c*B + d*A
        print(z, i, j, k)
# a = [int(input()) for _ in range(N)]
# S = str(input())
# L = len(S)
# T = str(input())

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

while True:
    N = int(input())
    if N == 0:
        break
    
    for i in range(N):
        x1,y1,z1,w1,x2,y2,z2,w2 = [int(j) for j in input().split()]
        a1 = x1*x2 - y1*y2 - z1*z2 - w1*w2
        a2 = x1*y2 + y1*x2 + z1*w2 - w1*z2
        a3 = x1*z2 - y1*w2 + z1*x2 + w1*y2
        a4 = x1*w2 + y1*z2 - z1*y2 + w1*x2
        print(a1,a2,a3,a4)

Submission status: Accepted

Solution code:
while 1:
    n=int(input())
    if n==0:break
    ans=[0]*(n+1)**2
    for i in range(0,n):
        a,b,c,d,e,f,g,h=map(int,input().split())
        print(a*e-b*f-c*g-d*h,a*f+b*e+c*h-d*g,a*g-b*h+c*e+d*f,a*h+b*g-c*f+d*e)

Submission status: Accepted

Solution code:
while 1:
    n = int(input())
    if n == 0:
        break

    for _ in range(n):
        data = list(map(int, input().split()))
        c = data[0]*data[4] - data[1]*data[5] - \
            data[2]*data[6] - data[3]*data[7]
        i = data[0]*data[5] + data[1]*data[4] + \
            data[2]*data[7] - data[3]*data[6]
        j = data[0]*data[6] - data[1]*data[7] + \
            data[2]*data[4] + data[3]*data[5]
        k = data[0]*data[7] + data[1]*data[6] - \
            data[2]*data[5] + data[3]*data[4]
        print(c, i, j, k)

Submission status: Accepted

Solution code:
while True:
  n = int(input())
  if n == 0:
    break
  for _ in range(n):
    x1, y1, z1, w1, x2, y2, z2, w2 = map(int, input().split())
    x3 = x1 * x2 - y1 * y2 - z1 * z2 - w1 * w2
    y3 = x1 * y2 + y1 * x2 + z1 * w2 - w1 * z2
    z3 = x1 * z2 - y1 * w2 + z1 * x2 + w1 * y2
    w3 = x1 * w2 + y1 * z2 - z1 * y2 + w1 * x2
    print(x3, y3, z3, w3)