結果
| 問題 |
No.696 square1001 and Permutation 5
|
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 15:23:33 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,001 bytes |
| コンパイル時間 | 180 ms |
| コンパイル使用メモリ | 82,276 KB |
| 実行使用メモリ | 96,412 KB |
| 最終ジャッジ日時 | 2025-06-12 15:23:35 |
| 合計ジャッジ時間 | 1,766 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 1 WA * 11 |
ソースコード
MOD = 10**9 + 7
class FenwickTree:
def __init__(self, size):
self.size = size
self.tree = [0] * (size + 2)
def update(self, index, delta):
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
def main():
import sys
input = sys.stdin.read().split()
N = int(input[0])
p = list(map(int, input[1:N+1]))
fact = [1] * (N + 1)
for i in range(1, N+1):
fact[i] = fact[i-1] * i % MOD
ft = FenwickTree(N)
total = 0
for i in range(N-1, -1, -1):
current = p[i]
smaller = ft.query(current - 1)
term = smaller * fact[N - i - 1] % MOD
total = (total + term) % MOD
ft.update(current, 1)
ans = (total + 1) % MOD
print(ans)
if __name__ == "__main__":
main()
gew1fw