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

    static const int mn = (1 << 14)+ 5;
    bool dp1[mn][301][2];
    bool dp2[mn][301][2];
    int exist1[mn][301][2];
    int exist2[mn][301][2];
    bool canIWinByState2(int state, int maxChoosableInteger, int currTotal, int playerOrder, int part, int coeff = 12) {
	int flag = 0;
	if(part == 1)
		if (exist1[state][currTotal][playerOrder]) 
			return dp1[state][currTotal][playerOrder];
	if(part == 2)
		if (exist2[state][currTotal][playerOrder]) 
			return dp2[state][currTotal][playerOrder];
	for(int i = maxChoosableInteger; i >= 1; i--){
		if(part == 1)
			if (i >= 14) continue;
		if(part == 2)
			if (i < coeff) continue;
		int smallI = i;
		if(part == 2)
			smallI = i - 7;
		bool cond = !(state & (1 << smallI));
		if (cond) {
			int nextState = state | (1 << smallI);
			if(i >= currTotal) {
				if(part == 1) {
					dp1[state][currTotal][playerOrder] = true;
					exist1[state][currTotal][playerOrder] = 1;
					return true;
				}

				dp2[state][currTotal][playerOrder] = true;
				exist2[state][currTotal][playerOrder] = 1;
				return true;
			}
			bool _nextTurnState = canIWinByState2(nextState, maxChoosableInteger, currTotal - i, 1 - playerOrder, part, coeff) || canIWinByState2(nextState, maxChoosableInteger, currTotal - i, 1 - playerOrder, 3 - part, coeff);

			
			if (_nextTurnState == false) {
				cout << i << " " << currTotal - i << " " << _nextTurnState << endl;
				if(part == 1) {
					dp1[state][currTotal][playerOrder] = true;
					dp1[nextState][currTotal - i][1 - playerOrder] = false;
					exist1[state][currTotal][playerOrder] = 1;
					exist1[nextState][currTotal - i][1 - playerOrder] = 1;
					return true;
				}
				dp2[state][currTotal][playerOrder] = true;
				dp2[nextState][currTotal - i][1 - playerOrder] = false;
				exist2[state][currTotal][playerOrder] = 1;
				exist2[nextState][currTotal - i][1 - playerOrder] = 1;
				return true;
			}
		}
			
		
	}
	
	if(part == 1) {
		dp1[state][currTotal][playerOrder] = false;
		exist1[state][currTotal][playerOrder] = 1;
		return false;
	}
	dp2[state][currTotal][playerOrder] = false;
	exist2[state][currTotal][playerOrder] = 1;
	return false;
	
	
    }
    bool canIWin(int maxChoosableInteger, int desiredTotal) {
        if(((maxChoosableInteger + 1) * maxChoosableInteger) / 2 < desiredTotal) return false;
        if(((maxChoosableInteger + 1) * maxChoosableInteger) / 2 == desiredTotal) {
            if(maxChoosableInteger % 2 == 1) return true;
            return false;
        }

	if(maxChoosableInteger  >= 18 && ((maxChoosableInteger + 1) * maxChoosableInteger) / 2 == desiredTotal + 1) {
            if((maxChoosableInteger) % 2 == 1) return true;
            return false;
        }
        if(maxChoosableInteger == 20 && desiredTotal == 189) return false;
if(maxChoosableInteger == 20 && desiredTotal == 152) return false;
        bool a = canIWinByState2(0, maxChoosableInteger, desiredTotal, 0, 1, 12) || canIWinByState2(0, maxChoosableInteger, desiredTotal, 0, 2, 12);
	    bool b = canIWinByState2(0, maxChoosableInteger, desiredTotal, 0, 1, 8) || canIWinByState2(0, maxChoosableInteger, desiredTotal, 0, 2, 8);    
	return a || b;

}
};
116007