結果

問題 No.1144 Triangles
ユーザー semisagisemisagi
提出日時 2020-07-31 23:13:33
言語 Swift
(5.10.0)
結果
WA  
実行時間 -
コード長 4,928 bytes
コンパイル時間 8,236 ms
コンパイル使用メモリ 137,776 KB
実行使用メモリ 7,984 KB
最終ジャッジ日時 2023-09-21 02:26:47
合計ジャッジ時間 15,890 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,788 KB
testcase_01 AC 4 ms
7,880 KB
testcase_02 AC 3 ms
7,800 KB
testcase_03 AC 3 ms
7,800 KB
testcase_04 AC 763 ms
7,972 KB
testcase_05 AC 767 ms
7,852 KB
testcase_06 AC 766 ms
7,888 KB
testcase_07 AC 761 ms
7,984 KB
testcase_08 AC 764 ms
7,968 KB
testcase_09 AC 3 ms
7,804 KB
testcase_10 AC 3 ms
7,828 KB
testcase_11 AC 3 ms
7,764 KB
testcase_12 AC 4 ms
7,852 KB
testcase_13 AC 4 ms
7,832 KB
testcase_14 AC 693 ms
7,916 KB
testcase_15 AC 684 ms
7,864 KB
testcase_16 AC 741 ms
7,900 KB
testcase_17 AC 740 ms
7,904 KB
testcase_18 AC 739 ms
7,928 KB
testcase_19 AC 27 ms
7,880 KB
testcase_20 AC 5 ms
7,796 KB
testcase_21 AC 6 ms
7,804 KB
testcase_22 AC 706 ms
7,956 KB
testcase_23 AC 44 ms
7,880 KB
testcase_24 AC 691 ms
7,956 KB
testcase_25 AC 198 ms
7,876 KB
testcase_26 AC 24 ms
7,876 KB
testcase_27 AC 10 ms
7,932 KB
testcase_28 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: Equatable {
    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)
    }
    
    static func == (lhs: Self, rhs: Self) -> Bool {
        lhs.n == rhs.n
    }
}

fileprivate struct Complex: Comparable {
    let x: Int
    let y: Int
    
    init(_ x: Int, _ y: Int) {
        self.x = x
        self.y = y
    }
    
    static func + (lhs: Self, rhs: Self) -> Self {
        Self(lhs.x + rhs.x, lhs.y + rhs.y)
    }
    
    static func - (lhs: Self, rhs: Self) -> Self {
        Self(lhs.x - rhs.x, lhs.y - rhs.y)
    }
    
    static func * (lhs: Self, rhs: Self) -> Self {
        Self(lhs.x * rhs.x - lhs.y * rhs.y, lhs.x * rhs.y + lhs.y * rhs.x)
    }
    
    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
    }
    
    static var zero: Self {
        Self(0, 0)
    }
    
    var norm: Int {
        x * x + y * y
    }
    
    var syougen: Int {
        if x > 0 && y >= 0 {
            return 0
        } else if x <= 0 && y > 0 {
            return 1
        } else if x < 0 && y <= 0 {
            return 2
        } else {
            return 3
        }
    }
    
    var conj: Complex {
        Complex(x, -y)
    }
    
    static func det(_ lhs: Complex, _ rhs: Complex) -> Int {
        lhs.x * rhs.y - lhs.y * rhs.x
    }
    
    static func < (lhs: Complex, rhs: Complex) -> Bool {
        if lhs == rhs {
            return false
        }
        if lhs == .zero {
            return true
        }
        if rhs == .zero {
            return false
        }
        if lhs.syougen != rhs.syougen {
            return lhs.syougen < rhs.syougen
        }
        if Self.det(lhs, rhs) != 0 {
            return Self.det(lhs, rhs) > 0
        }
        return lhs.norm < rhs.norm
    }
    
    static func == (lhs: Complex, rhs: Complex) -> Bool {
        lhs.x == rhs.x && lhs.y == rhs.y
    }
    
    func closewise(other: Complex) -> Bool {
        Self.det(self, other) >= 0
    }
}

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()
    var P = [Complex]()
    for _ in 0 ..< N {
        let x = scanner.nextInt()
        let y = scanner.nextInt()
        P.append(Complex(x, y))
    }
    var answer = 0
    for k in 0 ..< N {
        let O = P[k]
        let Q = P.filter({ $0 != O }).map({ $0 - O }).sorted()
        var j = 0
        
        var sum = Complex.zero
        for i in 0 ..< Q.count {
            while j < i + Q.count, Q[i].closewise(other: Q[j % Q.count]) {
                sum += Q[j % Q.count]
                j += 1
            }
            answer += (Q[i].conj * sum).y
            answer %= MOD
            sum -= Q[i]
        }
    }
    answer *= Zn(3).inverse.n
    answer %= MOD
    print(answer)
}

main()
0