Number of ways to arrange K different objects taking N objects at a time | C ++ implementation of the approach ; Function to return n ! % p ; ll res = 1 ; Initialize result ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Returns n ^ ( - 1 ) mod p ; Returns nCr % p using Fermat 's little theorem. ; Base case ; Fill factorial array so that we can find all factorial of r , n and n - r ; Function to return the number of ways to arrange K different objects taking N objects at a time ; Drivers Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE #define mod  (ll)(1e9 + 7) NEW_LINE ll factorial ( ll n , ll p ) { for ( int i = 2 ; i <= n ; i ++ ) res = ( res * i ) % p ; return res ; } ll power ( ll x , ll y , ll p ) { while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } ll modInverse ( ll n , ll p ) { return power ( n , p - 2 , p ) ; } ll nCrModP ( ll n , ll r , ll p ) { if ( r == 0 ) return 1 ; 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 ; } ll countArrangements ( ll n , ll k , ll p ) { return ( factorial ( n , p ) * nCrModP ( k , n , p ) ) % p ; } int main ( ) { ll N = 5 , K = 8 ; cout << countArrangements ( N , K , mod ) ; return 0 ; }