Skip to content
the poor coder

Sherlock and Permutations

Poor Man
Published
Updated
Reading time
1 min read

Watson asks Sherlock:
Given a string S of N 0's and M 1's, how many unique permutations of this string start with 1?

Help Sherlock by printing the answer modulo (109+7).

Input Format
First line contains T, the number of test cases.
Each test case consists of N and M separated by a space.

Output Format
For each test case, print the answer modulo (109+7).

Constraints
1 ≤ T ≤ 200
1 ≤ N,M ≤ 1000

Sample Input

Text
2
1 1
2 3

Sample Output

Text
1
6

Explanation
Test1: Out of all unique permutations ie. 01 and 10, only second permutation satisfies. Hence, output is 1.
Test2: Out of all unique permutations ie. 00111 01011 01101 01110 10011 10101 10110 11001 11010 11100, only 10011 10101 10110 11001 11010 11100 satisfy. Hence, output is 6.

Solution in Python#

Python
#!/bin/python3

import os
import sys


def facdiv(m,n):
    p = 1
    for i in range(m+1,n+1):
        p*=i
    return p

def solve(n, m):
    m-=1
    nums=[n,m]
    nums.sort()
    n,m = nums
    answer = facdiv(m,m+n) //facdiv(0,n)
    return int(answer%(10**9+7))

for _ in range(int(input())):
    n,m = list(map(int,input().split()))
    print(solve(n,m))

Keep going

2020

Hackerrank - Bus Station Solution

There are groups of friends, and each group is numbered from 1 to . The th group contains people. They live near a bus stop, and only a single bus operates on this route. An empty bus arrives at the bus stop and all the…

2020

Hackerrank - K Candy Store Solution

Jim enters a candy shop which has N different types of candies, each candy is of the same price. Jim has enough money to buy K candies. In how many different ways can he purchase K candies if there are infinite candies…

2020

Hackerrank - Diwali Lights Solution

On the eve of Diwali, Hari is decorating his house with a serial light bulb set. The serial light bulb set has N bulbs placed sequentially on a string which is programmed to change patterns every second. If at least one…

ESC