結果
| 問題 | 
                            No.643 Two Operations No.2
                             | 
                    
| コンテスト | |
| ユーザー | 
                             lam6er
                         | 
                    
| 提出日時 | 2025-03-20 18:58:50 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 41 ms / 2,000 ms | 
| コード長 | 1,247 bytes | 
| コンパイル時間 | 429 ms | 
| コンパイル使用メモリ | 82,576 KB | 
| 実行使用メモリ | 55,816 KB | 
| 最終ジャッジ日時 | 2025-03-20 18:59:48 | 
| 合計ジャッジ時間 | 1,307 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 13 | 
ソースコード
from collections import deque
def main():
    X, Y = map(int, input().split())
    if X == Y:
        print(0)
        return
    
    initial_max = max(abs(X), abs(Y))
    # Set a limit to avoid infinite expansion; 2^20 is arbitrary but large enough for given constraints
    LIMIT = initial_max * (2 ** 20)
    
    visited = set()
    q = deque()
    q.append((X, Y, 0))
    visited.add((X, Y))
    
    answer = -1
    found = False
    
    while q:
        x, y, steps = q.popleft()
        
        if x == y:
            answer = steps
            found = True
            break
        
        # Apply Operation 1: Swap
        new_x1, new_y1 = y, x
        if (new_x1, new_y1) not in visited:
            if abs(new_x1) <= LIMIT and abs(new_y1) <= LIMIT:
                visited.add((new_x1, new_y1))
                q.append((new_x1, new_y1, steps + 1))
        
        # Apply Operation 2: X+Y and X-Y
        new_x2 = x + y
        new_y2 = x - y
        if (new_x2, new_y2) not in visited:
            if abs(new_x2) <= LIMIT and abs(new_y2) <= LIMIT:
                visited.add((new_x2, new_y2))
                q.append((new_x2, new_y2, steps + 1))
    
    print(answer if found else -1)
if __name__ == "__main__":
    main()
            
            
            
        
            
lam6er