给定两个整数N和K ,任务是从范围[1,N]中查找整数的排列,以使gcd(p [i],i)> 1的索引数(从1开始的索引)正好ķ。如果无法进行此排列,则打印-1 。
例子:
Input: N = 4, K = 3
Output: 1 2 3 4
gcd(1, 1) = 1
gcd(2, 2) = 2
gcd(3, 3) = 3
gcd(4, 4) = 4
Therefore, there are exactly 3 indices where gcd(p[i], i) > 1
Input: N = 1, K = 1
Output: -1
方法:可以在此处进行一些观察:
- gcd(i,i + 1)= 1
- gcd(1,i)= 1
- gcd(i,i)= i
由于1的gcd与其他任何数字始终将为1,因此K最多可以为N – 1 。考虑其中p [i] = i的置换,其中gcd(p [i],i)> 1的索引数将为N – 1 。请注意,交换2个连续元素(不包括1个)将使此类索引的数目减少2。而与1交换将使计数的数目减少1。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to print the required permutation
void printPermutation(int n, int k)
{
// Not possible
if (k >= n || (n % 2 == 0 && k == 0)) {
cout << -1;
return;
}
int per[n + 1], i;
// Initial permutation
for (i = 1; i <= n; i++)
per[i] = i;
// Indices for which gcd(p[i], i) > 1
// in the initial permutation
int cnt = n - 1;
i = 2;
while (i < n) {
// Reduce the count by 2
if (cnt - 1 > k) {
swap(per[i], per[i + 1]);
cnt -= 2;
}
// Reduce the count by 1
else if (cnt - 1 == k) {
swap(per[1], per[i]);
cnt--;
}
else
break;
i += 2;
}
// Print the permutation
for (i = 1; i <= n; i++)
cout << per[i] << " ";
}
// Driver code
int main()
{
int n = 4, k = 3;
printPermutation(n, k);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to print the required permutation
static void printPermutation(int n, int k)
{
// Not possible
if (k >= n || (n % 2 == 0 && k == 0))
{
System.out.print(-1);
return;
}
int per[] = new int[n + 1], i;
// Initial permutation
for (i = 1; i <= n; i++)
{
per[i] = i;
}
// Indices for which gcd(p[i], i) > 1
// in the initial permutation
int cnt = n - 1;
i = 2;
while (i < n)
{
// Reduce the count by 2
if (cnt - 1 > k)
{
swap(per, i, i + 1);
cnt -= 2;
}
// Reduce the count by 1
else if (cnt - 1 == k)
{
swap(per, 1, i);
cnt--;
}
else
{
break;
}
i += 2;
}
// Print the permutation
for (i = 1; i <= n; i++)
{
System.out.print(per[i] + " ");
}
}
static int[] swap(int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
// Driver code
public static void main(String[] args)
{
int n = 4, k = 3;
printPermutation(n, k);
}
}
/* This code contributed by PrinciRaj1992 */
Python3
# Python3 implementation of the approach
# Function to print the required permutation
def printPermutation(n, k):
# Not possible
if (k >= n or (n % 2 == 0 and
k == 0)):
print(-1);
return;
per = [0] * (n + 1);
# Initial permutation
for i in range(1, n + 1):
per[i] = i;
# Indices for which gcd(p[i], i) > 1
# in the initial permutation
cnt = n - 1;
i = 2;
while (i < n):
# Reduce the count by 2
if (cnt - 1 > k):
t = per[i];
per[i] = per[i + 1];
per[i + 1] = t;
# swap(per[i], per[i + 1]);
cnt -= 2;
# Reduce the count by 1
elif (cnt - 1 == k):
# swap(per[1], per[i]);
t = per[1];
per[1] = per[i];
per[i] = t;
cnt -= 1;
else:
break;
i += 2;
# Print the permutation
for i in range(1, n + 1):
print(per[i], end = " ");
# Driver code
n = 4;
k = 3;
printPermutation(n, k);
# This code is contributed by mits
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to print the required permutation
static void printPermutation(int n, int k)
{
// Not possible
if (k >= n || (n % 2 == 0 && k == 0))
{
Console.Write(-1);
return;
}
int []per = new int[n + 1] ;
int i ;
// Initial permutation
for (i = 1; i <= n; i++)
{
per[i] = i;
}
// Indices for which gcd(p[i], i) > 1
// in the initial permutation
int cnt = n - 1;
i = 2;
while (i < n)
{
// Reduce the count by 2
if (cnt - 1 > k)
{
swap(per, i, i + 1);
cnt -= 2;
}
// Reduce the count by 1
else if (cnt - 1 == k)
{
swap(per, 1, i);
cnt--;
}
else
{
break;
}
i += 2;
}
// Print the permutation
for (i = 1; i <= n; i++)
{
Console.Write(per[i] + " ");
}
}
static int[] swap(int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
// Driver code
public static void Main()
{
int n = 4, k = 3;
printPermutation(n, k);
}
}
/* This code contributed by Ryuga */
PHP
= $n || ($n % 2 == 0 &&
$k == 0))
{
echo -1;
return;
}
$per[$n + 1] = array();
$i;
// Initial permutation
for ($i = 1; $i <= $n; $i++)
$per[$i] = $i;
// Indices for which gcd(p[i], i) > 1
// in the initial permutation
$cnt = $n - 1;
$i = 2;
while ($i < $n)
{
// Reduce the count by 2
if ($cnt - 1 > $k)
{
list($per[$i],
$per[$i + 1]) = array($per[$i + 1],
$per[$i]);
//swap(per[i], per[i + 1]);
$cnt -= 2;
}
// Reduce the count by 1
else if ($cnt - 1 == $k)
{
// swap(per[1], per[i]);
list($per[1],
$per[$i]) = array($per[$i],
$per[1]);
$cnt--;
}
else
break;
$i += 2;
}
// Print the permutation
for ($i = 1; $i <= $n; $i++)
echo $per[$i], " ";
}
// Driver code
$n = 4;
$k = 3;
printPermutation($n, $k);
// This code is contributed by ajit.
?>
输出:
1 2 3 4
时间复杂度: O(N)