DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

God-Blessed Blackjack

Back to problem

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 000, 1.5R1.5R1.5R, and −R-R−R according to the rules.

For the remaining normal game, divide all rewards by RRR: a win is +1+1+1, a draw is 000, and a loss is −1-1−1. Multiply the final expected value by RRR.

Number the NNN remaining cards from 000 to N−1N-1N−1. Let bitmask PPP be the set of cards additionally drawn by jtw7913 and bitmask DDD the set additionally drawn by the dealer. During jtw7913's turn, D=0. After jtw7913 s, 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 3N3^N3N possible (P,D)(P,D)(P,D) states.

Precompute for every mask MMM:

  • playerScore[M]\mathrm{playerScore}[M]playerScore[M]: the score of jtw7913's initial two cards plus the cards in MMM.
  • dealerScore[M]\mathrm{dealerScore}[M]dealerScore[M]: 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 sss. If there is at least one ace and s+10≤21s+10\leq21s+10≤21, count one ace as 11 and use s+10s+10s+10; otherwise use sss.

Let G(P,D)G(P,D)G(P,D) be the normalized expected result after jtw7913 has already Stayed, when his additional-card set is PPP and the dealer's is DDD. If the dealer score exceeds 21, then G(P,D)=1G(P,D)=1G(P,D)=. 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 UUU is the set of remaining cards,

G(P,D)=1∣U∣∑i∈UG(P,D∪{i}).G(P,D)=\frac{1}{|U|}\sum_{i\in U}G(P,D\cup\{i\}).G(P,D)=∣U∣1​

For memoization, encode each card by one ternary digit: 0 if it remains in the deck, 1 if it belongs to PPP, and 2 if it belongs to DDD. Thus all dealer states fit in one array of size 3N3^N3N.

Let F(P)F(P)F(P) be the maximum normalized expected result when jtw7913 has additional-card set PPP and may still choose an action. The value of Staying now is

S(P)=G(P,0).S(P)=G(P,0).S(P)=G(P,0).

If the current score is 21 or the deck is empty, jtw7913 automatically Stays, so F(P)=S(P)F(P)=S(P)F(P)=S(P). Otherwise he may also Hit. With remaining-card set UUU,

H(P)=1∣U∣∑i∈Uhi,H(P)=\frac{1}{|U|}\sum_{i\in U}h_i,H(P)=∣U∣1​i∈U

where hi=−1h_i=-1hi​=−1 if drawing card iii busts, and otherwise hi=F(P∪{i})h_i=F(P\cup\{i\})h. Therefore

F(P)=max⁡(S(P),H(P)).F(P)=\max(S(P),H(P)).F(P)=max(S(P),H(P)).

The answer for a normal game is F(0)⋅RF(0)\cdot RF(0)⋅R.

Score preprocessing takes O(N2N)O(N2^N)O(N2N) time. There are at most 3N3^N3N dealer states, and each examines at most NNN cards. The total time complexity is O(N3N and the space complexity is .

Solution written by GPT5.6

D=0
D=0
Stay
PPP
DDD
MM
M
1
1,0,−11,0,-11,0,−1
i∈U
∑
​
G
(
P
,
D
∪
{i}).
∑
​
hi​
,
i
​
=
F(P∪
{i})
)O(N3^N)
O(N3N)
O(3N)O(3^N)O(3N)