For same padding,
\begin{aligned}
(n + 2p -f) / s + 1 &= n \Leftrightarrow \\
n + 2p - f &= (n - 1) * s \Leftrightarrow \\
p &= ((n - 1) * s + f - n) / 2.
\end{aligned}
With your example:
\begin{aligned}
p &= ((39 - 1) * 1 + 3 - 39) / 2 \Rightarrow \\
p &= (38 + 3 - 39) /2 \Rightarrow \\
p &= 1.
\end{aligned}
Thus, if the padding is 1, you will have the same output size as the input size, hence it is called same convolution.
To double check
(n + 2p -f) / s + 1 = (39 + 2 * 1 - 3) / 1 + 1 = 39,
which is equal to the input size.
Valid convolution is no padding, hence p = 0.