📌  相关文章
📜  将数字表示为k个斐波那契数之和的方式数量

📅  最后修改于: 2021-05-07 18:32:19             🧑  作者: Mango

给定两个数字N和K。找到将N表示为K个斐波那契数之和的方式的数目。

例子

Input : n = 12, k = 1 
Output : 0

Input : n = 13, k = 3
Output : 2
Explanation : 2 + 3 + 8, 3 + 5 + 5.  

方法:对于i> 1,斐波那契级数为f(0)= 1,f(1)= 2和f(i)= f(i-1)+ f(i-2)。假设F(x,k,n)是使用f(0),f(1),…f(n-1)中的正好k个数字形成和x的方法的数量。要找到F(x,k,n)的递归,请注意有两种情况:总和是否为f(n-1)。

  • 如果f(n-1)不在总和中,则x会使用来自f(0),f(1),…,f(n-2)的准确k个数形成一个总和。
  • 如果f(n-1)在总和中,则剩余的xf(n-1)使用f(0),f(1),…,f(n-1)中的正好k-1个数形成。 (请注意,由于允许重复编号,因此仍包含f(n-1)。)。

因此,递归关系将是:

基本案例:

  • 如果k = 0,则该序列中有零个数,因此总和只能为0。因此,F(0,0,n)= 1。
  • 如果x不等于0,则F(x,0,n)= 0。

此外,还有其他情况使F(x,k,n)= 0,如下所示:

  • 如果k> 0且x = 0,因为具有至少一个正数必须得出一个正和。
  • 如果k> 0且n = 0,因为没有数字可供选择。
  • 如果x <0,因为无法使用有限数量的非负数形成负和。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
  
// to store fibonacci numbers
// 42 second number in fibonacci series
// largest possible integer
int fib[43] = { 0 };
  
// Function to generate fibonacci series
void fibonacci()
{
    fib[0] = 1;
    fib[1] = 2;
    for (int i = 2; i < 43; i++)
        fib[i] = fib[i - 1] + fib[i - 2];
}
  
// Recursive function to return the 
// number of ways 
int rec(int x, int y, int last)
{
    // base condition
    if (y == 0) {
        if (x == 0)
            return 1;
        return 0;
    }
    int sum = 0;
    // for recursive function call
    for (int i = last; i >= 0 and fib[i] * y >= x; i--) {
        if (fib[i] > x)
            continue;
        sum += rec(x - fib[i], y - 1, i);
    }
    return sum;
}
  
// Driver code
int main()
{
    fibonacci();
    int n = 13, k = 3;
    cout << "Possible ways are: "
         << rec(n, k, 42);
  
    return 0;
}


Java
//Java implementation of above approach
public class AQW {
  
    //to store fibonacci numbers
    //42 second number in fibonacci series
    //largest possible integer
    static int fib[] = new int[43];
  
    //Function to generate fibonacci series
    static void fibonacci()
    {
     fib[0] = 1;
     fib[1] = 2;
     for (int i = 2; i < 43; i++)
         fib[i] = fib[i - 1] + fib[i - 2];
    }
  
    //Recursive function to return the 
    //number of ways 
    static int rec(int x, int y, int last)
    {
     // base condition
     if (y == 0) {
         if (x == 0)
             return 1;
         return 0;
     }
     int sum = 0;
     // for recursive function call
     for (int i = last; i >= 0 && fib[i] * y >= x; i--) {
         if (fib[i] > x)
             continue;
         sum += rec(x - fib[i], y - 1, i);
     }
     return sum;
    }
  
    //Driver code
    public static void main(String[] args) {
          
        fibonacci();
         int n = 13, k = 3;
         System.out.println("Possible ways are: "+ rec(n, k, 42));
  
    }
  
}


Python3
# Python3 implementation of the above approach 
  
# To store fibonacci numbers 42 second 
# number in fibonacci series largest
# possible integer 
fib = [0] * 43
  
# Function to generate fibonacci
# series 
def fibonacci(): 
  
    fib[0] = 1
    fib[1] = 2
    for i in range(2, 43): 
        fib[i] = fib[i - 1] + fib[i - 2] 
  
# Recursive function to return the 
# number of ways 
def rec(x, y, last): 
  
    # base condition 
    if y == 0: 
        if x == 0: 
            return 1
        return 0
      
    Sum, i = 0, last 
      
    # for recursive function call 
    while i >= 0 and fib[i] * y >= x: 
        if fib[i] > x:
            i -= 1
            continue
        Sum += rec(x - fib[i], y - 1, i) 
        i -= 1
          
    return Sum
  
# Driver code 
if __name__ == "__main__":
  
    fibonacci() 
    n, k = 13, 3
    print("Possible ways are:", rec(n, k, 42))
  
# This code is contributed 
# by Rituraj Jain


C#
// C# implementation of above approach
using System; 
    
class GFG 
{ 
    // to store fibonacci numbers 
    // 42 second number in fibonacci series 
    // largest possible integer 
    static int[] fib = new int[43]; 
        
    // Function to generate fibonacci series 
    public static void fibonacci() 
    { 
        fib[0] = 1; 
        fib[1] = 2; 
        for (int i = 2; i < 43; i++) 
            fib[i] = fib[i - 1] + fib[i - 2]; 
    } 
        
    // Recursive function to return the  
    // number of ways  
    public static int rec(int x, int y, int last) 
    { 
        // base condition 
        if (y == 0) { 
            if (x == 0) 
                return 1; 
            return 0; 
        } 
        int sum = 0; 
        // for recursive function call 
        for (int i = last; i >= 0 && fib[i] * y >= x; i--) { 
            if (fib[i] > x) 
                continue; 
            sum += rec(x - fib[i], y - 1, i); 
        } 
        return sum; 
    } 
        
    // Driver code 
    static void Main() 
    { 
        for(int i = 0; i < 43; i++)
            fib[i] = 0;
        fibonacci(); 
        int n = 13, k = 3; 
        Console.Write("Possible ways are: " + rec(n, k, 42)); 
    }
    //This code is contributed by DrRoot_
}


PHP
= 0 and 
         $fib[$i] * $y >= $x; $i--) 
    {
        if ($fib[$i] > $x)
            continue;
        $sum += rec($x - $fib[$i], 
                    $y - 1, $i);
    }
    return $sum;
}
  
// Driver code
fibonacci();
$n = 13;
$k = 3;
echo "Possible ways are: " . 
            rec($n, $k, 42);
  
// This code is contributed by mits
?>


输出:
Possible ways are: 2