Обновлено: 2024-03-12
1 мин

Содержание

class Solution:
    def countArrangement(self, n: int) -> int:
        def dfs(i):
            nonlocal res, n
            if i == n + 1:
                res += 1
                return
            for j in match[i]:
                if not vis[j]:
                    vis[j] = True
                    dfs(i + 1)
                    vis[j] = False

        res = 0
        vis = [False] * (n + 1)
        match = defaultdict(list)
        for i in range(1, n + 1):
            for j in range(1, n + 1):
                if j % i == 0 or i % j == 0:
                    match[i].append(j)

        dfs(1)
        return res
Ранее
Далее