📌  相关文章
📜  查找满足给定条件的前N个自然数的排列

📅  最后修改于: 2021-05-06 18:14:28             🧑  作者: Mango

给定两个整数NK,该任务是找到第一N的自然数的转置P使得存在满足条件GCD(P [I]中,I)> 1对于所有1≤I≤Ñ恰好K个元素。
例子:

方法:将最后K个元素保留在原位。移动其余元素,以使i元素位于(i + 1)位置,第(N-K)元素保持在位置1,因为gcd(x,x + 1)= 1
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to find permutation(p) of first N
// natural numbers such that there are exactly K
// elements of permutation such that GCD(p[i], i)>1
void Permutation(int n, int k)
{
    int p[n + 1];
 
    // First place all the numbers
    // in their respective places
    for (int i = 1; i <= n; i++)
        p[i] = i;
 
    // Modify for first n-k integers
    for (int i = 1; i < n - k; i++)
        p[i + 1] = i;
 
    // In first index place n-k
    p[1] = n - k;
 
    // Print the permutation
    for (int i = 1; i <= n; i++)
        cout << p[i] << " ";
}
 
// Driver code
int main()
{
    int n = 5, k = 2;
    Permutation(n, k);
 
    return 0;
}


Java
// Java implementation of the approach
 
class GFG
{
     
    // Function to find permutation(p) of first N
    // natural numbers such that there are exactly K
    // elements of permutation such that GCD(p[i], i)>1
    static void Permutation(int n, int k)
    {
        int[] p = new int[n + 1];
 
        // First place all the numbers
        // in their respective places
        for (int i = 1; i <= n; i++)
            p[i] = i;
 
        // Modify for first n-k integers
        for (int i = 1; i < n - k; i++)
            p[i + 1] = i;
 
        // In first index place n-k
        p[1] = n - k;
 
        // Print the permutation
        for (int i = 1; i <= n; i++)
            System.out.print(p[i] + " ");
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 5, k = 2;
        Permutation(n, k);
    }
}
 
// This code is contributed by Naman_Garg


Python3
# Python 3 implementation of the approach
 
# Function to find permutation(p)
# of first N natural numbers such that
# there are exactly K elements of
# permutation such that GCD(p[i], i)>1
def Permutation(n, k):
    p = [0 for i in range(n + 1)]
 
    # First place all the numbers
    # in their respective places
    for i in range(1, n + 1):
        p[i] = i
 
    # Modify for first n-k integers
    for i in range(1, n - k):
        p[i + 1] = i
 
    # In first index place n-k
    p[1] = n - k
 
    # Print the permutation
    for i in range(1, n + 1):
        print(p[i], end = " ")
 
# Driver code
if __name__ == '__main__':
    n = 5
    k = 2
    Permutation(n, k)
     
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
 
class GFG
{
    // Function to find permutation(p) of first N
    // natural numbers such that there are exactly K
    // elements of permutation such that GCD(p[i], i)>1
    static void Permutation(int n, int k)
    {
        int[] p = new int[n + 1];
 
        // First place all the numbers
        // in their respective places
        for (int i = 1; i <= n; i++)
            p[i] = i;
 
        // Modify for first n-k integers
        for (int i = 1; i < n - k; i++)
            p[i + 1] = i;
 
        // In first index place n-k
        p[1] = n - k;
 
        // Print the permutation
        for (int i = 1; i <= n; i++)
            Console.Write(p[i] + " ");
    }
 
    // Driver code
    static public void Main ()
    {
        int n = 5, k = 2;
        Permutation(n, k);
    }
}
 
// This code is contributed by ajit.


PHP
1
function Permutation($n, $k)
{
    $p = array();
 
    // First place all the numbers
    // in their respective places
    for ($i = 1; $i <= $n; $i++)
        $p[$i] = $i;
 
    // Modify for first n-k integers
    for ($i = 1; $i < $n - $k; $i++)
        $p[$i + 1] = $i;
 
    // In first index place n-k
    $p[1] = $n - $k;
 
    // Print the permutation
    for ($i = 1; $i <= $n; $i++)
        echo $p[$i], " ";
}
 
// Driver code
$n = 5; $k = 2;
Permutation($n, $k);
 
// This code is contributed by AnkitRai01
?>


Javascript


输出:
3 1 2 4 5