結果

問題 No.1141 田グリッド
ユーザー semisagisemisagi
提出日時 2020-07-31 21:38:50
言語 Swift
(5.10.0)
結果
WA  
実行時間 -
コード長 2,922 bytes
コンパイル時間 5,886 ms
コンパイル使用メモリ 131,688 KB
実行使用メモリ 12,296 KB
最終ジャッジ日時 2023-09-20 22:15:26
合計ジャッジ時間 13,947 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,740 KB
testcase_01 AC 3 ms
7,740 KB
testcase_02 AC 3 ms
7,768 KB
testcase_03 AC 3 ms
7,760 KB
testcase_04 AC 3 ms
7,820 KB
testcase_05 AC 4 ms
7,712 KB
testcase_06 AC 3 ms
7,888 KB
testcase_07 AC 3 ms
7,728 KB
testcase_08 AC 13 ms
7,836 KB
testcase_09 AC 7 ms
7,760 KB
testcase_10 AC 14 ms
7,876 KB
testcase_11 AC 10 ms
7,884 KB
testcase_12 AC 16 ms
7,824 KB
testcase_13 WA -
testcase_14 TLE -
testcase_15 AC 195 ms
9,240 KB
testcase_16 AC 195 ms
9,208 KB
testcase_17 AC 199 ms
8,952 KB
testcase_18 AC 199 ms
8,924 KB
testcase_19 AC 192 ms
9,228 KB
testcase_20 AC 191 ms
9,172 KB
testcase_21 AC 192 ms
9,240 KB
testcase_22 AC 192 ms
9,328 KB
testcase_23 AC 191 ms
9,200 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
権限があれば一括ダウンロードができます

ソースコード

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

main()
0