팩토리얼
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
// ISC
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int V = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int signal = Integer.parseInt(st.nextToken());
// D2F
int[] TP = new int[21];
int[] S = new int[21];
boolean[] visited = new boolean[21];
TP[0] = 1;
for (int i = 1; i <= V; i++) {
TP[i] = TP[i-1]*i;
}
// OEC + OP
if (signal == 1) {
int order = Integer.parseInt(st.nextToken());
for (int i = 1; i <= V; i++) {
for (int j = 1, counter = 1; j <= V; j++) {
if (visited[j])
continue;
if (order <= counter * TP[V-i]) {
order -= ((counter-1) * (TP[V-i]));
S[i] = j;
visited[j] = true;
break;
}
counter++;
}
}
for (int i = 1; i <= V; i++) {
System.out.print(S[i] + " ");
}
} else {
int order = 1;
for (int i = 1; i <= V; i++) {
S[i] = Integer.parseInt(st.nextToken());
int counter = 0;
for (int j = 1; j < S[i]; j++) {
if (!visited[j])
counter++;
}
order += counter * TP[V-i];
visited[S[i]] = true;
}
System.out.println(order);
}
}
}
'Hard deck > reindexing d3' 카테고리의 다른 글
078 : 부녀회장이 될 테야 (0) | 2023.06.29 |
---|---|
080 : 조약돌 꺼내기 (0) | 2023.06.28 |
079 : 다리 놓기 (0) | 2023.06.28 |
077 : 이항계수 구하기 2 (0) | 2023.06.28 |
019 : Quick sort / K번째 수 구하기 (0) | 2023.06.28 |