Given two positive numbers ‘n’ and ‘m’ (n <= m) which represent total number of items of first and second type of sets respectively. Find total number of ways to select at-least one pair by picking one item from first type(I) and another item from second type(II). In any arrangement, an item should not be common between any two pairs.
Note: Since answer can be large, output it in modulo 1000000007.
Input: 2 2 Output: 6 Explanation Let's denote the items of I type as a, b and II type as c, d i.e, Type I - a, b Type II - c, d Ways to arrange one pair at a time 1. a --- c 2. a --- d 3. b --- c 4. b --- d Ways to arrange two pairs at a time 5. a --- c, b --- d 6. a --- d, b --- c Input: 2 3 Output: 12 Input: 1 2 Output: 2
The approach is simple, we only need the combination of choosing ‘i‘ items from ‘n‘ type and ‘i‘ items from ‘m‘ type and multiply them(Rule of product) where ‘i‘ varies from 1 to ‘n‘. But we can also permute the resultant product in ‘i’ ways therefore we need to multiply with i!. After that take the sum(Rule of sum) of all resultant product to get the final answer.
Time complexity: O(m*log(mod))
Auxiliary space: O(m)
This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Number of ways to cut a stick of length N into K pieces
- Count Pairs Of Consecutive Zeros
- Number of ways to make mobile lock pattern
- Count ways to spell a number with repeated digits
- Ways to represent a number as a sum of 1's and 2's
- Ways to sum to N using array elements with repetition allowed
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j])
- Counting pairs when a person can form pair with at most one
- Ways to color a 3*N board using 4 colors
- Counting sets of 1s and 0s in a binary matrix
- Count ways to form minimum product triplets
- Ways to paint stairs with two colors such that two adjacent are not yellow
- Count ways to express 'n' as sum of odd integers
- Count ways to express even number ‘n’ as sum of even integers
- Number of ways to get even sum by choosing three numbers from 1 to N
- Ways of filling matrix such that product of all rows and all columns are equal to unity
- Different ways to sum n using numbers greater than or equal to m
- Number of ways to form a heap with n distinct integers
- Count ways to reach the nth stair using step 1, 2 or 3
- Count number of equal pairs in a string