Number of binary strings of length $n$, where every $1$, if any, is followed by at most $k$ $0$s

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite
1












This question is similar to the one asked here by another user, but I want to ask it again since it was worded poorly there and not enough context was provided.



Actually, this question was asked as a coding problem in a placement test (which is over now of course), where the input is given as two numbers $n$ and $k$, and we have to output the number of binary strings of length $n$, where every $1$ in the binary string, if any exists, is followed by at most $k$ zeroes.



For eg. for $n = 5, k = 0$, output must be $6$, which is the set $(00000, 00001, 00011, 00111, 01111, 11111)$.



I believe this can be done using dynamic programming, by developing a recurrence relation between a larger problem instance $f(n, k)$ and smaller ones, for eg. $f(n, k-1), f(n-1, k-1)$ etc., but I haven't been able to find one.



Base cases are easy to see: $f(n, 0) = n + 1$ $forall n$, $f(0, k) = 0$ $forall k$; and $f (n , k) = 2^n$ for $k geq n - 1$



Solution for a non-trivial instance - $f(6, 2) = 52$



Edit: I realized that Mike Earnest has found a solution in the linked question, based on combinatorics, so I am only interested in a recurrence relation. Also, from an algorithmic point of view, the time complexity of computing $n choose k$ is much higher than a polynomial solution that can be found with a recurrence relation, by building bottom-up from the base cases.










