結果
問題 |
No.683 Two Operations No.3
|
ユーザー |
![]() |
提出日時 | 2025-05-14 13:05:18 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 45 ms / 2,000 ms |
コード長 | 3,886 bytes |
コンパイル時間 | 173 ms |
コンパイル使用メモリ | 82,100 KB |
実行使用メモリ | 52,224 KB |
最終ジャッジ日時 | 2025-05-14 13:07:08 |
合計ジャッジ時間 | 1,501 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 16 |
ソースコード
import sys # Set a higher recursion depth limit for potentially deep recursive calls. # The problem constraints (A, B up to 10^18) might lead to deep recursion paths # if one coordinate decreases slowly (by 1). # The optimization for states on axes (X=0 or Y=0) should mitigate the worst cases, # but a higher limit is safer. If this causes issues on the judge (e.g., memory), # an iterative approach or further mathematical insight might be needed. try: # Set a reasonably large limit, e.g., 100,000. # Default is often 1000, which might be too low. # If the actual path length is bounded polylogarithmically, even ~2500 could be enough. # Let's use 100,000 as a safer value. sys.setrecursionlimit(100000) except Exception: # If setting recursion limit fails (e.g., due to OS limits or security restrictions), # we just proceed with the default limit. This might fail for some test cases. pass # Use a dictionary for memoization to store results of computed states (X, Y). # This avoids recomputing results for the same state, turning exponential complexity # into pseudo-polynomial (related to the number of reachable states). memo = {} def can_reach_zero(X, Y): """ Recursive function with memoization to check if state (X, Y) can reach (0, 0) using reverse operations. """ # Base case: If either coordinate is 0, we can reach (0, 0). # This is because states (N, 0) for N > 0 can only transition to (N-1, 0) via RevOp2, eventually reaching (0, 0). # States (0, M) for M > 0 can only transition to (0, M-1) via RevOp1, eventually reaching (0, 0). # This also correctly handles the target state (0, 0) itself. if X == 0 or Y == 0: return True # Base case: If coordinates become negative (should not happen from non-negative A, B with reverse ops), it's an invalid path. if X < 0 or Y < 0: return False # Check memoization table. If state result is already computed, return it. state = (X, Y) if state in memo: return memo[state] # Optimization: If both X and Y are odd positive integers, we are stuck. # Neither reverse operation can be applied, because RevOp1 requires X even, # and RevOp2 requires Y even. Since X > 0 and Y > 0, this state cannot reach (0, 0). if X % 2 != 0 and Y % 2 != 0: memo[state] = False return False # Variable to track if we found any path to (0, 0) from the current state. found_path = False # Try applying Reverse Operation 1: (X, Y) -> (X/2, Y-1) # This is possible if X is even. Since we are past the X=0 check, X > 0. # Also Y >= 1 is required, which is true since we are past the Y=0 check. if X % 2 == 0: # Recursively call for the resulting state. If it can reach (0, 0), set found_path to True. if can_reach_zero(X // 2, Y - 1): found_path = True # Try applying Reverse Operation 2: (X, Y) -> (X-1, Y/2) # This is possible if Y is even. Since we are past the Y=0 check, Y > 0. # Also X >= 1 is required, which is true since we are past the X=0 check. # We only explore this path if the RevOp1 path didn't already find a solution. # This is a short-circuiting optimization. if not found_path and Y % 2 == 0: # Recursively call for the resulting state. If it can reach (0, 0), set found_path to True. if can_reach_zero(X - 1, Y // 2): found_path = True # Store the result for the current state in the memoization table and return it. memo[state] = found_path return found_path # Read non-negative integers A and B from input. A, B = map(int, sys.stdin.readline().split()) # Call the function starting from the target state (A, B). # Print "Yes" if (A, B) can reach (0, 0), otherwise print "No". if can_reach_zero(A, B): print("Yes") else: print("No")