"""Check compact CRT coordinates and the reported median-relative gap address."""
from math import gcd
import json


def experiment():
    left,right=7,30
    reconstructed={(right*u+left*v)%(left*right) for u in range(left) for v in range(right)}
    assert reconstructed==set(range(left*right))
    modulus=614889782588491410
    total=108357096234044  # supplied global count
    mid=modulus//2
    stop=307444891294910147
    def gap_start(x):
        return gcd(x,modulus)==gcd(x+30,modulus)==1 and all(gcd(x+h,modulus)>1 for h in range(1,30))
    # Reflection x -> -30-x. Two short boundary intervals correct the half-period count.
    overlap=sum(gap_start(x) for x in range(mid-29,mid))
    missing=sum(gap_start(x) for x in range(modulus-29,modulus))
    count,remainder=divmod(total+overlap-missing,2)
    assert not remainder
    prev=None
    hits=[]
    for x in range(mid,stop+31):
        if gcd(x,modulus)==1:
            if prev is not None and x-prev==30:
                hits.append(prev)
            prev=x
    assert len(hits)==100 and hits[-1]==stop and count+99==54178548117121
    return dict(coordinate_pairs=len(reconstructed),global_count_supplied=total,overlap=overlap,
                uncovered=missing,median_prefix=count,local_hits=len(hits),last=hits[-1],zero_based_rank=count+99)


if __name__=="__main__":
    print(json.dumps(experiment(),indent=2))
