给定两个数字K和N。任务是将给定数K表示为几个N-bonacci数的总和。
例子:
Input: K = 21, N = 5
Output: 3
The three numbers of the 5-bonacci numbers are: 16, 4, 1.
Explanation:
For N = 5, the series will be: 1, 1, 2, 4, 8, 16, 31, 61, 120, and so on. The three numbers 16, 4 and 1 add up to 21.
Input: K = 500, N = 43
Output: 6
The numbers of 43-bonacci that add up to 500 are: 256 128 64 32 16 4
天真的方法:
最简单的方法是生成最多50个项的N-bonacci级数并将它们的值存储在数组中。找到具有给定总和的数组子集,然后打印该子集的大小。
时间复杂度: O(2 N )
高效方法:此方法基于本文讨论的如何形成N-bonacci数的高效方法。
请按照以下步骤解决问题:
- 生成N-bonacci系列并将值存储在数组中,例如N_bonacci [] 。 (最多50个学期)
- 初始化另一个数组ans [] ,以保存总和为K的序列中的数字。
- 以相反的顺序遍历N-bonacci数组,并继续检查K – N_bonacci [i]> = 0 。如果为true,则将N_bonacci [i]推到ans数组,并将K减小为K – N_bonacci [i] 。
- 继续递减K直到它变为0 ,然后最终输出ans数组的大小和元素。
下面是上述方法的实现:
C++
// C++ program for the above problem
#include
using namespace std;
// array to store the N-Bonacci series
long long N_bonacci[100];
// Function to express K as sum of
// several N_bonacci numbers
void N_bonacci_nums(int n, int k)
{
N_bonacci[0] = 1;
for (int i = 1; i <= 50; ++i) {
for (int j = i - 1;
j >= i - k and j >= 0;
--j)
N_bonacci[i]
+= N_bonacci[j];
}
vector ans;
for (int i = 50; i >= 0; --i)
if (n - N_bonacci[i] >= 0) {
ans.push_back(N_bonacci[i]);
n -= N_bonacci[i];
}
if (ans.size() == 1)
ans.push_back(0);
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); ++i)
cout << ans[i] << ", ";
}
// Driver code
int main()
{
int n = 21, k = 5;
N_bonacci_nums(n, k);
return 0;
}
Java
// Java program for the above problem
import java.util.*;
class GFG{
// Array to store the N-Bonacci series
public static long []N_bonacci= new long [100];
// Function to express K as sum of
// several N_bonacci numbers
@SuppressWarnings("unchecked")
public static void N_bonacci_nums(int n, int k)
{
N_bonacci[0] = 1;
for(int i = 1; i <= 50; ++i)
{
for(int j = i - 1;
j >= i - k && j >= 0 ;--j)
N_bonacci[i]+= N_bonacci[j];
}
Vector ans = new Vector();
for(int i = 50; i >= 0; --i)
if (n - N_bonacci[i] >= 0)
{
ans.add(N_bonacci[i]);
n -= N_bonacci[i];
}
if (ans.size() == 1)
ans.add(0);
System.out.println(ans.size());
for(int i = 0; i < ans.size(); ++i)
System.out.print(ans.get(i) + ", ");
}
// Driver code
public static void main(String args[])
{
int n = 21, k = 5;
N_bonacci_nums(n, k);
}
}
// This code is contributed by SoumikMondal
Python3
# Python3 program for the above problem
# Array to store the N-Bonacci series
N_bonacci = [0] * 100
# Function to express K as sum of
# several N_bonacci numbers
def N_bonacci_nums(n, k):
N_bonacci[0] = 1
for i in range(1, 51):
j = i - 1
while j >= i - k and j >= 0:
N_bonacci[i] += N_bonacci[j]
j -= 1
ans = []
for i in range(50, -1, -1):
if (n - N_bonacci[i] >= 0):
ans.append(N_bonacci[i])
n -= N_bonacci[i]
if (len(ans) == 1):
ans.append(0)
print(len(ans))
for i in ans:
print(i, end = ", ")
# Driver code
if __name__ == '__main__':
n = 21
k = 5
N_bonacci_nums(n, k)
# This code is contributed by mohit kumar 29
C#
// C# program for the above problem
using System;
using System.Collections.Generic;
class GFG{
// Array to store the N-Bonacci series
public static long []N_bonacci = new long [100];
// Function to express K as sum of
// several N_bonacci numbers
static void N_bonacci_nums(long n, long k)
{
N_bonacci[0] = 1;
for(int i = 1; i <= 50; ++i)
{
for(int j = i - 1;
j >= i - k && j >= 0; --j)
N_bonacci[i] += N_bonacci[j];
}
List ans = new List();
for(int i = 50; i >= 0; --i)
if (n - N_bonacci[i] >= 0)
{
ans.Add(N_bonacci[i]);
n -= N_bonacci[i];
}
if (ans.Count == 1)
ans.Add(0);
Console.WriteLine(ans.Count);
for(int i = 0; i < ans.Count; ++i)
Console.Write(ans[i] + ", ");
}
// Driver code
static void Main()
{
long n = 21, k = 5;
N_bonacci_nums(n, k);
}
}
// This code is contributed by divyeshrabadiya07
输出:
3
16, 4, 1,
时间复杂度: O(K * K)
辅助空间: O(1)