forked from sunnyshahabuddin/Coding-Ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitmasking.cpp
More file actions
50 lines (49 loc) · 1.08 KB
/
bitmasking.cpp
File metadata and controls
50 lines (49 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std;
vector<int> capList[101];
int dp[1025][101];
int allmask;
long long int countWaysUtil(int mask, int i)
{
if (mask == allmask) return 1;
if (i > 100) return 0;
if (dp[mask][i] != -1) return dp[mask][i];
long long int ways = countWaysUtil(mask, i+1);
int size = capList[i].size();
for (int j = 0; j < size; j++)
{
if (mask & (1 << capList[i][j])) continue;
else ways += countWaysUtil(mask | (1 << capList[i][j]), i+1);
ways %= MOD;
}
return dp[mask][i] = ways;
}
void countWays(int n)
{
string temp, str;
int x;
getline(cin, str);
for (int i=0; i<n; i++)
{
getline(cin, str);
stringstream ss(str);
while (ss >> temp)
{
stringstream s;
s << temp;
s >> x;
capList[x].push_back(i);
}
}
allmask = (1 << n) - 1;
memset(dp, -1, sizeof dp);
cout << countWaysUtil(0, 1) << endl;
}
int main()
{
int n;
cin >> n;
countWays(n);
return 0;
}