-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsorted_or_not.py
53 lines (43 loc) · 1.05 KB
/
sorted_or_not.py
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
recruited = []
P = 0
def factorial(N):
prod = 1
for i in range(2, N+1):
prod *= i
return(prod)
def recruitMinions(A, K, index):
global P
global recruited
if(index == 0):
P = 0
recruited = []
if(K == 0):
P += 1
else:
for i in range(len(A)):
if(index != 0):
if(int(A[i]) >= recruited[index-1]):
recruited.append(int(A[i]))
recruitMinions(A[0:i]+A[i+1:len(A)], K-1, index+1)
recruited.pop()
else:
recruited.append(int(A[i]))
recruitMinions(A[0:i]+A[i+1:len(A)], K-1, index+1)
recruited.pop()
def gcd(a, b):
if(a == 0):
return(b)
return(gcd(b % a, a))
def probablity(P2, Q2):
x = gcd(P2, Q2)
P = P2/x
Q = Q2/x
Q_inv = 1/(Q % (10e9+7))
return((P*Q_inv) % (10e9+7))
a = input().split(" ")
N = int(a[0])
K = int(a[1])
A = input().split(" ")
Q = factorial(N)/factorial(N-K)
recruitMinions(A, K, 0)
print(probablity(P, Q))