|
| 1 | +""" |
| 2 | +Consider the below problems statement. |
| 3 | +
|
| 4 | +There are 100 different types of caps each having a unique id from 1 to 100. Also, |
| 5 | +there are ‘n’ persons each having a collection of a variable number of caps. |
| 6 | +One day all of these persons decide to go in a party wearing a cap but to look unique |
| 7 | +they decided that none of them will wear the same type of cap. So, count the total number |
| 8 | +of arrangements or ways such that none of them is wearing the same type of cap. |
| 9 | +
|
| 10 | +""" |
| 11 | + |
| 12 | +# Python program to find number of ways to wear hats |
| 13 | +from collections import defaultdict |
| 14 | + |
| 15 | + |
| 16 | +class AssignCap: |
| 17 | + |
| 18 | + # Initialize variables |
| 19 | + def __init__(self): |
| 20 | + |
| 21 | + self.allmask = 0 |
| 22 | + |
| 23 | + self.total_caps = 100 |
| 24 | + |
| 25 | + self.caps = defaultdict(list) |
| 26 | + |
| 27 | + # Mask is the set of persons, i is the current cap number. |
| 28 | + def count_ways_util(self, dp, mask, cap_no): |
| 29 | + |
| 30 | + # If all persons are wearing a cap so we |
| 31 | + # are done and this is one way so return 1 |
| 32 | + if mask == self.allmask: |
| 33 | + return 1 |
| 34 | + |
| 35 | + # If not everyone is wearing a cap and also there are no more |
| 36 | + # caps left to process, so there is no way, thus return 0; |
| 37 | + if cap_no > self.total_caps: |
| 38 | + return 0 |
| 39 | + |
| 40 | + # If we have already solved this subproblem, return the answer. |
| 41 | + if dp[mask][cap_no] != -1: |
| 42 | + return dp[mask][cap_no] |
| 43 | + |
| 44 | + # Ways, when we don't include this cap in our arrangement |
| 45 | + # or solution set |
| 46 | + ways = self.count_ways_util(dp, mask, cap_no + 1) |
| 47 | + |
| 48 | + # assign ith cap one by one to all the possible persons |
| 49 | + # and recur for remaining caps. |
| 50 | + if cap_no in self.caps: |
| 51 | + |
| 52 | + for ppl in self.caps[cap_no]: |
| 53 | + |
| 54 | + # if person 'ppl' is already wearing a cap then continue |
| 55 | + if mask & (1 << ppl): |
| 56 | + continue |
| 57 | + |
| 58 | + # Else assign him this cap and recur for remaining caps with |
| 59 | + # new updated mask vector |
| 60 | + ways += self.count_ways_util(dp, mask | (1 << ppl), cap_no + 1) |
| 61 | + |
| 62 | + ways = ways % (10**9 + 7) |
| 63 | + |
| 64 | + # Save the result and return it |
| 65 | + dp[mask][cap_no] = ways |
| 66 | + |
| 67 | + return dp[mask][cap_no] |
| 68 | + |
| 69 | + def count_ways(self, N): |
| 70 | + |
| 71 | + # Reads n lines from standard input for current test case |
| 72 | + # create dictionary for cap. cap[i] = list of person having |
| 73 | + # cap no i |
| 74 | + for ppl in range(N): |
| 75 | + |
| 76 | + cap_possessed_by_person = map(int, input().strip().split()) |
| 77 | + |
| 78 | + for i in cap_possessed_by_person: |
| 79 | + |
| 80 | + self.caps[i].append(ppl) |
| 81 | + |
| 82 | + # allmask is used to check if all persons |
| 83 | + # are included or not, set all n bits as 1 |
| 84 | + self.allmask = (1 << N) - 1 |
| 85 | + |
| 86 | + # Initialize all entries in dp as -1 |
| 87 | + dp = [[-1 for j in range(self.total_caps + 1)] for i in range(2 ** N)] |
| 88 | + |
| 89 | + # Call recursive function countWaysUtil |
| 90 | + # result will be in dp[0][1] |
| 91 | + print(self.count_ways_util(dp, 0, 1,)) |
| 92 | + |
| 93 | +# Driver Program |
| 94 | + |
| 95 | + |
| 96 | +def main(): |
| 97 | + No_of_people = int(input()) # number of persons in every test case |
| 98 | + |
| 99 | + AssignCap().count_ways(No_of_people) |
0 commit comments