def f(n,k=1);(k..n).reduce(:*)||1;end def c(n,k);f(n,n-k+1)/f(k);end def d(n);n<10?n: d(n/10+n%10);end gets while gets l=$_.size-1 p d(l.times.reduce(0){|m,i|m+$_[i].hex*c(l-1,i)}) end