Número de formas de organizar K objetos diferentes tomando N objetos a la vez

Dados dos enteros K y N , la tarea es contar el número de formas de organizar K objetos diferentes tomando N objetos a la vez. Cada arreglo contiene un objeto como máximo una vez. La respuesta puede ser muy grande, así que devuelva la respuesta módulo 10 9 + 7.
Nota: 1 <= N <= K <= 10 5
Prerrequisitos: Factorial de un número , Calcular nCr % p
Ejemplos:
 

Entrada: N = 3, K = 3 
Salida:
Si 1, 2 y 3 son los K objetos diferentes, entonces los arreglos posibles son {123, 132, 213, 231, 312, 321}.
Entrada: N = 4, K = 6 
Salida: 360
 

Enfoque: El problema se puede resolver usando Permutación y Combinación. Como N siempre es menor o igual que K , siempre existirá una respuesta mayor que 0. Cualquier objeto entre los K objetos disponibles se puede utilizar como máximo una vez en un arreglo. Entonces, necesitamos seleccionar N objetos de los K objetos disponibles para un solo arreglo. Entonces, el número total de formas de seleccionar N objetos de K objetos es K C N . Cualquier grupo seleccionado de N objetos puede entonces permutarse entre ellos de N maneras. Entonces, la respuesta al problema es:
 

(N! * KCN ) % (10 9 +7
 

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
 
#include <bits/stdc++.h>
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

<?php
// PHP implementation of the approach
$mod = (1e9 + 7);
 
// Function to return n! % p
function factorial($n,$p)
{
    $res = 1; // Initialize result
    for ($i = 2; $i <= $n; $i++)
        $res = ($res * $i) % $p;
    return $res;
}
 
// Iterative Function to calculate (x^y)%p
// in O(log y)
function 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)==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*/

Javascript

<script>
 
// javascript implementation of the approach  
var mod =parseInt(1e4 + 7);
 
// Function to return n! % p
function factorial(n , p)
{
    var res = 1; // Initialize result
    for (var i = 2; i <= n; i++)
        res = (res * i) % p;
    return res;
}
 
// Iterative Function to calculate (x^y)%p
// in O(log y)
function power(x , y , p)
{
    var 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
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
    var fac = Array.from({length: n+1}, (_, i) => 0);;
    fac[0] = 1;
    for (var i = 1; i <= n; i++)
        fac[i] = fac[i - 1] * i % p;
 
    return (fac[parseInt(n)] * modInverse(fac[parseInt(r)], p) % p
                    * modInverse(fac[parseInt(n) - parseInt(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
var N = 5, K = 8;
// Function call
document.write(countArrangements(N, K, mod));
 
 
// This code contributed by Princi Singh
</script>
Producción: 

6720

 

Complejidad de tiempo: O(n), para llenar un arreglo factorial
Espacio auxiliar: O(n), espacio extra de tamaño n usado para hacer un arreglo factorial

Publicación traducida automáticamente

Artículo escrito por rupesh_rao y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *