結果

問題 No.1311 Reverse Permutation Index
ユーザー LyricalMaestroLyricalMaestro
提出日時 2024-01-08 00:47:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 41 ms / 1,500 ms
コード長 1,193 bytes
コンパイル時間 254 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 59,092 KB
最終ジャッジ日時 2024-01-08 00:47:52
合計ジャッジ時間 1,321 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,460 KB
testcase_01 AC 39 ms
59,092 KB
testcase_02 AC 40 ms
57,044 KB
testcase_03 AC 38 ms
53,460 KB
testcase_04 AC 37 ms
53,460 KB
testcase_05 AC 38 ms
53,460 KB
testcase_06 AC 41 ms
59,092 KB
testcase_07 AC 41 ms
53,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/1311


def main():
    N, S = map(int, input().split())

    def factorial(n):
        a = 1
        for j in range(1, n + 1):
            a *= j
        return a

    # N番目の順列を求める
    used = [False] * (S + 1)
    array = [0] * S
    a = 0
    for ind in range(S):
        for i in range(1, S + 1):
            if used[i]:
                continue
            f = factorial(S - ind - 1)
            if a + f <= N:
                a += f
            else:
                array[ind] = i
                used[i] = True
                break

    # 逆置換を計算する
    inv_array = [0] * S
    for i in range(S):
        for j in range(S):
            if array[j] == i + 1:
                inv_array[i] = j + 1
                break
    
    # 逆置換が何番目の純烈かを求める
    used = [False] * (S + 1)
    a = 0
    for i in range(S):
        j = inv_array[i]
        f = factorial(S - i - 1)
        non_used_num = 0
        for k in range(1, j):
            if not used[k]:
                non_used_num += 1
        a += non_used_num * f
        used[j] = True
    print(a)




if __name__ == "__main__":
    main()
0