Given two numbers N and K. Find the number of ways to represent N as the sum of K Fibonacci numbers.
Input : n = 12, k = 1 Output : 0 Input : n = 13, k = 3 Output : 2 Explanation : 2 + 3 + 8, 3 + 5 + 5.
Approach: The Fibonacci series is f(0)=1, f(1)=2 and f(i)=f(i-1)+f(i-2) for i>1. Let’s suppose F(x, k, n) be the number of ways to form the sum x using exactly k numbers from f(0), f(1), …f(n-1). To find a recurrence for F(x, k, n), notice that there are two cases: whether f(n-1) in the sum or not.
- If f(n-1) is not in the sum, then x is formed as a sum using exactly k numbers from f(0), f(1), …, f(n-2).
- If f(n-1) is in the sum, then the remaining x-f(n-1) is formed using exactly k-1 numbers from f(0), f(1), …, f(n-1). (Notice that f(n-1) is still included because duplicate numbers are allowed.).
So the recurrence relation will be:
F(x, k, n)= F(x, k, n-1)+F(x-f(n-1), k-1, n)
- If k=0, then there are zero numbers from the series, so the sum can only be 0. Hence, F(0, 0, n)=1.
- F(x, 0, n)=0, if x is not equals to 0.
Also, there are other cases that make F(x, k, n)=0, like the following:
- If k>0 and x=0 because having at least one positive number must result in a positive sum.
- If k>0 and n=0 because there’s no possible choice of numbers left.
- If x<0 because there's no way to form a negative sum using a finite number of nonnegative numbers.
Below is the implementation of above approach:
Possible ways are: 2
- Ways to represent a number as a sum of 1's and 2's
- Check if a M-th fibonacci number divides N-th fibonacci number
- Bell Numbers (Number of ways to Partition a Set)
- Count number of ways to arrange first N numbers
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Finding number of digits in n'th Fibonacci number
- Different ways to represent N as sum of K non-zero integers
- How to check if a given number is Fibonacci number?
- Number of ways to get a given sum with n number of m-faced dices
- Nth XOR Fibonacci number
- Nth Even Fibonacci Number
- Fibonacci number in an array
- Find the next fibonacci number
- Nth Fibonacci number using Pell's equation
- Program to find Nth odd Fibonacci Number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.