"""Independent implementation of the repeated-zero word injection and its inverse. This adapts Coester's sign-eliminating map. It is a verifier, not its proof: the proof (including treatment of all untouched zero words) is in the manuscript. Python standard library only. """ from collections import Counter from itertools import permutations, product import json def counts(words,n): c=Counter(a for w in words for a in w) return tuple(c[a] for a in range(1,n+1)) def forward(words,i,j,k): w={a:tuple(v) for a,v in enumerate(words,1)} L,U=len(w[i]),len(w[j]);m=U-L assert i<=k0 active=[i]+[next(a for a in w if len(w[a])==L+t) for t in range(1,m)] out=w.copy();tokens=w[j][L:];out[j]=w[j][:L];v=(k,) for a,b in reversed(list(zip(active,tokens))): if a>j: v=(b,)+v elif a>k: out[a]=w[a][:-1]+(b,);v=(w[a][-1],)+v else: out[a]=w[a]+v;v=(b,) assert v==(tokens[0],) return tuple(out[a] for a in range(1,len(words)+1)) def inverse(out,j,d): n=len(out);w={a:tuple(v) for a,v in enumerate(out,1)} deficit=tuple(a-b for a,b in zip(d,counts(out,n))) assert sum(deficit)==1 and min(deficit)>=0 b1=deficit.index(1)+1;assert b1>=j L=len(w[j]);U=min(len(v) for v in w.values() if len(v)>L and v[-1]j: tokens.append(v[0]);v=v[1:] else: tokens.append(w[a][-1]);recovered[a]=w[a][:-1]+(v[0],);v=v[1:] assert v==(k,) recovered[j]=w[j]+tuple(tokens) return tuple(recovered[a] for a in range(1,n+1)),i,k def check_case(n,capacity): seen=set();checked=0;zero_starts=0 expected=sorted([0]*(n-capacity)+list(range(1,capacity+1))) for assigned in permutations(range(n),capacity): lengths=[0]*n for level,label in enumerate(assigned,1):lengths[label]=level choices=[list(product(range(a+1,n+1),repeat=lengths[a])) for a in range(n)] for words in product(*choices): c=counts(words,n) for j in range(2,n+1): for i in range(1,j): if lengths[i-1]>=lengths[j-1]:continue for k in range(i,j): d=list(c);d[k-1]+=1;d=tuple(d) out=forward(words,i,j,k) assert sorted(map(len,out))==expected assert all(all(v>=a for v in w) for a,w in enumerate(out,1)) assert inverse(out,j,d)==(words,i,k) key=(j,d,out) assert key not in seen,'collision' seen.add(key);checked+=1;zero_starts+=lengths[i-1]==0 return {'N':n,'C':capacity,'inputs_checked':checked,'zero_start_inputs':zero_starts} if __name__=='__main__': records=[] for n,c in [(2,1),(3,1),(3,2),(4,1),(4,2),(4,3),(5,2),(5,3)]: r=check_case(n,c);records.append(r);print(json.dumps(r),flush=True) print(json.dumps({'total':sum(r['inputs_checked'] for r in records),'failures':0}),flush=True)