Help needed with this Dynamic Programming Question

I am preparing for INOI and am stuck on this problem here: http://acm.timus.ru/problem.aspx?space=1&num=1081

I am just a beginner with DP so I am not getting any idea on how we will solve this question. Can anyone explain me the proper logic of the solution to this problem instead of just giving the code?

Hi,

(Let’s call the least significant element to be element number 0.)

For each element, starting with element number 0, you can calculate the number of sequences starting from there, that begin with a 0, and those that begin with a 1. Basically, sequences_starting_with_zero[i] = sequences_starting_with_zero[i - 1] + sequences_starting_with_one[i - 1], and sequences_starting_with_one[i] = sequences_starting_with_zero[i - 1]. Do note that the starting condition is sequences_starting_with_one[0] = sequences_starting_with_zero[0] = 1.

Once you’ve such a table, calculating the Kth sequence should be simple. For starters, if K > sequences_starting_with_zero[N - 1] + sequences_starting_with_one[N - 1], then you can’t form the required lexicographical order. If you can form one, then, in a loop starting from the most significant element, if K > sequences_starting_with_zero[i], you output a 1, subtract sequences_starting_with_zero[i], and move further on. Else you output a 0, and move further on.

Cheers!

1 Like

I need a proper explanation but thanks!

@ketanhwr – proper enough?