1

Given a recurrence relation as follows:

$$ T(n,k) = \begin{cases} 1, & \text{if $n \leq k$} \\ T(n-1,k) + T(n-2,k) +... + T(n-k,k) & \text{otherwise} \end{cases} $$

Find $T(n,k)$.

Link to question: https://www.codechef.com/problems/KFIB

As the answer can be big, I have to find it

My observation is: $T(n, k) = 2T(n-1, k)-1$.

However, this observation does not seem to work for some cases.

How to solve this recurrence relation?

Sil
  • 17,976
  • 1
    Given the bounds in the problem statement, you could just calculate each term individually, with an array to store all of the terms which you calculate so that you don't have to recalculate them. Using your observation that $T(n + 1, k) = 2T(n, k) - T(n - k, k)$ for $n > k$ should be enough to have the calculation complete within the time limit. – Dylan Jul 31 '18 at 18:15
  • I doubt you'll be able to find an explicit formula. When $k=2,$ you have a $3-$term recurrence, and you can get a formula by solving a quadratic. When $k=3,$ you have a $4-$term recurrence, and you'll have to solve a cubic and so on. – saulspatz Jul 31 '18 at 18:29
  • Went I got stuck, I was actually going through the best submissions to this problem. Although, I tried my best to understand their logic, but I couldn't get around their solutions. It seems they were trying able to convert the recurrence relation to a simpler one. I just want to understand it rather than simply copy pasting it. – user3243499 Jul 31 '18 at 19:08
  • @user3243499 Did they use the fact that the solution needed was a modulo? – Sudix Aug 04 '18 at 20:28

2 Answers2

1

The general theorem related to this class of problems is:

let $c_i,r\in\Bbb{R}\space|\space r^k+c_1\cdot r^{k-1}+c_2\cdot r^{k-2}...+c_{k-2}\cdot r^2+c_{k-1}\cdot r+c_k=0$ is a polynomial with $k$ distinct roots $r_1,r_2,r_3,...,r_{k-1},r_k$.

Then $a_n=c_1\cdot a_{n-1}+c_2\cdot a_{n-2}+c_3\cdot a_{n-3}+...+c_{k-2}\cdot a_{n-k+2}+c_{k-1}\cdot a_{n-k+1}+c_k\cdot a_{n-k}$ is a recurrence relation of the sequence $\{a_0,a_1,a_2,a_3,...\}$ if and only if $a_n=\alpha_1\cdot r_1^n+\alpha_2\cdot r_2^n+\alpha_3\cdot r_3^n+...+\alpha_k\cdot r_k^n$ is the solution to the recurrence relation where $\alpha_i\in\Bbb{R}$ and are constants.

For $k=2$:

For $n>2\space\space T(n,2)=T(n-1,2)+T(n-2,2)$

$T(1,2)=1,\space T(2,2)=1$

so the next number in the sequence is generated by summing the previous two terms with starting with terms $1,1$ so this is the Fibonacci sequence. $$a_n=a_{n-1}+a_{n-2},\space a_1=1,\space a_2=1$$ $$r^2=r+1 \rightarrow r^2-r-1=0$$ Using the quadratic formula we obtain the two solutions for $r$: $$r_1=\frac{1+\sqrt{5}}{2},\space r_2=\frac{1-\sqrt{5}}{2}$$ $$\alpha_1\cdot\left(\frac{1+\sqrt{5}}{2}\right)^1+\alpha_2\cdot\left(\frac{1-\sqrt{5}}{2}\right)^1=1\space\space\space (eq. 1)$$ $$\alpha_1\cdot\left(\frac{1+\sqrt{5}}{2}\right)^2+\alpha_2\cdot\left(\frac{1-\sqrt{5}}{2}\right)^2=1\rightarrow\alpha_1\cdot\frac{3+\sqrt{5}}{2}+\alpha_2\cdot\frac{3-\sqrt{5}}{2}=1(eq. 2)$$ subtract equation 1 from equation 2 to get: $$\alpha_1+\alpha_2=0 \rightarrow \alpha_2=-\alpha_1$$ substitute $\alpha_2$ into equation 1: $$\alpha_1\cdot\frac{1+\sqrt{5}}{2}-\alpha_1\cdot\frac{1-\sqrt{5}}{2}=1\rightarrow\alpha_1\cdot\sqrt{5}=1\rightarrow\alpha_1=\frac{1}{\sqrt{5}},\space\alpha_2=-\frac{1}{\sqrt{5}}$$ $$T(n,2)=\frac{1}{\sqrt{5}}\cdot\left(\frac{1+\sqrt{5}}{2}\right)^n+\frac{1}{\sqrt{5}}\cdot\left(\frac{1-\sqrt{5}}{2}\right)^n$$

