結果

問題 No.1311 Reverse Permutation Index
ユーザー tentententen
提出日時 2023-01-12 09:02:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 45 ms / 1,500 ms
コード長 3,000 bytes
コンパイル時間 4,709 ms
コンパイル使用メモリ 85,440 KB
実行使用メモリ 49,796 KB
最終ジャッジ日時 2023-08-24 18:00:39
合計ジャッジ時間 3,867 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,796 KB
testcase_01 AC 45 ms
49,360 KB
testcase_02 AC 45 ms
49,484 KB
testcase_03 AC 45 ms
49,352 KB
testcase_04 AC 45 ms
49,472 KB
testcase_05 AC 45 ms
49,488 KB
testcase_06 AC 45 ms
49,368 KB
testcase_07 AC 45 ms
49,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
    }
    
}
0