結果

問題 No.1141 田グリッド
ユーザー semisagisemisagi
提出日時 2020-07-31 22:23:49
言語 Swift
(5.10.0)
結果
WA  
実行時間 -
コード長 3,467 bytes
コンパイル時間 13,782 ms
コンパイル使用メモリ 132,528 KB
実行使用メモリ 17,080 KB
最終ジャッジ日時 2024-07-06 18:46:05
合計ジャッジ時間 6,215 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
16,288 KB
testcase_01 AC 8 ms
9,216 KB
testcase_02 AC 8 ms
9,216 KB
testcase_03 AC 7 ms
9,088 KB
testcase_04 AC 7 ms
9,344 KB
testcase_05 AC 7 ms
9,216 KB
testcase_06 AC 7 ms
9,344 KB
testcase_07 AC 8 ms
9,344 KB
testcase_08 AC 17 ms
9,344 KB
testcase_09 AC 11 ms
9,344 KB
testcase_10 AC 17 ms
9,216 KB
testcase_11 AC 15 ms
9,600 KB
testcase_12 AC 20 ms
9,344 KB
testcase_13 WA -
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます

ソースコード

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 H = scanner.nextInt()
    let W = scanner.nextInt()
    
    var A = Array2D(repeating: 0, H, W)
    for i in 0 ..< H {
        for j in 0 ..< W {
            A[i, j] = scanner.nextInt()
        }
    }
    
    let row = (0 ..< H).map { i in
        (0 ..< W).map { j in A[i, j] == 0 ? Zn(1) : Zn(A[i, j]) }.reduce(Zn(1), *)
    }
    let col = (0 ..< H).map { j in
        (0 ..< H).map { i in A[i, j] == 0 ? Zn(1) : Zn(A[i, j]) }.reduce(Zn(1), *)
    }
    let total = (0 ..< H).flatMap { i in
        (0 ..< W).map { j in A[i, j] == 0 ? Zn(1) : Zn(A[i, j]) }
    }.reduce(Zn(1), *)
    
    let zeroRow = (0 ..< H).map { i in
        (0 ..< W).map { j in A[i, j] == 0 ? 1 : 0 }.reduce(0, +)
    }
    let zeroCol = (0 ..< H).map { j in
        (0 ..< H).map { i in A[i, j] == 0 ? 1 : 0 }.reduce(0, +)
    }
    let zeroTotal = (0 ..< H).flatMap { i in
        (0 ..< W).map { j in A[i, j] == 0 ? 1 : 0 }
    }.reduce(0, +)
    
    let Q = scanner.nextInt()
    for _ in 0 ..< Q {
        let y = scanner.nextInt() - 1
        let x = scanner.nextInt() - 1
        if zeroTotal - zeroRow[y] - zeroCol[x] + (A[y, x] == 0 ? 1 : 0) == 0 {
            let answer = total * row[y].inverse * col[x].inverse * Zn(A[y, x])
            print(answer.n)
        } else {
            print(0)
        }
    }
}

main()
0