[Java] Leetcode 204 Sieve of Eratosthenes + Factorial Static DP Beats 100%
\nclass Solution {\n    public static final int MOD = (int)1e9 + 7;\n    public static Map<Integer, Long> dp = new HashMap<>();\n    \n    public int numPrimeAr
```\nclass Solution {\n    public static final int MOD = (int)1e9 + 7;\n    public static Map<Integer, Long> dp = new HashMap<>();\n    \n    public int numPrimeArrangements(int n) {\n        int cnt = countPrimes(n);\n        return (int)(factorial(cnt) * factorial(n - cnt) % MOD);\n    }\n    \n    // Leetcode 204: Sieve of Eratosthenes\n    private int countPrimes(int n) {\n        boolean[] prime = new boolean[n + 1];\n        int cnt = 1;\n        Arrays.fill(prime, true);\n        for (int p = 2; p * p <= n; p++) {\n            if (prime[p]) {\n                for (int i = p * p; i <= n; i += p) {\n                    if (prime[i]) {\n                        prime[i] = false;\n                        cnt++;\n                    }\n                }\n            }\n        }\n        return n - cnt;\n    }\n    \n    private long factorial(int n) {\n        if (n < 2) {\n            return 1;\n        }\n        if (dp.containsKey(n)) {\n            return dp.get(n);\n        }\n        long res = (long)n * factorial(n - 1) % MOD;\n        dp.put(n, res);\n        return res;\n    }\n}\n```
[]