给定两个整数n和k ,任务是确定是否有可能将n表示为2的k次幂之和。如果可能,则打印k个正整数,使它们为2的幂,并且它们的和等于n,否则将打印Impossible 。
例子:
Input: n = 9, k = 4
Output: 1 2 2 4
1, 2 and 4 are all powers of 2 and 1 + 2 + 2 + 4 = 9.
Input: n = 3, k = 7
Output: Impossible
It is impossible since 3 cannot be represented as sum of 7 numbers which are powers of 2.
我们在查找k个数(其幂为2且总和为N)中讨论了解决此问题的一种方法。在本文中,正在讨论另一种方法。
方法:
- 创建一个大小为k的数组arr [] ,将所有元素初始化为1,并创建一个变量sum = k 。
- 现在从arr []的最后一个元素开始
- 如果sum + arr [i]≤n,则更新sum = sum + arr [i],而arr [i] = arr [i] * 2 。
- 否则跳过当前元素。
- 如果sum = n,则arr []的内容是必需的元素。
- 否则,不可能将n精确地表示为2的k次幂。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to print k numbers which are powers of two
// and whose sum is equal to n
void FindAllElements(int n, int k)
{
// Initialising the sum with k
int sum = k;
// Initialising an array A with k elements
// and filling all elements with 1
int A[k];
fill(A, A + k, 1);
for (int i = k - 1; i >= 0; --i) {
// Iterating A[] from k-1 to 0
while (sum + A[i] <= n) {
// Update sum and A[i]
// till sum + A[i] is less than equal to n
sum += A[i];
A[i] *= 2;
}
}
// Impossible to find the combination
if (sum != n) {
cout << "Impossible";
}
// Possible solution is stored in A[]
else {
for (int i = 0; i < k; ++i)
cout << A[i] << ' ';
}
}
// Driver code
int main()
{
int n = 12;
int k = 6;
FindAllElements(n, k);
return 0;
}
Java
// Java implementation of the above approach
import java.util.Arrays;
public class GfG {
// Function to print k numbers which are powers of two
// and whose sum is equal to n
public static void FindAllElements(int n, int k)
{
// Initialising the sum with k
int sum = k;
// Initialising an array A with k elements
// and filling all elements with 1
int[] A = new int[k];
Arrays.fill(A, 0, k, 1);
for (int i = k - 1; i >= 0; --i) {
// Iterating A[] from k-1 to 0
while (sum + A[i] <= n) {
// Update sum and A[i]
// till sum + A[i] is less than equal to n
sum += A[i];
A[i] *= 2;
}
}
// Impossible to find the combination
if (sum != n) {
System.out.print("Impossible");
}
// Possible solution is stored in A[]
else {
for (int i = 0; i < k; ++i)
System.out.print(A[i] + " ");
}
}
public static void main(String []args){
int n = 12;
int k = 6;
FindAllElements(n, k);
}
}
// This code is contributed by Rituraj Jain
Python3
# Python 3 implementation of the above approach
# Function to print k numbers which are
# powers of two and whose sum is equal to n
def FindAllElements(n, k):
# Initialising the sum with k
sum = k
# Initialising an array A with k elements
# and filling all elements with 1
A = [1 for i in range(k)]
i = k - 1
while(i >= 0):
# Iterating A[] from k-1 to 0
while (sum + A[i] <= n):
# Update sum and A[i] till
# sum + A[i] is less than equal to n
sum += A[i]
A[i] *= 2
i -= 1
# Impossible to find the combination
if (sum != n):
print("Impossible")
# Possible solution is stored in A[]
else:
for i in range(0, k, 1):
print(A[i], end = ' ')
# Driver code
if __name__ == '__main__':
n = 12
k = 6
FindAllElements(n, k)
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the above approach
using System;
class GfG
{
// Function to print k numbers
// which are powers of two
// and whose sum is equal to n
public static void FindAllElements(int n, int k)
{
// Initialising the sum with k
int sum = k;
// Initialising an array A with k elements
// and filling all elements with 1
int[] A = new int[k];
for(int i = 0; i < k; i++)
A[i] = 1;
for (int i = k - 1; i >= 0; --i)
{
// Iterating A[] from k-1 to 0
while (sum + A[i] <= n)
{
// Update sum and A[i]
// till sum + A[i] is less than equal to n
sum += A[i];
A[i] *= 2;
}
}
// Impossible to find the combination
if (sum != n)
{
Console.Write("Impossible");
}
// Possible solution is stored in A[]
else
{
for (int i = 0; i < k; ++i)
Console.Write(A[i] + " ");
}
}
// Driver code
public static void Main(String []args)
{
int n = 12;
int k = 6;
FindAllElements(n, k);
}
}
// This code contributed by Rajput-Ji
PHP
= 0; --$i)
{
// Iterating A[] from k-1 to 0
while ($sum + $A[$i] <= $n)
{
// Update sum and A[i] till
// sum + A[i] is less than equal to n
$sum += $A[$i];
$A[$i] *= 2;
}
}
// Impossible to find the combination
if ($sum != $n)
{
echo"Impossible";
}
// Possible solution is stored in A[]
else
{
for ($i = 0; $i < $k; ++$i)
echo $A[$i], ' ';
}
}
// Driver code
$n = 12;
$k = 6;
FindAllElements($n, $k);
// This code is contributed by Ryuga
?>
输出:
1 1 1 1 4 4