lib C fun strtoll(s : UInt8*, p : UInt8**, b : Int32) : Int64 end class String def to_i64 C.strtoll(self, nil, 10) end end def enumrate_combination(n) res = Array.new(n + 1) { Array.new(n + 1, 0i64) } (0..n).each do |i| res[i][0] = res[i][i] = 1 end (1..n).each do |i| (1...i).each do |j| res[i][j] = res[i - 1][j - 1] + res[i - 1][j] end end res end Comb = enumrate_combination(26) def enum_len(len, size) strings = Array(Array(Char)).new(size) (3..len).step(3) do |cnt5| (0...len - 1).to_a.each_combination(len - cnt5) do |ind3| string = Array.new(len, '5') ind3.each { |i| string[i] = '3' } strings << string end end strings end def func(n) sum = 0i64 (3..100).each do |len| tmp = (3..len).step(3).sum do |cnt5| Comb[len - 1][cnt5 - 1] end return {len, n - sum, tmp} if sum + tmp >= n sum += tmp end end n = read_line.to_i64 len, num, val = func(n).not_nil! puts enum_len(len, val).sort[num - 1].join