# D124 1175. Prime Arrangements

## 题目链接

1175. Prime Arrangements

## 最终代码

``````<?php
class Solution {

/**
* @param Integer \$n
* @return Integer
*/
function numPrimeArrangements(\$n) {
\$primes = [1 =>2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97];
\$nonPrimes = array_values(array_diff(range(1,100), \$primes));
\$primesBeforeIndex = array_flip(\$primes);
\$nonPrimesBeforeIndex = array_flip(\$nonPrimes);
if(isset(\$primesBeforeIndex[\$n])){
\$primeAmount = \$primesBeforeIndex[\$n];
\$nonePrimeAmount = \$n - \$primeAmount;
}
else{
\$nonePrimeAmount = \$nonPrimesBeforeIndex[\$n]+1;
\$primeAmount = \$n - \$nonePrimeAmount;
}
\$primesPermutation = array_reduce(array_values(range(1,\$primeAmount)), function(\$carry, \$item){
\$carry *= \$item;
return \$carry%(pow(10,9)+7);
},1);
\$primesPermutation = max(1, \$primesPermutation);
\$nonPrimePermutation = array_reduce(array_values(range(1,\$nonePrimeAmount)), function(\$carry, \$item){
\$carry *= \$item;
return \$carry%(pow(10,9)+7);
},1);
\$nonPrimePermutation = max(1, \$nonPrimePermutation);
return (\$primesPermutation * \$nonPrimePermutation)%(pow(10,9)+7);
}
}