Card Game (Codeforces 1739C)
Tags: Others, games

Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers:  the number of ways to distribute the cards so that Alex wins;  the number of ways to distribute the cards so that Boris wins;  the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows:  if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw;  if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.

Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases.Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).

Output: For each test case, print three integers:  the number of ways to distribute the cards so that Alex wins;  the number of ways to distribute the cards so that Boris wins;  the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.

Note: In the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case:   Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$;  Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$;  the game ends in a draw if Alex receives the cards $$$[2, 3]$$$. 

Sample Inputs:
5
2
4
6
8
60

Sample Outputs:
1 0 1
3 2 1
12 7 1
42 27 1
341102826 248150916 1

Solution (Python):
from math import comb as binomial_coefficient

def recursive_function(n):
    if n == 2:
        return 1
    return binomial_coefficient(n - 1, n // 2 - 1) + binomial_coefficient(n - 2, n // 2 - 1) - recursive_function(n - 2) - 1
modulus = 998244353
for _ in range(int(input())):
    n = int(input())
    result_a = recursive_function(n) % modulus
    result_b = (binomial_coefficient(n, n // 2) - result_a - 1) % modulus
    print(result_a, result_b, 1)

Solution (Python):
import math
p = 998244353
a = [(1, 0)]
for i in range(1, 31):
    b = (math.comb(2 * i, i) // 2 + a[i - 1][1]) % p
    a += [(b % p, (math.comb(2 * i, i) - b - 1) % p)]
for i in range(int(input())):
    print(*a[int(input()) // 2], 1)

Solution (Python):
import math
p = 998244353
a = [(1, 0)]
for i in range(1, 31):
    b = (math.comb(2 * i, i) // 2 + a[i - 1][1]) % p
    a += [(b % p, (math.comb(2 * i, i) - b - 1) % p)]
for i in range(int(input())):
    print(*a[int(input()) // 2], 1)