-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path526.cpp
More file actions
19 lines (19 loc) · 721 Bytes
/
526.cpp
File metadata and controls
19 lines (19 loc) · 721 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int dp(vector<vector<int>>& memo, int state, int position, int n) {
if (position == 0) return 1;
if (memo[state][position] != -1) return memo[state][position];
int res = 0;
for (int i = 1; i <= n; ++i) {
if ((((state & (1 << (i - 1))) == 0) && (((i % position) == 0) || (position % i) == 0))) {
res += dp(memo, (state | (1 << (i - 1))), position - 1, n);
}
}
return memo[state][position] = res;
}
int countArrangement(int n) {
// memo[state][position]: state => bitmask for usage
vector<vector<int>> memo(1 << n, vector<int>(n + 1, -1));
return dp(memo, 0, n, n);
}
};