526. 优美的排列
为保证权益,题目请参考 526. 优美的排列(From LeetCode).
解决方案1
Python
python
# 526. 优美的排列
# https://leetcode-cn.com/problems/beautiful-arrangement/
class Solution:
def countArrangement(self, n: int) -> int:
f = [0] * (1 << n)
f[0] = 1
for mask in range(1, 1 << n):
m = bin(mask).count("1")
for j in range(1, n + 1):
if mask & (1 << (j - 1)) and (m % j == 0 or j % m == 0):
f[mask] += f[mask ^ (1 << (j - 1))]
return f[(1 << n) - 1]
if __name__ == "__main__":
solution = Solution()
print(solution.countArrangement(5))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19