結果

問題 No.1142 XOR と XOR
ユーザー semisagisemisagi
提出日時 2020-07-31 22:07:36
言語 Swift
(5.10.0)
結果
AC  
実行時間 220 ms / 2,000 ms
コード長 3,533 bytes
コンパイル時間 12,195 ms
コンパイル使用メモリ 130,476 KB
実行使用メモリ 31,544 KB
最終ジャッジ日時 2024-11-08 03:27:10
合計ジャッジ時間 16,479 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
9,344 KB
testcase_01 AC 19 ms
9,216 KB
testcase_02 AC 19 ms
9,344 KB
testcase_03 AC 220 ms
31,544 KB
testcase_04 AC 155 ms
23,236 KB
testcase_05 AC 132 ms
21,544 KB
testcase_06 AC 161 ms
27,924 KB
testcase_07 AC 137 ms
30,264 KB
testcase_08 AC 193 ms
30,644 KB
testcase_09 AC 192 ms
30,776 KB
testcase_10 AC 191 ms
30,772 KB
testcase_11 AC 19 ms
9,344 KB
testcase_12 AC 19 ms
9,216 KB
testcase_13 AC 19 ms
9,344 KB
testcase_14 AC 111 ms
22,524 KB
testcase_15 AC 111 ms
23,192 KB
testcase_16 AC 26 ms
10,068 KB
testcase_17 AC 107 ms
25,568 KB
testcase_18 AC 38 ms
11,992 KB
testcase_19 AC 162 ms
28,316 KB
testcase_20 AC 111 ms
18,416 KB
testcase_21 AC 75 ms
14,800 KB
testcase_22 AC 51 ms
13,276 KB
testcase_23 AC 153 ms
27,608 KB
testcase_24 AC 169 ms
28,452 KB
testcase_25 AC 105 ms
22,388 KB
testcase_26 AC 158 ms
26,656 KB
testcase_27 AC 130 ms
24,860 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fileprivate 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())!
    }
}

fileprivate let MOD = 1000000007

fileprivate struct Zn {
    let n: Int
    
    init(_ n: Int) {
        self.n = n
    }
    
    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: Self) -> Self {
        let n = lhs.n - rhs.n
        return Zn(n >= 0 ? n : n + MOD)
    }
    
    static func * (lhs: Self, rhs: Self) -> Self {
        Zn((lhs.n * rhs.n) % MOD)
    }
    
    static func += (lhs: inout Self, rhs: Self) {
        lhs = lhs + rhs
    }
    
    static func -= (lhs: inout Self, rhs: Self) {
        lhs = lhs - rhs
    }
    
    static func *= (lhs: inout Self, rhs: Self) {
        lhs = lhs * 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)
    }
}

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


func main() {
    var scanner = Scanner()
    
    let N = scanner.nextInt()
    let M = scanner.nextInt()
    let K = scanner.nextInt()
    let A = (0 ..< N).map { _ in scanner.nextInt() }
    let B = (0 ..< M).map { _ in scanner.nextInt() }
    
    var S = [0]
    for i in 0 ..< N {
        S.append(S.last! ^ A[i])
    }
    var T = [0]
    for i in 0 ..< M {
        T.append(T.last! ^ B[i])
    }
    var F = [Int](repeating: 0, count: 1024)
    var G = [Int](repeating: 0, count: 1024)
    S.forEach { F[$0] += 1 }
    T.forEach { G[$0] += 1 }
    
    func f(dp: [Zn], array A: [Int]) -> [Zn] {
        var result = [Zn](repeating: Zn(0), count: 1024)
        for i in 0 ..< 1024 {
            for j in 0 ..< 1024 {
                result[i ^ j] += dp[i] * Zn(A[j])
            }
        }
        return result
    }
    
    func solve(array F: [Int]) -> Zn {
        var result = Zn(0)
        for i in 0 ..< 1024 {
            result += Zn(F[i]) * Zn(F[K ^ i])
        }
        return result
    }
    
    var dp = [Zn](repeating: Zn(0), count: 1024)
    dp[0] = Zn(1)
    dp = f(dp: dp, array: F)
    dp = f(dp: dp, array: F)
    dp = f(dp: dp, array: G)
    dp = f(dp: dp, array: G)
    
    var answer = dp[K]
    if K == 0 {
        answer += Zn(N + 1) * Zn(M + 1)
    }
    answer -= solve(array: F) * Zn(M + 1)
    answer -= solve(array: G) * Zn(N + 1)
    answer *= Zn(4).inverse
    print(answer.n)
}

main()
0