import fractions

class Value:
    "Class representing a polynomial in t, mod t^12 - t^6 + 1"

    def __init__(self, coeffs):
        # coeffs is an array with coeffs[i] giving the coefficient of t^i.
        # The input to this constructor can be any length; we'll reduce it.
        while len(coeffs) > 12:
            # Remove the highest-order coefficient
            coeff = coeffs.pop()
            n = len(coeffs)
            # Popping that coefficient subtracted coeff * t^n.
            # Add coeff * (t^(n-6) - t^(n-12)) to compensate.
            coeffs[n-6] += coeff
            coeffs[n-12] -= coeff
        # Pad with zeroes so that len(self.coeffs) is always exactly 12
        self.coeffs = coeffs + [0] * (12 - len(coeffs))

    def __add__(self, other):
        # Add corresponding coefficients
        return Value([u+v for u,v in zip(self.coeffs, other.coeffs)])

    def scale(self, scalar):
        # Multiply every coefficient by the scale
        return Value([scalar*u for u in self.coeffs])

    def __sub__(self, other):
        # Negate the RHS, then add
        return self + other.scale(-1)

    def mul_by_t(self):
        # Multiply by t by shifting all the coefficients upward by one
        return Value([0] + self.coeffs)

    def __mul__(self, other):
        total = Value([]) # zero
        other_times_power_of_t = other # other * t^0
        for coeff in self.coeffs:
            # Add coeff * other * t^i to the total
            total += other_times_power_of_t.scale(coeff)
            # Multiply by t to make the next (other * t^i)
            other_times_power_of_t = other_times_power_of_t.mul_by_t()
        return total

    def evaluate_at(self, x):
        total = Value([]) # zero
        power_of_x = Value([1]) # x^0
        for coeff in self.coeffs:
            # Add coeff * x^i to the total
            total += power_of_x.scale(coeff)
            # Multiply by x to make the next x^i
            power_of_x *= x
        return total

    @staticmethod
    def power_of_t(n):
        # t^36 = 1, so reduce mod 36, which also makes negative n positive
        n %= 36
        # Make the literal polynomial t^n and let the constructor reduce it
        return Value([0]*n + [1])

    def complex_conjugate(self):
        # Evaluate at t^-1
        return self.evaluate_at(self.power_of_t(-1))

    def reciprocal(self):
        # Evaluate at t^5, t^7, t^11, ..., t^35 and multiply them all together
        w = Value([1])
        for index in [5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35]:
            w *= self.evaluate_at(self.power_of_t(index))
        # Check that w * x is a constant (all coeffs after the 0th are zero)
        wx = self * w
        assert all(coeff == 0 for coeff in wx.coeffs[1:])
        # And divide by that constant, using fractions.Fraction for exactness
        return w.scale(fractions.Fraction(1, wx.coeffs[0]))

    def __truediv__(self, other):
        return self * other.reciprocal()

    def __str__(self):
        # Format as a mathematical expression in Python syntax
        return " + ".join(
            f"{u}" if i==0 else f"{u}*t" if i==1 else f"{u}*t**{i}"
            for i, u in enumerate(self.coeffs)
            if u != 0
        )

one = Value([1])
X = one
A = Value.power_of_t(10)
B = Value.power_of_t(-12)
D = X + (Value.power_of_t(4) - one) * (A - X) / (Value.power_of_t(-10) - one)
E = X + (Value.power_of_t(-6) - one) * (B - X) / (Value.power_of_t(8) - one)
z = (X - E) / (D - E)
q = z / z.complex_conjugate()

print("X =", X)
print("A =", A)
print("B =", B)
print("D =", D)
print("E =", E)
print("z =", z)
print("q =", q)
