-
Notifications
You must be signed in to change notification settings - Fork 0
/
Combinatorics13.java
53 lines (48 loc) · 1.38 KB
/
Combinatorics13.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
package ru.itmo.chizhikov;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Combinatorics13 {
static long fact(int n) {
long f = 1;
if (n == 0) {
return 1;
} else {
for (int i = 1; i <= n; i++) {
f = f * i;
}
return f;
}
}
static int[] getNumber(long k, int n) {
int old[] = new int[n + 1];
int[] perm = new int[n + 1];
for (int i = 1; i <= n; i++) {
long nf = fact(n - i);
int cur = 0;
long pref = k / nf;
k = k % nf;
for (int j = 1; j <= n; j++) {
if (old[j] == 0) {
cur++;
if (cur == pref + 1) {
perm[i] = j;
old[j] = 1;
}
}
}
}
return perm;
}
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(new File("num2perm.in"));
FileWriter fileWriter = new FileWriter(new File("num2perm.out"));
int n = scanner.nextInt();
long k = scanner.nextLong();
int[] res = getNumber(k, n);
for (int i = 1; i <= n; i++)
fileWriter.write(res[i] + " ");
fileWriter.flush();
}
}