4.065. In a finite geometric progression, the first term $a$, the last term $b$, and the sum $S$ of all its terms are known. Find the sum of the squares of all the terms of this progression.
## Solution.

Let $b_{1}, b_{2}, b_{3}, \ldots b_{n}$ be the terms of a geometric progression, where $b_{1}=a, b_{n}=b, S_{n}=S$. Then

$$
\begin{aligned}
& b_{1}^{2}+b_{2}^{2}+b_{3}^{2}+\ldots=b_{1}^{2}+b_{1}^{2} q^{2}+b_{1}^{2} q^{4}+\ldots=b_{1}^{2}\left(1+q^{2}+q^{4}+\ldots\right)= \\
& =a^{2}\left(1+q^{2}+q^{4}+\ldots\right)=a^{2} \cdot \frac{1-q^{2 n}}{1-q^{2}}
\end{aligned}
$$

From the condition, we have

$$
\left\{\begin{array}{l}
a q^{n-1}=b, \\
\frac{a\left(1-q^{n}\right)}{1-q}=S
\end{array} \Rightarrow q^{n-1}=\frac{b}{a}, \frac{q^{n}}{q}=\frac{b}{a}, q^{n}=\frac{b}{a} \cdot q\right.
$$

Substituting this value of $q^{n}$ into the second equation of the system, we get

$$
\frac{\left(1-\frac{b}{a} \cdot q\right)}{1-q}=S, a-b q=S-S q, S q-b q=S-a,(S-b) q=S-a, q=\frac{S-a}{S-b}
$$

Then $q^{n}=\frac{b}{a} \cdot \frac{S-a}{S-b}$. Substituting $q^{n}=\frac{b}{a} \cdot \frac{S-a}{S-b}$ into (1):

$$
\begin{aligned}
& a^{2}\left(\frac{1-\frac{b^{2}(S-a)^{2}}{a^{2}(S-b)^{2}}}{1-\frac{(S-a)^{2}}{(S-b)^{2}}}\right)=\frac{a^{2}-\frac{b^{2}(S-a)^{2}}{(S-b)^{2}}}{1-\frac{(S-a)^{2}}{(S-b)^{2}}}=\frac{a^{2}(S-b)^{2}-b^{2}(S-a)^{2}}{(S-b)^{2}-(S-a)^{2}}= \\
& =\frac{(a S-a b-b S+a b)(a S-a b+b S-a b)}{(S-b-S+a)(S-b+S-a)}=\frac{S(a-b)((a+b) S-2 a b)}{(a-b)(2 S-(a+b))}= \\
& =\frac{(a+b) S-2 a b}{2 S-(a+b)} \cdot S
\end{aligned}
$$

Answer: $\frac{(a+b) S-2 a b}{2 S-(a+b)} \cdot S$.