<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>

class Solution {
public:
    int dp[2][4918577];  // DP table for both players and all possible game states

    bool solve(int per, int t, int mask, int mi) {
        // If this state has been calculated before, return the stored result
        if(dp[per][mask] != -1) return dp[per][mask];
        
        // If no valid moves are left and t <= 0, the current player loses
        if(t <= 0) return dp[per][mask] = false;  // Current player loses if target `t` is reached or exceeded
        
        // Try all numbers from 1 to `mi`
        for(int i = 1; i <= mi; i++) {
            // Check if number `i` is available (not already picked)
            if((mask & (1 << (i))) == 0) {  // Number `i` corresponds to the `i-1` bit in mask
                // If the opponent can't win in the next move, current player wins
                if(!solve(1 - per, t - i, mask | (1 << (i)), mi)) {
                    return dp[per][mask] = true;  // Current player wins if they force the opponent to lose
                }
            }
        }
        
        // If no winning move is found, the current player loses
        return dp[per][mask] = false;
    }
    
    bool canIWin(int mi, int t) {
        // If the sum of all numbers is less than the target, it's impossible to win
        int lar = (mi * (mi + 1)) / 2;
        if(t > lar) return false;
        if(t==0)
        return true;
        // Initialize dp array with -1 (unvisited states)
        memset(dp, -1, sizeof(dp));
        
        // Start the game with player 0, target `t`, and an empty mask (no numbers chosen)
        return solve(0, t, 0, mi);
    }
};

49677