結果

問題 No.1311 Reverse Permutation Index
ユーザー shotoyooshotoyoo
提出日時 2020-12-08 00:24:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 35 ms / 1,500 ms
コード長 1,041 bytes
コンパイル時間 185 ms
コンパイル使用メモリ 81,944 KB
実行使用メモリ 53,676 KB
最終ジャッジ日時 2023-10-17 16:47:47
合計ジャッジ時間 1,078 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")

n,s = map(int, input().split())
g = [1]
v = 1
for i in range(1,s+2):
    v *= i
    g.append(v)
def ps(n,v):
    """長さnの順列で、辞書順v番目
    """
    seen = [False]*n
    ans = []
    for i in range(n):
        num = v//g[n-i-1]
        count = 0
        for j in range(n):
            if seen[j]:
                continue
            if count==num:
                break
            if not seen[j]:
                count += 1
        ans.append(j)
        seen[j] = True
        v = v%g[n-i-1]
    return ans
def index(ps):
    """順列psの辞書順
    """
    n = len(ps)
    v = 0
    seen = [False] * n
    for i in range(n):
        num = 0
        for j in range(ps[i]):
            if not seen[j]:
                num += 1
        v += num * g[n-i-1]
        seen[ps[i]] = True
    return v
p = ps(s,n)
pp = [None]*s
for i,v in enumerate(p):
    pp[v] = i
print(index(pp))
0