forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathI.java
More file actions
73 lines (63 loc) · 2.18 KB
/
I.java
File metadata and controls
73 lines (63 loc) · 2.18 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
71
72
73
import java.io.*;
import java.math.*;
import java.util.*;
public class I {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(new File("superinv.in"));
PrintWriter pw = new PrintWriter(new File("superinv.out"));
int n = sc.nextInt();
int k = sc.nextInt();
int[] b = new int[k];
for(int i = 0; i < k; ++i) {
b[i] = sc.nextInt();
}
BigInteger[][] c = new BigInteger[211][211];
for(int i = 0; i < 211; ++i) {
c[i][0] = BigInteger.ONE;
for(int j = 1; j <= i; ++j)
c[i][j] = c[i-1][j-1].add(c[i-1][j]);
for(int j = i+1; j < 211; ++j)
c[i][j] = BigInteger.ZERO;
}
BigInteger[] l = new BigInteger[211];
l[0] = BigInteger.ONE;
l[1] = BigInteger.ONE;
for(int i = 1; i < 210; ++i) {
l[i+1] = l[i].add(l[i-1].multiply(BigInteger.valueOf(i)));
}
BigInteger res = BigInteger.ZERO;
for(int r = 0; r <= Math.min(n-k, k); ++r) {
boolean good = true;
int cnt = k - r;
// Map to 1..cnt
int[] cur = new int[cnt];
boolean[] used = new boolean[cnt];
for(int i = 0; i < cnt; ++i)
used[i] = false;
for(int turn = 0; turn < cnt; ++turn) {
int nn = -1;
for(int i = 0; i < cnt; ++i)
if (!used[i])
if (nn == -1 || b[nn] > b[i]) {
nn = i;
}
used[nn] = true;
cur[nn] = turn;
}
// System.out.println(Arrays.toString(cur));
for(int i = 0; i < cnt; ++i) {
if (cur[cur[i]] != i) {
good = false;
break;
}
}
// System.out.println("good = " + good);
if (good) {
res = res.add(c[n-k][r].multiply(l[n-k-r]));
}
}
pw.println(res);
sc.close();
pw.close();
}
}