Rencontres Number (Counting partial derangements)

Given two numbers, n >= 0 and 0 <= k <= n, count the number of derangements with k fixed points.

Examples:

Input : n = 3, k = 0
Output : 2
Since k = 0, no point needs to be on its
original position. So derangements
are {3, 1, 2} and {2, 3, 1}

Input : n = 3, k = 1
Output : 3
Since k = 1, one point needs to be on its
original position. So partial derangements
are {1, 3, 2}, {3, 2, 1} and {2, 1, 3}

Input : n = 7, k = 2
Output : 924


In combinatorial mathematics, the rencontres number< or D(n, k) represents count of partial derangements.

The recurrence relation to find Rencontres Number Dn, k:

D(0, 0) = 1
D(0, 1) = 0
D(n+2, 0) = (n+1) * (D(n+1, 0) + D(n, 0))
D(n, k) = nCk * D(n-k, 0))

Given the two positive integer n and k. The task is find rencontres number D(n, k) for giver n and k.

Below is Recursive solution of this approach:

C++

// Recursive CPP program to find n-th Rencontres 
// Number
#include <bits/stdc++.h>
using namespace std;
  
// Returns value of Binomial Coefficient C(n, k)
int binomialCoeff(int n, int k)
{
    // Base Cases
    if (k == 0 || k == n)
        return 1;
  
    // Recurrence relation
    return binomialCoeff(n - 1, k - 1) +
           binomialCoeff(n - 1, k);
}
  
// Return Recontres number D(n, m)
int RencontresNumber(int n, int m)
{
    // base condition
    if (n == 0 && m == 0)
        return 1;
  
    // base condition
    if (n == 1 && m == 0)
        return 0;
  
    // base condition
    if (m == 0)
        return (n - 1) * (RencontresNumber(n - 1, 0) +
                          RencontresNumber(n - 2, 0));
  
    return binomialCoeff(n, m) * RencontresNumber(n - m, 0);
}
  
// Driver Program
int main()
{
    int n = 7, m = 2;
    cout << RencontresNumber(n, m) << endl;
    return 0;
}

Java

// Recursive Java program to find n-th Rencontres
// Number
import java.io.*;
  
class GFG {
      
    // Returns value of Binomial Coefficient
    // C(n, k)
    static int binomialCoeff(int n, int k)
    {
          
        // Base Cases
        if (k == 0 || k == n)
            return 1;
  
        // Recurrence relation
        return binomialCoeff(n - 1, k - 1) + 
                         binomialCoeff(n - 1, k);
    }
  
    // Return Recontres number D(n, m)
    static int RencontresNumber(int n, int m)
    {
          
        // base condition
        if (n == 0 && m == 0)
            return 1;
  
        // base condition
        if (n == 1 && m == 0)
            return 0;
  
        // base condition
        if (m == 0)
            return (n - 1) * (RencontresNumber(n - 1, 0
                          + RencontresNumber(n - 2, 0));
  
        return binomialCoeff(n, m) * 
                             RencontresNumber(n - m, 0);
    }
  
    // Driver Program
    public static void main(String[] args)
    {
        int n = 7, m = 2;
        System.out.println(RencontresNumber(n, m));
    }
}
  
// This code is contributed by vt_m.

Python3

# Recursive CPP program to find
# n-th Rencontres Number
  
# Returns value of Binomial Coefficient C(n, k)
def binomialCoeff(n, k):
  
    # Base Cases
    if (k == 0 or k == n):
        return 1
  
