function find_a(x) --find a where a(a-1) = x b = 1 + math.ceil(math.sqrt(x)) cand = b * (b - 1) while(x <= cand) do if(x == cand) then return true, b end b = b - 1 cand = b * (b - 1) end return false, 0 end k = io.read("*n") k = k * 2 -- find n, a where k = a(a-1) * 2^(n-a) c, cpow = 1, 0 -- c = 2^(n-a), cpow = n - a while(c < 2 * k) do if(k % c ~= 0) then c, cpow = c / 2, cpow - 1 break end c, cpow = c * 2, cpow + 1 end found, a = false, 0 -- search from large 2^(n-a) for i_c = cpow, 0, -1 do rem = k / c found, a = find_a(rem) if(found) then cpow = i_c break end c = c / 2 end if(found) then print(a + cpow) io.write("1") for i = 2, a do io.write(" 1") end for i = 1, cpow do io.write(" 0") end io.write("\n") end