Solution (source code)

= Solution

Attach a <quantum ancilla> prepared with amplitude
$$
c=\frac1{2\sqrt p}
$$
on $|1\rangle$; this is possible because $p>1/4$. Mark a state as good only when both the original success qubit and this ancilla equal one. The enlarged state's good probability is $pc^2=1/4$, so its good amplitude is $1/2=\sin(\pi/6)$. One <amplitude amplification> iteration rotates the angle from $\pi/6$ to $3\pi/6=\pi/2$. It therefore prepares $|\psi_1\rangle$ exactly, after which the flag and ancillary qubits may be discarded. This is an instance of <exact amplitude amplification>.