-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrangeprimes.py
More file actions
70 lines (38 loc) · 1.08 KB
/
Copy patharrangeprimes.py
File metadata and controls
70 lines (38 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Arrange Primes
# Given an integer N. Print the count of permutations for the numbers from 1 to N, considering that prime numbers should be placed at positions with prime indices (1 - based indexing). As the result might be a large number, print the output % 1e9 + 7.
# Input Format
# The first and only line of input contains an integer N.
# Output Format
# Print the count of permutations.
# Constraints
# 1 ≤ N ≤ 100
# Example
# Input
# 8
# Output
# 576
# Explanation
# Self Explanatory
mod=int(1e9+7)
def fact(n):
res=1
for i in range(2,n+1):
res=(res*i)%mod
return res
def sieve(n):
isprime=[True]*(n+1)
isprime[0]=isprime[1]=False
for i in range(2,int(n**0.5)+1):
if isprime[i]:
for j in range(i*i,n+1,i):
isprime[j]=False
return isprime
def arrange(n):
isprime=sieve(n)
primecnt=sum(1 for i in range(1,n+1)if isprime[i])
nonprimecnt=n-primecnt
primefact=fact(primecnt)
nonprimefact=fact(nonprimecnt)
return (primefact*nonprimefact)%mod
n=int(input())
print(arrange(n))