結果
問題 |
No.696 square1001 and Permutation 5
|
ユーザー |
![]() |
提出日時 | 2025-06-12 18:05:29 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,085 bytes |
コンパイル時間 | 198 ms |
コンパイル使用メモリ | 82,636 KB |
実行使用メモリ | 91,536 KB |
最終ジャッジ日時 | 2025-06-12 18:07:11 |
合計ジャッジ時間 | 1,629 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 1 WA * 11 |
ソースコード
MOD = 10**9 + 7 class FenwickTree: def __init__(self, size): self.n = size self.tree = [0] * (self.n + 1) # 1-based indexing def update(self, idx, delta): 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 n = int(input()) p = list(map(int, input().split())) # Precompute factorials modulo MOD up to (n-1)! fact = [1] * n for i in range(1, n): fact[i] = fact[i-1] * i % MOD # Initialize Fenwick Tree with all elements present ft = FenwickTree(n) for i in range(1, n+1): ft.update(i, 1) result = 0 for i in range(n): x = p[i] # Number of elements less than x that are still present k = ft.query(x - 1) # Multiply by (n-1 - i)! and add to result result = (result + k * fact[n - 1 - i]) % MOD # Remove x from the Fenwick Tree ft.update(x, -1) # The rank is result + 1 (convert from 0-based to 1-based) print((result + 1) % MOD)