📜  查找良好排列的数量

📅  最后修改于: 2021-04-29 12:56:29             🧑  作者: Mango

给定两个整数NK。任务是找到前N个自然数的良好排列数。如果至少存在N – K个索引i (1≤i≤N),使得P i = i ,则该置换被称为良。

例子:

方法:让我们迭代m ,它是索引的数量,以使P i不等于i 。显然, 0≤m≤k
为了计算与固定置换的数量,我们需要选择具有属性P不等于的指标-有n C M的方式来做到这一点,然后我们需要构建一个排列Q代表这种选择指数对于每个选择的索引Q, i不等于i 。具有这种性质的排列被称为紊乱,并且可以使用由于m≤4穷举搜索来计算固定大小的紊乱的次数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count of good permutations
int Permutations(int n, int k)
{
    // For m = 0, ans is 1
    int ans = 1;
  
    // If k is greater than 1
    if (k >= 2)
        ans += (n) * (n - 1) / 2;
  
    // If k is greater than 2
    if (k >= 3)
        ans += (n) * (n - 1) * (n - 2) * 2 / 6;
  
    // If k is greater than 3
    if (k >= 4)
        ans += (n) * (n - 1) * (n - 2) * (n - 3) * 9 / 24;
  
    return ans;
}
  
// Driver code
int main()
{
    int n = 5, k = 2;
    cout << Permutations(n, k);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG 
{
  
// Function to return the count of good permutations
static int Permutations(int n, int k)
{
    // For m = 0, ans is 1
    int ans = 1;
  
    // If k is greater than 1
    if (k >= 2)
        ans += (n) * (n - 1) / 2;
  
    // If k is greater than 2
    if (k >= 3)
        ans += (n) * (n - 1) * (n - 2) * 2 / 6;
  
    // If k is greater than 3
    if (k >= 4)
        ans += (n) * (n - 1) * (n - 2) * (n - 3) * 9 / 24;
  
    return ans;
}
  
// Driver code
public static void main(String[] args) 
{
    int n = 5, k = 2;
    System.out.println(Permutations(n, k));
}
}
  
// This code contributed by Rajput-Ji


Python3
# Python3 implementation of the approach 
  
# Function to return the count 
# of good permutations 
def Permutations(n, k): 
  
    # For m = 0, ans is 1 
    ans = 1
  
    # If k is greater than 1 
    if k >= 2: 
        ans += (n) * (n - 1) // 2
  
    # If k is greater than 2 
    if k >= 3:
        ans += ((n) * (n - 1) * 
                (n - 2) * 2 // 6)
  
    # If k is greater than 3 
    if k >= 4:
        ans += ((n) * (n - 1) * (n - 2) *
                      (n - 3) * 9 // 24)
  
    return ans 
  
# Driver code 
if __name__ == "__main__":
  
    n, k = 5, 2
    print(Permutations(n, k)) 
      
# This code is contributed
# by Rituraj Jain


C#
// C# implementation of the above approach.
using System;
  
class GFG 
{
  
// Function to return the count of good permutations
static int Permutations(int n, int k)
{
    // For m = 0, ans is 1
    int ans = 1;
  
    // If k is greater than 1
    if (k >= 2)
        ans += (n) * (n - 1) / 2;
  
    // If k is greater than 2
    if (k >= 3)
        ans += (n) * (n - 1) * (n - 2) * 2 / 6;
  
    // If k is greater than 3
    if (k >= 4)
        ans += (n) * (n - 1) * (n - 2) * (n - 3) * 9 / 24;
  
    return ans;
}
  
// Driver code
public static void Main() 
{
    int n = 5, k = 2;
    Console.WriteLine(Permutations(n, k));
}
}
  
/* This code contributed by PrinciRaj1992 */


PHP
= 2)
        $ans += ($n) * ($n - 1) / 2;
  
    // If k is greater than 2
    if ($k >= 3)
        $ans += ($n) * ($n - 1) * 
                       ($n - 2) * 2 / 6;
  
    // If k is greater than 3
    if ($k >= 4)
        $ans += ($n) * ($n - 1) * ($n - 2) * 
                       ($n - 3) * 9 / 24;
  
    return $ans;
}
  
// Driver code
$n = 5; $k = 2;
echo(Permutations($n, $k));
  
// This code contributed by Code_Mech.
?>


输出:
11