    # Recurrence relation
    return (binomialCoeff(n - 1, k - 1
          + binomialCoeff(n - 1, k))
  
# Return Recontres number D(n, m)
def RencontresNumber(n, m):
  
    # base condition
    if (n == 0 and m == 0):
        return 1
  
    # base condition
    if (n == 1 and m == 0):
        return 0
  
    # base condition
    if (m == 0):
        return ((n - 1) * (RencontresNumber(n - 1, 0
                         + RencontresNumber(n - 2, 0)))
  
    return (binomialCoeff(n, m) *
            RencontresNumber(n - m, 0))
  
# Driver Program
n = 7; m = 2
print(RencontresNumber(n, m))
  
# This code is contributed by Smitha Dinesh Semwal.

C#

// Recursive C# program to find n-th Rencontres
// Number
using System;
  
class GFG {
      
    // Returns value of Binomial Coefficient
    // C(n, k)
    static int binomialCoeff(int n, int k)
    {
          
        // Base Cases
        if (k == 0 || k == n)
            return 1;
  
        // Recurrence relation
        return binomialCoeff(n - 1, k - 1) + 
                     binomialCoeff(n - 1, k);
    }
  
    // Return Recontres number D(n, m)
    static int RencontresNumber(int n, int m)
    {
          
        // base condition
        if (n == 0 && m == 0)
            return 1;
  
        // base condition
        if (n == 1 && m == 0)
            return 0;
  
        // base condition
        if (m == 0)
            return (n - 1) * 
                (RencontresNumber(n - 1, 0) 
                + RencontresNumber(n - 2, 0));
  
        return binomialCoeff(n, m) * 
                 RencontresNumber(n - m, 0);
    }
  
    // Driver Program
    public static void Main()
    {
        int n = 7, m = 2;
          
        Console.Write(RencontresNumber(n, m));
    }
}
  
// This code is contributed by 
// Smitha Dinesh Semwal

PHP

<?php
// Recursive PHP program to
// find n-th Rencontres 
// Number
  
// Returns value of Binomial
// Coefficient C(n, k)
function binomialCoeff($n, $k)
{
      
    // Base Cases
    if ($k == 0 || $k == $n)
        return 1;
  
    // Recurrence relation
    return binomialCoeff($n - 1,$k - 1) +
              binomialCoeff($n - 1, $k);
}
  
// Return Recontres number D(n, m)
function RencontresNumber($n, $m)
{
      
    // base condition
    if ($n == 0 && $m == 0)
        return 1;
  
    // base condition
    if ($n == 1 && $m == 0)
        return 0;
  
    // base condition
    if ($m == 0)
        return ($n - 1) * (RencontresNumber($n - 1, 0) +
                           RencontresNumber($n - 2, 0));
  
    return binomialCoeff($n, $m) * 
           RencontresNumber($n - $m, 0);
}
  
    // Driver Code
    $n = 7; 
    $m = 2;
    echo RencontresNumber($n, $m),"\n";
      
// This code is contributed by ajit. 
?>

Output:

924

Below is the implementation using Dynamic Programming:

C++

// DP based CPP program to find n-th Rencontres 
// Number
#include <bits/stdc++.h>
using namespace std;
#define MAX 100
  
// Fills table C[n+1][k+1] such that C[i][j]
// represents table of binomial coefficient
// iCj
int binomialCoeff(int C[][MAX], int n, int k)
{
    // Calculate value of Binomial Coefficient
    // in bottom up manner
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= min(i, k); j++) {
  
            // Base Cases
            if (j == 0 || j == i)
                C[i][j] = 1;
  
            // Calculate value using previously
            // stored values
            else
                C[i][j] = C[i - 1][j - 1] + 
                          C[i - 1][j];
        }
    }
}
  
// Return Recontres number D(n, m)
int RencontresNumber(int C[][MAX], int n, int m)
{
    int dp[n+1][m+1] = { 0 };
  
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            if (j <= i) {
  
                // base case
                if (i == 0 && j == 0)
                    dp[i][j] = 1;
  
                // base case
                else if (i == 1 && j == 0)
                    dp[i][j] = 0;
  
                else if (j == 0)
                    dp[i][j] = (i - 1) * (dp[i - 1][0] + 
                                          dp[i - 2][0]);
                else
                    dp[i][j] = C[i][j] * dp[i - j][0];
            }
        }
    }
  
    return dp[n][m];
}
  
// Driver Program
int main()
{
    int n = 7, m = 2;
  
    int C[MAX][MAX];
    binomialCoeff(C, n, m);
  
    cout << RencontresNumber(C, n, m) << endl;
    return 0;
}

Java

// DP based Java program to find n-th Rencontres
// Number
  
import java.io.*;
  
class GFG {
  
    static int MAX = 100;
  
