给定整数N ,任务是计算将N表示为2的幂的和的方式数。
例子:
Input: N = 4
Output: 4
Explanation: All possible ways to obtains sum N using powers of 2 are {4, 2+2, 1+1+1+1, 2+1+1}.
Input: N = 5
Output: 4
Explanation: All possible ways to obtains sum N using powers of 2 are {4 + 1, 2+2 + 1, 1+1+1+1 + 1, 2+1+1 + 1}
天真的方法:解决该问题的最简单方法是使值小于N的2的所有次幂并打印所有组合以表示总和N。
高效方法:为了优化上述方法,我们的想法是使用递归。定义一个函数f(N,K) ,该函数表示将N表示为2的幂的和的方式的数量,其中所有数量的幂均小于或等于k,其中K (= log 2 (N) )最大的2个电源,其满足2 k≤ N.
If (power(2, K) ≤ N) :
f(N, K) = f(N – power(2, K), K) + f(N, K – 1) //to check if power(2, k) can be one of the number.
Otherwise:
f(N, K)=f(N, K – 1)
Base cases :
- If (N = 0) f(N, K)=1 (Only 1 possible way exists to represent N)
- If (k==0) f(N, K)=1 (Only 1 possible way exists to represent N by taking 20)
下面是上述方法的实现:
C++
// C++ program for above implementation
#include
using namespace std;
int numberOfWays(int n, int k)
{
// Base Cases
if (n == 0)
return 1;
if (k == 0)
return 1;
// Check if 2^k can be used as
// one of the numbers or not
if (n >= pow(2, k)) {
int curr_val = pow(2, k);
return numberOfWays(n - curr_val, k)
+ numberOfWays(n, k - 1);
}
// Otherwise
else
// Count number of ways to
// N using 2 ^ k - 1
return numberOfWays(n, k - 1);
}
// Driver Code
int main()
{
int n = 4;
int k = log2(n);
cout << numberOfWays(n, k) << endl;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
static int numberOfWays(int n, int k)
{
// Base Cases
if (n == 0)
return 1;
if (k == 0)
return 1;
// Check if 2^k can be used as
// one of the numbers or not
if (n >= (int)Math.pow(2, k))
{
int curr_val = (int)Math.pow(2, k);
return numberOfWays(n - curr_val, k)
+ numberOfWays(n, k - 1);
}
// Otherwise
else
// Count number of ways to
// N using 2 ^ k - 1
return numberOfWays(n, k - 1);
}
// Driver code
public static void main(String[] args)
{
int n = 4;
int k = (int)(Math.log(n) / Math.log(2));
System.out.println(numberOfWays(n, k));
}
}
// This code is contributed by susmitakundugoaldanga.
Python3
# Python3 program for above implementation
from math import log2
def numberOfWays(n, k):
# Base Cases
if (n == 0):
return 1
if (k == 0):
return 1
# Check if 2^k can be used as
# one of the numbers or not
if (n >= pow(2, k)):
curr_val = pow(2, k)
return numberOfWays(n - curr_val, k) + numberOfWays(n, k - 1)
# Otherwise
else:
# Count number of ways to
# N using 2 ^ k - 1
return numberOfWays(n, k - 1)
# Driver Code
if __name__ == '__main__':
n = 4
k = log2(n)
print(numberOfWays(n, k))
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
class GFG
{
static int numberOfWays(int n, int k)
{
// Base Cases
if (n == 0)
return 1;
if (k == 0)
return 1;
// Check if 2^k can be used as
// one of the numbers or not
if (n >= (int)Math.Pow(2, k))
{
int curr_val = (int)Math.Pow(2, k);
return numberOfWays(n - curr_val, k)
+ numberOfWays(n, k - 1);
}
// Otherwise
else
// Count number of ways to
// N using 2 ^ k - 1
return numberOfWays(n, k - 1);
}
// Driver code
public static void Main(String[] args)
{
int n = 4;
int k = (int)(Math.Log(n) / Math.Log(2));
Console.WriteLine(numberOfWays(n, k));
}
}
// This code is contributed by 29AjayKumar
Javascript
4
时间复杂度: O((logN + K) K ),其中K为log 2 (N)
辅助空间: O(1)