結果

問題 No.1624 三角形の反射
ユーザー gew1fw
提出日時 2025-06-12 17:04:39
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,863 bytes
コンパイル時間 234 ms
コンパイル使用メモリ 82,336 KB
実行使用メモリ 90,300 KB
最終ジャッジ日時 2025-06-12 17:04:51
合計ジャッジ時間 5,696 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 18 WA * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def main():
    a_str = sys.stdin.readline().strip()
    a = float(a_str)
    
    from fractions import Fraction
    a_frac = Fraction(a).limit_denominator()
    p = a_frac.numerator
    q = a_frac.denominator
    a = p / q  # Use float for calculations
    
    x, y = 0.0, 0.0
    dx, dy = 1.0, a
    reflections = 0
    epsilon = 1e-8
    
    while True:
        t_ab = t_ac = t_bc = None
        
        # Compute t for AB (y=0)
        if abs(dy) > epsilon:
            t_ab = (0.0 - y) / dy
            if t_ab > epsilon:
                x_ab = x + dx * t_ab
                y_ab = 0.0
                if abs(x_ab) < 1.0 + epsilon and abs(y_ab) < 1.0 + epsilon:
                    t_ab_valid = True
                else:
                    t_ab = None
            else:
                t_ab = None
        else:
            t_ab = None
        
        # Compute t for AC (x=0)
        if abs(dx) > epsilon:
            t_ac = (0.0 - x) / dx
            if t_ac > epsilon:
                x_ac = 0.0
                y_ac = y + dy * t_ac
                if abs(x_ac) < 1.0 + epsilon and abs(y_ac) < 1.0 + epsilon:
                    t_ac_valid = True
                else:
                    t_ac = None
            else:
                t_ac = None
        else:
            t_ac = None
        
        # Compute t for BC (x + y = 1)
        denominator = dx + dy
        if abs(denominator) > epsilon:
            numerator = 1.0 - x - y
            t_bc = numerator / denominator
            if t_bc > epsilon:
                x_bc = x + dx * t_bc
                y_bc = y + dy * t_bc
                if abs(x_bc) < 1.0 + epsilon and abs(y_bc) < 1.0 + epsilon and abs(x_bc + y_bc - 1.0) < epsilon:
                    t_bc_valid = True
                else:
                    t_bc = None
            else:
                t_bc = None
        else:
            t_bc = None
        
        # Collect valid t's
        t_list = []
        if t_ab is not None:
            t_list.append(t_ab)
        if t_ac is not None:
            t_list.append(t_ac)
        if t_bc is not None:
            t_list.append(t_bc)
        
        if not t_list:
            print(-1)
            return
        
        min_t = min(t_list)
        if min_t == t_ab:
            x1 = x + dx * min_t
            y1 = 0.0
            edge = 'AB'
        elif min_t == t_ac:
            x1 = 0.0
            y1 = y + dy * min_t
            edge = 'AC'
        else:
            x1 = x + dx * min_t
            y1 = y + dy * min_t
            edge = 'BC'
        
        # Check if (x1, y1) is a vertex
        is_vertex = False
        vertex = None
        if abs(x1) < epsilon and abs(y1) < epsilon:
            vertex = 'A'
            is_vertex = True
        elif abs(x1 - 1.0) < epsilon and abs(y1) < epsilon:
            vertex = 'B'
            is_vertex = True
        elif abs(x1) < epsilon and abs(y1 - 1.0) < epsilon:
            vertex = 'C'
            is_vertex = True
        
        if is_vertex:
            print(f"{vertex} {reflections}")
            return
        
        # Reflect direction
        if edge == 'AB':
            # Hit y=0, reflect dy
            new_dx = dx
            new_dy = -dy
        elif edge == 'AC':
            # Hit x=0, reflect dx
            new_dx = -dx
            new_dy = dy
        else:  # BC
            # Compute reflection over BC (normal (1,1))
            dot = dx + dy
            new_dx = dx - 2 * dot / 2
            new_dy = dy - 2 * dot / 2
        
        dx, dy = new_dx, new_dy
        
        # Update position
        x, y = x1, y1
        
        # Increment reflections
        reflections += 1
        
        # Check for infinite loop (unlikely but possible)
        if reflections > 10**6:
            print(-1)
            return

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