×

# Help needed with this Dynamic Programming Question

 0 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? asked 06 Jan '15, 11:10 6★ketanhwr 1.9k●3●18●44 accept rate: 15%

 1 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! answered 06 Jan '15, 13:17 0★idraumr 246●3 accept rate: 45% I need a proper explanation but thanks! (06 Jan '15, 13:20) ketanhwr6★ @ketanhwr -- proper enough? (06 Jan '15, 13:57) idraumr0★
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×2,212
×399
×195
×166
×71
×28

question asked: 06 Jan '15, 11:10

question was seen: 3,646 times

last updated: 06 Jan '15, 13:57