結果

問題 No.1340 おーじ君をさがせ
ユーザー semisagisemisagi
提出日時 2021-01-15 22:38:27
言語 Swift
(5.10.0)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 4,536 bytes
コンパイル時間 15,294 ms
コンパイル使用メモリ 184,484 KB
実行使用メモリ 16,256 KB
最終ジャッジ日時 2024-05-05 21:52:24
合計ジャッジ時間 18,736 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
15,744 KB
testcase_01 AC 10 ms
16,000 KB
testcase_02 AC 9 ms
15,360 KB
testcase_03 AC 8 ms
15,616 KB
testcase_04 AC 8 ms
15,360 KB
testcase_05 AC 9 ms
15,616 KB
testcase_06 AC 9 ms
16,000 KB
testcase_07 AC 10 ms
15,744 KB
testcase_08 AC 9 ms
15,616 KB
testcase_09 AC 8 ms
15,744 KB
testcase_10 AC 10 ms
15,616 KB
testcase_11 AC 25 ms
15,616 KB
testcase_12 AC 13 ms
15,616 KB
testcase_13 AC 23 ms
15,488 KB
testcase_14 AC 43 ms
15,872 KB
testcase_15 AC 39 ms
15,616 KB
testcase_16 AC 11 ms
15,744 KB
testcase_17 AC 18 ms
15,616 KB
testcase_18 AC 26 ms
15,744 KB
testcase_19 AC 10 ms
15,616 KB
testcase_20 AC 9 ms
15,360 KB
testcase_21 AC 28 ms
15,744 KB
testcase_22 AC 46 ms
15,744 KB
testcase_23 AC 29 ms
15,872 KB
testcase_24 AC 71 ms
16,240 KB
testcase_25 AC 22 ms
15,616 KB
testcase_26 AC 19 ms
15,744 KB
testcase_27 AC 15 ms
15,616 KB
testcase_28 AC 20 ms
15,616 KB
testcase_29 AC 14 ms
15,616 KB
testcase_30 AC 29 ms
15,744 KB
testcase_31 AC 77 ms
15,996 KB
testcase_32 AC 100 ms
15,860 KB
testcase_33 AC 99 ms
15,856 KB
testcase_34 AC 85 ms
15,728 KB
testcase_35 AC 104 ms
15,984 KB
testcase_36 AC 10 ms
15,616 KB
testcase_37 AC 19 ms
15,616 KB
testcase_38 AC 110 ms
16,256 KB
testcase_39 AC 39 ms
15,744 KB
testcase_40 AC 41 ms
15,872 KB
testcase_41 AC 39 ms
15,872 KB
testcase_42 AC 9 ms
15,872 KB
testcase_43 AC 9 ms
15,616 KB
testcase_44 AC 9 ms
16,000 KB
testcase_45 AC 9 ms
15,616 KB
testcase_46 AC 26 ms
15,360 KB
testcase_47 AC 27 ms
15,488 KB
testcase_48 AC 28 ms
15,744 KB
testcase_49 AC 27 ms
15,744 KB
testcase_50 AC 28 ms
15,872 KB
testcase_51 AC 28 ms
15,488 KB
testcase_52 AC 46 ms
15,872 KB
testcase_53 AC 46 ms
15,872 KB
testcase_54 AC 46 ms
16,000 KB
testcase_55 AC 37 ms
15,744 KB
testcase_56 AC 9 ms
15,872 KB
testcase_57 AC 10 ms
16,000 KB
testcase_58 AC 9 ms
15,488 KB
testcase_59 AC 19 ms
16,000 KB
testcase_60 AC 9 ms
15,872 KB
testcase_61 AC 21 ms
15,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import Foundation

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 {
        defer { index += 1 }
        return peek()
    }

    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 = 1000000007

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

    static func / (lhs: Zn, rhs: Int) -> Self {
        return lhs * Zn(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)
    }

    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 M = scanner.nextInt()
let T = scanner.nextInt()

var A = Array2D<Bool>(repeating: false, N, N)

func * (lhs: Array2D<Bool>, rhs: Array2D<Bool>) -> Array2D<Bool> {
    var result = Array2D<Bool>(repeating: false, lhs.n1, rhs.n2)
    let n = lhs.n1
    for i in 0 ..< n {
        for j in 0 ..< n {
            for k in 0 ..< n {
                result[i, j] = result[i, j] || (lhs[i, k] && rhs[k, j])
            }
        }
    }
    return result
}

func power(_ a: Array2D<Bool>, _ k: Int) -> Array2D<Bool> {
    if k == 0 {
        var result = Array2D<Bool>(repeating: false, a.n1, a.n1)
        for i in 0 ..< a.n1 {
            result[i, i] = true
        }
        return result
    } else if k % 2 == 1 {
        return a * power(a, k - 1)
    } else {
        return power(a * a, k / 2)
    }
}

for _ in 0 ..< M {
    let a = scanner.nextInt()
    let b = scanner.nextInt()
    A[b, a] = true
}
let B = power(A, T)

var answer = 0
for i in 0 ..< N {
    if B[i, 0] {
        answer += 1
    }
}
print(answer)
0