結果

問題 No.1239 Multiplication -2
ユーザー semisagisemisagi
提出日時 2020-09-25 22:31:59
言語 Swift
(5.10.0)
結果
AC  
実行時間 445 ms / 2,000 ms
コード長 3,886 bytes
コンパイル時間 1,630 ms
コンパイル使用メモリ 132,372 KB
実行使用メモリ 18,496 KB
最終ジャッジ日時 2024-06-28 06:58:54
合計ジャッジ時間 8,989 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
9,216 KB
testcase_01 AC 8 ms
9,344 KB
testcase_02 AC 9 ms
9,344 KB
testcase_03 AC 12 ms
9,344 KB
testcase_04 AC 12 ms
9,344 KB
testcase_05 AC 13 ms
9,472 KB
testcase_06 AC 10 ms
9,216 KB
testcase_07 AC 10 ms
9,216 KB
testcase_08 AC 8 ms
9,344 KB
testcase_09 AC 9 ms
9,088 KB
testcase_10 AC 9 ms
9,216 KB
testcase_11 AC 8 ms
9,216 KB
testcase_12 AC 8 ms
9,344 KB
testcase_13 AC 9 ms
9,216 KB
testcase_14 AC 9 ms
9,344 KB
testcase_15 AC 94 ms
11,548 KB
testcase_16 AC 149 ms
13,772 KB
testcase_17 AC 251 ms
18,468 KB
testcase_18 AC 345 ms
18,208 KB
testcase_19 AC 193 ms
18,496 KB
testcase_20 AC 263 ms
18,472 KB
testcase_21 AC 445 ms
18,200 KB
testcase_22 AC 306 ms
17,672 KB
testcase_23 AC 329 ms
17,956 KB
testcase_24 AC 232 ms
17,620 KB
testcase_25 AC 41 ms
14,112 KB
testcase_26 AC 299 ms
14,712 KB
testcase_27 AC 129 ms
11,480 KB
testcase_28 AC 402 ms
17,832 KB
testcase_29 AC 431 ms
17,760 KB
testcase_30 AC 158 ms
13,132 KB
testcase_31 AC 233 ms
14,504 KB
testcase_32 AC 388 ms
18,292 KB
testcase_33 AC 273 ms
17,880 KB
testcase_34 AC 250 ms
14,912 KB
testcase_35 AC 128 ms
11,552 KB
testcase_36 AC 117 ms
11,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

struct Scanner {
    private var elements = [String]()
    private var index = 0

    mutating func peek() -> String {
        while elements.count == index {
            elements = readLine()!.split(separator: " ").map(String.init)
            index = 0
        }
        return elements[index]
    }

    mutating func next() -> String {
        let value = peek()
        index += 1
        return value
    }

    mutating func nextInt() -> Int {
        return Int(next())!
    }

    mutating func nextDouble() -> Double {
        return Double(next())!
    }
}

let MOD = 998244353

struct Zn: CustomStringConvertible {
    let n: Int

    init(_ n: Int) {
        self.n = n
    }

    var description: String {
        String(self.n)
    }

    static var zero = Zn(0)
    static var one = Zn(1)

    static func + (lhs: Self, rhs: Self) -> Self {
        let n = lhs.n + rhs.n
        return Zn(n < MOD ? n : n - MOD)
    }

    static func + (lhs: Self, rhs: Int) -> Self {
        return lhs + Zn(rhs)
    }

    static func + (lhs: Int, rhs: Self) -> Self {
        return Zn(lhs) + rhs
    }

    static func - (lhs: Self, rhs: Self) -> Self {
        let n = lhs.n - rhs.n
        return Zn(n >= 0 ? n : n + MOD)
    }

    static func - (lhs: Self, rhs: Int) -> Self {
        return lhs - Zn(rhs)
    }

    static func - (lhs: Int, rhs: Self) -> Self {
        return Zn(lhs) - rhs
    }

    static func * (lhs: Self, rhs: Self) -> Self {
        Zn((lhs.n * rhs.n) % MOD)
    }

    static func * (lhs: Self, rhs: Int) -> Self {
        return lhs * Zn(rhs)
    }

    static func * (lhs: Int, rhs: Self) -> Self {
        return Zn(lhs) * rhs
    }

    static func += (lhs: inout Self, rhs: Self) {
        lhs = lhs + rhs
    }

    static func += (lhs: inout Self, rhs: Int) {
        lhs = lhs + Zn(rhs)
    }

    static func -= (lhs: inout Self, rhs: Self) {
        lhs = lhs - rhs
    }

    static func -= (lhs: inout Self, rhs: Int) {
        lhs = lhs - Zn(rhs)
    }

    static func *= (lhs: inout Self, rhs: Self) {
        lhs = lhs * rhs
    }

    static func *= (lhs: inout Self, rhs: Int) {
        lhs = lhs * Zn(rhs)
    }

    func pow(_ n: Int) -> Self {
        if n == 0 {
            return Zn(1)
        } else if n % 2 == 1 {
            return self * self.pow(n - 1)
        } else {
            return (self * self).pow(n / 2)
        }
    }

    var inverse: Self {
        self.pow(MOD - 2)
    }
}

struct Array2D<Element> {
    let n1: Int
    let n2: Int
    private var elements: [Element]

    init(repeating: Element, _ n1: Int, _ n2: Int) {
        self.n1 = n1
        self.n2 = n2
        self.elements = [Element](repeating: repeating, count: n1 * n2)
    }

    subscript(i: Int, j: Int) -> Element {
        get {
            elements[i * n2 + j]
        }
        set {
            elements[i * n2 + j] = newValue
        }
    }
}

var scanner = Scanner()
let N = scanner.nextInt()
let A = (0 ..< N).map { _ in scanner.nextInt() }


func f(_ a: [Int], bounded: Bool) -> [Zn] {
    var xor = 0
    var prob = Zn.one
    if !bounded || a.count > 0 {
        prob *= Zn(2).inverse
    }
    var result = [prob, .zero]
    for i in a.indices {
        if a[i] == -1 {
            xor ^= 1
        }
        if !bounded || i < a.count - 1 {
            prob *= Zn(2).inverse
        }
        result[xor] += prob
    }
    return result
}

var answer = Zn.zero
for i in 0 ..< N where abs(A[i]) == 2 {
    var l = i
    var r = i + 1
    while l - 1 >= 0 && abs(A[l - 1]) == 1 {
        l -= 1
    }
    while r + 1 <= N && abs(A[r]) == 1 {
        r += 1
    }
    let L = f([Int](A[l ..< i].reversed()), bounded: l == 0)
    let R = f([Int](A[i + 1 ..< r]), bounded: r == N)

    if A[i] == 2 {
        answer += L[0] * R[1] + L[1] * R[0]
    } else  {
        answer += L[0] * R[0] + L[1] * R[1]
    }
}

print(answer)


0