[step:Run long division using the nonzero leading coefficient of the divisor]
Assume now that $g \ne 0$. Since the input for $g$ is canonical, its leading coefficient $v_b$ satisfies $v_b \ne 0$, so division by $v_b$ is a permitted field operation.
Initialize a mutable remainder polynomial $R_0 \in k[x]$ by $R_0=f$ and initialize a quotient polynomial $Q_0 \in k[x]$ by $Q_0=0$. At the start of each loop iteration, store $R_t$ as a dense list of length at most $a+1$ and determine whether $R_t=0$ and, if not, determine $\deg R_t$ by scanning this list downward with the zero-test algorithm from the canonicalization procedure. For $t=0,1,\dots$, while $R_t \ne 0$ and $\deg R_t \ge b$, let $d_t=\deg R_t$, let $\rho_t \in k$ be the coefficient of $x^{d_t}$ in $R_t$, and define
\begin{align*}
c_t=\rho_t/v_b.
\end{align*}
Set
\begin{align*}
Q_{t+1}=Q_t+c_t x^{d_t-b},
\end{align*}
and
\begin{align*}
R_{t+1}=R_t-c_t x^{d_t-b}g.
\end{align*}
The coefficient of $x^{d_t}$ in $R_{t+1}$ is $\rho_t-c_t v_b=0$, so either $R_{t+1}=0$ or $\deg R_{t+1}<d_t$. Thus the degree of the mutable remainder strictly decreases at every iteration. Since initially $\deg R_0 \le a$, the loop has at most $a+1 \le N$ iterations.
At termination, define $q=Q_T$ and $r=R_T$, where $T$ is the number of loop iterations. By summing the update identity
\begin{align*}
R_{t+1}=R_t-(Q_{t+1}-Q_t)g
\end{align*}
over all iterations, we obtain
\begin{align*}
f=R_0=Q_Tg+R_T=qg+r.
\end{align*}
The stopping condition gives $r=0$ or $\deg r<b=\deg g$.
The algorithm stores $R_t$ densely with length at most $a+1$ and stores $Q_t$ densely with length at most $\max\{1,a-b+1\}$. In one loop iteration it computes one field division, scans at most $a+1 \le N$ coefficients to determine the current degree, and then updates at most $b+1 \le N$ coefficients of the remainder, so the total number of field operations and zero tests over all iterations is bounded by a polynomial in $N$. Every coefficient appearing in any $Q_t$ or $R_t$ is obtained from the input coefficients by a straight-line computation of polynomial length using the allowed field operations and divisions by the already verified nonzero element $v_b$. Hence all intermediate encodings actually produced by the operation algorithms have polynomial length by the size-bound hypothesis, and the field operations and zero tests performed by the loop take polynomial time in $N$.
Canonicalizing the final dense coefficient lists of $q$ and $r$ uses polynomially many zero tests on polynomially bounded encodings. Therefore the [division algorithm](/theorems/725) runs in polynomial time and returns canonical dense encodings of polynomials $q,r$ satisfying the required division identity and degree condition.
[/step]