結果

問題 No.1311 Reverse Permutation Index
ユーザー brthyyjpbrthyyjp
提出日時 2021-07-20 21:15:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 77 ms / 1,500 ms
コード長 1,648 bytes
コンパイル時間 316 ms
コンパイル使用メモリ 86,788 KB
実行使用メモリ 71,352 KB
最終ジャッジ日時 2023-09-24 12:55:58
合計ジャッジ時間 1,696 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,020 KB
testcase_01 AC 73 ms
71,204 KB
testcase_02 AC 74 ms
71,348 KB
testcase_03 AC 77 ms
71,352 KB
testcase_04 AC 72 ms
71,336 KB
testcase_05 AC 72 ms
71,056 KB
testcase_06 AC 72 ms
71,100 KB
testcase_07 AC 73 ms
71,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BIT:
    def __init__(self, n):
        self.n = n
        self.bit = [0]*(self.n+1) # 1-indexed

    def init(self, init_val):
        for i, v in enumerate(init_val):
            self.add(i, v)

    def add(self, i, x):
        # i: 0-indexed
        i += 1 # to 1-indexed
        while i <= self.n:
            self.bit[i] += x
            i += (i & -i)

    def sum(self, i, j):
        # return sum of [i, j)
        # i, j: 0-indexed
        return self._sum(j) - self._sum(i)

    def _sum(self, i):
        # return sum of [0, i)
        # i: 0-indexed
        res = 0
        while i > 0:
            res += self.bit[i]
            i -= i & (-i)
        return res

    def lower_bound(self, x):
        s = 0
        pos = 0
        depth = self.n.bit_length()
        v = 1 << depth
        for i in range(depth, -1, -1):
            k = pos + v
            if k <= self.n and s + self.bit[k] < x:
                    s += self.bit[k]
                    pos += v
            v >>= 1
        return pos

    def __str__(self): # for debug
        arr = [self.sum(i,i+1) for i in range(self.n)]
        return str(arr)

n, s = map(int, input().split())
R = []
for d in range(1, s+1):
    r = n%d
    n = n//d
    R.append(r)
A = list(range(1, s+1))
B = []
for i in reversed(range(s)):
    k = R[i]
    B.append(A[k])
    A.pop(k)

B = [b-1 for b in B]
C = [-1]*s
for i, b in enumerate(B):
    C[b] = i

fac = [0]*21
fac[0] = 1
for i in range(1, 21):
    fac[i] = fac[i-1]*i

bit = BIT(s+2)
for c in C:
    bit.add(c, 1)

ans = 0
for i, c in enumerate(C):
    x = bit.sum(0, c)
    ans += x*fac[s-i-1]
    bit.add(c, -1)

print(ans)
0