結果

問題 No.1311 Reverse Permutation Index
ユーザー Kude
提出日時 2020-12-08 01:28:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 43 ms / 1,500 ms
コード長 637 bytes
コンパイル時間 139 ms
コンパイル使用メモリ 81,992 KB
実行使用メモリ 63,700 KB
最終ジャッジ日時 2024-09-17 14:14:38
合計ジャッジ時間 992 ms
ジャッジサーバーID
(参考情報)
judge2 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

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