結果

問題 No.1311 Reverse Permutation Index
ユーザー trineutron
提出日時 2020-12-08 05:37:29
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 31 ms / 1,500 ms
コード長 688 bytes
コンパイル時間 131 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-09-17 14:22:21
合計ジャッジ時間 755 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import factorial

def calc_order(a):
    n = len(a)
    if n == 0:
        return 0
    b = a[1:]
    for i in range(n - 1):
        if b[i] > a[0]:
            b[i] -= 1
    return a[0] * factorial(n - 1) + calc_order(b)

def calc_rev(a):
    n = len(a)
    res = [0] * n
    for i in range(n):
        res[a[i]] = i
    return res

def reproduce(k, n):
    a = [0] * n
    used = [False] * n
    for i in range(n):
        while used[:a[i]].count(False) <= k // factorial(n - i - 1):
            a[i] += 1
        a[i] -= 1
        k %= factorial(n - i - 1)
        used[a[i]] = True
    return a

n, s = map(int, input().split())
print(calc_order(calc_rev(reproduce(n, s))))
0