5. Given positive integers $p$ and $q$, a rope of length 1 is cut successively. At each step, the longest segments are selected and each is divided into two parts with a length ratio of $p: q$. After several steps, the longest segment has a length of $x$. Find the number of distinct values among the lengths of all segments $a(x)$.
5. When $p=q$, $a=1$.

For the case $p \neq q$, without loss of generality, assume $p>q, (p, q)=1$.
Lemma If positive integers $a, b, c, d$ satisfy
$$
\left(\frac{p}{p+q}\right)^{a}\left(\frac{q}{p+q}\right)^{b}=\left(\frac{p}{p+q}\right)^{c}\left(\frac{q}{p+q}\right)^{d},
$$

then $a=c, b=d$.
Proof Without loss of generality, assume $a \geqslant c$. Then
$$
1 \geqslant\left(\frac{p}{p+q}\right)^{a-c}=\left(\frac{q}{p+q}\right)^{d-b} \text {. }
$$

Thus, $d \geqslant b$.
Without loss of generality, assume $a-c \geqslant d-b$. Then
$$
p^{a-c}=q^{d-b}(p+q)^{a-c-d+b} \text {. }
$$

If $b \neq d$, then $q\left|q^{d-b} \Rightarrow q\right| p^{a-c}$.
By $(p, q)=1$, we get $q=1$.
Hence $p^{a-c}=(p+1)^{a-c-d+b}$.
But $(p, p+1)=1$, and $p>1$, then
$$
\begin{aligned}
a-c & =a-c-d+b=0 \\
\Rightarrow a & =c, b=d,
\end{aligned}
$$

a contradiction.
Thus, $b=d$.
Hence $\left(\frac{p}{p+q}\right)^{a-c}=1 \Rightarrow a=c$.
The lemma is proved.
At any moment, the length of each rope segment is of the form
$$
\left(\frac{p}{p+q}\right)^{a}\left(\frac{q}{p+q}\right)^{b}(a, b \in \mathbf{N}),
$$

represented by the integer point $(b, a)$ in the Cartesian plane. By the lemma, this correspondence is one-to-one.

A line with slope $-\frac{\ln \frac{q}{p+q}}{\ln \frac{p}{p+q}}$ is called an $\alpha$ line. The lemma shows that each $\alpha$ line passes through at most one integer point.

Define $(a, b)>(c, d)$ if and only if the $\alpha$ line through $(a, b)$ is below the $\alpha$ line through $(c, d)$.

For any point $P, l_{P}$ is the $\alpha$ line through point $P$, and the set of non-negative integer points below $l_{P}$ (excluding point $P$) is denoted as $D_{P}$. For a point $P=(a, b) \in \mathbf{N}^{2}$, define
$$
\begin{array}{l}
N(P)=\{(a+1, b),(a, b+1)\}, \\
N(T)=\underset{P \in T}{\cup} N(P), \\
H_{P}=N\left(D_{P}\right) \backslash D_{P},
\end{array}
$$
$S$ represents the set of integer points corresponding to the lengths of all rope segments at a certain moment.
By induction on the number of operations, it is easy to see that for a certain moment, if the longest rope segment corresponds to the integer point $P$, then $S=H_{P}$.

Let the length of the longest rope segment be $x=\left(\frac{p}{p+q}\right)^{a}\left(\frac{q}{p+q}\right)^{b}$, we only need to find $\left|H_{(a, b)}\right|$.

When $p>q$, we only need to find the number of integer points between $l_{(a, b)}$ and $l_{(a+1, b)}$, and add 1 (the added point is $(a, b)$).
We need to find $(c, d)$ such that
$$
\begin{array}{l}
\left(\frac{p}{p+q}\right)^{a}\left(\frac{q}{p+q}\right)^{b}>\left(\frac{p}{p+q}\right)^{c}\left(\frac{q}{p+q}\right)^{d} \\
>\left(\frac{p}{p+q}\right)^{a+1}\left(\frac{q}{p+q}\right)^{b},
\end{array}
$$

When $d$ is fixed, $c$ is uniquely determined.
Because $0 \leqslant dq ; \\
\left\lceil\log _{\frac{p}{p+q}} x\right\rceil+1, & p<q .
\end{array}\right.
\end{array}
$$