B = gets.to_i S = gets.chomp L = S.size N = S.to_i(B) #N = gets.to_i(B) #N = 9**100000 # guchoku # str = ((0..999).map{|e|e.to_s(B)}*"") # puts str[0..50] # puts str[N] def dig(y, w) # range "000".."999" i = y/w d = y%w i.to_s(B)[d] || "0" end # deprecated def climb(x, sz, ofs, w) # range "100".."999" len = (sz-ofs)*w # p [x,sz,ofs,w] # p (ofs...sz).map{|e|e.to_s(B)} if x >= len climb(x-len, sz*B, sz, w+1) else # p B**(w-1) # ((ofs...sz).map{|e|e.to_s(B)}*"")[x] dig(x+B**(w-1)*w, w) end end # deprecated def climb_flat(x, sz, ofs, w) len = (sz-ofs)*w while x >= len x, sz, ofs, w = x-len, sz*B, sz, w+1 len = (sz-ofs)*w end dig(x+B**(w-1)*w, w) end # deprecated def climb_flat2(x) ofs, w = 1, 1 len = (B-1)*w # ((B-1)*w)*B**w l = 0 while x >= len l += len x, ofs, w = x-len, ofs*B, w+1 len = ofs*(w*(B-1)) end dig(x+len/(B-1), w) end # deprecated def climb_flat3(x) ((L-10)..(L-1)).each do |w| len = ( ((B-1)*(w)+B-1).to_s(B)+"0"*(w) ).to_i(B) sum = ( (w-1).to_s(B)+(B-2).to_s*(w-1)+(B-1).to_s ).to_i(B) return dig(x-sum+len/(B-1), w+1) if x < len+sum end len = ( ((B-1)*(L-1)+B-1).to_s(B)+"0"*(L-1) ).to_i(B) sum = ( (L-2).to_s(B)+(B-2).to_s*(L-2)+(B-1).to_s ).to_i(B) return dig(x-sum+len/(B-1), L) end def climb_flat4(x) ww = len = sum = nil L.downto(0) do |w| l = ( ((B-1)*(w)+B-1).to_s(B)+"0"*(w) ).to_i(B) s = ( (w-1).to_s(B)+(B-2).to_s*(w-1)+(B-1).to_s ).to_i(B) break unless x < l+s ww = w len = l sum = s end return dig(x-sum+len/(B-1), ww+1) end if N == 0 p 0 else puts climb_flat2(N-1) if L <= 20 puts climb_flat4(N-1) unless L <= 20 end