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
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Number of distinct ways to represent a number as sum of K unique primes
- Ways to represent a number as a sum of 1's and 2's
- Array range queries to count the number of Fibonacci numbers with updates
- Count number of ways to arrange first N numbers
- Bell Numbers (Number of ways to Partition a Set)
- Number of ways to split N as sum of K numbers from the given range
- Check if a M-th fibonacci number divides N-th fibonacci number
- Number of ways to arrange N numbers which are in a range from 1 to K under given constraints.
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
- 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
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.