<p>In the &quot;100 game&quot; two players take turns adding, to a running total, any integer from <code>1</code> to <code>10</code>. The player who first causes the running total to <strong>reach or exceed</strong> 100 wins.</p>

<p>What if we change the game so that players <strong>cannot</strong> re-use integers?</p>

<p>For example, two players might take turns drawing from a common pool of numbers from 1 to 15 without replacement until they reach a total &gt;= 100.</p>

<p>Given two integers <code>maxChoosableInteger</code> and <code>desiredTotal</code>, return <code>true</code> if the first player to move can force a win, otherwise, return <code>false</code>. Assume both players play <strong>optimally</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> maxChoosableInteger = 10, desiredTotal = 11
<strong>Output:</strong> false
<strong>Explanation:</strong>
No matter which integer the first player choose, the first player will lose.
The first player can choose an integer from 1 up to 10.
If the first player choose 1, the second player can only choose integers from 2 up to 10.
The second player will win by choosing 10 and get a total = 11, which is &gt;= desiredTotal.
Same with other integers chosen by the first player, the second player will always win.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> maxChoosableInteger = 10, desiredTotal = 0
<strong>Output:</strong> true
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> maxChoosableInteger = 10, desiredTotal = 1
<strong>Output:</strong> true
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= maxChoosableInteger &lt;= 20</code></li>
	<li><code>0 &lt;= desiredTotal &lt;= 300</code></li>
</ul>

# working it out think there is a constant-time solution for "100 game".
# to win, 2nd to last move must be between 90-99, so you have to play 89.
# then to be able to play 89, 3rd to last move must be 79-88, which means
# you need 78, etc. extrapolating your 2nd move must get you to 12, so you
# play 1 as your first move. So I think it may be possible to get a O(1)
# solution. What is hard about unique numbers is that now there are gaps.
# Is there a better way than min-max dp?
# optimize the set copy later.

# upsolve, even though passes, I glimpsed much simpler and faster solutions.
# some math, (maybe constant time?) some just much simpler versions of this.

# save factor of m w/ backtracking and maybe some nice constant w/ bitmask
# as can no longer use frozenset as must be able to add/remove and most
# containers do not provide O(1) for those operations and are hashable.
# Actually, cannot backtrack precisely because must cache, first requires
# mutability, second requires immutabilty. So just get a nice constant?
class Solution:
    def canIWin(self, maxChoosableInteger: int, desiredTotal: int) -> bool:
        def remove(a: int, b:int):
            return a & ~(1<<(b-1))
        def add(a: int, b:int):
            return a | (1<<(b-1))
        def tolist(a: int) -> list[int]:
            return[i+1 for i, bit in enumerate(bin(a)[-1:1:-1]) if bit == '1']
        @cache
        def opp(moves: int, total: int) -> bool:
            """Returns True if opponent wins."""
            lmoves = tolist(moves)
            if any(m >= total for m in lmoves): return True
            if all(you(remove(moves, m), total-m) for m in lmoves): return False
            return True
        @cache
        def you(moves: int, total: int) -> bool:
            """Returns True if you win."""
            lmoves = tolist(moves)
            if any(m >= total for m in lmoves): return True 
            if not all(opp(remove(moves, m), total-m) for m in lmoves): return True
            return False
        # short-circuit, make sure the desired total is reachable
        n = maxChoosableInteger
        if (n+1)*n/2 < desiredTotal: return False
        return you((1<<n)-1, desiredTotal)


# barely passes w/ caching w/ frozenset (that was a hint I saw by accident,
# makes sets hashable).
#class Solution:
#    def canIWin(self, maxChoosableInteger: int, desiredTotal: int) -> bool:
#        def opp(moves: frozenset, total: int) -> bool:
#            """Returns True if opponent wins."""
#            if any(m >= total for m in moves): return True 
#            if all(you(moves-{m}, total-m) for m in moves): return False
#            return True
#        @cache
#        def you(moves: frozenset, total: int) -> bool:
#            """Returns True if you win."""
#            if any(m >= total for m in moves): return True 
#            if not all(opp(moves-{m}, total-m) for m in moves): return True
#            return False
#        # short-circuit, make sure the desired total is reachable
#        n = maxChoosableInteger
#        if (n+1)*n/2 < desiredTotal: return False
#        return you(frozenset(i for i in range(1, maxChoosableInteger+1)), desiredTotal)

# # ok so this solution looks correct and clear, now just need to optimize
# class Solution:
#     def canIWin(self, maxChoosableInteger: int, desiredTotal: int) -> bool:
#         def play(moves: set, total: int) -> bool:
#             # m is short for move, c for counter
#             for m in moves:
#                 if m >= total: return True
#                 cmoves = moves - {m}
#                 if any(c >= total-m for c in cmoves): continue
#                 if all(play(moves-{m,c}, total-(m+c)) for c in cmoves): return True
#             return False
#         return play({i for i in range(1, maxChoosableInteger+1)}, desiredTotal)
98165