using
System;
using
System.Collections.Generic;
class
Program {
static
int
gcd(
int
a,
int
b)
{
if
(a == 0) {
return
b;
}
return
gcd(b % a, a);
}
static
void
find4Sum(
int
[] A,
int
N,
int
B)
{
Dictionary<
int
, List<Tuple<
int
,
int
> > > mp
=
new
Dictionary<
int
,
List<Tuple<
int
,
int
> > >();
HashSet<Tuple<
int
,
int
,
int
,
int
> > st
=
new
HashSet<Tuple<
int
,
int
,
int
,
int
> >();
for
(
int
i = 0; i < N - 1; i++) {
for
(
int
j = i + 1; j < N; j++) {
int
sum = A[i] + A[j];
if
(!mp.ContainsKey(sum)) {
mp[sum] =
new
List<Tuple<
int
,
int
> >();
}
mp[sum].Add(
new
Tuple<
int
,
int
>(i, j));
}
}
for
(
int
i = 0; i < N - 1; i++) {
for
(
int
j = i + 1; j < N; j++) {
int
sum = A[i] + A[j];
if
(mp.ContainsKey(B - sum)) {
List<Tuple<
int
,
int
> > v = mp[B - sum];
foreach
(Tuple<
int
,
int
> it
in
v)
{
if
(it.Item1 != i && it.Item2 != i
&& it.Item1 != j
&& it.Item2 != j) {
Tuple<
int
,
int
,
int
,
int
> temp
= Tuple.Create(A[i], A[j],
A[it.Item1],
A[it.Item2]);
int
gc = Math.Abs(temp.Item1);
gc = gcd(Math.Abs(temp.Item2),
gc);
gc = gcd(Math.Abs(temp.Item3),
gc);
gc = gcd(Math.Abs(temp.Item4),
gc);
if
(gc == 1) {
int
[] arr = { temp.Item1,
temp.Item2,
temp.Item3,
temp.Item4 };
Array.Sort(arr);
st.Add(Tuple.Create(
arr[0], arr[1], arr[2],
arr[3]));
}
}
}
}
}
}
foreach
(Tuple<
int
,
int
,
int
,
int
> it
in
st)
{
Console.WriteLine(
"{0} {1} {2} {3}"
, it.Item1,
it.Item2, it.Item3, it.Item4);
}
}
static
void
Main(
string
[] args)
{
int
N = 6;
int
[] A = { 1, 0, -1, 0, -2, 2 };
int
B = 0;
find4Sum(A, N, B);
}
}