📜  计算特殊排列的数量

📅  最后修改于: 2021-04-22 07:14:30             🧑  作者: Mango

给定两个正整数nk ,任务是计算特殊排列的数量。特殊置换P定义为前n个自然数的置换,其中至少存在(n-k)个索引,使得P i = i
先决条件:混乱

例子:

方法:让函数f x表示特殊置换的数量,其中恰好存在x个索引,使得P i = i 。因此,所需的答案将是:

现在,可以通过从n处选择x个索引来计算f x ,其中P i = i并计算其他索引的(n – i)个其他索引的排列数,因为它们的i不等于i,然后将结果乘以n C x ,因为可以有不同的方法从n中选择x个索引。

下面是上述方法的实现:

C++
// C++ program to count the number
// of required permutations
#include 
using namespace std;
#define ll long long int
  
// Function to return the number of ways
// to choose r objects out of n objects
int nCr(int n, int r)
{
    int ans = 1;
    if (r > n - r)
        r = n - r;
    for (int i = 0; i < r; i++) {
        ans *= (n - i);
        ans /= (i + 1);
    }
    return ans;
}
  
// Function to return the number
// of derangements of n
int countDerangements(int n)
{
    int der[n + 1];
  
    der[0] = 1;
    der[1] = 0;
    der[2] = 1;
  
    for (int i = 3; i <= n; i++)
        der[i] = (i - 1) * (der[i - 1]
                         + der[i - 2]);
    return der[n];
}
  
// Function to return the required
// number of permutations
ll countPermutations(int n, int k)
{
    ll ans = 0;
    for (int i = n - k; i <= n; i++) {
  
        // Ways to choose i indices from n indices
        int ways = nCr(n, i);
  
        // Dearangements of (n - i) indices
        ans += ways * countDerangements(n - i);
    }
    return ans;
}
  
// Driver Code to test above functions
int main()
{
    int n = 5, k = 3;
    cout << countPermutations(n, k);
    return 0;
}


Java
// Java program to count the number 
// of required permutations
      
public class GFG{
      
    // Function to return the number of ways 
    // to choose r objects out of n objects 
    static int nCr(int n, int r) 
    { 
        int ans = 1; 
        if (r > n - r) 
            r = n - r; 
        for (int i = 0; i < r; i++) { 
            ans *= (n - i); 
            ans /= (i + 1); 
        } 
        return ans; 
    } 
      
    // Function to return the number 
    // of derangements of n 
    static int countDerangements(int n) 
    { 
        int der[] = new int[ n + 3]; 
      
        der[0] = 1; 
        der[1] = 0; 
        der[2] = 1; 
      
        for (int i = 3; i <= n; i++) 
            der[i] = (i - 1) * (der[i - 1] 
                            + der[i - 2]); 
        return der[n]; 
    } 
      
    // Function to return the required 
    // number of permutations 
    static int countPermutations(int n, int k) 
    { 
        int ans = 0; 
        for (int i = n - k; i <= n; i++) { 
      
            // Ways to choose i indices from n indices 
            int ways = nCr(n, i); 
      
            // Dearangements of (n - i) indices 
            //System.out.println(ans) ;
            ans += (ways * countDerangements(n- i)); 
        } 
        return ans; 
    } 
      
    // Driver Code to test above functions 
    public static void main(String []args)
    { 
        int n = 5, k = 3; 
        System.out.println(countPermutations(n, k)) ;
    } 
    // This code is contributed by Ryuga
}


Python3
# Python 3 program to count the number
# of required premutation
  
# function to return the number of ways 
# to chooser objects out of n objects
def nCr(n, r):
    ans = 1
    if r > n - r:
        r = n - r
    for i in range(r):
        ans *= (n - i)
        ans /= (i + 1)
    return ans
  
# function to return the number of
# degrangemnets of n
def countDerangements(n):
    der = [0 for i in range(n + 3)]
      
    der[0] = 1
    der[1] = 0
    der[2] = 1
      
    for i in range(3, n + 1):
        der[i] = (i - 1) * (der[i - 1] + 
                            der[i - 2])
          
    return der[n]
  
# function to return the required 
# number of premutations
def countPermutations(n, k):
    ans = 0
    for i in range(n - k, n + 1):
          
        # ways to chose i indices 
        # from n indices
        ways = nCr(n, i)
          
        # Dearrangements of (n-i) indices
        ans += ways * countDerangements(n - i)
    return ans
  
# Driver Code
n, k = 5, 3
  
print(countPermutations(n, k))
  
# This code is contributed by
# Mohit kumar 29 (IIIT gwalior)


C#
// C# program to count the number 
// of required permutations
using System;    
public class GFG{
       
    // Function to return the number of ways 
    // to choose r objects out of n objects 
    static int nCr(int n, int r) 
    { 
        int ans = 1; 
        if (r > n - r) 
            r = n - r; 
        for (int i = 0; i < r; i++) { 
            ans *= (n - i); 
            ans /= (i + 1); 
        } 
        return ans; 
    } 
       
    // Function to return the number 
    // of derangements of n 
    static int countDerangements(int n) 
    { 
        int []der = new int[ n + 3]; 
       
        der[0] = 1; 
        der[1] = 0; 
        der[2] = 1; 
       
        for (int i = 3; i <= n; i++) 
            der[i] = (i - 1) * (der[i - 1] 
                            + der[i - 2]); 
        return der[n]; 
    } 
       
    // Function to return the required 
    // number of permutations 
    static int countPermutations(int n, int k) 
    { 
        int ans = 0; 
        for (int i = n - k; i <= n; i++) { 
       
            // Ways to choose i indices from n indices 
            int ways = nCr(n, i); 
       
            // Dearangements of (n - i) indices 
            //System.out.println(ans) ;
            ans += (ways * countDerangements(n- i)); 
        } 
        return ans; 
    } 
       
    // Driver Code to test above functions 
    public static void Main()
    { 
        int n = 5, k = 3; 
        Console.WriteLine(countPermutations(n, k)) ;
    } 
}
// This code is contributed by 29AjayKumar


PHP
 $n - $r)
        $r = $n - $r;
    for ($i = 0; $i < $r; $i++)
    {
        $ans *= ($n - $i);
        $ans /= ($i + 1);
    }
    return $ans;
}
  
// Function to return the number
// of derangements of n
function countDerangements($n)
{
    $der = array($n + 1);
  
    $der[0] = 1;
    $der[1] = 0;
    $der[2] = 1;
  
    for ($i = 3; $i <= $n; $i++)
        $der[$i] = ($i - 1) * 
                   ($der[$i - 1] + 
                    $der[$i - 2]);
    return $der[$n];
}
  
// Function to return the required
// number of permutations
function countPermutations($n, $k)
{
    $ans = 0;
    for ($i = $n - $k; $i <= $n; $i++)
    {
  
        // Ways to choose i indices
        // from n indices
        $ways = nCr($n, $i);
  
        // Dearangements of (n - i) indices
        $ans += $ways * countDerangements($n - $i);
    }
    return $ans;
}
  
// Driver Code
$n = 5;
$k = 3;
echo (countPermutations($n, $k));
  
// This code is contributed 
// by Shivi_Aggarwal 
?>


输出:
31