Given a number **N**, the task is to find the number of ways in which the integer **N** can be represented as a sum of Fibonacci numbers without repetition of any Fibonacci number.

**Examples:**

Input:N = 13Output:3Explanation:

The possible ways to select N as 13 are: {13} {8, 5} {8, 3, 2}. Note that it is not possible to select {5 + 5 + 3} because 5 appears twice.

Input:N = 87Output: 5Explanation:

The possible ways to select N as 13 are: {55 + 21 + 8 + 3}, {55 + 21 + 8 + 2 + 1}, {55 + 21 + 5 + 3 + 2 + 1}, {55 + 13 + 8 + 5 + 3 + 2 + 1}, {34 + 21 + 13 + 8 + 5 + 3 + 2 + 1}.

**Naive Approach: **The naive idea is to write all the possible combinations that add up to given number **N**. Check if any combination has repeated integers then don’t increase the counter otherwise increase the count by 1 each time. Return the count at the end.

**Time Complexity: **O(N)**Auxiliary Space:** O(1)

**Efficient Approach:** The idea is to use Dynamic Programming to optimize the above approach. Below are the steps:

- Let us represent a number in the Fibonacci code.

Imagine Fibonacci coding by following way: the

**i-th bit**of number corresponds to the**i-th Fibonacci number**.

For Example: 16 = 13 + 3 will be written as 100100.

- Write Fibonacci Code for every positive number such that no two adjacent bit is
**1**. - This is true for all numbers because if there are two adjacent bits are 1-bits then we can transform it into a single
**1-bit**by property of Fibonacci number. Let’s call this representation as canonical representation. - Get the Canonical Representation. Generate several Fibonacci numbers (about
**90**) and after that try to subtract all of them in the decreasing order. - Let’s store positions of 1-bits of the canonical representation of a given number into an array v in the increasing order and decompose any
**1-bits**into two**1-bits**as follows:

Starting canonical representation: 1000000001

After decomposing leftmost 1-bit into two smaller 1-bits: 0110000001

After decomposing 2’nd leftmost 1-bit into two smaller 1-bits: 0101100001

After decomposing 3’rd leftmost 1-bit into two smaller 1-bits: 0101011001

After decomposing 4’th leftmost 1-bit into two smaller 1-bits: 0101010111

- After a number of such operations, we will get the next
**1-bit**(or the end of the number). This**1-bit**also can be decomposed, but it can be**shifted**by only one bit. - Initialize a dp array
**dp1[]**,**dp1[i]**is a number of ways to represent a number that consists of**i**leftmost**1-bits**of the number for the case where all the remaining 1-bits are**not**decomposed. Also, take**dp2[i]**which marks the number of ways to represent a number that consists of**i**leftmost**1-bits**of the number for the case where all the remaining**1-bits**are decomposed.

For Example:

N= 87Canonical form of N = 101010100

Other 4 possible representations of N are 101010011, 101001111, 100111111, 011111111

Below is the illustration of the same:

