import
java.math.*;
import
java.util.*;
class
SqrtMod {
static
BigInteger modInverse(BigInteger a,
BigInteger m)
{
BigInteger m0 = m;
BigInteger y =
new
BigInteger(
"0"
),
x =
new
BigInteger(
"1"
);
if
(m.compareTo(
new
BigInteger(
"1"
)) ==
0
)
return
new
BigInteger(
"0"
);
while
(a.compareTo(
new
BigInteger(
"1"
)) >
0
) {
if
(m.toString().equals(
"0"
))
break
;
BigInteger q = a.divide(m);
BigInteger t = m;
m = a.mod(m);
a = t;
t = y;
y = x.subtract(q.multiply(y));
x = t;
}
while
(x.compareTo(
new
BigInteger(
"0"
)) <
0
)
x = x.add(m0);
return
x;
}
static
String CRT(BigInteger num[],
BigInteger rem[],
int
k)
{
BigInteger prod =
new
BigInteger(
"1"
);
for
(
int
i =
0
; i < k; i++)
prod = prod.multiply(num[i]);
BigInteger result =
new
BigInteger(
"0"
);
for
(
int
i =
0
; i < k; i++) {
BigInteger pp = prod.divide(num[i]);
result = result.add(rem[i]
.multiply(modInverse(pp,
num[i]))
.multiply(pp));
}
return
result.mod(prod).toString();
}
static
BigInteger powMod(BigInteger base1,
BigInteger exponent,
BigInteger modulus)
{
BigInteger result =
new
BigInteger(
"1"
);
base1 = base1.mod(modulus);
while
(exponent
.compareTo(
new
BigInteger(
"0"
))
>
0
) {
if
(exponent
.mod(
new
BigInteger(
"2"
))
.equals(
new
BigInteger(
"1"
)))
result = (result.multiply(base1))
.mod(modulus);
exponent = exponent
.divide(
new
BigInteger(
"2"
));
base1 = base1
.multiply(base1)
.mod(modulus);
}
return
result;
}
static
BigInteger squareRoot(
BigInteger n,
BigInteger p)
{
if
(!p.mod(
new
BigInteger(
"4"
))
.equals(
new
BigInteger(
"3"
))) {
System.out.print(
"Invalid Input"
);
return
new
BigInteger(
"-1"
);
}
n = n.mod(p);
BigInteger x = powMod(n,
(p.add(
new
BigInteger(
"1"
)))
.divide(
new
BigInteger(
"4"
)),
p);
if
((x.multiply(x)).mod(p).equals(n)) {
return
x;
}
x = p.subtract(x);
if
((x.multiply(x)).mod(p).equals(n)) {
return
x;
}
return
new
BigInteger(
"-1"
);
}
public
static
BigInteger
sqrtC(BigInteger x)
{
if
(x.compareTo(BigInteger.ZERO) <
0
) {
return
new
BigInteger(
"-1"
);
}
if
(x == BigInteger.ZERO
|| x == BigInteger.ONE) {
return
x;
}
BigInteger two
= BigInteger.valueOf(2L);
BigInteger y;
for
(y = x.divide(two);
y.compareTo(x.divide(y)) >
0
;
y = ((x.divide(y)).add(y)).divide(two))
;
if
(x.compareTo(y.multiply(y)) ==
0
) {
return
y;
}
else
{
return
y.add(BigInteger.ONE);
}
}
static
BigInteger[] factorise(BigInteger p)
{
BigInteger ans[] =
new
BigInteger[
2
];
BigInteger b =
new
BigInteger(
"2"
);
BigInteger ONE =
new
BigInteger(
"1"
);
BigInteger ZERO =
new
BigInteger(
"0"
);
for
(; b.compareTo(
sqrtC(p).add(ONE))
<
0
;
b = b.add(ONE)) {
if
(p.mod(b).equals(ZERO)) {
ans[
0
] = b;
ans[
1
] = p.divide(b);
}
}
return
ans;
}
public
static
void
solve(BigInteger a, BigInteger m)
{
BigInteger s[] = factorise(m);
BigInteger ZERO =
new
BigInteger(
"0"
);
if
(!s[
0
].multiply(s[
1
]).equals(m)) {
System.out.println(
"Not a product "
+
"of two primes"
);
}
else
{
BigInteger s1[] =
new
BigInteger[
4
];
BigInteger a1[] =
new
BigInteger[
4
];
BigInteger a2[] =
new
BigInteger[
2
];
a1[
0
] = squareRoot(a.mod(s[
0
]), s[
0
]);
a1[
1
] = squareRoot(a.mod(s[
1
]), s[
1
]);
a1[
2
] = a1[
0
].multiply(
new
BigInteger(
"-1"
));
a1[
3
] = a1[
1
].multiply(
new
BigInteger(
"-1"
));
while
(a1[
2
].compareTo(ZERO) <
0
)
a1[
2
] = a1[
2
].add(s[
0
]);
while
(a1[
3
].compareTo(ZERO) <
0
)
a1[
3
] = a1[
3
].add(s[
1
]);
s1[
0
] = s[
0
];
s1[
1
] = s[
1
];
s1[
2
] = s[
0
];
s1[
3
] = s[
1
];
if
(a1[
0
].equals(
new
BigInteger(
"-1"
))
|| a1[
1
].equals(
new
BigInteger(
"-1"
))) {
System.out.println(
"No Solution"
);
}
else
{
System.out.print(
CRT(s, a1,
2
) +
" "
);
a2[
0
] = a1[
0
];
a2[
1
] = a1[
3
];
System.out.print(
CRT(s, a2,
2
) +
" "
);
a2[
0
] = a1[
2
];
a2[
1
] = a1[
1
];
System.out.print(
CRT(s, a2,
2
) +
" "
);
a2[
0
] = a1[
2
];
a2[
1
] = a1[
3
];
System.out.print(
CRT(s, a2,
2
) +
" "
);
}
}
}
public
static
void
main(String[] args)
throws
Exception
{
BigInteger N =
new
BigInteger(
"188"
);
BigInteger P =
new
BigInteger(
"437"
);
solve(N, P);
}
}