# get number in factorial base, ignoring the place of 0! (always 0)
r=lambda n,q=[],i=2:n and r(n//i,q+[n%i],i+1)or q
# rewrite a number in a form using only 1s by converingconverting its factorial base, the range only reuiresrequires using up to 12 places, again ignoring the 0! place so we only hard code 1 and [5-12] (9 numbers)
def g(n):
k=['','1']+['1'+'+1'*i for i in range(1,4)]+['(11-1)/(1+1)','t(1+1+1)','1+t(1+1+1)','11-1-1-1','11-1-1','11-1','11','1+11']
q=r(n)
return n<13and k[n]or(q[0]and'1+'or'')+'+'.join((v>1and'('+k[v]+')*'or'')+(i>2and't'or'')+'('+k[i]+')'for i,v in enumerate(q[1:],2)if v)
#get g(n) representations after differencing from 0, 11, 111, 1111, ... then return the one with the minimal stand-alone score
def h(n):
o=[g(n)]+[str(v)+(v<n and'+('or'-(')+g(abs(v-n))+')'for v in[int('1'*l)for l in range(2,11)]]
s=[sum(map(v.count,'+-*/t'))*v.count('1')for v in o]
return o[s.index(min(s))]
# A Factorial function for analysis with eval
def t(n):
r = 1
while n:
r *= n
n -= 1
return r