-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathQueue.java
109 lines (96 loc) · 3.15 KB
/
Queue.java
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.stream.LongStream;
public class Queue {
private static final BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
private final int n;
private final int k;
private final int[] buckets;
private final int[] queue;
private final int[] values;
private final boolean[] skip;
private long total;
private long counter;
private static final long[] factorial = LongStream.rangeClosed(0, 13)
.map(f -> f == 0 ? 1
: LongStream.rangeClosed(1, f).reduce(1L,
(x, y) -> x * y))
.toArray();
private static final Long[][][] cache = new Long[13 + 1][13 + 1][13 + 1];
public Queue(int n, int l, int r) {
this.n = n;
this.k = l - 1;
buckets = new int[n + 1];
skip = new boolean[n + 1];
queue = new int[l + r - 2];
values = new int[n - (l + r - 2) - 1 < 0 ? 0 : n - (l + r - 2) - 1];
skip[n] = true;
if (cache[n][l][r] != null) {
total = cache[n][l][r];
} else {
select(0);
cache[n][l][r] = total;
}
}
private void select(int pos) {
if (pos == queue.length) {
for (int i = 1, j = 0; i <= n; ++i) {
if (!skip[i]) {
values[j++] = i;
}
}
counter = 0;
count(0);
int nn = queue.length;
total += counter * factorial[nn] /
(factorial[k] * factorial[nn - k]);
return;
}
int start = pos - 1 >= 0 ? queue[pos - 1] + 1 : 1;
for (int i = start; i < n; ++i) {
queue[pos] = i;
skip[i] = true;
select(pos + 1);
skip[i] = false;
}
}
private void count(int pos) {
if (pos == values.length) {
long permutations = 1;
for (int i = 0; i < buckets.length; ++i) {
permutations *= factorial[buckets[i]];
}
counter += permutations;
return;
}
for (int bucket : queue) {
if (bucket > values[pos]) {
buckets[bucket]++;
count(pos + 1);
buckets[bucket]--;
}
}
}
public static void main(String[] args) throws IOException {
int n = Integer.parseInt(reader.readLine().trim());
for (int i = 0; i < n; ++i) {
List<Integer> line = stream(reader.readLine().split(" "))
.filter(x -> !x.equals(""))
.map(Integer::parseInt)
.collect(toList());
int l = line.get(1);
int r = line.get(2);
if (l + r - 1 > 13) {
System.out.println(0);
} else {
Queue q = new Queue(line.get(0), l, r);
System.out.println(q.total);
}
}
}
}