def naive(A): a=A[:] n=len(a) i=0 while 4*max(a)>=2**i:i+=1 maxx=2**i ans=set() for x in range(maxx+1): b=[] for i in range(n): b.append(a[i]^x) b.sort() for i in range(n):b[i]^=x ans.add(tuple(b)) return len(ans) def sol1(A): a=A[:] n=len(a) a.sort() ans=1 block=[(0,n-1)] for j in range(65,-1,-1): res=1 new=[] for l,r in block: if (a[l]>>j)&1==(a[r]>>j)&1: new.append((l,r)) continue mid=-1 for i in range(l,r): if (a[i]>>j)&1<(a[i+1]>>j)&1: mid=i break res=2 new.append((l,mid)) new.append((mid+1,r)) ans*=res block=new[:] return ans from random import randrange as rd cnt=0 while 0: cnt+=1 print(cnt) n=rd(1,100) a=[rd(100) for i in range(n)] ans1=naive(a) ans2=sol1(a) if ans1!=ans2: print(n) print(*a) exit()