結果
| 問題 |
No.696 square1001 and Permutation 5
|
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 20:47:14 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,419 bytes |
| コンパイル時間 | 186 ms |
| コンパイル使用メモリ | 81,792 KB |
| 実行使用メモリ | 96,036 KB |
| 最終ジャッジ日時 | 2025-06-12 20:48:15 |
| 合計ジャッジ時間 | 1,965 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 1 WA * 11 |
ソースコード
MOD = 10**9 + 7
def main():
import sys
input = sys.stdin.read().split()
idx = 0
N = int(input[idx])
idx += 1
p = list(map(int, input[idx:idx+N]))
# Precompute factorials modulo MOD up to N
fact = [1] * (N + 1)
for i in range(1, N + 1):
fact[i] = fact[i - 1] * i % MOD
# Fenwick Tree implementation
class FenwickTree:
def __init__(self, size):
self.size = size
self.tree = [0] * (size + 1)
def update(self, index, delta=1):
while index <= self.size:
self.tree[index] += delta
index += index & -index
def query(self, index):
res = 0
while index > 0:
res += self.tree[index]
index -= index & -index
return res
ft = FenwickTree(N)
for num in p:
ft.update(num)
rank = 0
for i in range(N):
current = p[i]
# Query the number of elements less than current
c = ft.query(current - 1)
remaining = N - i - 1
if remaining >= 0:
fact_val = fact[remaining] if remaining < N else 0
term = c * fact_val % MOD
rank = (rank + term) % MOD
# Remove current from the Fenwick Tree
ft.update(current, -1)
print((rank + 1) % MOD)
if __name__ == '__main__':
main()
gew1fw