Return the number of permutations of 1 to n
so that prime numbers are at prime indices (1-indexed.)
(Recall that an integer is prime if and only if it is greater than 1, and cannot be written as a product of two positive integers both smaller than it.)
Since the answer may be large, return the answer modulo 10^9 + 7
.
Example 1:
Input: n = 5 Output: 12 Explanation: For example [1,2,5,4,3] is a valid permutation, but [5,2,3,4,1] is not because the prime number 5 is at index 1.
Example 2:
Input: n = 100 Output: 682289015
Constraints:
1 <= n <= 100
program main
implicit none
integer :: i, j, k, l, m, n, p, q, r, s, t, u, v, w, x, y, z
integer :: count, modulus
logical :: is_prime(100)
! Modulus for the result
modulus = 1000000007
! Initialize the array of primes
do i = 2, 100
is_prime(i) = .true.
end do
! Mark non-primes
do i = 2, 100
if (is_prime(i)) then
j = i * i
do while (j <= 100)
is_prime(j) = .false.
j = j + i
end do
end if
end do
! Count the number of permutations
count = 0
do i = 1, 100
if (is_prime(i)) then
count = count + 1
end if
end do
! Output the result modulo the modulus
print '(I0)', count
end program main
25
```
program prime_permutation
implicit none
integer, parameter :: modulus = 1000000007
integer :: n, i, j, count
logical, allocatable :: is_prime(:)
! read input
read (*,*) n
! allocate array for prime numbers
allocate (is_prime(n))
! initialize array with true
is_prime = .true.
! mark non-prime numbers as false
do i = 2, n
if (is_prime(i)) then
do j = i * i, n, i
is_prime(j) = .false.
end do
end if
end do
! count prime permutations
count = 0
do i = 1, n
if (is_prime(i)) then
count = count + 1
end if
end do
! output result modulo 10^9 + 7
write (*,*) count
! deallocate array
deallocate (is_prime)
end program prime_permutation
```
temp.f95:1:2: 1 | ``` | 1 Error: Invalid character in name at (1) temp.f95:40:2: 40 | ``` | 1 Error: Invalid character in name at (1)
def numPrimeArrangements(n: int) -> int:
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True
prime_count = sum(1 for i in range(2, n + 1) if is_prime(i))
composite_count = n - prime_count
MOD = 10**9 + 7
res = 1
for i in range(1, prime_count + 1):
res = res * i % MOD
for i in range(1, composite_count + 1):
res = res * i % MOD
return res
The algorithm calculates the total number of primes and composites up to the given integer n. Then, it multiplies the factorials of the two counts (mod 10^9 + 7), eventually returning the result as the number of permutations of 1 to n where prime numbers are at the prime indices.
#include <vector>
using namespace std;
const int MOD = 1e9 + 7;
int numPrimeArrangements(int n) {
vector<bool> is_prime(n + 1, true);
is_prime[0] = false;
is_prime[1] = false;
for (int i = 2; i * i <= n; i++) {
if (is_prime[i]) {
for (int j = i * i; j <= n; j += i) {
is_prime[j] = false;
}
}
}
int prime_count = 0;
for (int i = 2; i <= n; i++) {
if (is_prime[i]) prime_count++;
}
int composite_count = n - prime_count;
long long res = 1;
for (int i = 1; i <= prime_count; i++) {
res = res * i % MOD;
}
for (int i = 1; i <= composite_count; i++) {
res = res * i % MOD;
}
return res;
}
The algorithm calculates the total number of primes and composites up to the given integer n. Then, it multiplies the factorials of the two counts (mod 10^9 + 7), eventually returning the result as the number of permutations of 1 to n where prime numbers are at the prime indices.