defisprime(x): for i inrange(2, x): if x % i == 0: returnFalse elif i * i > x: returnTrue returnTrue for i inrange(2, n + 1): if isprime(i): p += 1 else: np += 1 ans = 1 for i in chain(range(1, p + 1), range(1, np + 1)): ans = (ans * i) % mod return ans
// Code language: C++ classSolution { public: intnumPrimeArrangements(int n){ int p = 0, np = 1; long ans = 1, mod = 1000000007; for (int i = 2; i <= n; ++i) { if (isprime(i)) ++p; else ++np; } for (int i = 2; i <= p; ++i) ans = (ans * i) % mod; for (int i = 2; i <= np; ++i) ans = (ans * i) % mod; return (int) ans; }
boolisprime(int x){ for (int i = 2; i * i <= x; ++i) { if (x % i == 0) returnfalse; } returntrue; } };
// Code language: C# publicclassSolution { publicintNumPrimeArrangements(int n) { int p = 0, np = 1; long ans = 1, mod = 1000000007; for (int i = 2; i <= n; ++i) { if (isprime(i)) ++p; else ++np; } for (int i = 2; i <= p; ++i) ans = (ans * i) % mod; for (int i = 2; i <= np; ++i) ans = (ans * i) % mod; return (int) ans; }
boolisprime(int x) { for (int i = 2; i * i <= x; ++i) { if (x % i == 0) returnfalse; } returntrue; } }
// Code language: JavaScript /** * @param {number} n * @return {number} */ var numPrimeArrangements = function (n) { /** * 判断给定值是否是质数 * @param {number} x * @returns {number} */ var isprime = function (x) { for (let i = 2; i * i <= x; ++i) { if (x % i == 0) returnfalse; } returntrue; };
let p = 0, np = 1; for (let i = 2; i <= n; ++i) { if (isprime(i)) ++p; else ++np; } var ans = 1; const mod = 1000000007; for (let i = 2; i <= p; ++i) ans = (ans * i) % mod; for (let i = 2; i <= np; ++i) ans = (ans * i) % mod; return ans; };