結果

問題 No.1289 RNG and OR
ユーザー semisagisemisagi
提出日時 2020-11-14 12:06:50
言語 Swift
(5.10.0)
結果
AC  
実行時間 186 ms / 2,000 ms
コード長 3,358 bytes
コンパイル時間 10,062 ms
コンパイル使用メモリ 132,576 KB
実行使用メモリ 20,380 KB
最終ジャッジ日時 2023-09-30 04:51:12
合計ジャッジ時間 11,902 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,916 KB
testcase_01 AC 3 ms
7,916 KB
testcase_02 AC 3 ms
7,900 KB
testcase_03 AC 3 ms
7,920 KB
testcase_04 AC 3 ms
7,964 KB
testcase_05 AC 3 ms
7,992 KB
testcase_06 AC 3 ms
7,964 KB
testcase_07 AC 3 ms
7,956 KB
testcase_08 AC 3 ms
8,012 KB
testcase_09 AC 3 ms
7,956 KB
testcase_10 AC 3 ms
7,992 KB
testcase_11 AC 4 ms
7,988 KB
testcase_12 AC 4 ms
8,040 KB
testcase_13 AC 5 ms
8,188 KB
testcase_14 AC 6 ms
8,024 KB
testcase_15 AC 9 ms
8,340 KB
testcase_16 AC 15 ms
8,724 KB
testcase_17 AC 26 ms
9,476 KB
testcase_18 AC 50 ms
10,548 KB
testcase_19 AC 96 ms
13,640 KB
testcase_20 AC 186 ms
20,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

func zeta(_ a: [Zn]) -> [Zn] {
    var result = a
    func f(l: Int, r: Int) {
        guard r - l > 1 else { return }
        let m = (l + r) / 2
        f(l: l, r: m)
        f(l: m, r: r)
        for i in 0 ..< (r - l) / 2 {
            result[m + i] += result[l + i]
        }
    }
    f(l: 0, r: a.count)
    return result
}

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 nextInts(_ n: Int) -> [Int] {
        return (0 ..< n).map { _ in nextInt() }
    }

    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: Zn, rhs: Zn) -> Self {
        return lhs * rhs.inverse
    }

    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)
    }
}

var scanner = Scanner()
let N = scanner.nextInt()
let A = scanner.nextInts(1 << N)
let sum = Zn(A.reduce(0, +))
let P = A.map { $0 * sum.inverse }
let ZP = zeta(P)

var answer = Zn(1)
let U = (1 << N) - 1
for i in 1 ..< (1 << N) {
    let v = ZP[U ^ i] / (1 - ZP[U ^ i])
    if i.nonzeroBitCount % 2 == 1 {
        answer += v
    } else {
        answer -= v
    }
}
print(answer)
0