結果
問題 | 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 = sizeself.tree = [0] * (self.n + 2) # Use 1-based indexingdef update(self, idx, delta=1):while idx <= self.n:self.tree[idx] += deltaidx += idx & -idxdef query(self, idx):res = 0while idx > 0:res += self.tree[idx]idx -= idx & -idxreturn resdef main():import sysinput = sys.stdin.readdata = 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 - 1fact = [1] * (max_fact + 1)for i in range(1, max_fact + 1):fact[i] = fact[i - 1] * ift = FenwickTree(n)for i in range(1, n + 1):ft.update(i)result = 0for 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 = 1result += count * factorial# Mark 'current' as usedft.update(current, -1)print(result + 1)if __name__ == "__main__":main()