Для игры, описанной ранее, найдите такое значение , при котором одновременно выполняются два условия:
– у Саске есть выигрышная стратегия, позволяющая ему выиграть первым или вторым ходом при любой игре Наруто;
– у Саске нет стратегии, которая позволит ему гарантированно выиграть первым ходом.
Если такого значения нет, в ответ запишите 0.
from functools import lru_cache
@lru_cache(None)
def f(first_heap, second_heap, third_heap, c=0):
if first_heap + second_heap + third_heap <= 32:
return 0
if c > 4:
return 123123
moves = []
if first_heap > 1:
moves.append(f(first_heap - 1, second_heap, third_heap, c + 1))
moves.append(f((first_heap + 1) // 2, second_heap, third_heap, c + 1))
if second_heap > 1:
moves.append(f(first_heap, second_heap - 1, third_heap, c + 1))
moves.append(f(first_heap, (second_heap + 1) // 2, third_heap, c + 1))
if third_heap > 1:
moves.append(f(first_heap, second_heap, third_heap - 1, c + 1))
moves.append(f(first_heap, second_heap, (third_heap + 1) // 2, c + 1))
petya_win = [i for i in moves if i <= 0]
if petya_win:
return -max(petya_win) + 1
else:
return -max(moves)
for i in range(11, 100):
if f(11, 11, i) == -2:
print(i)
Ответ: 24