An application on Bertrand’s ballot theorem
Last Updated :
24 Jun, 2021
Given the number of ‘X’ and ‘Y’ in a string which consists of characters from the set {‘X’, ‘Y’}, the task is to find the number of permutations which satisfy the condition where every sub-string of the permutation starting from the first character has count(‘X’) > count(‘Y’). Print the answer modulo 1000000007. Note that the number of ‘X’ will always be greater than number of ‘Y’ in the given string.
Examples:
Input: X = 2, Y = 1
Output: 1
The possible distributions are “XYX”, “XXY” and “YXX”
Distribution 1: Till 1st index (X = 1, Y = 0), till 2nd index (X = 1, Y = 1) and till 3rd index (X = 2, Y = 1). Number of X isn’t always greater than Y so this distribution is not valid.
Distribution 2: 1st index (X = 1, Y = 0), 2nd index (X = 2, Y = 0) and 3rd index (X = 2, Y = 1). This is a valid distribution as X is always greater than Y.
Distribution 3: 1st index (X = 0, Y = 1), 2nd index (X = 1, Y = 1) and 3rd index (X = 2, Y = 1). Invalid distribution.
Input: X = 3, Y = 1
Output: 1
Approach: This type of problem can be solved by Bertrand’s Ballot Theorem. Out of all possible distributions, the probability that X always remains in the lead is (X – Y) / (X + Y). And the total number of distributions are (X + Y)! / (X! * Y!). Hence the distributions in which X always has the lead are (X + Y – 1)! * (X – Y) / (X! * Y!).
For calculating (X + Y – 1)! * (X – Y) / (X! * Y!), we need to calculate multiplicative modular inverse of X! and thus similarly for Y. Since 1000000007 is a prime, according to Fermat’s Little Theorem : (1 / X!) % 1000000007 = ((X!)(1000000007 – 2)) % 1000000007. Similar result also holds for Y.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll mod = 1000000007;
ll arr[1000001] = { 0 };
void cal_factorial()
{
arr[0] = 1;
for ( int i = 1; i <= 1000000; i++) {
arr[i] = (arr[i - 1] * i) % mod;
}
}
ll mod_exponent(ll num, ll p)
{
if (p == 0)
return 1;
if (p & 1) {
return ((num % mod)
* (mod_exponent((num * num) % mod, p / 2))
% mod)
% mod;
}
else if (!(p & 1))
return (mod_exponent((num * num) % mod, p / 2))
% mod;
}
ll getCount(ll x, ll y)
{
ll ans = arr[x + y - 1];
ans *= mod_exponent(arr[x], mod - 2);
ans %= mod;
ans *= mod_exponent(arr[y], mod - 2);
ans %= mod;
ans *= (x - y);
ans %= mod;
return ans;
}
int main()
{
cal_factorial();
ll x = 3, y = 1;
cout << getCount(x, y);
return 0;
}
|
Java
class GFG
{
static long mod = 1000000007 ;
static long [] arr = new long [ 1000001 ];
static void cal_factorial()
{
arr[ 0 ] = 1 ;
for ( int i = 1 ; i <= 1000000 ; i++)
{
arr[i] = ((arr[i - 1 ] * i) % mod);
}
}
static long mod_exponent( long num, long p)
{
if (p == 0 )
return 1 ;
if ((p & 1 ) != 0 )
{
return ((num % mod)
* (mod_exponent((num * num) % mod, p / 2 ))
% mod)
% mod;
}
else
return (mod_exponent((num * num) % mod, p / 2 ))
% mod;
}
static long getCount( long x, long y)
{
long ans = arr[( int )x + ( int )y - 1 ];
ans *= mod_exponent(arr[( int )x], mod - 2 );
ans %= mod;
ans *= mod_exponent(arr[( int )y], mod - 2 );
ans %= mod;
ans *= (x - y);
ans %= mod;
return ans;
}
public static void main (String[] args)
{
cal_factorial();
long x = 3 , y = 1 ;
System.out.println(getCount(x, y));
}
}
|
Python3
mod = 1000000007
arr = [ 0 ] * ( 1000001 )
def cal_factorial():
arr[ 0 ] = 1
for i in range ( 1 , 1000001 ):
arr[i] = (arr[i - 1 ] * i) % mod
def mod_exponent(num, p):
if (p = = 0 ):
return 1
if (p & 1 ) :
return ((num % mod) * (mod_exponent((num * num) %
mod, p / / 2 )) % mod) % mod
elif ( not (p & 1 )):
return (mod_exponent((num * num) %
mod, p / / 2 )) % mod
def getCount(x, y):
ans = arr[x + y - 1 ]
ans * = mod_exponent(arr[x], mod - 2 )
ans % = mod
ans * = mod_exponent(arr[y], mod - 2 )
ans % = mod
ans * = (x - y)
ans % = mod
return ans
if __name__ = = '__main__' :
cal_factorial()
x = 3
y = 1
print (getCount(x, y))
|
C#
class GFG
{
static long mod = 1000000007;
static long [] arr= new long [1000001];
static void cal_factorial()
{
arr[0] = 1;
for ( long i = 1; i <= 1000000; i++)
{
arr[i] = (arr[i - 1] * i) % mod;
}
}
static long mod_exponent( long num, long p)
{
if (p == 0)
return 1;
if ((p & 1)!=0)
{
return ((num % mod)
* (mod_exponent((num * num) % mod, p / 2))
% mod)
% mod;
}
else
return (mod_exponent((num * num) % mod, p / 2))
% mod;
}
static long getCount( long x, long y)
{
long ans = arr[x + y - 1];
ans *= mod_exponent(arr[x], mod - 2);
ans %= mod;
ans *= mod_exponent(arr[y], mod - 2);
ans %= mod;
ans *= (x - y);
ans %= mod;
return ans;
}
static void Main()
{
cal_factorial();
long x = 3, y = 1;
System.Console.WriteLine(getCount(x, y));
}
}
|
PHP
<?php
$mod = 1000000007;
$arr = array_fill (0, 10001, 0);
function cal_factorial()
{
global $arr , $mod ;
$arr [0] = 1;
for ( $i = 1; $i <= 10000; $i ++)
{
$arr [ $i ] = ( $arr [ $i - 1] * $i ) % $mod ;
}
}
function mod_exponent( $num , $p )
{
global $mod ;
if ( $p == 0)
return 1;
if (( $p & 1))
{
return (( $num % $mod )* (mod_exponent(( $num * $num ) %
$mod , $p / 2)) % $mod ) % $mod ;
}
else if (!( $p & 1))
return (mod_exponent(( $num * $num ) %
$mod , $p / 2))% $mod ;
}
function getCount( $x , $y )
{
global $arr , $mod ;
$ans = $arr [ $x + $y - 1];
$ans *= mod_exponent( $arr [ $x ], $mod - 2);
$ans %= $mod ;
$ans *= mod_exponent( $arr [ $y ], $mod - 2);
$ans %= $mod ;
$ans *= ( $x - $y );
$ans %= $mod ;
return $ans ;
}
cal_factorial();
$x = 3;
$y = 1;
print (getCount( $x , $y ));
?>
|
Javascript
<script>
let mod = 1000000007;
let arr = new Array(1000001);
arr.fill(0);
function cal_factorial()
{
arr[0] = 1;
for (let i = 1; i <= 1000000; i++)
{
arr[i] = ((arr[i - 1] * i) % mod);
}
}
function mod_exponent(num, p)
{
if (p != 0)
return 1;
if ((p & 1) != 0)
{
return ((num % mod) * (mod_exponent(
(num * num) % mod,
parseInt(p / 2, 10))) % mod) % mod;
}
else
return (mod_exponent((num * num) % mod,
parseInt(p / 2, 10))) % mod;
}
function getCount(x, y)
{
let ans = arr[x + y - 1];
ans *= 0*mod_exponent(arr[x], mod - 2);
ans++;
ans %= mod;
ans *= mod_exponent(arr[y], mod - 2);
ans %= mod;
ans *= (x - y);
ans %= mod;
return ans;
}
cal_factorial();
let x = 3, y = 1;
document.write(getCount(x, y));
</script>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...