All Expressions
http://golf.shinh.org/p.rb?All+Expressions
あとは速さを100倍に・・・
# all-expression2-5.rb $x=[] def g j,i,c a=("0"*c+j.to_s(3))[-c..-1] if(a[i..i]!=nil);a[i..i]=="0" ? "+":a[i..i]=="1" ? "*":"-";else;"-";end end def coun a,m c=0 a.length.times{|i| c+= (a[i..i]==m ? 1:0) } c end def make fnk,vl,kak l=vl.length n=[""]*(l+3) l.times{|i| po=0; (kak[i]+1).times{|m| while n[po].scan(")")!=[] po+=1 end po+=1 } po-=1 n[po]+="(" pot=po+coun(n[po],"(") while n[pot].scan("(")!=[] pot+=1 end n[pot]+=")" } x="" l.times{|i| if n[i][0..0]=="("; x+= n[i]+vl[i]+fnk[i] else; x+= vl[i]+n[i]+fnk[i] end } x+="0"+n[l]+n[l+1] x end def kakko f,a,c,k if c==0 $x.push(eval(make(f,a,k))) else c-=1 (c+1).times{|i| k+=[i] #p k,c kakko(f,a,c,k) k.pop } end $x end def chk f f.each{|i| if i=="*";return true;end } false end a=gets.split(" ") c=a.length-1 (3**c).times{|j| b="" f=[] (c+1).times{|i| s=g j,i,c f+=[s] b+=a[i]+s } b+="0" $x+=[eval b] k=[] if(chk(f));$x.push(kakko(f,a,c+1,k));$x.sort!.uniq!;end } #p $x puts $x.sort.uniq