結果

問題 No.132 点と平面との距離
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-08-26 15:21:17
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,071 bytes
コンパイル時間 281 ms
コンパイル使用メモリ 82,548 KB
実行使用メモリ 81,604 KB
最終ジャッジ日時 2024-04-25 16:59:51
合計ジャッジ時間 13,366 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 721 ms
78,944 KB
testcase_01 AC 4,648 ms
81,604 KB
testcase_02 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env pypy3

import collections
import functools
import itertools
import math
import operator


# Based on http://thira.plavox.info/blog/2008/06/_c.html
def in_place_det(a):
    n = len(a)
    for i, j in itertools.product(range(n), repeat=2):
        if i < j:
            b = a[j][i] / a[i][i]
            for k in range(n):
                a[j][k] -= a[i][k] * b
    d = functools.reduce(operator.mul, (a[i][i] for i in range(n)))
    return d


class Vector3(collections.namedtuple("Vector3", "x y z")):

    __slots__ = ()

    def __add__(self, other):
        return Vector3(*(s + o for s, o in zip(self, other)))

    def __sub__(self, other):
        return Vector3(*(s - o for s, o in zip(self, other)))

    def __mul__(self, other):  # cross product
        return abs(self) * abs(other) * math.sin(self.angle(other))

    def __neg__(self):
        return Vector3(-self.x, -self.y, -self.z)

    def __pos__(self):
        return Vector3(+self.x, +self.y, +self.z)

    def __abs__(self):  # norm
        return math.sqrt(sum(s * s for s in self))

    def dotproduct(self, other):
        return sum(s * o for s, o in zip(self, other))

    def angle(self, other):
        return math.acos(self.dotproduct(other) / abs(self) / abs(other))

    def scale(self, k):
        return Vector3(k * self.x, k * self.y, k * self.z)


def volume_of_trigonal_pyramid(p, q1, q2, q3):
    v1 = q1 - p
    v2 = q2 - p
    v3 = q3 - p
    a = [list(v1), list(v2), list(v3)]
    return abs(in_place_det(a) / 6)


def volume_of_triangle(q1, q2, q3):
    a = q2 - q1
    b = q3 - q1
    return abs(a * b) / 2


def dist(p, q1, q2, q3):
    d = volume_of_trigonal_pyramid(p, q1, q2, q3) * 3
    d /= volume_of_triangle(q1, q2, q3)
    return d


def solve(p, qs):
    return sum(dist(p, *q123) for q123 in itertools.combinations(qs, 3))


def main():
    n = int(input())
    p = Vector3(*map(float, input().split()))
    qs = [Vector3(*map(float, input().split())) for _ in range(n)]
    print("{:.12f}".format(solve(p, qs)))


if __name__ == '__main__':
    main()
0