forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1434.cpp
26 lines (24 loc) · 735 Bytes
/
1434.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
class Solution {
public:
int numberWays(vector<vector<int>>& hats) {
int n = hats.size(), mod = 1000000007;
int f[2][1024] = {};
vector<int> a[41];
for (int i = 0; i < hats.size(); i++) {
for (int hat: hats[i]) a[hat].push_back(i);
}
f[0][0] = 1;
for (int i = 1; i <= 40; i++) {
for (int j = 0; j < (1 << n); j++) {
f[i % 2][j] = f[(i - 1) % 2][j];
for (int x : a[i]) {
if (((j >> x) & 1) != 0) {
f[i % 2][j] += f[(i - 1) % 2][j ^ (1 << x)];
f[i % 2][j] %= mod;
}
}
}
}
return f[0][(1 << n) - 1];
}
};