Для игры, описанной в предыдущем задании, найдите количество таких значений , при которых у Наруто есть выигрышная стратегия, причём Наруто не может выиграть за один ход и Наруто может выиграть своим вторым ходом независимо от того, как будет ходить Саске.
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)
count = 0
for i in range(11, 100):
if f(11, 11, i) == 2:
count += 1
print(count)
Ответ: 5