코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int N;
static long count = 0, k;
static boolean[] visit;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
visit = new boolean[N + 1];
StringTokenizer st = new StringTokenizer(br.readLine());
int problemNum = Integer.parseInt(st.nextToken());
if (problemNum == 1) {
System.out.println(ansOfProblemOne(st));
} else {
System.out.println(ansOfProblemTwo(st));
}
}
private static String ansOfProblemOne(StringTokenizer st) {
k = Long.parseLong(st.nextToken());
int[] ans = new int[N];
long toAdd = getFactorial(N - 1);
createSet(0, ans);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < N; i++) {
sb.append(ans[i]).append(" ");
}
return sb.toString();
}
private static void createSet(int depth, int[] ans) {
if (depth == N) return;
long toAdd = getFactorial(N - 1 - depth);
for (int i = 1; i <= N; i++) {
if (visit[i]) continue;
count += toAdd;
if (count < k) continue;
ans[depth] = i;
visit[i] = true;
count -= toAdd;
createSet(depth + 1, ans);
break;
}
}
private static String ansOfProblemTwo(StringTokenizer st) {
int[] set = new int[N];
long count = 1;
for (int i = 0; i < N; i++) {
set[i] = Integer.parseInt(st.nextToken());
}
for (int i = 0; i < N; i++) {
long toAdd = getFactorial(N - 1 - i);
visit[set[i]] = true;
for (int num = 1; num <= set[i]; num++) {
if (visit[num]) continue;
count += toAdd;
}
}
return Long.toString(count);
}
private static long getFactorial(int n) {
long ans = 1;
for (int i = 1; i <= n; i++) {
ans *= i;
}
return ans;
}
}