結果

問題 No.1311 Reverse Permutation Index
ユーザー wolgnikwolgnik
提出日時 2020-12-08 20:34:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 40 ms / 1,500 ms
コード長 1,571 bytes
コンパイル時間 1,607 ms
コンパイル使用メモリ 81,204 KB
実行使用メモリ 53,324 KB
最終ジャッジ日時 2023-10-18 22:14:17
合計ジャッジ時間 1,069 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import sys
input = sys.stdin.readline
N, S = map(int, input().split())

class BIT:
  def __init__(self, n):
    self.n = n
    self.data = [0] * (n + 1)
    self.el = [0] * (n + 1)
  def sum(self, i):
    s = 0
    while i > 0:
      s += self.data[i]
      i -= i & -i
    return s
  def add(self, i, x):
    self.el[i] += x
    while i <= self.n:
      self.data[i] += x
      i += i & -i
  def get(self, i, j = None):
    if j is None:
      return self.el[i]
    return self.sum(j) - self.sum(i)
  def lowerbound(self, s):
    x = 0
    y = 0
    for i in range(self.n.bit_length(), -1, -1):
      k = x + (1 << i)
      if k <= self.n and (y + self.data[k] < s):
        y += self.data[k]
        x += 1 << i
    return x + 1

class numeroBIT:
  def __init__(self, mx):
    self.mx = mx
    self.fwk = BIT(mx)
  def add(self, x): self.fwk.add(x, 1)
  def discard(self, x):
    if self.fwk.get(x): self.fwk.add(x, -1)
  def kthmax(self, k):
    n = self.fwk.sum(self.mx)
    return self.fwk.lowerbound(n - k + 1)

nfwk = numeroBIT(S)
for x in range(1, S + 1): nfwk.add(x)
f = 1
for i in range(1, S): f *= i
a = []
for i in range(S - 1, -1, -1):
  x = nfwk.kthmax(i + 1 - N // f)
  nfwk.discard(x)
  a.append(x)
  N %= f
  if i: f //= i
#print(a)
b = []
for i in range(S):
  for j in range(S):
    if i + 1 == a[j]:
      b.append(j + 1)
      break
#print(b)
for x in range(1, S + 1): nfwk.add(x)
for i in range(1, S): f *= i
res = 0
for i in range(S):
  x = b[i]
  y = nfwk.fwk.sum(x) - 1
  res += y * f
  nfwk.discard(x)
  if S - i - 1: f //= (S - i - 1)
print(res)
0