結果

問題 No.696 square1001 and Permutation 5
ユーザー gew1fw
提出日時 2025-06-12 20:48:49
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,100 bytes
コンパイル時間 239 ms
コンパイル使用メモリ 82,076 KB
実行使用メモリ 96,068 KB
最終ジャッジ日時 2025-06-12 20:50:53
合計ジャッジ時間 1,947 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 1 WA * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

class FenwickTree:
    def __init__(self, size):
        self.n = size
        self.tree = [0] * (self.n + 1)
    
    def update(self, index, delta):
        while index <= self.n:
            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]))
    
    # Precompute factorials modulo MOD
    fact = [1] * (N + 1)
    for i in range(1, N + 1):
        fact[i] = (fact[i-1] * i) % MOD
    
    fenwick = FenwickTree(N)
    for i in range(1, N+1):
        fenwick.update(i, 1)
    
    sum_rank = 0
    for i in range(N):
        x = p[i]
        cnt = fenwick.query(x-1)
        term = (cnt * fact[N - i - 1]) % MOD
        sum_rank = (sum_rank + term) % MOD
        fenwick.update(x, -1)
    
    result = (sum_rank + 1) % MOD
    print(result)

if __name__ == "__main__":
    main()
0