import gmpy2, sys, time, math, numpy as np
from gmpy2 import mpz
N=int(sys.argv[1])
for (p,q) in [(2,5),(2,3),(2,7),(3,5)]:
    b=p*q; t=time.time()
    P=mpz(p)**N; Q=mpz(q)**N; M=P*Q
    e=Q*gmpy2.invert(Q,P)%M
    assert (e*e-e)%M==0 and e%P==1 and e%Q==0
    s=gmpy2.digits(e,b).rjust(N,'0')[::-1]
    d=np.frombuffer(s.encode(),dtype=np.uint8).astype(np.int64)
    d=np.where(d>=97,d-87,d-48)
    out=[]
    code=np.zeros(N,dtype=np.int64)
    for L in range(1,8):
        K=b**L
        if K>N: break
        code=(code[:N-L+1] if L==1 else code[:N-L+1]*b+d[L-1:])
        if L==1: code=d.copy()
        cnt=np.bincount(code,minlength=K); n=N-L+1; ex=n/K
        chi=((cnt-ex)**2/ex).sum(); z=(chi-(K-1))/math.sqrt(2*(K-1))
        out.append(f"L={L}: blocks missing {int((cnt==0).sum())}/{K}, chi2 z={z:+.2f}")
    print(f"base {b} (e=1 mod {p}^N, 0 mod {q}^N), N={N}: "+"; ".join(out), f"[{time.time()-t:.0f}s]",flush=True)