Hence, the answer is** dp1[cnt] + dp2[cnt]**, where **cnt** is the total number of **1-bits** in the canonical representation.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `long` `long` `fib[101], dp1[101]; ` `long` `long` `dp2[101], v[101]; ` ` ` `// Function to generate the ` `// fibonacci number ` `void` `fibonacci() ` `{ ` ` ` `// First two number of ` ` ` `// fibonacci sqequence ` ` ` `fib[1] = 1; ` ` ` `fib[2] = 2; ` ` ` ` ` `for` `(` `int` `i = 3; i <= 87; i++) { ` ` ` `fib[i] = fib[i - 1] + fib[i - 2]; ` ` ` `} ` `} ` ` ` `// Function to find maximum ways to ` `// represent num as the sum of ` `// fibonacci number ` `int` `find(` `int` `num) ` `{ ` ` ` `int` `cnt = 0; ` ` ` ` ` `// Generate the Canonical form ` ` ` `// of given number ` ` ` `for` `(` `int` `i = 87; i > 0; i--) { ` ` ` `if` `(num >= fib[i]) { ` ` ` `v[cnt++] = i; ` ` ` `num -= fib[i]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Reverse the number ` ` ` `reverse(v, v + cnt); ` ` ` ` ` `// Base condition of dp1 and dp2 ` ` ` `dp1[0] = 1; ` ` ` `dp2[0] = (v[0] - 1) / 2; ` ` ` ` ` `// Iterate from 1 to cnt ` ` ` `for` `(` `int` `i = 1; i < cnt; i++) { ` ` ` ` ` `// Calculate dp1[] ` ` ` `dp1[i] = dp1[i - 1] + dp2[i - 1]; ` ` ` ` ` `// Calculate dp2[] ` ` ` `dp2[i] = ((v[i] - v[i - 1]) / 2) ` ` ` `* dp2[i - 1] ` ` ` `+ ((v[i] - v[i - 1] - 1) / 2) ` ` ` `* dp1[i - 1]; ` ` ` `} ` ` ` ` ` `// Return final ans ` ` ` `return` `(dp1[cnt - 1] + dp2[cnt - 1]); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Function call to generate the ` ` ` `// fibonacci numbers ` ` ` `fibonacci(); ` ` ` ` ` `// Given Number ` ` ` `int` `num = 13; ` ` ` ` ` `// Function Call ` ` ` `cout << find(num); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `static` `long` `[] fib = ` `new` `long` `[` `101` `]; ` `static` `long` `[] dp1 = ` `new` `long` `[` `101` `]; ` `static` `long` `[] dp2 = ` `new` `long` `[` `101` `]; ` `static` `long` `[] v = ` `new` `long` `[` `101` `]; ` ` ` `// Function to generate the ` `// fibonacci number ` `static` `void` `fibonacci() ` `{ ` ` ` ` ` `// First two number of ` ` ` `// fibonacci sqequence ` ` ` `fib[` `1` `] = ` `1` `; ` ` ` `fib[` `2` `] = ` `2` `; ` ` ` ` ` `for` `(` `int` `i = ` `3` `; i <= ` `87` `; i++) ` ` ` `{ ` ` ` `fib[i] = fib[i - ` `1` `] + fib[i - ` `2` `]; ` ` ` `} ` `} ` ` ` `// Function to find maximum ways to ` `// represent num as the sum of ` `// fibonacci number ` `static` `long` `find(` `int` `num) ` `{ ` ` ` `int` `cnt = ` `0` `; ` ` ` ` ` `// Generate the Canonical form ` ` ` `// of given number ` ` ` `for` `(` `int` `i = ` `87` `; i > ` `0` `; i--) ` ` ` `{ ` ` ` `if` `(num >= fib[i]) ` ` ` `{ ` ` ` `v[cnt++] = i; ` ` ` `num -= fib[i]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Reverse the number ` ` ` `for` `(` `int` `i = ` `0` `; i < cnt / ` `2` `; i++) ` ` ` `{ ` ` ` `long` `t = v[i]; ` ` ` `v[i] = v[cnt - i - ` `1` `]; ` ` ` `v[cnt - i - ` `1` `] = t; ` ` ` `} ` ` ` ` ` `// Base condition of dp1 and dp2 ` ` ` `dp1[` `0` `] = ` `1` `; ` ` ` `dp2[` `0` `] = (v[` `0` `] - ` `1` `) / ` `2` `; ` ` ` ` ` `// Iterate from 1 to cnt ` ` ` `for` `(` `int` `i = ` `1` `; i < cnt; i++) ` ` ` `{ ` ` ` ` ` `// Calculate dp1[] ` ` ` `dp1[i] = dp1[i - ` `1` `] + dp2[i - ` `1` `]; ` ` ` ` ` `// Calculate dp2[] ` ` ` `dp2[i] = ((v[i] - v[i - ` `1` `]) / ` `2` `) * ` ` ` `dp2[i - ` `1` `] + ` ` ` `((v[i] - v[i - ` `1` `] - ` `1` `) / ` `2` `) * ` ` ` `dp1[i - ` `1` `]; ` ` ` `} ` ` ` ` ` `// Return final ans ` ` ` `return` `(dp1[cnt - ` `1` `] + dp2[cnt - ` `1` `]); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` ` ` `// Function call to generate the ` ` ` `// fibonacci numbers ` ` ` `fibonacci(); ` ` ` ` ` `// Given number ` ` ` `int` `num = ` `13` `; ` ` ` ` ` `// Function call ` ` ` `System.out.print(find(num)); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` `fib ` `=` `[` `0` `] ` `*` `101` `dp1 ` `=` `[` `0` `] ` `*` `101` `dp2 ` `=` `[` `0` `] ` `*` `101` `v ` `=` `[` `0` `] ` `*` `101` ` ` `# Function to generate the ` `# fibonacci number ` `def` `fibonacci(): ` ` ` ` ` `# First two number of ` ` ` `# fibonacci sqequence ` ` ` `fib[` `1` `] ` `=` `1` ` ` `fib[` `2` `] ` `=` `2` ` ` ` ` `for` `i ` `in` `range` `(` `3` `, ` `87` `+` `1` `): ` ` ` `fib[i] ` `=` `fib[i ` `-` `1` `] ` `+` `fib[i ` `-` `2` `] ` ` ` `# Function to find maximum ways to ` `# represent num as the sum of ` `# fibonacci number ` `def` `find(num): ` ` ` ` ` `cnt ` `=` `0` ` ` ` ` `# Generate the Canonical form ` ` ` `# of given number ` ` ` `for` `i ` `in` `range` `(` `87` `, ` `0` `, ` `-` `1` `): ` ` ` `if` `(num >` `=` `fib[i]): ` ` ` `v[cnt] ` `=` `i ` ` ` `cnt ` `+` `=` `1` ` ` `num ` `-` `=` `fib[i] ` ` ` ` ` `# Reverse the number ` ` ` `v[::` `-` `1` `] ` ` ` ` ` `# Base condition of dp1 and dp2 ` ` ` `dp1[` `0` `] ` `=` `1` ` ` `dp2[` `0` `] ` `=` `(v[` `0` `] ` `-` `1` `) ` `/` `/` `2` ` ` ` ` `# Iterate from 1 to cnt ` ` ` `for` `i ` `in` `range` `(` `1` `, cnt): ` ` ` ` ` `# Calculate dp1[] ` ` ` `dp1[i] ` `=` `dp1[i ` `-` `1` `] ` `+` `dp2[i ` `-` `1` `] ` ` ` ` ` `# Calculate dp2[] ` ` ` `dp2[i] ` `=` `(((v[i] ` `-` `v[i ` `-` `1` `]) ` `/` `/` `2` `) ` `*` ` ` `dp2[i ` `-` `1` `] ` `+` ` ` `((v[i] ` `-` `v[i ` `-` `1` `] ` `-` `1` `) ` `/` `/` `2` `) ` `*` ` ` `dp1[i ` `-` `1` `]) ` ` ` ` ` `# Return final ans ` ` ` `return` `dp1[cnt ` `-` `1` `] ` `+` `dp2[cnt ` `-` `1` `] ` ` ` `# Driver Code ` ` ` `# Function call to generate the ` `# fibonacci numbers ` `fibonacci() ` ` ` `# Given number ` `num ` `=` `13` ` ` `# Function call ` `print` `(find(num)) ` ` ` `# This code is contributed by Shivam Singh ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(log N)**Auxiliary Space:** O(log N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of ways in which N can be represented as the sum of two positive integers
- Find two Fibonacci numbers whose sum can be represented as N
- Count numbers which can be represented as sum of same parity primes
- K length words that can be formed from given characters without repetition
- Ways to sum to N using array elements with repetition allowed
- Smallest subarray which upon repetition gives the original array
- Check if given number can be represented as sum of two great numbers
- Check if a number can be represented as a sum of 2 triangular numbers
- Count array elements that can be represented as sum of at least two consecutive array elements
- Find the smallest positive number which can not be represented by given digits
- Number of ways to represent a number as sum of k fibonacci numbers
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Count numbers which can be constructed using two numbers
- Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
- Count elements in an Array that can be represented as difference of two perfect squares
- Check whether a number can be represented by sum of two squares
- Check if N can be represented as sum of integers chosen from set {A, B}
- Check if a number can be represented as sum of non zero powers of 2
- Check whether a number can be represented as sum of K distinct positive integers
- Smallest number greater than n that can be represented as a sum of distinct power of k

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.