結果
| 問題 |
No.696 square1001 and Permutation 5
|
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 18:05:14 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,192 bytes |
| コンパイル時間 | 200 ms |
| コンパイル使用メモリ | 82,716 KB |
| 実行使用メモリ | 96,252 KB |
| 最終ジャッジ日時 | 2025-06-12 18:06:38 |
| 合計ジャッジ時間 | 1,630 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 1 WA * 11 |
ソースコード
MOD = 10**9 + 7
def main():
import sys
input = sys.stdin.read().split()
n = int(input[0])
p = list(map(int, input[1:n+1]))
# Precompute factorial mod MOD
fact_mod = [1] * (n + 1)
for i in range(1, n + 1):
fact_mod[i] = (fact_mod[i-1] * i) % MOD
class FenwickTree:
def __init__(self, size):
self.n = size
self.tree = [0] * (self.n + 1)
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
ft = FenwickTree(n)
# Initialize Fenwick Tree with 1s
for i in range(1, n+1):
ft.update(i, 1)
result = 0
for i in range(n):
current = p[i]
remaining = n - i - 1
count = ft.query(current - 1)
result = (result + count * fact_mod[remaining]) % MOD
ft.update(current, -1)
result = (result + 1) % MOD
print(result)
if __name__ == "__main__":
main()
gew1fw