M = [-1, 0, 1] D = gets.to_i A = gets.split.take(D + 1).map(&:to_i) def f(a, m) res = [] while a.size > m.size n = a[-1].quo(m[-1]) res.unshift(n) (-m.size..-1).each{|i| a[i] -= n * m[i] } a.pop end a end ans = f(A, M) take = ans.rindex{|v|v != 0} || 0 p take puts ans.take(take + 1).map(&:to_i) * " "