For general $k$ the recurrence relation is $a_n=a_{n-1}+a_{n-2}+a_{n-3}+\cdots+a_{n-k}$ and $j\in\Bbb{N}\space |\space 1\leq j\leq k$ all $a_j=1$

you then have to find the roots of the corresponding polynomial: $r^k-r^{k-1}-r^{k-2}-r^{k-3}-\cdots-r^2-r-1=0$

then to find the alpha constants you have to solve the system of equations: $$ \begin{matrix} \alpha_1\cdot r_1 & + & \alpha_2\cdot r_2 & + & \alpha_3\cdot r_3 & + & \cdots & + & \alpha_k\cdot r_k & = & 1 \\ \alpha_1\cdot r_1^2 & + & \alpha_2\cdot r_2^2 & + & \alpha_3\cdot r_3^2 & + & \cdots & + & \alpha_k\cdot r_k^2 & = & 1 \\ \alpha_1\cdot r_1^3 & + & \alpha_2\cdot r_2^3 & + & \alpha_3\cdot r_3^3 & + & \cdots & + & \alpha_k\cdot r_k^3 & = & 1 \\ \vdots & & \vdots & & \vdots & & \ddots & & \vdots \\ \alpha_1\cdot r_1^k & + & \alpha_2\cdot r_2^k & + & \alpha_3\cdot r_3^k & + & \cdots & + & \alpha_k\cdot r_k^k & = & 1\\ \end{matrix} $$ Then you would have the solution for the equation:

$$T(n,k)=a_n=\alpha_1\cdot r_1^n+\alpha_2\cdot r_2^n+\alpha_3\cdot r_3^n+\cdots+\alpha_k\cdot r_k^n $$

However these equations become difficult to manage very quickly I started the process for $k=3$ below, and already the expressions became very unwieldy. The best you are probably going to be able to do when $k\gt3$ is to have close enough approximations for the alphas and r's so that you can find $a_n$ with an error bound of $\lt 0.5$ to make sure $a_n$ is correct to the nearest whole number.

For $n>3\space\space T(n,3)=T(n-1,2)+T(n-2,2)+T(n-3,3)$

$T(1,3)=1,\space T(2,3)=1\space T(3,3)$

$$a_n=a_{n-1}+a_{n-2}+a_{n-3},\space a_1=1,\space a_2=1,\space a_3=1$$ $$r^3=r^2+r+1 \rightarrow r^3-r^2-r-1=0$$ $$r_1=\frac{1 + \sqrt[3]{19 - 3\cdot\sqrt{33}} + \sqrt[3]{19 + 3\cdot\sqrt{33}}}{3}$$ $$ r_2=\frac{2 + \left(-1+i\cdot\sqrt{3}\right)\sqrt[3]{19 - 3\cdot\sqrt{33}} + \left(-1-i\cdot\sqrt{3}\right)\sqrt[3]{19 + 3\cdot\sqrt{33}}}{6}$$ $$ r_3=\frac{2 + \left(-1-i\cdot\sqrt{3}\right)\sqrt[3]{19 - 3\cdot\sqrt{33}} + \left(-1+i\cdot\sqrt{3}\right)\sqrt[3]{19 + 3\cdot\sqrt{33}}}{6}$$

quantus14
  • 2,614
1

Your observation is the key, although you have it wrong. It should be $$T_{n+1,k}=2T_{n,k}-T_{n-k,k}.$$ for $n>k$. The $n>k$ condition is important, as it is used for using the definition of $T_{n,k}$ and $T_{n+1,k}$. This allows you to reformulate the original recursion, which is indeed what the other answers did. Eventually we can rewrite the recurrence as

$$ T_{n,k} = \begin{cases} 1 & n < k+1 \\ k & n = k+1 \\ 2T_{n-1,k}-T_{n-1-k,k} & n > k+1 \end{cases} $$

(we have only shifted indices from $n+1$ to $n$ and dealt with special case $n=k+1$ on its own).

Then there are few further optimizations you can do, such as (not limited to):

  • using dynamic programming and basically storing the intermediate results.

  • starting from $1$ and calculating the values with increasing $n$ (as opposed to starting from desired $n$ and calling the function recursively). So you would avoid actual function recursion by storing last $k$ values, and updating the values continually. This automatically also satisfies the first bullet by storing last $k$ values.

  • calculating modulo along the way at each step, since that will keep the numbers small and avoids overflow

Of course you could also calculate a closed form solution of this new recurrence, but it won't be of much practical use, since the numbers will be too big and you will be operating in floating point arithmetic (unless you are using computer algebra system), so the number would overflow quickly (you wouldn't be able to decrease the number by modulo in intermediate steps, since you would directly calculate the final, large value).

Sil
  • 17,976