share|cite|improve this question



























    up vote
    1
    down vote

    favorite
    1












    This question is similar to the one asked here by another user, but I want to ask it again since it was worded poorly there and not enough context was provided.



    Actually, this question was asked as a coding problem in a placement test (which is over now of course), where the input is given as two numbers $n$ and $k$, and we have to output the number of binary strings of length $n$, where every $1$ in the binary string, if any exists, is followed by at most $k$ zeroes.



    For eg. for $n = 5, k = 0$, output must be $6$, which is the set $(00000, 00001, 00011, 00111, 01111, 11111)$.



    I believe this can be done using dynamic programming, by developing a recurrence relation between a larger problem instance $f(n, k)$ and smaller ones, for eg. $f(n, k-1), f(n-1, k-1)$ etc., but I haven't been able to find one.



    Base cases are easy to see: $f(n, 0) = n + 1$ $forall n$, $f(0, k) = 0$ $forall k$; and $f (n , k) = 2^n$ for $k geq n - 1$



    Solution for a non-trivial instance - $f(6, 2) = 52$



    Edit: I realized that Mike Earnest has found a solution in the linked question, based on combinatorics, so I am only interested in a recurrence relation. Also, from an algorithmic point of view, the time complexity of computing $n choose k$ is much higher than a polynomial solution that can be found with a recurrence relation, by building bottom-up from the base cases.










    share|cite|improve this question

























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      This question is similar to the one asked here by another user, but I want to ask it again since it was worded poorly there and not enough context was provided.



      Actually, this question was asked as a coding problem in a placement test (which is over now of course), where the input is given as two numbers $n$ and $k$, and we have to output the number of binary strings of length $n$, where every $1$ in the binary string, if any exists, is followed by at most $k$ zeroes.



      For eg. for $n = 5, k = 0$, output must be $6$, which is the set $(00000, 00001, 00011, 00111, 01111, 11111)$.



      I believe this can be done using dynamic programming, by developing a recurrence relation between a larger problem instance $f(n, k)$ and smaller ones, for eg. $f(n, k-1), f(n-1, k-1)$ etc., but I haven't been able to find one.



      Base cases are easy to see: $f(n, 0) = n + 1$ $forall n$, $f(0, k) = 0$ $forall k$; and $f (n , k) = 2^n$ for $k geq n - 1$



      Solution for a non-trivial instance - $f(6, 2) = 52$



      Edit: I realized that Mike Earnest has found a solution in the linked question, based on combinatorics, so I am only interested in a recurrence relation. Also, from an algorithmic point of view, the time complexity of computing $n choose k$ is much higher than a polynomial solution that can be found with a recurrence relation, by building bottom-up from the base cases.










      share|cite|improve this question















      This question is similar to the one asked here by another user, but I want to ask it again since it was worded poorly there and not enough context was provided.



      Actually, this question was asked as a coding problem in a placement test (which is over now of course), where the input is given as two numbers $n$ and $k$, and we have to output the number of binary strings of length $n$, where every $1$ in the binary string, if any exists, is followed by at most $k$ zeroes.



      For eg. for $n = 5, k = 0$, output must be $6$, which is the set $(00000, 00001, 00011, 00111, 01111, 11111)$.



      I believe this can be done using dynamic programming, by developing a recurrence relation between a larger problem instance $f(n, k)$ and smaller ones, for eg. $f(n, k-1), f(n-1, k-1)$ etc., but I haven't been able to find one.



      Base cases are easy to see: $f(n, 0) = n + 1$ $forall n$, $f(0, k) = 0$ $forall k$; and $f (n , k) = 2^n$ for $k geq n - 1$



      Solution for a non-trivial instance - $f(6, 2) = 52$



      Edit: I realized that Mike Earnest has found a solution in the linked question, based on combinatorics, so I am only interested in a recurrence relation. Also, from an algorithmic point of view, the time complexity of computing $n choose k$ is much higher than a polynomial solution that can be found with a recurrence relation, by building bottom-up from the base cases.







      combinatorics recurrence-relations contest-math binary dynamic-programming






      share|cite|improve this question















      share|cite|improve this question













      share|cite|improve this question




      share|cite|improve this question








      edited Sep 2 at 13:22

























      asked Sep 2 at 12:14









      ab123

      1,417421




      1,417421




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Let $a_n$ be the number of strings of length $n$ where every $1$ is followed by at most $k$ zeroes. There are two cases; either the string has a one, or it doesn't. In the former case, the maximal run of zeroes at the end must be of length at most $k$. Therefore, for all $n>k+1$,
          $$
          a_n = 1+a_n-1+a_n-2+dots+a_n-k-1
          $$
          As you said, the base cases are $a_n=2^n$ for $nle k+1$. (Note the base case $a_0=1$, because there is one valid string of length $0$, namely the empty string).



          For example, when $k=0$, the recurrence is $a_n=a_n-1+1$, which implies $a_n=n+1$. Also,
          $$
          f(4,2) = 1+f(3,2) + f(2,2) + f(1,2) =1+8 + 4 + 2 = 15\
          f(5,2) = 1+f(4,2) + f(3,2) + f(2,2) =1+15+8+4= 28\
          f(6,2) = 1+f(5,2) + f(4,2) + f(3,2) =1+28+15+8= 52
          $$



          You can even simplify this recurrence a bit more. The recurrence when $n$ is replaced with $n-1$ is
          $$
          a_n-1 = 1+a_n-2+a_n-3+dots+a_n-k-2tag$n>k+2$
          $$
          Subtracting these equations
          $$
          boxeda_n=2a_n-1-a_n-k-2.
          $$



          You can compute $a_n$ in $O(k^3log n)$ time if you are careful. The last recurrence can be written as a matrix equation, which I illustrate when $k=2$:
          $$
          beginbmatrixa_n \ a_n-1 \ a_n-2 \a_n-3endbmatrix=
          beginbmatrix 2 & 0 & 0 & -1 \ 1 & 0 & 0 & 0 \0 & 1 & 0 & 0 \0 & 0 & 1 & 0 endbmatrix
          beginbmatrix a_n-1 \ a_n-2 \a_n-3\a_n-4endbmatrix
          $$
          Letting $A$ be the $(k+2)times (k+2)$ matrix, iterating the recurrence implies
          $$
          beginbmatrixa_n \ a_n-1 \ vdots \a_n-k-1endbmatrix=A^n-k-2beginbmatrixa_k+2 \ a_k+1 \ vdots \a_1endbmatrix
          $$
          Therefore, you can compute the vector on the left by computing a power of the matrix $A$. Using exponentiation by squaring, this takes only $O(log n)$ matrix multiplication, each of which naively takes $O(k^3)$ arithmetic operations.






          share|cite|improve this answer






















          • Thanks for the detailed answer, but I don't understand this part - "In the latter case, the maximal run of zeroes at the end must of length at most $n$. Therefore, for all $n>k+1, a_n = 1+a_n-1+a_n-2+dots+a_n-k-1$". Isn't the latter case the strings with no '1', so just the zero string $000dots0$
            – ab123
            Sep 5 at 10:13











          • So does $a_n = 1+a_n-1+a_n-2+dots+a_n-k-1$ mean that we are adding $1$ for string with all zeroes, then we append zeroes from left to each of the smaller string $a_n -1, a_n - 2, dots , a_n - k -1 $ to make them of length $n$, for eg. add 2 zeroes in beginnning of any string belonging to $a_n - 2$, etc. , so that every '1' is still followed by no more than $k$ zeroes and this covers all possible strings.
            – ab123
            Sep 5 at 13:21











          • @ab123 Pretty much. Except you are not just appending zeroes, you are appending a single "1" followed by "0"s. The case $a_n-2$ counts all valid strings of length $n-2$ with a "10" added to the end.
            – Mike Earnest
            Sep 5 at 13:25










          • But if I let $k = 3$ and $n = 6$, $1000$ belongs to $a_4 (= a_n - 2)$, if I add $10$ at the end of it, then I will get $100010$, but it does not belong to $a_n$ because the first "1" now has $4 (> k)$ zeroes after it
            – ab123
            Sep 5 at 13:38











          • @ab123 I was interpreting "followed by $k$ zeroes" to mean "followed by $k$ zeroes in a row". I thought $100010$ is legal because the first "1" is only followed by three zeroes in a row.
            – Mike Earnest
            Sep 5 at 13:41

















          up vote
          0
          down vote













          Let $f(n)$ denote the number of valid strings of length which end with $1$.
          Base case $f(0)$ shall be equal to $1$.



          $$f(n)= sum_i=0^k+1 f(n-i)$$



          Now the final answer shall be the sum of all $f(i)$'s, i.e ans$ = sum_i=0^n f(i)$.
          The given algorithm would take quadratic time. To enhance it further we can use a prefix array to store sums of previously calculated subproblems so that the RHS can be computed in constant time.






          share|cite|improve this answer






















          • How exactly do you define $f(0)$, and how is $f(0) = 1$? Your solution isn't clear. Does it provide correct answers for $(5, 0)$ and $(6, 2)$?
            – ab123
            Sep 2 at 13:16










          • f(0) is basically covers the case of all 0's,since the other f(i)'s in my definition won't cover that particular case. And yes,my optimal substructure gives the correct answer for the two cases you mentioned.
            – Akash Verma
            Sep 3 at 10:45











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "69"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          noCode: true, onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2902659%2fnumber-of-binary-strings-of-length-n-where-every-1-if-any-is-followed-by%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          Let $a_n$ be the number of strings of length $n$ where every $1$ is followed by at most $k$ zeroes. There are two cases; either the string has a one, or it doesn't. In the former case, the maximal run of zeroes at the end must be of length at most $k$. Therefore, for all $n>k+1$,
          $$
          a_n = 1+a_n-1+a_n-2+dots+a_n-k-1
          $$
          As you said, the base cases are $a_n=2^n$ for $nle k+1$. (Note the base case $a_0=1$, because there is one valid string of length $0$, namely the empty string).



          For example, when $k=0$, the recurrence is $a_n=a_n-1+1$, which implies $a_n=n+1$. Also,
          $$
          f(4,2) = 1+f(3,2) + f(2,2) + f(1,2) =1+8 + 4 + 2 = 15\
          f(5,2) = 1+f(4,2) + f(3,2) + f(2,2) =1+15+8+4= 28\
          f(6,2) = 1+f(5,2) + f(4,2) + f(3,2) =1+28+15+8= 52
          $$



          You can even simplify this recurrence a bit more. The recurrence when $n$ is replaced with $n-1$ is
          $$
          a_n-1 = 1+a_n-2+a_n-3+dots+a_n-k-2tag$n>k+2$
          $$
          Subtracting these equations
          $$
          boxeda_n=2a_n-1-a_n-k-2.
          $$



          You can compute $a_n$ in $O(k^3log n)$ time if you are careful. The last recurrence can be written as a matrix equation, which I illustrate when $k=2$:
          $$
          beginbmatrixa_n \ a_n-1 \ a_n-2 \a_n-3endbmatrix=
          beginbmatrix 2 & 0 & 0 & -1 \ 1 & 0 & 0 & 0 \0 & 1 & 0 & 0 \0 & 0 & 1 & 0 endbmatrix
          beginbmatrix a_n-1 \ a_n-2 \a_n-3\a_n-4endbmatrix
          $$
          Letting $A$ be the $(k+2)times (k+2)$ matrix, iterating the recurrence implies
          $$
          beginbmatrixa_n \ a_n-1 \ vdots \a_n-k-1endbmatrix=A^n-k-2beginbmatrixa_k+2 \ a_k+1 \ vdots \a_1endbmatrix
          $$
          Therefore, you can compute the vector on the left by computing a power of the matrix $A$. Using exponentiation by squaring, this takes only $O(log n)$ matrix multiplication, each of which naively takes $O(k^3)$ arithmetic operations.






          share|cite|improve this answer






















          • Thanks for the detailed answer, but I don't understand this part - "In the latter case, the maximal run of zeroes at the end must of length at most $n$. Therefore, for all $n>k+1, a_n = 1+a_n-1+a_n-2+dots+a_n-k-1$". Isn't the latter case the strings with no '1', so just the zero string $000dots0$
            – ab123
            Sep 5 at 10:13











          • So does $a_n = 1+a_n-1+a_n-2+dots+a_n-k-1$ mean that we are adding $1$ for string with all zeroes, then we append zeroes from left to each of the smaller string $a_n -1, a_n - 2, dots , a_n - k -1 $ to make them of length $n$, for eg. add 2 zeroes in beginnning of any string belonging to $a_n - 2$, etc. , so that every '1' is still followed by no more than $k$ zeroes and this covers all possible strings.
            – ab123
            Sep 5 at 13:21











          • @ab123 Pretty much. Except you are not just appending zeroes, you are appending a single "1" followed by "0"s. The case $a_n-2$ counts all valid strings of length $n-2$ with a "10" added to the end.
            – Mike Earnest
            Sep 5 at 13:25










          • But if I let $k = 3$ and $n = 6$, $1000$ belongs to $a_4 (= a_n - 2)$, if I add $10$ at the end of it, then I will get $100010$, but it does not belong to $a_n$ because the first "1" now has $4 (> k)$ zeroes after it
            – ab123
            Sep 5 at 13:38











          • @ab123 I was interpreting "followed by $k$ zeroes" to mean "followed by $k$ zeroes in a row". I thought $100010$ is legal because the first "1" is only followed by three zeroes in a row.
            – Mike Earnest
            Sep 5 at 13:41














          up vote
          1
          down vote



          accepted










          Let $a_n$ be the number of strings of length $n$ where every $1$ is followed by at most $k$ zeroes. There are two cases; either the string has a one, or it doesn't. In the former case, the maximal run of zeroes at the end must be of length at most $k$. Therefore, for all $n>k+1$,
          $$
          a_n = 1+a_n-1+a_n-2+dots+a_n-k-1
          $$
          As you said, the base cases are $a_n=2^n$ for $nle k+1$. (Note the base case $a_0=1$, because there is one valid string of length $0$, namely the empty string).



          For example, when $k=0$, the recurrence is $a_n=a_n-1+1$, which implies $a_n=n+1$. Also,
          $$
          f(4,2) = 1+f(3,2) + f(2,2) + f(1,2) =1+8 + 4 + 2 = 15\
          f(5,2) = 1+f(4,2) + f(3,2) + f(2,2) =1+15+8+4= 28\
          f(6,2) = 1+f(5,2) + f(4,2) + f(3,2) =1+28+15+8= 52
          $$



          You can even simplify this recurrence a bit more. The recurrence when $n$ is replaced with $n-1$ is
          $$
          a_n-1 = 1+a_n-2+a_n-3+dots+a_n-k-2tag$n>k+2$
          $$
          Subtracting these equations
          $$
          boxeda_n=2a_n-1-a_n-k-2.
          $$



          You can compute $a_n$ in $O(k^3log n)$ time if you are careful. The last recurrence can be written as a matrix equation, which I illustrate when $k=2$:
          $$
          beginbmatrixa_n \ a_n-1 \ a_n-2 \a_n-3endbmatrix=
          beginbmatrix 2 & 0 & 0 & -1 \ 1 & 0 & 0 & 0 \0 & 1 & 0 & 0 \0 & 0 & 1 & 0 endbmatrix
          beginbmatrix a_n-1 \ a_n-2 \a_n-3\a_n-4endbmatrix
          $$
          Letting $A$ be the $(k+2)times (k+2)$ matrix, iterating the recurrence implies
          $$
          beginbmatrixa_n \ a_n-1 \ vdots \a_n-k-1endbmatrix=A^n-k-2beginbmatrixa_k+2 \ a_k+1 \ vdots \a_1endbmatrix
          $$
          Therefore, you can compute the vector on the left by computing a power of the matrix $A$. Using exponentiation by squaring, this takes only $O(log n)$ matrix multiplication, each of which naively takes $O(k^3)$ arithmetic operations.






          share|cite|improve this answer






















          • Thanks for the detailed answer, but I don't understand this part - "In the latter case, the maximal run of zeroes at the end must of length at most $n$. Therefore, for all $n>k+1, a_n = 1+a_n-1+a_n-2+dots+a_n-k-1$". Isn't the latter case the strings with no '1', so just the zero string $000dots0$
            – ab123
            Sep 5 at 10:13











          • So does $a_n = 1+a_n-1+a_n-2+dots+a_n-k-1$ mean that we are adding $1$ for string with all zeroes, then we append zeroes from left to each of the smaller string $a_n -1, a_n - 2, dots , a_n - k -1 $ to make them of length $n$, for eg. add 2 zeroes in beginnning of any string belonging to $a_n - 2$, etc. , so that every '1' is still followed by no more than $k$ zeroes and this covers all possible strings.
            – ab123
            Sep 5 at 13:21











          • @ab123 Pretty much. Except you are not just appending zeroes, you are appending a single "1" followed by "0"s. The case $a_n-2$ counts all valid strings of length $n-2$ with a "10" added to the end.
            – Mike Earnest
            Sep 5 at 13:25










          • But if I let $k = 3$ and $n = 6$, $1000$ belongs to $a_4 (= a_n - 2)$, if I add $10$ at the end of it, then I will get $100010$, but it does not belong to $a_n$ because the first "1" now has $4 (> k)$ zeroes after it
            – ab123
            Sep 5 at 13:38











          • @ab123 I was interpreting "followed by $k$ zeroes" to mean "followed by $k$ zeroes in a row". I thought $100010$ is legal because the first "1" is only followed by three zeroes in a row.
            – Mike Earnest
            Sep 5 at 13:41












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Let $a_n$ be the number of strings of length $n$ where every $1$ is followed by at most $k$ zeroes. There are two cases; either the string has a one, or it doesn't. In the former case, the maximal run of zeroes at the end must be of length at most $k$. Therefore, for all $n>k+1$,
          $$
          a_n = 1+a_n-1+a_n-2+dots+a_n-k-1
          $$
          As you said, the base cases are $a_n=2^n$ for $nle k+1$. (Note the base case $a_0=1$, because there is one valid string of length $0$, namely the empty string).



          For example, when $k=0$, the recurrence is $a_n=a_n-1+1$, which implies $a_n=n+1$. Also,
          $$
          f(4,2) = 1+f(3,2) + f(2,2) + f(1,2) =1+8 + 4 + 2 = 15\
          f(5,2) = 1+f(4,2) + f(3,2) + f(2,2) =1+15+8+4= 28\
          f(6,2) = 1+f(5,2) + f(4,2) + f(3,2) =1+28+15+8= 52
          $$



          You can even simplify this recurrence a bit more. The recurrence when $n$ is replaced with $n-1$ is
          $$
          a_n-1 = 1+a_n-2+a_n-3+dots+a_n-k-2tag$n>k+2$
          $$
          Subtracting these equations
          $$
          boxeda_n=2a_n-1-a_n-k-2.
          $$



          You can compute $a_n$ in $O(k^3log n)$ time if you are careful. The last recurrence can be written as a matrix equation, which I illustrate when $k=2$:
          $$
          beginbmatrixa_n \ a_n-1 \ a_n-2 \a_n-3endbmatrix=
          beginbmatrix 2 & 0 & 0 & -1 \ 1 & 0 & 0 & 0 \0 & 1 & 0 & 0 \0 & 0 & 1 & 0 endbmatrix
          beginbmatrix a_n-1 \ a_n-2 \a_n-3\a_n-4endbmatrix
          $$
          Letting $A$ be the $(k+2)times (k+2)$ matrix, iterating the recurrence implies
          $$
          beginbmatrixa_n \ a_n-1 \ vdots \a_n-k-1endbmatrix=A^n-k-2beginbmatrixa_k+2 \ a_k+1 \ vdots \a_1endbmatrix
          $$
          Therefore, you can compute the vector on the left by computing a power of the matrix $A$. Using exponentiation by squaring, this takes only $O(log n)$ matrix multiplication, each of which naively takes $O(k^3)$ arithmetic operations.






          share|cite|improve this answer














          Let $a_n$ be the number of strings of length $n$ where every $1$ is followed by at most $k$ zeroes. There are two cases; either the string has a one, or it doesn't. In the former case, the maximal run of zeroes at the end must be of length at most $k$. Therefore, for all $n>k+1$,
          $$
          a_n = 1+a_n-1+a_n-2+dots+a_n-k-1
          $$
          As you said, the base cases are $a_n=2^n$ for $nle k+1$. (Note the base case $a_0=1$, because there is one valid string of length $0$, namely the empty string).



          For example, when $k=0$, the recurrence is $a_n=a_n-1+1$, which implies $a_n=n+1$. Also,
          $$
          f(4,2) = 1+f(3,2) + f(2,2) + f(1,2) =1+8 + 4 + 2 = 15\
          f(5,2) = 1+f(4,2) + f(3,2) + f(2,2) =1+15+8+4= 28\
          f(6,2) = 1+f(5,2) + f(4,2) + f(3,2) =1+28+15+8= 52
          $$



          You can even simplify this recurrence a bit more. The recurrence when $n$ is replaced with $n-1$ is
          $$
          a_n-1 = 1+a_n-2+a_n-3+dots+a_n-k-2tag$n>k+2$
          $$
          Subtracting these equations
          $$
          boxeda_n=2a_n-1-a_n-k-2.
          $$



          You can compute $a_n$ in $O(k^3log n)$ time if you are careful. The last recurrence can be written as a matrix equation, which I illustrate when $k=2$:
          $$
          beginbmatrixa_n \ a_n-1 \ a_n-2 \a_n-3endbmatrix=
          beginbmatrix 2 & 0 & 0 & -1 \ 1 & 0 & 0 & 0 \0 & 1 & 0 & 0 \0 & 0 & 1 & 0 endbmatrix
          beginbmatrix a_n-1 \ a_n-2 \a_n-3\a_n-4endbmatrix
          $$
          Letting $A$ be the $(k+2)times (k+2)$ matrix, iterating the recurrence implies
          $$
          beginbmatrixa_n \ a_n-1 \ vdots \a_n-k-1endbmatrix=A^n-k-2beginbmatrixa_k+2 \ a_k+1 \ vdots \a_1endbmatrix
          $$
          Therefore, you can compute the vector on the left by computing a power of the matrix $A$. Using exponentiation by squaring, this takes only $O(log n)$ matrix multiplication, each of which naively takes $O(k^3)$ arithmetic operations.







          share|cite|improve this answer














          share|cite|improve this answer



          share|cite|improve this answer








          edited Sep 5 at 13:09

























          answered Sep 4 at 14:17









          Mike Earnest

          18.2k11850




          18.2k11850











          • Thanks for the detailed answer, but I don't understand this part - "In the latter case, the maximal run of zeroes at the end must of length at most $n$. Therefore, for all $n>k+1, a_n = 1+a_n-1+a_n-2+dots+a_n-k-1$". Isn't the latter case the strings with no '1', so just the zero string $000dots0$
            – ab123
            Sep 5 at 10:13











          • So does $a_n = 1+a_n-1+a_n-2+dots+a_n-k-1$ mean that we are adding $1$ for string with all zeroes, then we append zeroes from left to each of the smaller string $a_n -1, a_n - 2, dots , a_n - k -1 $ to make them of length $n$, for eg. add 2 zeroes in beginnning of any string belonging to $a_n - 2$, etc. , so that every '1' is still followed by no more than $k$ zeroes and this covers all possible strings.
            – ab123
            Sep 5 at 13:21











          • @ab123 Pretty much. Except you are not just appending zeroes, you are appending a single "1" followed by "0"s. The case $a_n-2$ counts all valid strings of length $n-2$ with a "10" added to the end.
            – Mike Earnest
            Sep 5 at 13:25










          • But if I let $k = 3$ and $n = 6$, $1000$ belongs to $a_4 (= a_n - 2)$, if I add $10$ at the end of it, then I will get $100010$, but it does not belong to $a_n$ because the first "1" now has $4 (> k)$ zeroes after it
            – ab123
            Sep 5 at 13:38











          • @ab123 I was interpreting "followed by $k$ zeroes" to mean "followed by $k$ zeroes in a row". I thought $100010$ is legal because the first "1" is only followed by three zeroes in a row.
            – Mike Earnest
            Sep 5 at 13:41
















          • Thanks for the detailed answer, but I don't understand this part - "In the latter case, the maximal run of zeroes at the end must of length at most $n$. Therefore, for all $n>k+1, a_n = 1+a_n-1+a_n-2+dots+a_n-k-1$". Isn't the latter case the strings with no '1', so just the zero string $000dots0$
            – ab123
            Sep 5 at 10:13











          • So does $a_n = 1+a_n-1+a_n-2+dots+a_n-k-1$ mean that we are adding $1$ for string with all zeroes, then we append zeroes from left to each of the smaller string $a_n -1, a_n - 2, dots , a_n - k -1 $ to make them of length $n$, for eg. add 2 zeroes in beginnning of any string belonging to $a_n - 2$, etc. , so that every '1' is still followed by no more than $k$ zeroes and this covers all possible strings.
            – ab123
            Sep 5 at 13:21











          • @ab123 Pretty much. Except you are not just appending zeroes, you are appending a single "1" followed by "0"s. The case $a_n-2$ counts all valid strings of length $n-2$ with a "10" added to the end.
            – Mike Earnest
            Sep 5 at 13:25










          • But if I let $k = 3$ and $n = 6$, $1000$ belongs to $a_4 (= a_n - 2)$, if I add $10$ at the end of it, then I will get $100010$, but it does not belong to $a_n$ because the first "1" now has $4 (> k)$ zeroes after it
            – ab123
            Sep 5 at 13:38











          • @ab123 I was interpreting "followed by $k$ zeroes" to mean "followed by $k$ zeroes in a row". I thought $100010$ is legal because the first "1" is only followed by three zeroes in a row.
            – Mike Earnest
            Sep 5 at 13:41















          Thanks for the detailed answer, but I don't understand this part - "In the latter case, the maximal run of zeroes at the end must of length at most $n$. Therefore, for all $n>k+1, a_n = 1+a_n-1+a_n-2+dots+a_n-k-1$". Isn't the latter case the strings with no '1', so just the zero string $000dots0$
          – ab123
          Sep 5 at 10:13





          Thanks for the detailed answer, but I don't understand this part - "In the latter case, the maximal run of zeroes at the end must of length at most $n$. Therefore, for all $n>k+1, a_n = 1+a_n-1+a_n-2+dots+a_n-k-1$". Isn't the latter case the strings with no '1', so just the zero string $000dots0$
          – ab123
          Sep 5 at 10:13













          So does $a_n = 1+a_n-1+a_n-2+dots+a_n-k-1$ mean that we are adding $1$ for string with all zeroes, then we append zeroes from left to each of the smaller string $a_n -1, a_n - 2, dots , a_n - k -1 $ to make them of length $n$, for eg. add 2 zeroes in beginnning of any string belonging to $a_n - 2$, etc. , so that every '1' is still followed by no more than $k$ zeroes and this covers all possible strings.
          – ab123
          Sep 5 at 13:21





          So does $a_n = 1+a_n-1+a_n-2+dots+a_n-k-1$ mean that we are adding $1$ for string with all zeroes, then we append zeroes from left to each of the smaller string $a_n -1, a_n - 2, dots , a_n - k -1 $ to make them of length $n$, for eg. add 2 zeroes in beginnning of any string belonging to $a_n - 2$, etc. , so that every '1' is still followed by no more than $k$ zeroes and this covers all possible strings.
          – ab123
          Sep 5 at 13:21













          @ab123 Pretty much. Except you are not just appending zeroes, you are appending a single "1" followed by "0"s. The case $a_n-2$ counts all valid strings of length $n-2$ with a "10" added to the end.
          – Mike Earnest
          Sep 5 at 13:25




          @ab123 Pretty much. Except you are not just appending zeroes, you are appending a single "1" followed by "0"s. The case $a_n-2$ counts all valid strings of length $n-2$ with a "10" added to the end.
          – Mike Earnest
          Sep 5 at 13:25












          But if I let $k = 3$ and $n = 6$, $1000$ belongs to $a_4 (= a_n - 2)$, if I add $10$ at the end of it, then I will get $100010$, but it does not belong to $a_n$ because the first "1" now has $4 (> k)$ zeroes after it
          – ab123
          Sep 5 at 13:38





          But if I let $k = 3$ and $n = 6$, $1000$ belongs to $a_4 (= a_n - 2)$, if I add $10$ at the end of it, then I will get $100010$, but it does not belong to $a_n$ because the first "1" now has $4 (> k)$ zeroes after it
          – ab123
          Sep 5 at 13:38













          @ab123 I was interpreting "followed by $k$ zeroes" to mean "followed by $k$ zeroes in a row". I thought $100010$ is legal because the first "1" is only followed by three zeroes in a row.
          – Mike Earnest
          Sep 5 at 13:41




          @ab123 I was interpreting "followed by $k$ zeroes" to mean "followed by $k$ zeroes in a row". I thought $100010$ is legal because the first "1" is only followed by three zeroes in a row.
          – Mike Earnest
          Sep 5 at 13:41










          up vote
          0
          down vote













          Let $f(n)$ denote the number of valid strings of length which end with $1$.
          Base case $f(0)$ shall be equal to $1$.



          $$f(n)= sum_i=0^k+1 f(n-i)$$



          Now the final answer shall be the sum of all $f(i)$'s, i.e ans$ = sum_i=0^n f(i)$.
          The given algorithm would take quadratic time. To enhance it further we can use a prefix array to store sums of previously calculated subproblems so that the RHS can be computed in constant time.






          share|cite|improve this answer






















          • How exactly do you define $f(0)$, and how is $f(0) = 1$? Your solution isn't clear. Does it provide correct answers for $(5, 0)$ and $(6, 2)$?
            – ab123
            Sep 2 at 13:16










          • f(0) is basically covers the case of all 0's,since the other f(i)'s in my definition won't cover that particular case. And yes,my optimal substructure gives the correct answer for the two cases you mentioned.
            – Akash Verma
            Sep 3 at 10:45















          up vote
          0
          down vote













          Let $f(n)$ denote the number of valid strings of length which end with $1$.
          Base case $f(0)$ shall be equal to $1$.



          $$f(n)= sum_i=0^k+1 f(n-i)$$



          Now the final answer shall be the sum of all $f(i)$'s, i.e ans$ = sum_i=0^n f(i)$.
          The given algorithm would take quadratic time. To enhance it further we can use a prefix array to store sums of previously calculated subproblems so that the RHS can be computed in constant time.






          share|cite|improve this answer






















          • How exactly do you define $f(0)$, and how is $f(0) = 1$? Your solution isn't clear. Does it provide correct answers for $(5, 0)$ and $(6, 2)$?
            – ab123
            Sep 2 at 13:16










          • f(0) is basically covers the case of all 0's,since the other f(i)'s in my definition won't cover that particular case. And yes,my optimal substructure gives the correct answer for the two cases you mentioned.
            – Akash Verma
            Sep 3 at 10:45













          up vote
          0
          down vote










          up vote
          0
          down vote









          Let $f(n)$ denote the number of valid strings of length which end with $1$.
          Base case $f(0)$ shall be equal to $1$.



          $$f(n)= sum_i=0^k+1 f(n-i)$$



          Now the final answer shall be the sum of all $f(i)$'s, i.e ans$ = sum_i=0^n f(i)$.
          The given algorithm would take quadratic time. To enhance it further we can use a prefix array to store sums of previously calculated subproblems so that the RHS can be computed in constant time.






          share|cite|improve this answer














          Let $f(n)$ denote the number of valid strings of length which end with $1$.
          Base case $f(0)$ shall be equal to $1$.



          $$f(n)= sum_i=0^k+1 f(n-i)$$



          Now the final answer shall be the sum of all $f(i)$'s, i.e ans$ = sum_i=0^n f(i)$.
          The given algorithm would take quadratic time. To enhance it further we can use a prefix array to store sums of previously calculated subproblems so that the RHS can be computed in constant time.







          share|cite|improve this answer














          share|cite|improve this answer



          share|cite|improve this answer








          edited Sep 3 at 11:01









          Arnaud D.

          14.9k52142




          14.9k52142










          answered Sep 2 at 13:01









          Akash Verma

          111




          111











          • How exactly do you define $f(0)$, and how is $f(0) = 1$? Your solution isn't clear. Does it provide correct answers for $(5, 0)$ and $(6, 2)$?
            – ab123
            Sep 2 at 13:16










          • f(0) is basically covers the case of all 0's,since the other f(i)'s in my definition won't cover that particular case. And yes,my optimal substructure gives the correct answer for the two cases you mentioned.
            – Akash Verma
            Sep 3 at 10:45

















          • How exactly do you define $f(0)$, and how is $f(0) = 1$? Your solution isn't clear. Does it provide correct answers for $(5, 0)$ and $(6, 2)$?
            – ab123
            Sep 2 at 13:16










          • f(0) is basically covers the case of all 0's,since the other f(i)'s in my definition won't cover that particular case. And yes,my optimal substructure gives the correct answer for the two cases you mentioned.
            – Akash Verma
            Sep 3 at 10:45
















          How exactly do you define $f(0)$, and how is $f(0) = 1$? Your solution isn't clear. Does it provide correct answers for $(5, 0)$ and $(6, 2)$?
          – ab123
          Sep 2 at 13:16




          How exactly do you define $f(0)$, and how is $f(0) = 1$? Your solution isn't clear. Does it provide correct answers for $(5, 0)$ and $(6, 2)$?
          – ab123
          Sep 2 at 13:16












          f(0) is basically covers the case of all 0's,since the other f(i)'s in my definition won't cover that particular case. And yes,my optimal substructure gives the correct answer for the two cases you mentioned.
          – Akash Verma
          Sep 3 at 10:45





          f(0) is basically covers the case of all 0's,since the other f(i)'s in my definition won't cover that particular case. And yes,my optimal substructure gives the correct answer for the two cases you mentioned.
          – Akash Verma
          Sep 3 at 10:45


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2902659%2fnumber-of-binary-strings-of-length-n-where-every-1-if-any-is-followed-by%23new-answer', 'question_page');

          );

          Post as a guest













































































          這個網誌中的熱門文章

          Is there any way to eliminate the singular point to solve this integral by hand or by approximations?

          Why am i infinitely getting the same tweet with the Twitter Search API?

          Amount of Number Combinations to Reach a Sum of 10 With Integers 1-9 Using 2 or More Integers [closed]