結果

問題 No.1311 Reverse Permutation Index
ユーザー tentententen
提出日時 2024-03-13 15:16:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 60 ms / 1,500 ms
コード長 2,633 bytes
コンパイル時間 6,130 ms
コンパイル使用メモリ 78,256 KB
実行使用メモリ 53,980 KB
最終ジャッジ日時 2024-03-13 15:16:43
合計ジャッジ時間 7,918 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
53,956 KB
testcase_01 AC 55 ms
53,976 KB
testcase_02 AC 55 ms
53,980 KB
testcase_03 AC 56 ms
53,976 KB
testcase_04 AC 56 ms
53,972 KB
testcase_05 AC 58 ms
53,968 KB
testcase_06 AC 60 ms
53,932 KB
testcase_07 AC 57 ms
53,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static long[] facts;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        long x = sc.nextLong();
        int n = sc.nextInt();
        facts = new long[n];
        facts[0] = 1;
        for (int i = 1; i < n; i++) {
            facts[i] = facts[i - 1] * i;
        }
        System.out.println(getCount(getReverse(getArray(x, n))));
    }
    
    static int[] getArray(long value, int size) {
        int[] ans = new int[size];
        boolean[] used = new boolean[size];
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if (used[j]) {
                    continue;
                }
                if (value < facts[size - i - 1]) {
                    used[j] = true;
                    ans[i] = j;
                    break;
                }
                value -= facts[size - i - 1];
            }
        }
        return ans;
    }
    
    static int[] getReverse(int[] arr) {
        int[] ans = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            ans[arr[i]] = i;
        }
        return ans;
    }
    
    static long getCount(int[] arr) {
        long ans = 0;
        boolean[] used = new boolean[arr.length];
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                if (used[j]) {
                    continue;
                }
                if (arr[i] == j) {
                    used[j] = true;
                    break;
                }
                ans += facts[arr.length - i - 1];
            }
        }
        return ans;
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0