結果
| 問題 |
No.1311 Reverse Permutation Index
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2023-01-12 09:02:11 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 61 ms / 1,500 ms |
| コード長 | 3,000 bytes |
| コンパイル時間 | 6,004 ms |
| コンパイル使用メモリ | 91,380 KB |
| 実行使用メモリ | 36,936 KB |
| 最終ジャッジ日時 | 2024-12-23 02:29:19 |
| 合計ジャッジ時間 | 4,094 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 6 |
ソースコード
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class Main {
static long[] fact;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
long n = sc.nextLong();
int length = sc.nextInt();
fact = new long[length + 1];
fact[0] = 1;
for (int i = 1; i <= length; i++) {
fact[i] = fact[i - 1] * i;
}
int[] order = new int[length];
getOrder(length - 1, n, order, new boolean[length]);
int[] reverse = new int[length];
for (int i = length - 1; i >= 0; i--) {
reverse[length - order[i] - 1] = length - i - 1;
}
System.out.println(getNumber(length - 1, reverse, new boolean[length]));
}
static void getOrder(int idx, long value, int[] order, boolean[] used) {
if (idx < 0) {
return;
}
long count = value / fact[idx];
for (int i = 0; i < used.length; i++) {
if (used[i]) {
continue;
}
if (count == 0) {
used[i] = true;
order[idx] = i;
getOrder(idx - 1, value % fact[idx], order, used);
return;
}
count--;
}
}
static long getNumber(int idx, int[] order, boolean[] used) {
if (idx < 0) {
return 0;
}
int count = 0;
for (int i = 0; i < used.length; i++) {
if (used[i]) {
continue;
}
if (i == order[idx]) {
used[i] = true;
return fact[idx] * count + getNumber(idx - 1, order, used);
}
count++;
}
return 0;
}
}
class Utilities {
static String arrayToLineString(Object[] arr) {
return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
}
static String arrayToLineString(int[] arr) {
return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
}
}
class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
StringBuilder sb = new StringBuilder();
public Scanner() throws Exception {
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
public long nextLong() throws Exception {
return Long.parseLong(next());
}
public double nextDouble() throws Exception {
return Double.parseDouble(next());
}
public int[] nextIntArray() throws Exception {
return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
}
public String next() throws Exception {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten