You are not logged in. Please login at to post your questions!


Help needed with this Dynamic Programming Question

I am preparing for INOI and am stuck on this problem here:

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

ketanhwr's gravatar image

accept rate: 15%


(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.



answered 06 Jan '15, 13:17

idraumr's gravatar image

accept rate: 45%

edited 06 Jan '15, 13:35

I need a proper explanation but thanks!

(06 Jan '15, 13:20) ketanhwr6★

@ketanhwr -- proper enough?

(06 Jan '15, 13:57) idraumr0★
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • 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:


question asked: 06 Jan '15, 11:10

question was seen: 3,646 times

last updated: 06 Jan '15, 13:57