给定两个整数K和N ,任务是计算一次放置N个对象的K个不同对象的排列方式的数量。每个布置最多包含一个对象。答案可能非常大,因此请以10 9 + 7为模返回答案。
注意: 1 <= N <= K <= 10 5 。
先决条件:阶乘,计算nCr%p
例子:
Input : N = 3, K = 3
Output : 6
If 1, 2 and 3 be the K different objects, then the possible arrangements are {123, 132, 213, 231, 312, 321}.
Input : N = 4, K = 6
Output : 360
方法:可以使用置换和组合解决问题。由于N总是小于或等于K ,所以答案总是大于0。在一个排列中, K个可用对象中的任何对象最多可以使用一次。因此,我们需要为单个排列从K个可用对象中选择N个对象。因此,从K个对象中选择N个对象的方法总数为K C N。然后,可以以N种方式在它们之间对N个对象的任何选定组进行置换。因此,问题的答案是:
(N! * KCN) % (109 +7)
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
#define ll long long
#define mod (ll)(1e9 + 7)
// Function to return n! % p
ll factorial(ll n, ll p)
{
ll res = 1; // Initialize result
for (int i = 2; i <= n; i++)
res = (res * i) % p;
return res;
}
// Iterative Function to calculate (x^y)%p
// in O(log y)
ll power(ll x, ll y, ll p)
{
ll res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0) {
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
ll modInverse(ll n, ll p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's little
// theorem.
ll nCrModP(ll n, ll r, ll p)
{
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
ll fac[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[n] * modInverse(fac[r], p) % p
* modInverse(fac[n - r], p) % p) % p;
}
// Function to return the number of ways to
// arrange K different objects taking N objects
// at a time
ll countArrangements(ll n, ll k, ll p)
{
return (factorial(n, p) * nCrModP(k, n, p)) % p;
}
// Drivers Code
int main()
{
ll N = 5, K = 8;
// Function call
cout << countArrangements(N, K, mod);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
static long mod =(long) (1e9 + 7);
// Function to return n! % p
static long factorial(long n, long p)
{
long res = 1; // Initialize result
for (int i = 2; i <= n; i++)
res = (res * i) % p;
return res;
}
// Iterative Function to calculate (x^y)%p
// in O(log y)
static long power(long x, long y, long p)
{
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1)==1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static long modInverse(long n, long p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's little
// theorem.
static long nCrModP(long n, long r, long p)
{
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
long fac[] = new long[(int)n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[(int)n] * modInverse(fac[(int)r], p) % p
* modInverse(fac[(int)n - (int)r], p) % p) % p;
}
// Function to return the number of ways to
// arrange K different objects taking N objects
// at a time
static long countArrangements(long n, long k, long p)
{
return (factorial(n, p) * nCrModP(k, n, p)) % p;
}
// Driver Code
public static void main(String[] args)
{
long N = 5, K = 8;
// Function call
System.out.println(countArrangements(N, K, mod));
}
}
/* This code is contributed by PrinciRaj1992 */
Python3
# Python3 implementation of the approach
mod = 10**9 + 7
# Function to return n! % p
def factorial(n, p):
res = 1 # Initialize result
for i in range(2, n + 1):
res = (res * i) % p
return res
# Iterative Function to calculate
# (x^y)%p in O(log y)
def power(x, y, p):
res = 1 # Initialize result
x = x % p # Update x if it is
# more than or equal to p
while (y > 0):
# If y is odd,
# multiply x with result
if (y & 1):
res = (res * x) % p
# y must be even now
y = y >> 1 # y = y/2
x = (x * x) % p
return res
# Returns n^(-1) mod p
def modInverse(n, p):
return power(n, p - 2, p)
# Returns nCr % p using Fermat's little
# theorem.
def nCrModP(n, r, p):
# Base case
if (r == 0):
return 1
# Fifactorial array so that we
# can find afactorial of r, n
# and n-r
fac = [0 for i in range(n + 1)]
fac[0] = 1
for i in range(1, n + 1):
fac[i] = fac[i - 1] * i % p
return (fac[n] * modInverse(fac[r], p) % p *
modInverse(fac[n - r], p) % p) % p
# Function to return the number of ways
# to arrange K different objects taking
# N objects at a time
def countArrangements(n, k, p):
return (factorial(n, p) *
nCrModP(k, n, p)) % p
# Drivers Code
N = 5
K = 8
# Function call
print(countArrangements(N, K, mod))
# This code is contributed by mohit kumar
C#
// C# implementation of the approach
using System;
class GFG
{
static long mod =(long) (1e9 + 7);
// Function to return n! % p
static long factorial(long n, long p)
{
long res = 1; // Initialize result
for (int i = 2; i <= n; i++)
res = (res * i) % p;
return res;
}
// Iterative Function to calculate (x^y)%p
// in O(log y)
static long power(long x, long y, long p)
{
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1)==1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static long modInverse(long n, long p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's little
// theorem.
static long nCrModP(long n, long r, long p)
{
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
long[] fac = new long[(int)n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[(int)n] * modInverse(fac[(int)r], p) % p
* modInverse(fac[(int)n - (int)r], p) % p) % p;
}
// Function to return the number of ways to
// arrange K different objects taking N objects
// at a time
static long countArrangements(long n, long k, long p)
{
return (factorial(n, p) * nCrModP(k, n, p)) % p;
}
// Driver Code
public static void Main()
{
long N = 5, K = 8;
// Function call
Console.WriteLine(countArrangements(N, K, mod));
}
}
/* This code is contributed by Code_Mech */
PHP
0)
{
// If y is odd, multiply x with result
if (($y & 1)==1)
$res = ($res * $x) % $p;
// y must be even now
$y = $y >> 1; // y = y/2
$x = ($x * $x) % $p;
}
return $res;
}
// Returns n^(-1) mod p
function modInverse($n, $p)
{
return power($n, $p - 2, $p);
}
// Returns nCr % p using Fermat's little
// theorem.
function nCrModP($n, $r, $p)
{
// Base case
if ($r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
$fac= array((int)$n + 1);
$fac[0] = 1;
for ($i = 1; $i <= $n; $i++)
$fac[$i] = $fac[$i - 1] * $i % $p;
return ($fac[(int)$n] * modInverse($fac[(int)$r], $p) % $p
* modInverse($fac[(int)$n - (int)$r], $p) % $p) % $p;
}
// Function to return the number of ways to
// arrange K different objects taking N objects
// at a time
function countArrangements($n, $k, $p)
{
return (factorial($n, $p) * nCrModP($k, $n, $p)) % $p;
}
// Driver Code
{
$N = 5; $K = 8;
// Function call
echo(countArrangements($N, $K, $mod));
}
/* This code is contributed by Code_Mech*/
输出:
6720
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。