結果
問題 | No.696 square1001 and Permutation 5 |
ユーザー |
![]() |
提出日時 | 2025-03-31 17:21:18 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,200 bytes |
コンパイル時間 | 247 ms |
コンパイル使用メモリ | 82,540 KB |
実行使用メモリ | 277,276 KB |
最終ジャッジ日時 | 2025-03-31 17:22:51 |
合計ジャッジ時間 | 23,147 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 2 |
other | TLE * 1 -- * 11 |
ソースコード
def main():import sysinput = sys.stdin.readdata = input().split()n = int(data[0])p = list(map(int, data[1:n+1]))class BIT:def __init__(self, size):self.size = sizeself.tree = [0] * (self.size + 1)def update(self, idx, delta):while idx <= self.size:self.tree[idx] += deltaidx += idx & -idxdef query(self, idx):res = 0while idx > 0:res += self.tree[idx]idx -= idx & -idxreturn res# Precompute (n-1)! by multiplying from 1 to n-1current_fact = 1for i in range(1, n):current_fact *= ibit = BIT(n)for i in range(1, n+1):bit.update(i, 1)ans = 0for i in range(n):x = p[i]sum_k = bit.query(x - 1)ans += sum_k * current_factbit.update(x, -1)remaining = n - i - 1if remaining > 0:current_fact = current_fact // remainingelse:current_fact = 0print(ans + 1)if __name__ == "__main__":main()