fork(1) download
  1. def trace lists, n
  2. puts "="+lists.reverse.inject(n.to_s){|a,b|
  3. if b.size==1 && b[0]==1
  4. a = 'f('+a+')'
  5. else a = 'f['+b.join+']('+a+')'
  6. end
  7. }
  8. end
  9.  
  10. def brace list, n
  11. lists = [list]
  12. puts 'f['+list.join+']('+n.to_s+')'
  13. while true
  14. last = lists.last
  15. if last.size==1
  16. if last[0]==1 then return
  17. else
  18. last[0]-=1
  19. (n-1).times do lists << last.clone end
  20. end
  21. elsif last[0]==1
  22. index = last.find_index {|a| a!=1 }
  23. last.fill n, 0, index
  24. last[index] -= 1
  25. last.pop if last.last==1
  26. elsif last.last==1
  27. last.pop
  28. next
  29. else
  30. last[0] -= 1
  31. last.pop if last.last==1
  32. (n-1).times do lists << last.clone end
  33. end
  34. trace lists, n
  35. end
  36. end
  37.  
  38. $, = ','
  39.  
  40. #forkしたのち、以下を、例えばf[1,2,2](3)ならbrace [1,2,2],3と書き換えてください
  41. brace [2,2,2],3
Success #stdin #stdout 0.02s 7432KB
stdin
Standard input is empty
stdout
f[2,2,2](3)
=f[1,2,2](f[1,2,2](f[1,2,2](3)))
=f[1,2,2](f[1,2,2](f[3,1,2](3)))
=f[1,2,2](f[1,2,2](f[2,1,2](f[2,1,2](f[2,1,2](3)))))
=f[1,2,2](f[1,2,2](f[2,1,2](f[2,1,2](f[1,1,2](f[1,1,2](f[1,1,2](3)))))))
=f[1,2,2](f[1,2,2](f[2,1,2](f[2,1,2](f[1,1,2](f[1,1,2](f[3,3](3)))))))
=f[1,2,2](f[1,2,2](f[2,1,2](f[2,1,2](f[1,1,2](f[1,1,2](f[2,3](f[2,3](f[2,3](3)))))))))
=f[1,2,2](f[1,2,2](f[2,1,2](f[2,1,2](f[1,1,2](f[1,1,2](f[2,3](f[2,3](f[1,3](f[1,3](f[1,3](3)))))))))))
=f[1,2,2](f[1,2,2](f[2,1,2](f[2,1,2](f[1,1,2](f[1,1,2](f[2,3](f[2,3](f[1,3](f[1,3](f[3,2](3)))))))))))
=f[1,2,2](f[1,2,2](f[2,1,2](f[2,1,2](f[1,1,2](f[1,1,2](f[2,3](f[2,3](f[1,3](f[1,3](f[2,2](f[2,2](f[2,2](3)))))))))))))
=f[1,2,2](f[1,2,2](f[2,1,2](f[2,1,2](f[1,1,2](f[1,1,2](f[2,3](f[2,3](f[1,3](f[1,3](f[2,2](f[2,2](f[1,2](f[1,2](f[1,2](3)))))))))))))))
=f[1,2,2](f[1,2,2](f[2,1,2](f[2,1,2](f[1,1,2](f[1,1,2](f[2,3](f[2,3](f[1,3](f[1,3](f[2,2](f[2,2](f[1,2](f[1,2](f[3](3)))))))))))))))
=f[1,2,2](f[1,2,2](f[2,1,2](f[2,1,2](f[1,1,2](f[1,1,2](f[2,3](f[2,3](f[1,3](f[1,3](f[2,2](f[2,2](f[1,2](f[1,2](f[2](f[2](f[2](3)))))))))))))))))
=f[1,2,2](f[1,2,2](f[2,1,2](f[2,1,2](f[1,1,2](f[1,1,2](f[2,3](f[2,3](f[1,3](f[1,3](f[2,2](f[2,2](f[1,2](f[1,2](f[2](f[2](f(f(f(3)))))))))))))))))))