結果

問題 No.2382 Amidakuji M
ユーザー sharusharu
提出日時 2023-07-14 23:36:18
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 821 bytes
コンパイル時間 85 ms
コンパイル使用メモリ 11,004 KB
実行使用メモリ 30,200 KB
最終ジャッジ日時 2023-10-14 14:06:41
合計ジャッジ時間 9,784 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,084 KB
testcase_01 AC 15 ms
8,100 KB
testcase_02 AC 14 ms
8,004 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 120 ms
11,136 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 804 ms
23,236 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 763 ms
23,324 KB
testcase_13 AC 15 ms
8,432 KB
testcase_14 WA -
testcase_15 AC 15 ms
8,020 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 13 ms
7,996 KB
testcase_19 WA -
testcase_20 AC 1,033 ms
30,120 KB
testcase_21 AC 1,065 ms
30,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd


class fenwick_tree:
    def __init__(self, N):
        self.n = N
        self.data = [0] * N

    def add(self, p, x):
        assert 0 <= p < self.n
        p += 1
        while p <= self.n:
            self.data[p - 1] += x
            p += p & -p

    def sum(self, l, r):
        """return sum(a[l:r-1])"""
        assert 0 <= l and l <= r and r <= self.n
        return self._sum(r) - self._sum(l)

    def _sum(self, r):
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r
        return s


n, m = map(int, input().split())
p = list(map(int, input().split()))
ft = fenwick_tree(n)
l = 0
for i in range(n):
    l += ft.sum(p[i], n)
    ft.add(p[i] - 1, i)

if gcd(2, m) != 1:
    print(-1)
else:
    k = (-l * pow(2, -1, m)) % m
    print(l + 2 * k)
0