通过任意对任意次数交换位来最小化前 2^K–1 个自然数的乘积
给定一个正整数K , 任务是通过任意次数交换任意两个数的对应位置的位来最小化前(2 K – 1) 个自然数的正积。
例子:
Input: K = 3
Output: 1512
Explanation: The original product is 5040. The given array in binary notation is {001, 010, 011, 100, 101, 110, 111}
- In the first operation swap the leftmost bit of the second and fifth elements. The resulting array is [001, 110, 011, 100, 001, 110, 111].
- In the second operation swap the middle bit of the third and fourth elements. The resulting array is [001, 110, 001, 110, 001, 110, 111].
After the above operations, the array elements are {1, 6, 1, 6, 1, 6, 7}. Therefore, the product is 1 * 6 * 1 * 6 * 1 * 6 * 7 = 1512, which is the minimum possible positive product.
Input: K = 2
Output: 6
Explanation: The original permutation in binary notation is {’00’, ’01’, ’10’}. Any swap will lead to product zero or does not change at all. Hence 6 is the correct output.
方法:给定的问题可以基于以下观察来解决:为了获得最小的正积,数字1的频率应该通过交换任意两个数字的位来最大。请按照以下步骤解决给定的问题:
- 找到范围的值(2 K – 1) 。
- 通过将除第0位之外的所有奇数位转换为偶数,将range/2元素转换为 1。
- 因此, range/2数字将为 1 , range/2数字将为range – 1 ,并且数组中的最后一个数字将与range的值相同。
- 求上述步骤中形成的所有数的合成乘积,作为得到的最小正乘积。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum positive
// product after swapping bits at the
// similar position for any two numbers
int minimumPossibleProduct(int K)
{
// Stores the resultant number
int res = 1;
int range = (1 << K) - 1;
// range/2 numbers will be 1 and
// range/2 numbers will be range-1
for (int i = 0; i < K; i++) {
res *= (range - 1);
}
// Multiply with the final number
res *= range;
// Return the answer
return res;
}
// Driver Code
int main()
{
int K = 3;
cout << minimumPossibleProduct(K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find the minimum positive
// product after swapping bits at the
// similar position for any two numbers
static int minimumPossibleProduct(int K)
{
// Stores the resultant number
int res = 1;
int range = (1 << K) - 1;
// range/2 numbers will be 1 and
// range/2 numbers will be range-1
for (int i = 0; i < K; i++) {
res *= (range - 1);
}
// Multiply with the final number
res *= range;
// Return the answer
return res;
}
// Driver Code
public static void main (String[] args) {
int K = 3;
System.out.println(minimumPossibleProduct(K));
}
}
// This code is contributed by Dharanendra L V.
Python3
# python program for the above approach
# Function to find the minimum positive
# product after swapping bits at the
# similar position for any two numbers
def minimumPossibleProduct(K):
# Stores the resultant number
res = 1
r = (1 << K) - 1
# range/2 numbers will be 1 and
# range/2 numbers will be range-1
for i in range(0, K):
res *= (r - 1)
# Multiply with the final number
res *= r
# Return the answer
return res
# Driver Code
K = 3
print(minimumPossibleProduct(K))
# This code is contributed by amreshkumar3.
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the minimum positive
// product after swapping bits at the
// similar position for any two numbers
static int minimumPossibleProduct(int K)
{
// Stores the resultant number
int res = 1;
int range = (1 << K) - 1;
// range/2 numbers will be 1 and
// range/2 numbers will be range-1
for (int i = 0; i < K; i++) {
res *= (range - 1);
}
// Multiply with the final number
res *= range;
// Return the answer
return res;
}
// Driver Code
public static void Main(string[] args)
{
int K = 3;
Console.WriteLine(minimumPossibleProduct(K));
}
}
// This code is contributed by ukasp.
Javascript
1512
时间复杂度: O(K)
辅助空间: O(1)