给定两个数字 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.
方法:斐波那契数列是 f(0)=1, f(1)=2 和 f(i)=f(i-1)+f(i-2) 对 i>1。让我们假设 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) 仍然包括在内,因为允许重复数字。)。
所以递归关系将是:
F(x, k, n)= F(x, k, n-1)+F(x-f(n-1), k-1, n)
基本情况:
- 如果 k=0,则系列中的数字为零,因此总和只能为 0。因此,F(0, 0, n)=1。
- F(x, 0, n)=0,如果 x 不等于 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
?>
Javascript
输出:
Possible ways are: 2
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。