-
Notifications
You must be signed in to change notification settings - Fork 5
/
code(O - Matching).cpp
63 lines (52 loc) · 1.24 KB
/
code(O - Matching).cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define endl '\n'
#define pb push_back
#define mp make_pair
#define sp(x) fixed<<setprecision(x)
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
ll mod = 1e9+7;
ll dp[22][(1<<22)];
ll fun( vector< vector<ll>> &comp, ll N, ll i, ll woman_bit)
{
//Base case
if(i == N+1)
{
if(woman_bit == 0)
return 1;
return 0;
}
//Lookup
if(dp[i][woman_bit] != -1)
return dp[i][woman_bit];
//Rec case
ll ans = 0;
for(ll w=0;w<N;w++)
{
bool is_avail = ( ((1<<w) & woman_bit) != 0) ? 1 : 0;
if(is_avail && comp[i][w+1])
{
ans += fun( comp,N,i+1, (woman_bit ^ (1<<w)));
ans %= mod;
}
}
return dp[i][woman_bit] = ans;
}
int main()
{
fast_io;
ll N;
cin>>N;
vector< vector<ll>> comp(N+1, vector<ll>(N+1));
//int comp[22][22];
for(ll i=1;i<=N;i++)
{
for(ll j=1;j<=N;j++)
cin>>comp[i][j];
}
memset(dp,-1,sizeof(dp));
cout<<fun(comp,N,1,(1<<N)-1)<<endl;
return 0;
}