C++ 0 ms solution w/ optimized sieve
Algorithm Explanation: \nAssume we have up to n.\nThere are k primes <= n , so k prime slots. We have k! ways to assign all prime numbers.\nWe have another (n-k
Algorithm Explanation: \nAssume we have up to `n`.\nThere are `k` primes `<= n` , so `k` prime slots. We have `k!` ways to assign all prime numbers.\nWe have another `(n-k)` slots remaining. We have `(n-k)!` ways of assigning the rest of the numbers.\nIn total, there are `(n-k)! * k!` possible permutations.\n\nThe number of primes (`k`) is calculated using sieve of E. (Can\'t remember his name). To optimize, we skip all even numbers, and start at `3`. For count we just add `2` after everything.\n\nRun-time is `O(n)`. We need to go through half of the numbers using seive, and need to multiply `n` numbers together.\n\n```\nclass Solution {\npublic:\n    int numPrimeArrangements(int n) {\n        bitset<100> primes;\n        primes.set();\n        for(int i = 3, stop = (n+1)>>1; i <= stop; i += 2)\n            if(primes[i]) \n                for(int j = 2; i*j <= n; ++j) primes[i*j] = false;\n\t\t\t\t\n        int64_t k = 1, res = 1;\n        for(int i = 3; i <= n; i += 2) k += primes[i];\n        for(int i = 2, stop = n-k; i <= stop; ++i) res = (res * i) % MOD;\n        for(int i = 2; i <= k; ++i) res = (res * i) % MOD;\n        return res;\n    }\n    static const int MOD = 1e9+7;\n};\n```
[]