d=->n{n<10?n: d[n/10+n%10]} f=->n,k=1{(k..n).reduce(:*)||1} gets $<.each{|s|l=s=~/$/;p d[l.times.reduce(0){|m,i|m+s[i].hex*f[l-1,l-i]/f[i]}]}