给定一个整数N ,任务是通过交换任意两个数字的任意第i位任意次数来找到前N – 1 个自然数的最小正积,即[1, (N – 1)] 。
注意: N 总是 2 的完美幂。由于乘积可能非常大,打印答案模10 9 + 7 。
例子:
Input: N = 4
Output: 6
Explanation:
No swapping of bits is required. Therefore, the minimum product is 1*2*3 = 6.
Input: N = 8
Output: 1512
Explanation:
Let the array arr[] stores all the value from 1 to N as {1, 2, 3, 4, 5, 6, 7}
Follow the below steps:
Step 1: In elements 2 = (0010) and 5 = (0101), swap 0th and 1st bit. Therefore, replace 2 with 1 and 5 with 6. arr[] = {1, 1, 3, 4, 6, 6, 7}.
Step 2: In elements 3 = (0011) and 4 = (0100), swap 1th bit. Therefore, replace 3 with 1 and 4 with 6. arr[] = {1, 1, 1, 6, 6, 6, 7}.
Hence, the minimum product = 1*1*1*6*6*6*7 = 1512 % 1e9+7 = 1512.
方法:这个想法是做一些观察。例如,如果N = 8且arr[] = {1, 2, 3, 4, 5, 6, 7} ,请注意要使乘积最小,必须有三个六,即必须有一个元素具有值(N – 2)出现频率为(1 + (N – 4)/2)并且必须有三个,即必须有(1 + (N – 4)/2)个。最后将当前乘积乘以(N – 1) 。因此,公式变为:
Minimum product for any value N = ((N – 1) * (N – 2)(N – 4)/2 + 1) % 1e9 + 7
请按照以下步骤解决问题:
- 将ans初始化为1 。
- 迭代范围[0, 1 + (N – 4)/2] 。
- 在每次遍历中,将ans乘以N – 2并将ans更新为ans mod 1e9+7 。
- 经过以上步骤,打印ans*(N – 1) mod 1e9+7 的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
int mod = 1e9 + 7;
// Function to find the minimum product
// of 1 to N - 1 after performing the
// given operations
void minProduct(int n)
{
// Initialize ans with 1
int ans = 1;
// Multiply ans with N-2
// ((N - 4)/2) times
for (int i = 1;
i <= (n - 4) / 2; i++) {
ans = (1LL * ans
* (n - 2))
% mod;
}
// Multiply ans with N - 1
// and N - 2 once
ans = (1LL * ans
* (n - 2) * (n - 1))
% mod;
// Print ans
cout << ans << endl;
}
// Driver Code
int main()
{
// Given Number N
int N = 8;
// Function Call
minProduct(N);
return 0;
}
Java
// Java program for the
// above approach
import java.util.*;
class GFG{
static int mod = (int)1e9 + 7;
// Function to find the
// minimum product of 1
// to N - 1 after performing
// the given operations
static void minProduct(int n)
{
// Initialize ans with 1
int ans = 1;
// Multiply ans with N-2
// ((N - 4)/2) times
for (int i = 1;
i <= (n - 4) / 2; i++)
{
ans = (int)(1L * ans *
(n - 2)) % mod;
}
// Multiply ans with N - 1
// and N - 2 once
ans = (int)(1L * ans *
(n - 2) * (n - 1)) % mod;
// Print ans
System.out.print(ans + "\n");
}
// Driver Code
public static void main(String[] args)
{
// Given Number N
int N = 8;
// Function Call
minProduct(N);
}
}
// This code is contributed by gauravrajput1
Python3
# Python3 program for the above approach
mod = 1e9 + 7
# Function to find the minimum product
# of 1 to N - 1 after performing the
# given operations
def minProduct(n):
# Initialize ans with 1
ans = 1
# Multiply ans with N-2
# ((N - 4)/2) times
for i in range(1, (n - 4) // 2 + 1):
ans = (ans * (n - 2)) % mod
# Multiply ans with N - 1
# and N - 2 once
ans = (ans * (n - 2) * (n - 1)) % mod
# Print ans
print(int(ans))
# Driver Code
if __name__ == '__main__':
# Given number N
N = 8
# Function call
minProduct(N)
# This code is contributed by mohit kumar 29
C#
// C# program for the
// above approach
using System;
class GFG{
static int mod = (int)1e9 + 7;
// Function to find the
// minimum product of 1
// to N - 1 after performing
// the given operations
static void minProduct(int n)
{
// Initialize ans with 1
int ans = 1;
// Multiply ans with N-2
// ((N - 4)/2) times
for (int i = 1;
i <= (n - 4) / 2; i++)
{
ans = (int)(1L * ans *
(n - 2)) % mod;
}
// Multiply ans with N - 1
// and N - 2 once
ans = (int)(1L * ans *
(n - 2) *
(n - 1)) % mod;
// Print ans
Console.Write(ans + "\n");
}
// Driver Code
public static void Main(String[] args)
{
// Given Number N
int N = 8;
// Function Call
minProduct(N);
}
}
// This code is contributed by Rajput-Ji
Javascript
1512
时间复杂度: O(N),其中 N 是给定的整数。
辅助空间: O(1)