给定n和m ,任务是找到n个不同事物的排列数目,一次将它们全部吸收,以使m个特定事物永远不会在一起。
例子:
Input : 7, 3
Output : 420
Input : 9, 2
Output : 282240
方法:
公式的推导–
使用n个不同的对象一次取走的所有布置的总数=
当m个特定的事物总是在一起时,一次包含全部n个不同事物的排列数是
因此, 不同的事物一次全部占据, 特殊的事情永远不会融合在一起–
Permutations = n! - (n-m+1)! × m!
以下是上述方法的Python实现–
C++
#include
using namespace std;
int factorial(int n)
{
int fact = 1;
for (int i = 2; i <= n; i++)
fact = fact * i ;
return (fact);
}
int result(int n, int m)
{
return(factorial(n) -
factorial(n - m + 1) *
factorial(m));
}
// Driver Code
int main()
{
cout(result(5, 3));
}
// This code is contributed by Mohit Kumar
Java
class GFG
{
static int factorial(int n)
{
int fact = 1;
for (int i = 2; i <= n; i++)
fact = fact * i ;
return (fact);
}
static int result(int n, int m)
{
return(factorial(n) -
factorial(n - m + 1) *
factorial(m));
}
// Driver Code
public static void main(String args[])
{
System.out.println(result(5, 3));
}
}
// This code is contributed by Arnab Kundu
Python3
def factorial(n):
fact = 1;
for i in range(2, n + 1):
fact = fact * i
return (fact)
def result(n, m):
return(factorial(n) - factorial(n - m + 1) * factorial(m))
# driver code
print(result(5, 3))
C#
using System;
class GFG
{
static int factorial(int n)
{
int fact = 1;
for (int i = 2; i <= n; i++)
fact = fact * i ;
return (fact);
}
static int result(int n, int m)
{
return(factorial(n) -
factorial(n - m + 1) *
factorial(m));
}
// Driver Code
public static void Main()
{
Console.WriteLine(result(5, 3));
}
}
// This code is contributed by AnkitRai01
Javascript
输出 :
84