FAST-DIVMOD returns the quotient and remainder of division with remainder in O(M(n)) operations full
This proof shows: correctness.
Let R be a commutative ring with 1, a,b∈R[x] with b=0 and lc(b) a unit, n=dega and m=degb after line L1. For f with degf≤j write revj(f)=xjf(1/x), a polynomial of degree at most j whose coefficient of xi is the coefficient of xj−i in f.
Lemma 1 (uniqueness). There is at most one pair (q,r) with a=qb+r and degr<m.
Proof. If qb+r=q′b+r′ with degr,degr′<m, then (q−q′)b=r′−r. If q=q′, the leading coefficient of (q−q′)b is lc(q−q′)lc(b), which is nonzero because lc(b) is a unit. So deg((q−q′)b)≥m>deg(r′−r), a contradiction. So q=q′ and then r=r′. □
Existence of the pair follows from long division, which needs only that lc(b) is a unit. It also follows from the theorem below, which constructs the pair.
Lemma 2 (reversal). Let n≥m, and let q,r satisfy a=qb+r with degq≤n−m and degr≤m−1. Then
revn(a)=revn−m(q)revm(b)+xn−m+1revm−1(r), where revm−1(r)=0 if m=0 (then r=0).
Proof. Substitute 1/x for x in a=qb+r in the ring R[x,1/x] and multiply by xn=xn−m⋅xm=xn−m+1⋅xm−1. This gives xna(1/x)=xn−mq(1/x)⋅xmb(1/x)+xn−m+1⋅xm−1r(1/x). Each of the four factors of the form xjf(1/x) has degf≤j, so it is the polynomial revj(f). □
Lemma 3 (the reversed divisor is invertible). The constant term of revm(b) is lc(b), a unit, so for each k≥1 there is a unique h of degree less than k with revm(b)⋅h≡1(modxk), and line L4 computes it.
Proof. The constant term is the coefficient of xm in b, which is lc(b). Existence and uniqueness of h, and the fact that NEWTON-INVERSE returns it, are Lemma 1 and the correctness theorem of the proof for Newton power series inversion. □
Theorem (correctness). FAST-DIVMOD(a,b) returns the unique pair (q,r) with a=qb+r and degr<degb.
Proof. If a=0 or n<m, line L2 returns (0,a), which satisfies a=0⋅b+a with dega<m. By Lemma 1 it is the unique pair.
Let n≥m and k=n−m+1≥1 (line L3). Let (q∗,r∗) be the pair of long division, so a=q∗b+r∗, degr∗<m, and degq∗=n−m because deg(q∗b)=deg(a−r∗)=n and lc(b) is a unit. By Lemma 2,
revn(a)≡revn−m(q∗)revm(b)(modxk). Multiply both sides by the h of line L4. By Lemma 3, revm(b)h≡1(modxk), so
revn(a)h≡revn−m(q∗)(modxk). Line L5 sets s=revn(a)modxk, and sh≡revn(a)h(modxk). So the t of line L6, which has degree less than k, is congruent to revn−m(q∗) modulo xk. Both have degree less than k, so t=revn−m(q∗), and line L7 gives q=revn−m(t)=q∗, since reversing twice with the same index is the identity on polynomials of degree at most n−m.
If m=0, then r∗=0 and the algorithm returns (q∗,0). Otherwise line L8 sets u=q∗bmodxm and line L9 sets r=(amodxm)−u=(a−q∗b)modxm=r∗modxm=r∗, because degr∗<m. So the algorithm returns (q∗,r∗). □
Theorem (cost). Assume that multiplying a polynomial with j coefficients by one with at most j coefficients costs at most M(j) ring operations, and that M(j)/j is nondecreasing. Let k=n−m+1. Then FAST-DIVMOD uses at most
4M(k)+M(m)+2⌈log2k⌉kM(k)+O(n) ring operations, plus one inversion of lc(b), and this is at most 4M(n+1)+o(M(n))+O(n). With a precomputed inverse it uses at most M(k)+M(m)+O(n).
Proof. Lines L1, L3, L5, L7 and L9 touch each coefficient a bounded number of times: O(n). Line L4 costs at most 3M(k)+2⌈log2k⌉M(k)/k+O(k) by the cost theorem for Newton inversion. Line L6 multiplies two polynomials with k coefficients: at most M(k). Line L8 needs only qmodxm, which has at most min(k,m) coefficients, times bmodxm, which has m coefficients: at most M(m). Adding these gives the first bound, and dropping line L4 gives the bound with a precomputed inverse.
For the second bound, M(j)/j nondecreasing gives M(k)≤k+mkM(k+m) and M(m)≤k+mmM(k+m), so 4M(k)+M(m)≤4(M(k)+M(m))≤4M(k+m)=4M(n+1). Also 2⌈log2k⌉M(k)/k≤2⌈log2(n+1)⌉M(n+1)/(n+1), which is o(M(n+1)) because logn/n→0. ■