给定两个整数N和K ,任务是找出是否可以将N表示为2的恰好K 次幂的总和。如果可能,则打印K 个正整数,使它们是2 的幂并且它们的总和恰好等于N。否则,打印“不可能” 。如果存在多个答案,则打印任何一个。
例子:
Input: N = 5, K = 2
Output: 4 1
Explanation:
The only way of representing N as K numbers that are powers of 2 is {4, 1}.
Input: N = 7, K = 4
Output: 4 1 1 1
Explanation:
The possible ways of representing N as K numbers that are powers of 2 are {4, 1, 1, 1} and {2, 2, 2, 1}.
基于优先队列的方法:参考这篇文章使用优先队列解决问题。
递归方法:参考这篇文章使用递归解决问题。
替代方法:这个想法是使用贪婪方法来解决这个问题。以下是步骤:
- 初始化一个整数,比如num = 31和一个整数向量,比如res ,以存储K的 2 次幂数字。
- 检查 N 中的位数是否大于K或N是否小于K,然后打印“Impossible”。
- 在num ≥ 0 和 K > 0 时进行迭代:
- 检查N – 2 num是否小于K – 1 。如果发现为真,则将num减一并继续。
- 否则,将K减一, N减2 num并将num推入向量res 。
- 最后,打印向量res 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find K numbers with
// sum N that are powers of 2
void nAsKPowersOfTwo(int N, int K)
{
// Count the number of set bits
int x = __builtin_popcount(N);
// Not-possible condition
if (K < x || K > N) {
cout << "Impossible";
return;
}
int num = 31;
// To store K numbers
// which are powers of 2
vector res;
// Traverse while num >= 0
while (num >= 0 && K) {
// Calculate current bit value
int val = pow(2, num);
// Check if remaining N
// can be represented as
// K-1 numbers that are
// power of 2
if (N - val < K - 1) {
// Decrement num by one
--num;
continue;
}
// Decrement K by one
--K;
// Decrement N by val
N -= val;
// Push the num in the
// vector res
res.push_back(num);
}
// Print the vector res
for (auto x : res)
cout << pow(2, x) << " ";
}
// Driver Code
int main()
{
// Given N & K
int N = 7, K = 4;
// Function Call
nAsKPowersOfTwo(N, K);
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find K numbers with
// sum N that are powers of 2
static void nAsKPowersOfTwo(int N, int K)
{
// Count the number of set bits
int x = Integer.bitCount(N);
// Not-possible condition
if (K < x || K > N)
{
System.out.print("Impossible");
return;
}
int num = 31;
// To store K numbers
// which are powers of 2
Vector res = new Vector();
// Traverse while num >= 0
while (num >= 0 && K > 0)
{
// Calculate current bit value
int val = (int) Math.pow(2, num);
// Check if remaining N
// can be represented as
// K-1 numbers that are
// power of 2
if (N - val < K - 1)
{
// Decrement num by one
--num;
continue;
}
// Decrement K by one
--K;
// Decrement N by val
N -= val;
// Push the num in the
// vector res
res.add(num);
}
// Print the vector res
for (int i : res)
System.out.print((int)Math.pow(2, i)+ " ");
}
// Driver Code
public static void main(String[] args)
{
// Given N & K
int N = 7, K = 4;
// Function Call
nAsKPowersOfTwo(N, K);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Function to find K numbers with
# sum N that are powers of 2
def nAsKPowersOfTwo(N, K):
# Count the number of set bits
x = bin(N).count('1')
# Not-possible condition
if (K < x or K > N):
cout << "Impossible"
return
num = 31
# To store K numbers
# which are powers of 2
res = []
# Traverse while num >= 0
while (num >= 0 and K):
# Calculate current bit value
val = pow(2, num)
# Check if remaining N
# can be represented as
# K-1 numbers that are
# power of 2
if (N - val < K - 1):
# Decrement num by one
num -= 1
continue
# Decrement K by one
K -= 1
# Decrement N by val
N -= val
# Push the num in the
# vector res
res.append(num)
# Prthe vector res
for x in res:
print(pow(2, x), end = " ")
# Driver Code
if __name__ == '__main__':
# Given N & K
N, K = 7, 4
# Function Call
nAsKPowersOfTwo(N, K)
# This code is contributed mohit kumar 29.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find K numbers with
// sum N that are powers of 2
static void nAsKPowersOfTwo(int N, int K)
{
// Count the number of set bits
int x = countSetBits(N);
// Not-possible condition
if (K < x || K > N)
{
Console.Write("Impossible");
return;
}
int num = 31;
// To store K numbers
// which are powers of 2
List res = new List();
// Traverse while num >= 0
while (num >= 0 && K > 0)
{
// Calculate current bit value
int val = (int) Math.Pow(2, num);
// Check if remaining N
// can be represented as
// K-1 numbers that are
// power of 2
if (N - val < K - 1)
{
// Decrement num by one
--num;
continue;
}
// Decrement K by one
--K;
// Decrement N by val
N -= val;
// Push the num in the
// vector res
res.Add(num);
}
// Print the vector res
foreach (int i in res)
Console.Write((int)Math.Pow(2, i)+ " ");
}
static int countSetBits(long x)
{
int setBits = 0;
while (x != 0)
{
x = x & (x - 1);
setBits++;
}
return setBits;
}
// Driver Code
public static void Main(String[] args)
{
// Given N & K
int N = 7, K = 4;
// Function Call
nAsKPowersOfTwo(N, K);
}
}
// This code is contributed by shikhasingrajput
Javascript
输出:
4 1 1 1
时间复杂度: O(32)
辅助空间: O(1)