結果
問題 |
No.696 square1001 and Permutation 5
|
ユーザー |
![]() |
提出日時 | 2025-03-20 21:09:24 |
言語 | PyPy3 (7.3.15) |
結果 |
MLE
|
実行時間 | - |
コード長 | 1,336 bytes |
コンパイル時間 | 142 ms |
コンパイル使用メモリ | 82,496 KB |
実行使用メモリ | 849,124 KB |
最終ジャッジ日時 | 2025-03-20 21:10:05 |
合計ジャッジ時間 | 3,705 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 2 |
other | MLE * 1 -- * 11 |
ソースコード
class FenwickTree: def __init__(self, size): self.n = size self.tree = [0] * (self.n + 2) # Use 1-based indexing def update(self, idx, delta=1): while idx <= self.n: self.tree[idx] += delta idx += idx & -idx def query(self, idx): res = 0 while idx > 0: res += self.tree[idx] idx -= idx & -idx return res def main(): import sys input = sys.stdin.read data = input().split() n = int(data[0]) p = list(map(int, data[1:n+1])) if n == 0: print(1) return # Precompute factorials up to (n-1)! max_fact = n - 1 fact = [1] * (max_fact + 1) for i in range(1, max_fact + 1): fact[i] = fact[i - 1] * i ft = FenwickTree(n) for i in range(1, n + 1): ft.update(i) result = 0 for i in range(n): current = p[i] # Number of available elements less than 'current' count = ft.query(current - 1) # Factorial term (n - i - 1)! if (n - i - 1) >= 0: factorial = fact[n - i - 1] else: factorial = 1 result += count * factorial # Mark 'current' as used ft.update(current, -1) print(result + 1) if __name__ == "__main__": main()