結果

問題 No.1311 Reverse Permutation Index
ユーザー H3PO4H3PO4
提出日時 2020-12-08 10:09:20
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 31 ms / 1,500 ms
コード長 619 bytes
コンパイル時間 96 ms
コンパイル使用メモリ 11,916 KB
実行使用メモリ 10,072 KB
最終ジャッジ日時 2023-10-17 17:01:32
合計ジャッジ時間 898 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
10,068 KB
testcase_01 AC 29 ms
10,072 KB
testcase_02 AC 31 ms
10,072 KB
testcase_03 AC 29 ms
10,072 KB
testcase_04 AC 28 ms
10,068 KB
testcase_05 AC 28 ms
10,068 KB
testcase_06 AC 28 ms
10,072 KB
testcase_07 AC 28 ms
10,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N, S = map(int, input().split())
factorial = [1]
for i in range(1, S + 1):
    factorial.append(factorial[-1] * i)


def f(x):
    """x番目の順列が何か求める。O(S^2)"""
    cur = x
    numbers = list(range(1, S + 1))
    res = []
    for i in range(1, S + 1):
        d, cur = divmod(cur, factorial[S - i])
        res.append(numbers[d])
        numbers.pop(d)
    return res


perm = f(N)
inv_perm = [0] * S
for i, x in enumerate(perm, 1):
    inv_perm[x - 1] = i

# bisect
l, r = -1, factorial[S]
while r - l > 1:
    m = (r + l) // 2
    if f(m) < inv_perm:
        l = m
    else:
        r = m
print(r)
0