📌  相关文章
📜  N可以表示为斐波纳契数之和而无需重复的方式计数

📅  最后修改于: 2021-05-04 07:29:55             🧑  作者: Mango

给定数字N ,任务是找到将整数N表示为斐波纳契数之和而不重复任何斐波纳契数的方式的数目。

例子:

幼稚的方法:幼稚的想法是写出所有可能的组合,加起来等于给定的数字N。检查是否有任何组合具有重复的整数,然后不增加计数器,否则每次将计数增加1。最后返回计数。

时间复杂度: O(N)
辅助空间: O(1)

高效方法:想法是使用动态编程来优化上述方法。步骤如下:

  • 让我们用斐波那契代码表示一个数字。
  • 为每个正数写斐波那契代码,这样两个相邻位都不为1
  • 这对于所有数字都是正确的,因为如果有两个相邻的位为1位,则可以通过斐波那契数的属性将其转换为单个1位。我们将此表示形式称为规范表示形式。
  • 获取规范表示。生成几个斐波那契数(大约90 ),然后尝试按降序将所有数减。
  • 让我们将给定数字的标准表示形式的1位位置按升序存储到数组v中,并将任何1位分解为两个1位,如下所示:
  • 经过多次此类操作后,我们将获得下一个1位(或数字的末尾)。这个1位也可以分解,但是只能移位一位。
  • 初始化dp数组dp1 [] ,对于所有剩余的1位分解的情况, dp1 [i]是表示数字的最多种方法,该数字由i个最左边的1位组成。同样,采用dp2 [i]来表示表示数字的方式数目,该数字由i个剩余的1位分解为数字的最左边的1位组成。

下面是相同的插图:

因此,答案是dp1 [cnt] + dp2 [cnt] ,其中cnt是规范表示中1位的总数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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;
}


Java
// Java program for 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


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


C#
// C# program for the above approach
using System;
 
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(long 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
static void Main()
{
     
    // Function call to generate the 
    // fibonacci numbers 
    fibonacci(); 
       
    // Given number 
    int num = 13; 
       
    // Function call 
    Console.Write(find(num));
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:
3

时间复杂度: O(log N)
辅助空间: O(log N)