#P5316. Reconstruct the Numbers from Pairwise GCD and LCM
Reconstruct the Numbers from Pairwise GCD and LCM
Reconstruct the Numbers from Pairwise GCD and LCM
You are given an integer \( k \) with \(2\le k\le 4\) representing the number of unknown positive integers \(a_1,a_2,\dots,a_k\). For every pair \((i,j)\) with \(1\le i<j\le k\), you are provided two integers: the greatest common divisor \(\gcd(a_i,a_j)\) and the least common multiple \(\operatorname{lcm}(a_i,a_j)\). It is guaranteed that the given data is consistent and the original numbers can be recovered uniquely.
Recall that for any two positive integers \(a\) and \(b\), the following holds: \[ a\times b = \gcd(a,b)\times \operatorname{lcm}(a,b). \]
Your task is to reconstruct the original \(k\) numbers. If \(k=2\), there is only one pair \((1,2)\) and you can take \(a_1 = \gcd(a_1,a_2)\) and \(a_2 = \operatorname{lcm}(a_1,a_2)\). For \(k\ge3\), note that for any three distinct indices \(1,2,3\) the following is true: \[ a_1 = \sqrt{\frac{(a_1a_2)\,(a_1a_3)}{a_2a_3}} = \sqrt{\frac{(\gcd(a_1,a_2)\,\operatorname{lcm}(a_1,a_2))\,(\gcd(a_1,a_3)\,\operatorname{lcm}(a_1,a_3))}{\gcd(a_2,a_3)\,\operatorname{lcm}(a_2,a_3)}}. \]
After computing \(a_1\), the remaining numbers can be computed as \(a_i = \frac{a_1a_i}{a_1}\) for \(i=2,3,\dots,k\) (using the pair \((1,i)\)).
inputFormat
The first line contains a single integer \( k \) (\(2\le k\le4\)). The next \(\frac{k(k-1)}{2}\) lines each contain four space‐separated integers: \(i\), \(j\) (with \(i<j\)), \(g\) and \(L\), representing that \(\gcd(a_i,a_j)=g\) and \(\operatorname{lcm}(a_i,a_j)=L\).
outputFormat
Output \( k \) integers separated by a space, denoting the reconstructed numbers \(a_1,a_2,\dots,a_k\) in order.
sample
2
1 2 4 12
4 12