    // Fills table C[n+1][k+1] such that C[i][j]
    // represents table of binomial coefficient
    // iCj
    static void binomialCoeff(int C[][], int n, int k)
    {
  
        // Calculate value of Binomial Coefficient
        // in bottom up manner
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= Math.min(i, k); j++)
            {
  
                // Base Cases
                if (j == 0 || j == i)
                    C[i][j] = 1;
  
                // Calculate value using previously
                // stored values
                else
                    C[i][j] = C[i - 1][j - 1] + 
                                         C[i - 1][j];
            }
        }
    }
  
    // Return Recontres number D(n, m)
    static int RencontresNumber(int C[][], int n, int m)
    {
        int dp[][] = new int[n + 1][m + 1];
  
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= m; j++) {
                if (j <= i) {
  
                    // base case
                    if (i == 0 && j == 0)
                        dp[i][j] = 1;
  
                    // base case
                    else if (i == 1 && j == 0)
                        dp[i][j] = 0;
  
                    else if (j == 0)
                        dp[i][j] = (i - 1) * (dp[i - 1][0
                                           + dp[i - 2][0]);
                    else
                        dp[i][j] = C[i][j] * dp[i - j][0];
                }
            }
        }
  
        return dp[n][m];
    }
  
    // Driver Program
    public static void main(String[] args)
    {
        int n = 7, m = 2;
  
        int C[][] = new int[MAX][MAX];
        binomialCoeff(C, n, m);
  
        System.out.println(RencontresNumber(C, n, m));
    }
}
  
// This code is contributed by vt_m.

Python 3

# DP based Python 3 program to find n-th
# Rencontres Number
  
MAX = 100
  
# Fills table C[n+1][k+1] such that C[i][j]
# represents table of binomial coefficient
# iCj
def binomialCoeff(C, n, k) :
      
    # Calculate value of Binomial Coefficient
    # in bottom up manner
    for i in range(0, n + 1) :
        for j in range(0, min(i, k) + 1) :
              
            # Base Cases
            if (j == 0 or j == i) :
                C[i][j] = 1
  
            # Calculate value using previously
            # stored values
            else :
                C[i][j] = (C[i - 1][j - 1
                               + C[i - 1][j])
                  
  
# Return Recontres number D(n, m)
def RencontresNumber(C, n, m) :
    w, h = m+1, n+1
    dp= [[0 for x in range(w)] for y in range(h)] 
      
  
    for i in range(0, n+1) :
        for j in range(0, m+1) :
            if (j <= i) :
                  
                # base case
                if (i == 0 and j == 0) :
                    dp[i][j] = 1
  
                # base case
                elif (i == 1 and j == 0) :
                    dp[i][j] = 0
  
                elif (j == 0) :
                    dp[i][j] = ((i - 1) * 
                     (dp[i - 1][0] + dp[i - 2][0]))
                else :
                    dp[i][j] = C[i][j] * dp[i - j][0]
                      
    return dp[n][m]
  
  
# Driver Program
n = 7
m = 2
C = [[0 for x in range(MAX)] for y in range(MAX)] 
  
binomialCoeff(C, n, m)
  
print(RencontresNumber(C, n, m))
  
# This code is contributed by Nikita Tiwari.

C#

// DP based C# program 
// to find n-th Rencontres 
// Number
using System;
  
class GFG
{
    static int MAX = 100;
  
    // Fills table C[n+1][k+1]
    // such that C[i][j]
    // represents table of 
    // binomial coefficient iCj
    static void binomialCoeff(int [,]C, 
                              int n, int k)
    {
  
        // Calculate value of 
        // Binomial Coefficient
        // in bottom up manner
        for (int i = 0; i <= n; i++) 
        {
            for (int j = 0; 
                     j <= Math.Min(i, k); j++)
            {
  
                // Base Cases
                if (j == 0 || j == i)
                    C[i,j] = 1;
  
                // Calculate value using 
                // previously stored values
                else
                    C[i, j] = C[i - 1, j - 1] + 
                              C[i - 1, j];
            }
        }
    }
  
    // Return Recontres 
    // number D(n, m)
    static int RencontresNumber(int [,]C, 
                                int n, int m)
    {
        int [,]dp = new int[n + 1, 
                            m + 1];
  
        for (int i = 0; i <= n; i++) 
        {
            for (int j = 0; j <= m; j++) 
            {
                if (j <= i) 
                {
  
                    // base case
                    if (i == 0 && j == 0)
                        dp[i, j] = 1;
  
                    // base case
                    else if (i == 1 && j == 0)
                        dp[i, j] = 0;
  
                    else if (j == 0)
                        dp[i, j] = (i - 1) * 
                                   (dp[i - 1, 0] + 
                                    dp[i - 2, 0]);
                    else
                        dp[i, j] = C[i, j] *
                                  dp[i - j, 0];
                }
            }
        }
  
        return dp[n, m];
    }
  
    // Driver Code
    static public void Main ()
    {
        int n = 7, m = 2;
        int [,]C = new int[MAX, MAX];
        binomialCoeff(C, n, m);
      
        Console.WriteLine(RencontresNumber(C, n, m));
    }
}
  
// This code is contributed
// by akt_mit

Output:

924


My Personal Notes arrow_drop_up


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.