# timeout: 120
import numpy as np

# Pour aller vite, on applique les deux réflexions directement au vecteur d'amplitudes :
# l'oracle change un signe, la diffusion fait « 2 · moyenne − a » (vérifié en 02).
print("  n |         N | itérations | P(succès) | essais classiques en moyenne, (N + 1) / 2")
for n in (4, 8, 12, 16, 20):
    N = 2**n
    marked = N // 3  # une chaîne marquée quelconque
    theta = np.arcsin(1 / np.sqrt(N))
    k = int(np.floor(np.pi / (4 * theta)))  # nombre d'itérations optimal
    a = np.full(N, 1 / np.sqrt(N))
    for _ in range(k):
        a[marked] = -a[marked]  # oracle
        a = 2 * a.mean() - a  # diffusion
    print(f"{n:>3} | {N:>9} | {k:>10} | {round(float(a[marked] ** 2), 4):>9.4f} | {(N + 1) / 2:>12.1f}")
