import
java.util.*;
class
Complex {
public
double
real, imag;
public
Complex(
double
real,
double
imag)
{
this
.real = real;
this
.imag = imag;
}
public
Complex add(Complex other)
{
return
new
Complex(real + other.real,
imag + other.imag);
}
public
Complex subtract(Complex other)
{
return
new
Complex(real - other.real,
imag - other.imag);
}
public
Complex multiply(Complex other)
{
double
re = real * other.real - imag * other.imag;
double
im = real * other.imag + imag * other.real;
return
new
Complex(re, im);
}
public
Complex divide(Complex other)
{
double
denom = other.real * other.real
+ other.imag * other.imag;
double
re = (real * other.real + imag * other.imag)
/ denom;
double
im = (imag * other.real - real * other.imag)
/ denom;
return
new
Complex(re, im);
}
}
class
GFG {
public
static
void
fft(ArrayList<Complex> a,
boolean
invert)
{
int
n = a.size();
if
(n ==
1
) {
return
;
}
ArrayList<Complex> a0 =
new
ArrayList<>();
ArrayList<Complex> a1 =
new
ArrayList<>();
for
(
int
i =
0
;
2
* i < n; i++) {
a0.add(a.get(
2
* i));
a1.add(a.get(
2
* i +
1
));
}
fft(a0, invert);
fft(a1, invert);
double
ang =
2
* Math.PI / n * (invert ? -
1
:
1
);
Complex w =
new
Complex(
1
,
0
);
Complex wn
=
new
Complex(Math.cos(ang), Math.sin(ang));
for
(
int
i =
0
;
2
* i < n; i++) {
a.set(i, a0.get(i).add(w.multiply(a1.get(i))));
a.set(i + n /
2
, a0.get(i).subtract(
w.multiply(a1.get(i))));
if
(invert) {
a.set(i,
a.get(i).divide(
new
Complex(
2
,
0
)));
a.set(i + n /
2
, a.get(i + n /
2
).divide(
new
Complex(
2
,
0
)));
}
w = w.multiply(wn);
}
}
public
static
ArrayList<Integer> multiply(
int
[] a,
int
[] b)
{
ArrayList<Complex> fa =
new
ArrayList<>();
ArrayList<Complex> fb =
new
ArrayList<>();
for
(
int
i =
0
; i < a.length; i++) {
fa.add(
new
Complex(a[i],
0
));
}
for
(
int
i =
0
; i < b.length; i++) {
fb.add(
new
Complex(b[i],
0
));
}
int
n =
1
;
while
(n < a.length + b.length) {
n <<=
1
;
}
while
(fa.size() < n) {
fa.add(
new
Complex(
0
,
0
));
}
while
(fb.size() < n) {
fb.add(
new
Complex(
0
,
0
));
}
fft(fa,
false
);
fft(fb,
false
);
for
(
int
i =
0
; i < n; i++) {
fa.set(i, fa.get(i).multiply(fb.get(i)));
}
fft(fa,
true
);
ArrayList<Integer> result =
new
ArrayList<>();
for
(
int
i =
0
; i < n; i++) {
result.add((
int
)Math.round(fa.get(i).real));
}
return
result;
}
public
static
void
findCount(ArrayList<Integer> arr1,
ArrayList<Integer> arr2)
{
int
MAX = Math.max(Collections.max(arr1),
Collections.max(arr2));
int
n = arr1.size();
int
m = arr2.size();
int
[] A =
new
int
[MAX +
1
];
for
(
int
i =
0
; i < n; i++) {
A[arr1.get(i)]++;
}
int
[] B =
new
int
[MAX +
1
];
for
(
int
i =
0
; i < m; i++) {
B[arr2.get(i)]++;
}
ArrayList<Integer> P = multiply(A, B);
for
(
int
i =
1
; i <=
2
* MAX; i++) {
if
(P.get(i) >
0
) {
System.out.println(i +
"->"
+ P.get(i));
}
}
System.out.println();
}
public
static
void
main(String[] args)
{
ArrayList<Integer> arr1 =
new
ArrayList<Integer>();
arr1.add(
1
);
arr1.add(
2
);
ArrayList<Integer> arr2 =
new
ArrayList<Integer>();
arr2.add(
1
);
arr2.add(
2
);
arr2.add(
1
);
findCount(arr1, arr2);
}
}