def Morph(f,w): m='' for i in range(len(w)): m+=f[int(w[i])] return m def CountWords(n,k,filter,start=''): long = start l = '0' Count=0 while l: w = start + l if not filter(w): if len(w) == n: Count+=1 a = int(l[-1]) l=l[0:-1] while l and a == k-1: a = int(l[-1]) l=l[0:-1] if not a == k-1: l+=str(a+1) else: l+='0' else: a = int(l[-1]) l=l[0:-1] while l and a == k-1: a = int(l[-1]) l=l[0:-1] if not a == k-1: l+=str(a+1) return Count def AdditiveFourthPowerSuffix(w): n=len(w) W=[0] T=0 for i in range(n): T+=int(w[i]) W.append(T) for r in range(1,n//4+1): i=n-4*r if W[i+r]-W[i]==W[i+2*r]-W[i+r]==W[i+3*r]-W[i+2*r]==W[i+4*r]-W[i+3*r]: return w[i:i+r],w[i+r:i+2*r],w[i+2*r:i+3*r],w[i+3*r:i+4*r] return None for t in range(2,51): C=CountWords(t,2,AdditiveFourthPowerSuffix,'0') print(t,2*C)