Editorial
Whether either side has blackjack is determined immediately from the initial two cards. If at least one side has blackjack, the answer is directly one of , , and according to the rules.
For the remaining normal game, divide all rewards by : a win is , a draw is , and a loss is . Multiply the final expected value by .
Number the remaining cards from to . Let bitmask be the set of cards additionally drawn by jtw7913 and bitmask the set additionally drawn by the dealer. During jtw7913's turn, . After jtw7913 Stays, is fixed and only grows.
Every card is in exactly one of three states: still in the deck, drawn by jtw7913, or drawn by the dealer. Hence there are at most possible states.
Precompute for every mask :
- : the score of jtw7913's initial two cards plus the cards in .
- : the score of the dealer's initial two cards plus the cards in .
To evaluate a hand, first count every ace as 1 and let the sum be . If there is at least one ace and , count one ace as 11 and use ; otherwise use .
Let be the normalized expected result after jtw7913 has already Stayed, when his additional-card set is and the dealer's is . If the dealer score exceeds 21, then . If the dealer score is at least 17 or the deck is empty, compare the final scores and return one of .
Otherwise the dealer must Hit. If is the set of remaining cards,
For memoization, encode each card by one ternary digit: 0 if it remains in the deck, 1 if it belongs to , and 2 if it belongs to . Thus all dealer states fit in one array of size .
Let be the maximum normalized expected result when jtw7913 has additional-card set and may still choose an action. The value of Staying now is
If the current score is 21 or the deck is empty, jtw7913 automatically Stays, so . Otherwise he may also Hit. With remaining-card set ,
where if drawing card busts, and otherwise . Therefore
The answer for a normal game is .
Score preprocessing takes time. There are at most dealer states, and each examines at most cards. The total time complexity is and the space complexity is .
Solution written by GPT5.6