Number Theory and Applications

Time limit0.2sMemory limit16 MB

Summary
Given two Gaussian integers up to 10^9 in each coordinate, output every greatest common divisor in lexicographic order.
Level

Hard8 of 10

Topics
Number theory, Math, Divide and conquer, Sorting
Solved
No attempts yet

Problem

Let us apply number theory. A complex number whose real and imaginary parts are integers is called a Gaussian integer.

\begin{equation*} G=\left{x+yi:::x,y\in\mathbb{Z}\right},\qquad\text{where}\quad i^2=-1 \end{equation*}

The notions of divisor, common divisor, and greatest common divisor carry over from the integers to the Gaussian integers.

  • For two Gaussian integers n,mn,m, if there is some Gaussian integer k∈Gk\in G with m=n⋅km=n\cdot k, then nn is called a divisor of mm. The relation that nn is a divisor of mm is written n∣mn\mid m.
  • For two Gaussian integers n,mn,m, a Gaussian integer dd that is a divisor of both nn and mm is called a common divisor of nn and mm.
  • A common divisor gg of two Gaussian integers n,mn,m is called a greatest common divisor if d∣gd\mid g holds for every other common divisor dd.

Given two Gaussian integers, write a program that finds all of their greatest common divisors.

Input

The first line gives the number of test cases TT. There are at most 1,000 test cases.

From the second line through the (T+1)(T+1)-th line, each line gives one test case. Each test case consists of four integers a,b,c,da,b,c,d between −109-10^9 and 10910^9, separated by spaces. This input corresponds to the Gaussian integers n=a+bin=a+bi and m=c+dim=c+di. The cases a=b=0a=b=0 and c=d=0c=d=0 are not given.

Output

A Gaussian integer x+yix+yi is printed by printing xx, a space, and then yy. For each test case, print the number of greatest common divisors on the first line, and print the greatest common divisors on the second line in lexicographic order. That is, a+bia+bi is printed before c+dic+di when a<ca<c, or when a=ca=c and b<db<d.

Hint

The greatest common divisor is not unique even among the integers. For example, if an integer gg is a greatest common divisor of two integers n,mn,m, then −g-g is also a greatest common divisor of n,mn,m.

Examples1

  1. Example 1

    Input
    3
    6 0 9 0
    2 1 -2 -1
    6 -7 3 5
    Expected output
    4
    -3 0 0 -3 0 3 3 0
    4
    -2 -1 -1 2 1 -2 2 1
    4
    -4 -1 -1 4 1 -4 4 1