"""Exact finite counts by separated splits, including incomplete periods."""
from functools import cache
from alternating import profile,score
G='001011001011001011010011'
LONG=''.join(str(2687066841669990205510125655672349568346167410879636543049>>i&1) for i in range(192))
def dot(x,y):return sum(a*b for a,b in zip(x,y))
def add(x,y):return tuple(a+b for a,b in zip(x,y))
def tally(w,n):
    return tuple(w.count(c)*(n//len(w))+w[:n%len(w)].count(c) for c in '01')
def tris(lo,hi):
    f,u,_,t=lo;g,_,d,s=hi
    return t+s+dot(f,d)+dot(g,u)
@cache
def horton(w,n):
    f=tally(w,n)
    if n<=1:return f,(0,0),(0,0),0
    v=w if len(w)%2==0 else w+w
    l=horton(v[::2],(n+1)//2); h=horton(v[1::2],n//2)
    adj=tuple(tally(''.join(str(int(x==y==c)) for x,y in zip(w,w[1:]+w[:1])),n-1)[1] for c in '01')
    return f,add(adj,h[1]),add(adj,l[2]),tris(l,h)
def sizes(n):
    # g full guard points per 32; count guard positions in last partial block
    slots=''.join('G'+('F' if x==y else '') for x,y in zip(G,G[1:]+G[:1]))
    g=n//len(slots)*len(G)+slots[:n%len(slots)].count('G')
    return n-g,g
@cache
def layer(n):
    if n<=1:return tally('01',n),(0,0),0
    k,g=sizes(n)
    f,u,t=layer(k)
    lo=f[::-1],u[::-1],None,t
    hi=horton(G,g)
    return add(lo[0],hi[0]),hi[1],tris(lo,hi)
def final(n):
    f,u,t=layer((n+1)//2);h,v,s=layer(n//2)
    return t+s+dot(f,v[::-1])+dot(u,h[::-1])
if __name__=='__main__':
    scout24=''.join(str(11715388>>i&1) for i in range(24))
    for w in ['100',scout24,G,LONG]:
        print('period',len(w),profile(w))
    print('guard lift',score(G))
    for n in [33,65,97,128,193,257,1025,65537,10**6,10**9+7,10**15+37]:
        t=final(n);print(n,t,'%.10f'%(t/n**2))
