実行環境
- Python 3.10.8
- PyPy 7.3.11 with MSC v.1929 64 bit (AMD64)
質問
AtCoder Beginner Contest 236 のD問題に回答している際に,同じスクリプト・標準入力に対してPyPyとPythonで標準出力が異なるという現象に遭遇しました.
PyPyでは再帰が途中で終了しており,これはPythonのような意図する動作ではありません.
これは仕様ですか.またPyPyでPythonと同じような出力を得るために,何をすべきですか.
問題のスクリプト
# script.py
import operator
import functools
# input -----
n = int(input())
a = [[0] * n * 2 for _ in range(n * 2)]
for i in range(2 * n - 1):
for j, e in enumerate(map(int, input().split()), i + 1):
a[i][j] = a[j][i] = e
# -----------
def rec(i):
global ans
if not __debug__:
print(f"called rec({i = })\n {pairs = }\n {not_selected = }\n {ans = }\n")
if i == 2 * n:
ans = max(
ans,
functools.reduce(
operator.xor,
(a[e1 - 1][e2 - 1] for e1, e2 in zip(*[iter(pairs)] * 2)),
0,
),
)
return
if i % 2 == 0:
e = min(not_selected)
pairs.append(e)
not_selected.discard(e)
rec(i + 1)
pairs.pop()
not_selected.add(e)
else:
for e in not_selected:
pairs.append(e)
not_selected.discard(e)
rec(i + 1)
pairs.pop()
not_selected.add(e)
pairs = []
not_selected = set(range(2 * n))
ans = 0
rec(0)
print(ans)
標準入力
2
4 0 1
5 3
2
Pythonでの実行結果
$ python -O script.py
~ console input ~
called rec(i = 0)
pairs = []
not_selected = {0, 1, 2, 3}
ans = 0
called rec(i = 1)
pairs = [0]
not_selected = {1, 2, 3}
ans = 0
called rec(i = 2)
pairs = [0, 1]
not_selected = {2, 3}
ans = 0
called rec(i = 3)
pairs = [0, 1, 2]
not_selected = {3}
ans = 0
called rec(i = 4)
pairs = [0, 1, 2, 3]
not_selected = set()
ans = 0
called rec(i = 2)
pairs = [0, 2]
not_selected = {1, 3}
ans = 4
called rec(i = 3)
pairs = [0, 2, 1]
not_selected = {3}
ans = 4
called rec(i = 4)
pairs = [0, 2, 1, 3]
not_selected = set()
ans = 4
called rec(i = 2)
pairs = [0, 3]
not_selected = {1, 2}
ans = 4
called rec(i = 3)
pairs = [0, 3, 1]
not_selected = {2}
ans = 4
called rec(i = 4)
pairs = [0, 3, 1, 2]
not_selected = set()
ans = 4
called rec(i = 2)
pairs = [0, 2]
not_selected = {1, 3}
ans = 6
called rec(i = 3)
pairs = [0, 2, 1]
not_selected = {3}
ans = 6
called rec(i = 4)
pairs = [0, 2, 1, 3]
not_selected = set()
ans = 6
called rec(i = 2)
pairs = [0, 3]
not_selected = {2, 1}
ans = 6
called rec(i = 3)
pairs = [0, 3, 1]
not_selected = {2}
ans = 6
called rec(i = 4)
pairs = [0, 3, 1, 2]
not_selected = set()
ans = 6
called rec(i = 4)
pairs = [0, 3, 1, 2]
not_selected = set()
ans = 6
6
PyPyでの実行結果
$ pypy -O test.py
~ console input ~
called rec(i = 0)
pairs = []
not_selected = {0, 1, 2, 3}
ans = 0
called rec(i = 1)
pairs = [0]
not_selected = {1, 2, 3}
ans = 0
called rec(i = 2)
pairs = [0, 1]
not_selected = {2, 3}
ans = 0
called rec(i = 3)
pairs = [0, 1, 2]
not_selected = {3}
ans = 0
called rec(i = 4)
pairs = [0, 1, 2, 3]
not_selected = set()
ans = 0
4