A question about modulus for polynomials The other day my friend was asked to find $A$ and $B$ in the equation
$$(x^3+2x+1)^{17} \equiv Ax+B \pmod {x^2+1}$$
A method was proposed by our teacher to use complex numbers and especially to let $x=i$ where $i$ is the imaginary unit. We obtain from that substitution
$$(i+1)^{17} \equiv Ai+B \pmod {0}$$
which if we have understood it correctly is valid if we define  $a \equiv b \pmod n$ to be $a=b+dn$. Running through with this definition we have 
$$\begin{align*}
(i+1)^{17} &=\left(\sqrt{2}\left(\cos\left(\frac{\pi}{4}\right)+\sin\left(\frac{\pi}{4}\right)\right)\right)^{17}\\
&=\sqrt{2}^{17}\left(\cos\left(\frac{17\pi}{4}\right)+\sin\left(\frac{17\pi}{4}\right)\right) \tag{De Moivre}\\
&=256\left(\sqrt{2}\left(\cos\left(\frac{\pi}{4}\right)+\sin\left(\frac{\pi}{4}\right)\right)\right)\\
&=256\left(1+i\right) \\
&=256+256i\end{align*}
$$
which gives the correct coefficient values for $A$ and $B$.
Our questions are


*

*Why is this substitution valid to begin with?

*It seems here that the special case ($x=i$) implies the general case ($x$), why is that valid?

 A: *

*It comes from the exponential form of complex numbers: $1+i$ has modulus $\sqrt 2$ and argument $\frac\pi 4$, so it writes as 
$$1+i=\sqrt2\,\mathrm e^{\tfrac\pi 4},\quad\text{and similarly}\quad 1-+i=\sqrt2\,\mathrm e^{-\tfrac\pi 4} $$
The substitution is valid because of the meaning of the congruence:
\begin{align}&(x^3+2x+1)^{17} \equiv Ax+B\enspace (\!\bmod {x^2+1)}\\ 
&\qquad\qquad\iff \exists q(x):\;(x^3+2x+1)^{17} =(x^2+1)q(x)+Ax+B  ,
\end{align}
so when you set $x=\pm i$, the first  term in the r.h.s. cancels.

*Setting $x=i$ yields an equation for $A$ and $B$, that's all. 

A: Here I think it's easier to see what's going on if we forgo the modular arithmetic and look at simple factoring and remainder. We have
$$
(x^3+2x+1)^{17}=(x^2+1)Q(x)+Ax+B
$$
for some polynomial $Q$. Which polynomial? We don't really care. The main point is that the left-hand side and the right-hand side are the same polynomial.
And since they are the same, they must give the same value when we evaluate them at $x=i$. So we insert $x=i$ and get
$$
(i^3+2i+1)^{17}=(i^2+1)Q(i)+Ai+B\\
(i+1)^{17}=0\cdot Q(i)+Ai+B
$$
Knowing that $A,B$ are real means we can find them directly from this, as $Q$ disappears.
