1175. Prime Arrangements

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

Содержание

LeetCode problem 1175

class Solution:
    def numPrimeArrangements(self, n: int) -> int:
        def count(n):
            cnt = 0
            primes = [True] * (n + 1)
            for i in range(2, n + 1):
                if primes[i]:
                    cnt += 1
                    for j in range(i + i, n + 1, i):
                        primes[j] = False
            return cnt

        cnt = count(n)
        res = factorial(cnt) * factorial(n - cnt)
        return res % (10**9 + 7)