結果

問題 No.1311 Reverse Permutation Index
ユーザー tentententen
提出日時 2023-03-23 08:41:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 55 ms / 1,500 ms
コード長 2,950 bytes
コンパイル時間 5,160 ms
コンパイル使用メモリ 97,084 KB
実行使用メモリ 53,444 KB
最終ジャッジ日時 2023-10-18 19:23:00
合計ジャッジ時間 4,708 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
53,440 KB
testcase_01 AC 54 ms
53,436 KB
testcase_02 AC 54 ms
53,424 KB
testcase_03 AC 54 ms
53,440 KB
testcase_04 AC 53 ms
53,440 KB
testcase_05 AC 55 ms
53,436 KB
testcase_06 AC 53 ms
53,432 KB
testcase_07 AC 53 ms
53,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        long n = sc.nextLong();
        int s = sc.nextInt();
        int[] base = new int[s];
        getArray(0, base, new boolean[s], n);
        int[] reverse = new int[s];
        getReverse(base, reverse);
        System.out.println(getIdx(0, reverse, new boolean[s]));
    }
    
    static void getArray(int idx, int[] arr, boolean[] used, long value) {
        long f = fact(arr.length - idx - 1);
        for (int i = 0; i < used.length; i++) {
            if (used[i]) {
                continue;
            }
            if (value >= f) {
                value -= f;
            } else {
                used[i] = true;
                arr[idx] = i;
                getArray(idx + 1, arr, used, value);
            }
        }
    }
    
    static void getReverse(int[] base, int[] reverse) {
        for (int i = 0; i < base.length; i++) {
            reverse[base[i]] = i;
        }
    }
    
    static long getIdx(int idx, int[] arr, boolean[] used) {
        long f = fact(arr.length - idx - 1);
        long ans = 0;
        for (int i = 0; i < arr.length; i++) {
            if (used[i]) {
                continue;
            }
            if (i == arr[idx]) {
                used[i] = true;
                ans += getIdx(idx + 1, arr, used);
                break;
            } else {
                ans += f;
            }
        }
        return ans;
    }
    
    static long fact(int x) {
        long ans = 1;
        for (int i = 2; i <= x; i++) {
            ans *= i;
        }
        return ans;
    }
}
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