結果

問題 No.1311 Reverse Permutation Index
コンテスト
ユーザー Kude
提出日時 2020-12-08 01:28:34
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 37 ms / 1,500 ms
コード長 637 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 326 ms
コンパイル使用メモリ 84,352 KB
実行使用メモリ 62,976 KB
最終ジャッジ日時 2026-04-06 07:57:30
合計ジャッジ時間 951 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 6
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

n, s = map(int, input().split())

def fact(n):
    return 1 if n <= 1 else n * fact(n - 1)

p = []
used = [False] * s
for i in range(s):
    for x in range(s):
        if used[x]:
            continue
        if n >= fact(s - 1 - i):
            n -= fact(s - 1 - i)
        else:
            p.append(x)
            used[x] = True
            break
q = [None] * s
for i in range(s):
    q[p[i]] = i

ans = 0
used = [False] * s
for i in range(s):
    for x in range(s):
        if used[x]:
            continue
        if x != q[i]:
            ans += fact(s - 1 - i)
        else:
            used[x] = True
            break
print(ans)
0