📌  相关文章
📜  一次将n个事物与k个事物组合在一起的排列

📅  最后修改于: 2021-04-22 09:29:50             🧑  作者: Mango

给定n,r和K。任务是找到n  不同的事情r  在这样的时间k  特定的事物总是在一起发生的。
例子:

Input : n = 8, r = 5, k = 2
Output : 960

Input : n = 6, r = 2, k = 2
Output : 2

方法:

  1. 一捆k  特定的事物可以(r-k + 1)的方式放置在r个地方。
  2. 捆绑中的k个特定事物可以自己排列成k个!方法。
  3. 现在(n – k)个事物将被安排在(r – k)个地方$n-k_{P_r-k}$  方法。

因此,使用计数的基本原理,所需的排列数将是:

下面是上述方法的实现:

C++
// CPP program to find the number of permutations of
// n different things taken r at a time
// with k things grouped together
 
#include 
using namespace std;
 
// Function to find factorial
// of a number
int factorial(int n)
{
    int fact = 1;
 
    for (int i = 2; i <= n; i++)
        fact = fact * i;
 
    return fact;
}
 
// Function to calculate p(n, r)
int npr(int n, int r)
{
    int pnr = factorial(n) / factorial(n - r);
 
    return pnr;
}
 
// Function to find the number of permutations of
// n different things taken r at a time
// with k things grouped together
int countPermutations(int n, int r, int k)
{
    return factorial(k) * (r - k + 1) * npr(n - k, r - k);
}
 
// Driver code
int main()
{
    int n = 8;
    int r = 5;
    int k = 2;
 
    cout << countPermutations(n, r, k);
 
    return 0;
}


Java
// Java program to find the number of permutations of
// n different things taken r at a time
// with k things grouped together
 
class GFG{
// Function to find factorial
// of a number
static int factorial(int n)
{
    int fact = 1;
 
    for (int i = 2; i <= n; i++)
        fact = fact * i;
 
    return fact;
}
 
// Function to calculate p(n, r)
static int npr(int n, int r)
{
    int pnr = factorial(n) / factorial(n - r);
 
    return pnr;
}
 
// Function to find the number of permutations of
// n different things taken r at a time
// with k things grouped together
static int countPermutations(int n, int r, int k)
{
    return factorial(k) * (r - k + 1) * npr(n - k, r - k);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 8;
    int r = 5;
    int k = 2;
 
    System.out.println(countPermutations(n, r, k));
}
}
// this code is contributed by mits


Python3
# Python3 program to find the number of permutations of
# n different things taken r at a time
# with k things grouped together
 
# def to find factorial
# of a number
def factorial(n):
  
    fact = 1;
 
    for i in range(2,n+1):
        fact = fact * i;
 
    return fact;
  
 
# def to calculate p(n, r)
def npr(n, r):
  
    pnr = factorial(n) / factorial(n - r);
 
    return pnr;
  
 
# def to find the number of permutations of
# n different things taken r at a time
# with k things grouped together
def countPermutations(n, r, k):
  
    return int(factorial(k) * (r - k + 1) * npr(n - k, r - k));
  
 
# Driver code
n = 8;
r = 5;
k = 2;
 
print(countPermutations(n, r, k));
     
# this code is contributed by mits


C#
// C# program to find the number of
// permutations of n different things
// taken r at a time with k things
// grouped together
using System;
 
class GFG
{
     
// Function to find factorial
// of a number
static int factorial(int n)
{
    int fact = 1;
 
    for (int i = 2; i <= n; i++)
        fact = fact * i;
 
    return fact;
}
 
// Function to calculate p(n, r)
static int npr(int n, int r)
{
    int pnr = factorial(n) /
              factorial(n - r);
 
    return pnr;
}
 
// Function to find the number of
// permutations of n different
// things taken r at a time with
// k things grouped together
static int countPermutations(int n,
                             int r, int k)
{
    return factorial(k) * (r - k + 1) *
                    npr(n - k, r - k);
}
 
// Driver code
static void Main()
{
    int n = 8;
    int r = 5;
    int k = 2;
 
    Console.WriteLine(countPermutations(n, r, k));
}
}
 
// This code is contributed by mits


PHP


Javascript


输出:
960