#!/usr/bin/python

import math

c = int("2A4C9AA52257B56837369D5DD7019451C0EC04427EB95EB741D0273D55", 16)
n = int("0D8A7A45D9BE42BB3F03F710CF105628E8080F6105224612481908DC721", 16)
t = int("1398ED7F59A62962D5A47DD0D32B71156DD6AF6B46BEA949976331B8E1", 16)

# print(len(hex(t)[2:])*4)

def linear_diophantine_equation(a, b):
    if b > a:
        return linear_diophantine_equation(b, a)

    if b == 0:
        return a, 1, 0

    d, x, y = linear_diophantine_equation(b, a % b)
    return (d, y, x - (a // b) * y)

def multiplicative_inverse(a, b):
    d, x, y = linear_diophantine_equation(a, b)
    if y < 0:
        y = (b if b > a else a) + y
    return y

def gcd(a, b):
    d, x, y = linear_diophantine_equation(a, b)
    return d

def test_solution(m):
    return (m**2) % n == c

def is_square_num(n):
    if n <= 0:
        return False
    return math.sqrt(n)**2 == n

def is_int(n):
    return int(n) == n

# m*m - k*n = c
# (m*m)/c - (k*n)/c = 1       k' = k * c
# m * m * c^-1 - k' * n = 1

# c = m*m - k*n
# c = 1*x - k*n  mit x = m^2

# c = gcd(m, n)

d, x, y = linear_diophantine_equation(n, c)
print(d, x, y)

for i in range(-10, 10):
    print(i, test_solution(y + i * t))
    # h = hex(y)[2:]
    # print(''.join([chr(int(h[i:i+2], 16)) for i in range(len(h))]))

# print(y * c + x * n)

# y1 * c + x1 * n = 1
# y2 * m*m + x2 * n = 1

# y2 * m*m + x2 * n = y1 * c + x1 * n
# y2 * m*m + (x2-x1)*n = y1 * c
# -y1*c + (x2-x1)*n = y2*m*m

lcm = c * n
while not is_square_num(lcm):
    lcm += n

print(hex(lcm))
print(is_square_num(lcm))

# tmp = -1 * y * c - x * n
#
# solution = tmp + n*n
# while not is_square_num(solution) and not test_solution(math.sqrt(solution)):
#     solution += n
#
#     if is_square_num(solution):
#         print(len(hex(math.sqrt(solution))[2:]), hex(int(math.sqrt(solution))))
#

#
# print(is_square_num(x))
# print(test_solution(x))
# # print(hex(d))

# ggT(m², n) = ggT(c, n)

# print(gcd(c,n))

# gcd(m**2, n) = 1
#
# 1 = x*m**2 + y*n
# 1 = x*m**2*c + y*n
#
# x1*m**2 + y1*n = x2*m**2*c + y2*n
# 0 = m**2*x2*c-x1*m**2 + (y1-y2)*n
#
# gcd(m**2*c, n) = gcd(m**2, n)
#
# print(gcd(c, n))

# m = int("c20cd4b471c96cc2eaab1d1c6e33494219679ae97e48506e311ddbba35", 16)
# print(m**2 % n - c)
# print(test_solution(m))

# mult_inverse = multiplicative_inverse(c, n)
#
# d, x, y = linear_diophantine_equation(mult_inverse, n)
# print(d,x,y)
#
# print(mult_inverse*x - y*n)
#
# print(is_square_num(x))
# print(is_square_num(y))

# print(is_square_num(c))

# n > t > c

# m              = flag

# m**2 % n = x**2
# m**2 + k*n = c
# m % n = x
# m - k*n = x
# m = x + k * n

# x = int(math.sqrt(c))
#
# while not test_solution(x):
#     x += n
#     print(hex(x), hex(((x**2)%n)-c))
# print(x)
#
#
# d, x, y = linear_diophantine_equation(mult_inverse, mult_inverse*n)
# print(hex(d + 6*t - 2*c), test_solution(d + 6*t - 2*c))

# x = math.sqrt(n)
# print(x)
#
# print(test_solution(c * math.sqrt()))

# print(test_solution(math.floor(math.sqrt(c * n))))

# c = (m**2) % n
# c = m*m - k*n
# 1 = (m**2)/c - (k*n)/c
# 1 = (m**2)/c - k*(n/c)

# k = 1
# test = k * n / c
# while test != math.floor(test):
#     k += 1
#     test = k * n / c
#
# print(k, test)


# x = m*m
# c = x % n
# c = x - k*n
# c = k*n+x
# x ~ (48 56 31 38 2d) ^ 2
# m_guess = int("485631382d616161612d616161612d616161612d616161612d61616161", 16)
# x_guess = m_guess ** 2
# k_guess = (c - x_guess) / n
#
# i = 0
# while not test_solution(m_guess):
#     m_guess += 1
#     i += 1
#
# print(i, m_guess)

# print(test_solution(math.sqrt(c + n)))

# print(math.sqrt(c))

# c = x**2
# m**2 % n = x**2
# m % n = x

# i = 0
# solutioni = 0
# while i < 100:
#     i += 1
#     if test_solution(i):
#         print(i, i**2%n)

# 4**2 % 13 = 3
# 9**2 % 13 = 3
# 17**2 % 13 = 3
# 22**2 % 13 = 3
# 30**2 % 13 = 3
